Skip to content

Commit f1225de

Browse files
committed
✅ Add // expect-output: convention for validating print output in tests
1 parent ebc26a9 commit f1225de

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

tests/programs/004_basic/028_op_assign_index_evaluated_once.ndc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ fn idx() {
66

77
let unit_list = [0];
88
unit_list[idx()] += 10;
9-
print("val:", unit_list[0],"invoked:", counter);
9+
assert_eq(unit_list[0], 10);
10+
assert_eq(counter, 1);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// expect-output: 1 2 3
12
print(1,2,3);

tests/src/programs.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ fn run_ndc_test(path: PathBuf) -> Result<(), std::io::Error> {
1414
.map(|s| s.trim().to_string())
1515
.unwrap_or_default();
1616

17+
let expect_output = contents
18+
.lines()
19+
.filter_map(|line| line.trim().strip_prefix("// expect-output:"))
20+
.map(|s| s.trim())
21+
.collect::<Vec<_>>()
22+
.join("\n");
23+
1724
print!("Running {path:?}...");
1825

1926
let mut interpreter = Interpreter::new(Vec::new());
@@ -36,6 +43,24 @@ fn run_ndc_test(path: PathBuf) -> Result<(), std::io::Error> {
3643
);
3744
}
3845

46+
if !expect_output.is_empty() {
47+
let environment = interpreter.environment();
48+
let environment = environment.borrow();
49+
let output = environment
50+
.get_output()
51+
.expect("interpreter must have output in test context");
52+
let output = String::from_utf8(output).expect("test output must be valid UTF-8");
53+
54+
if output.trim_end() != expect_output.trim_end() {
55+
println!(" {}", "ERR".red().bold());
56+
panic!(
57+
"\n\tThere was a problem running {path:?}\n\tActual output: {}\n\tdid not match\n\tExpected output: {}\n",
58+
output.trim_end(),
59+
expect_output.trim_end()
60+
);
61+
}
62+
}
63+
3964
println!(" {}", "OK".green().bold());
4065

4166
Ok(())

0 commit comments

Comments
 (0)