Lines
96.77 %
Functions
88.89 %
Branches
100 %
use super::CompileContext;
use crate::ast::Expr;
fn ctx_with_frames(thresholds: &[u32]) -> CompileContext {
let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
for &threshold in thresholds {
ctx.push_unwind_frame(threshold, vec![Expr::Nil]);
}
ctx
#[test]
fn an_exit_pops_one_crossed_frame_at_a_time_innermost_first() {
let mut ctx = ctx_with_frames(&[1, 3, 5]);
let popped: Vec<u32> = std::iter::from_fn(|| ctx.pop_unwind_frame_crossing(3))
.map(|frame| frame.threshold)
.collect();
assert_eq!(popped, vec![5, 3]);
assert_eq!(
ctx.pop_unwind_frame_crossing(3)
.map(|frame| frame.threshold),
None
);
fn an_exit_below_every_frame_pops_nothing() {
let mut ctx = ctx_with_frames(&[2, 4]);
assert!(ctx.pop_unwind_frame_crossing(5).is_none());
fn popped_frames_restore_in_their_original_order() {
let mut popped = Vec::new();
while let Some(frame) = ctx.pop_unwind_frame_crossing(0) {
popped.push(frame);
popped.reverse();
ctx.restore_unwind_frames(popped);
let again: Vec<u32> = std::iter::from_fn(|| ctx.pop_unwind_frame_crossing(0))
assert_eq!(again, vec![5, 3, 1]);