Skip to content

Commit b672a33

Browse files
committed
yeast: Test interpolation context isolation
1 parent 452ab94 commit b672a33

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

‎shared/yeast/tests/test.rs‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,126 @@ fn test_choice_fallback_can_reuse_consumed_splice() {
943943
);
944944
}
945945

946+
#[derive(Clone, Default)]
947+
struct InterpolationContext {
948+
counter: usize,
949+
}
950+
951+
#[test]
952+
fn test_output_template_interpolations_clone_user_context() {
953+
let input = "x = 1";
954+
let input_ast = parse_input(input);
955+
let mut ast = empty_output_ast(input);
956+
let mut user_ctx = InterpolationContext::default();
957+
let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx);
958+
let source = yeast::tree!((identifier "source"));
959+
960+
let nodes = yeast::trees!(
961+
(call
962+
location: {
963+
ctx.counter += 1;
964+
source
965+
}
966+
method: {
967+
ctx.counter += 1;
968+
let value = ctx.counter.to_string();
969+
ctx.literal("identifier", &value)
970+
}
971+
receiver: (identifier #{
972+
ctx.counter += 1;
973+
ctx.counter
974+
})
975+
)
976+
{
977+
ctx.counter += 1;
978+
let value = ctx.counter.to_string();
979+
[ctx.literal("identifier", &value)]
980+
}
981+
);
982+
983+
assert_eq!(ctx.counter, 0);
984+
assert_eq!(nodes.len(), 2);
985+
assert_dump_eq(
986+
&dump_ast(ctx.output, nodes[0], input),
987+
r#"
988+
call
989+
method: identifier "1"
990+
receiver: identifier "1"
991+
"#,
992+
);
993+
assert_dump_eq(
994+
&dump_ast(ctx.output, nodes[1], input),
995+
r#"
996+
identifier "1"
997+
"#,
998+
);
999+
}
1000+
1001+
#[test]
1002+
fn test_interpolation_preserves_build_ctx_method_borrowing() {
1003+
let input = "x = 1";
1004+
let input_ast = parse_input(input);
1005+
let mut ast = empty_output_ast(input);
1006+
let mut user_ctx = InterpolationContext::default();
1007+
let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx);
1008+
let source = yeast::tree!((identifier "source"));
1009+
1010+
let node = yeast::tree!((call
1011+
method: {
1012+
ctx.literal("identifier", &ctx.source_text(source))
1013+
}
1014+
));
1015+
1016+
assert_dump_eq(
1017+
&dump_ast(ctx.output, node, input),
1018+
r#"
1019+
call
1020+
method: identifier "source"
1021+
"#,
1022+
);
1023+
}
1024+
1025+
#[test]
1026+
fn test_interpolation_preserves_user_context_assignment() {
1027+
let input = "x = 1";
1028+
let input_ast = parse_input(input);
1029+
let mut ast = empty_output_ast(input);
1030+
let mut user_ctx = InterpolationContext::default();
1031+
let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx);
1032+
1033+
let node = yeast::tree!((identifier #{
1034+
*ctx = InterpolationContext { counter: 1 };
1035+
ctx.counter
1036+
}));
1037+
1038+
assert_eq!(ctx.counter, 0);
1039+
assert_dump_eq(
1040+
&dump_ast(ctx.output, node, input),
1041+
r#"
1042+
identifier "1"
1043+
"#,
1044+
);
1045+
}
1046+
1047+
#[derive(Clone, Default)]
1048+
struct BorrowedInterpolationContext {
1049+
locations: Vec<yeast::OutputId>,
1050+
}
1051+
1052+
#[test]
1053+
fn test_interpolation_accepts_value_borrowed_from_context() {
1054+
let input = "x = 1";
1055+
let input_ast = parse_input(input);
1056+
let mut ast = empty_output_ast(input);
1057+
let mut user_ctx = BorrowedInterpolationContext::default();
1058+
let mut ctx = yeast::build::BuildCtx::new(&input_ast, &mut ast, &mut user_ctx);
1059+
let location = yeast::tree!((identifier "location"));
1060+
ctx.locations.push(location);
1061+
let locations = ctx.locations.iter();
1062+
1063+
yeast::tree!((call location: {locations}));
1064+
}
1065+
9461066
#[test]
9471067
fn test_root_choice_returns_an_optional_node() {
9481068
let input = "x = 1";

0 commit comments

Comments
 (0)