Skip to content

Commit

Permalink
add fail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi committed Oct 6, 2024
1 parent 8f8d1e3 commit 04d751b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
10 changes: 10 additions & 0 deletions tests/programs/fail/hallo_world.vi
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

use std::io::{
println,
println,
print_ln,
};

fn main(std::io) {
io.println(hallo, world)
}
2 changes: 2 additions & 0 deletions tests/programs/fail/missing_no.vi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

mod missing = "no.vi";
6 changes: 6 additions & 0 deletions tests/snaps/vine/fail/hallo_world.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error tests/programs/fail/hallo_world.vi:4:3 - duplicate definition of `println`
error tests/programs/fail/hallo_world.vi:5:3 - cannot find `print_ln` in `::std::io`
error tests/programs/fail/hallo_world.vi:8:9 - invalid pattern; this path is not a struct or enum variant
error tests/programs/fail/hallo_world.vi:9:3 - cannot find `io` in `::hallo_world::main`
error tests/programs/fail/hallo_world.vi:9:14 - cannot find `hallo` in `::hallo_world::main`
error tests/programs/fail/hallo_world.vi:9:21 - cannot find `world` in `::hallo_world::main`
1 change: 1 addition & 0 deletions tests/snaps/vine/fail/missing_no.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error tests/programs/fail/missing_no.vi:2:1 - cannot read file `tests/programs/fail/no.vi`: No such file or directory (os error 2)
22 changes: 17 additions & 5 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ fn tests(t: &mut DynTester) {
test_vi(t, "tests/programs/option_party.vi", b"", ".txt");
test_vi(t, "tests/programs/pretty_div.vi", b"", ".txt");
test_vi(t, "tests/programs/so_random.vi", b"", ".txt");

t.group("fail", |t| {
test_vi_fail(t, "tests/programs/fail/hallo_world.vi");
test_vi_fail(t, "tests/programs/fail/missing_no.vi");
});
});
}

Expand All @@ -55,7 +60,7 @@ fn test_vi(t: &mut DynTester, path: &'static str, input: &'static [u8], output_e
t.group(name, |t| {
let (sender, receiver) = channel();
t.test("compile", move || {
let (stdout, stderr) = exec(VINE, &["build", path], &[]);
let (stdout, stderr) = exec(VINE, &["build", path], &[], true);
assert!(stderr.is_empty());
let path = test_snapshot(&["vine", name, "compiled.iv"], &stdout);
_ = sender.send(path);
Expand All @@ -68,6 +73,14 @@ fn test_vi(t: &mut DynTester, path: &'static str, input: &'static [u8], output_e
});
}

fn test_vi_fail(t: &mut DynTester, path: &'static str) {
let name = Path::file_stem(path.as_ref()).unwrap().to_str().unwrap();
t.test(name, move || {
let (_, stderr) = exec(VINE, &["build", path], &[], false);
test_snapshot(&["vine", "fail", &format!("{name}.txt")], &stderr);
});
}

fn test_iv(t: &mut DynTester, path: &'static str, input: &'static [u8], output_ext: &'static str) {
let name = Path::file_stem(path.as_ref()).unwrap().to_str().unwrap();
t.test(name, || {
Expand All @@ -76,7 +89,7 @@ fn test_iv(t: &mut DynTester, path: &'static str, input: &'static [u8], output_e
}

fn run_iv(group: &str, name: &str, path: &str, input: &[u8], output_ext: &str) {
let (stdout, stderr) = exec(IVY, &["run", path], input);
let (stdout, stderr) = exec(IVY, &["run", path], input, true);
test_snapshot(&[group, name, &format!("output{output_ext}")], &stdout);
let full_stats = String::from_utf8(stderr).unwrap();
let stats = full_stats.split_once("\nTime").unwrap().0;
Expand All @@ -85,7 +98,7 @@ fn run_iv(group: &str, name: &str, path: &str, input: &[u8], output_ext: &str) {
.unwrap();
}

fn exec(bin: &[&str], args: &[&str], input: &[u8]) -> (Vec<u8>, Vec<u8>) {
fn exec(bin: &[&str], args: &[&str], input: &[u8], success: bool) -> (Vec<u8>, Vec<u8>) {
let mut child = Command::new(env!("CARGO"))
.args(["run", "--quiet", "--bin"])
.args(bin)
Expand All @@ -103,12 +116,11 @@ fn exec(bin: &[&str], args: &[&str], input: &[u8]) -> (Vec<u8>, Vec<u8>) {
let stderr = parallel_read(child.stderr.take().unwrap());

let status = child.wait().unwrap();
if !status.success() {
if status.success() != success {
let err = String::from_utf8(stderr.join().unwrap()).unwrap();
eprintln!("{err}");
panic!("{status}");
}
assert!(status.success());

(stdout.join().unwrap(), stderr.join().unwrap())
}
Expand Down

0 comments on commit 04d751b

Please sign in to comment.