1
use super::CompileContext;
2
use crate::ast::Expr;
3

            
4
3
fn ctx_with_frames(thresholds: &[u32]) -> CompileContext {
5
3
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
6
3
    for &threshold in thresholds {
7
8
        ctx.push_unwind_frame(threshold, vec![Expr::Nil]);
8
    }
9
3
    ctx
10
3
}
11

            
12
#[test]
13
1
fn an_exit_pops_one_crossed_frame_at_a_time_innermost_first() {
14
1
    let mut ctx = ctx_with_frames(&[1, 3, 5]);
15
3
    let popped: Vec<u32> = std::iter::from_fn(|| ctx.pop_unwind_frame_crossing(3))
16
2
        .map(|frame| frame.threshold)
17
        .collect();
18
1
    assert_eq!(popped, vec![5, 3]);
19
1
    assert_eq!(
20
1
        ctx.pop_unwind_frame_crossing(3)
21
            .map(|frame| frame.threshold),
22
1
        None
23
    );
24
1
}
25

            
26
#[test]
27
1
fn an_exit_below_every_frame_pops_nothing() {
28
1
    let mut ctx = ctx_with_frames(&[2, 4]);
29
1
    assert!(ctx.pop_unwind_frame_crossing(5).is_none());
30
1
}
31

            
32
#[test]
33
1
fn popped_frames_restore_in_their_original_order() {
34
1
    let mut ctx = ctx_with_frames(&[1, 3, 5]);
35
1
    let mut popped = Vec::new();
36
4
    while let Some(frame) = ctx.pop_unwind_frame_crossing(0) {
37
3
        popped.push(frame);
38
    }
39
1
    popped.reverse();
40
1
    ctx.restore_unwind_frames(popped);
41
4
    let again: Vec<u32> = std::iter::from_fn(|| ctx.pop_unwind_frame_crossing(0))
42
3
        .map(|frame| frame.threshold)
43
        .collect();
44
1
    assert_eq!(again, vec![5, 3, 1]);
45
1
}