Skip to content

Commit

Permalink
test(vm): test write statement evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
taizu-jin committed Sep 25, 2023
1 parent 991f4bc commit 15cbef9
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@ mod tests {

use super::*;

#[derive(Default)]
struct DummyWriter(Vec<String>);

impl Writer for DummyWriter {
fn print(&mut self, arguments: Arguments) {
self.0.push(arguments.to_string())
}
}

trait GetInput {
fn input(&self) -> &'static str;
}
Expand Down Expand Up @@ -584,6 +593,23 @@ mod tests {
};
}

#[derive(Debug)]
struct TestCaseWriter {
input: &'static str,
expected_writer_content: Vec<String>,
}

macro_rules! def_case_writer {
($($input:literal, $($expected:literal),*);*) => {
vec![$(
TestCaseWriter {
input: $input,
expected_writer_content: vec![$($expected.to_string()),*]
}),*
]
};
}

#[test]
fn test_integer_arithmetic() -> Result<()> {
let tests = def_case_int!(
Expand Down Expand Up @@ -622,7 +648,7 @@ mod tests {
let mut compiler = Compiler::new();
compiler.compile(program)?;

let mut vm = VM::new(compiler.bytecode());
let mut vm = VM::new(compiler.bytecode(), DummyWriter::default());
vm.run()?;

let stack_element = vm.last_popped_stack_elem();
Expand Down Expand Up @@ -961,4 +987,39 @@ mod tests {

run_vm_tests(tests)
}

#[test]
fn test_write_statement() -> Result<()> {
let tests = def_case_writer!(
"WRITE: 'some string'.", "some string";
"WRITE: / 'some', / 'string'.", "\n", "some", "\n", "string");

for test in tests {
let program = parse(test.input.into());

let mut compiler = Compiler::new();
compiler.compile(program)?;

let mut vm = VM::new(compiler.bytecode(), DummyWriter::default());
vm.run()?;

assert_eq!(
vm.writer.0.len(),
test.expected_writer_content.len(),
"printed expression count does not match. got={} want={}",
vm.writer.0.len(),
test.expected_writer_content.len()
);

for (got, want) in vm.writer.0.iter().zip(test.expected_writer_content.iter()) {
assert_eq!(
got, want,
"printed component does not match expected value.\n\tgot={:?}\n\twant={:?}",
got, want
);
}
}

Ok(())
}
}

0 comments on commit 15cbef9

Please sign in to comment.