2021-07-30 20:02:16 +00:00
|
|
|
use assert_cmd::prelude::*;
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::process::Command;
|
|
|
|
use tempfile::NamedTempFile;
|
2021-07-17 18:52:50 +00:00
|
|
|
|
2021-07-30 20:02:16 +00:00
|
|
|
type TestResult = Result<(), Box<dyn std::error::Error>>;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn run_test(input: &str, expected: &str) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
|
|
|
let mut cmd = Command::cargo_bin("engine-q")?;
|
|
|
|
cmd.arg(name);
|
|
|
|
|
|
|
|
writeln!(file, "{}", input)?;
|
|
|
|
|
|
|
|
let output = cmd.output()?;
|
|
|
|
|
2021-07-30 21:57:22 +00:00
|
|
|
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
|
|
|
|
|
|
|
|
println!("stdout: {}", stdout);
|
|
|
|
println!("stderr: {}", stderr);
|
2021-07-30 20:02:16 +00:00
|
|
|
|
2021-07-30 21:57:22 +00:00
|
|
|
assert!(output.status.success());
|
2021-07-30 20:02:16 +00:00
|
|
|
|
2021-07-30 21:57:22 +00:00
|
|
|
assert_eq!(stdout.trim(), expected);
|
2021-07-30 20:02:16 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
fn fail_test(input: &str, expected: &str) -> TestResult {
|
|
|
|
let mut file = NamedTempFile::new()?;
|
|
|
|
let name = file.path();
|
|
|
|
|
|
|
|
let mut cmd = Command::cargo_bin("engine-q")?;
|
|
|
|
cmd.arg(name);
|
|
|
|
|
|
|
|
writeln!(file, "{}", input)?;
|
|
|
|
|
|
|
|
let output = cmd.output()?;
|
|
|
|
|
|
|
|
let output = String::from_utf8_lossy(&output.stderr).to_string();
|
2021-07-30 21:57:22 +00:00
|
|
|
println!("{}", output);
|
2021-07-30 20:02:16 +00:00
|
|
|
|
|
|
|
assert!(output.contains("Error:"));
|
|
|
|
assert!(output.contains(expected));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_simple() -> TestResult {
|
|
|
|
run_test("3 + 4", "7")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_simple2() -> TestResult {
|
|
|
|
run_test("3 + 4 + 9", "16")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn broken_math() -> TestResult {
|
|
|
|
fail_test("3 + ", "Incomplete")
|
|
|
|
}
|
2021-07-30 21:26:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_test1() -> TestResult {
|
|
|
|
run_test("if $true { 10 } else { 20 } ", "10")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_test2() -> TestResult {
|
|
|
|
run_test("if $false { 10 } else { 20 } ", "20")
|
|
|
|
}
|
2021-07-30 21:57:22 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-07-31 04:04:42 +00:00
|
|
|
fn no_scope_leak1() -> TestResult {
|
2021-07-30 21:57:22 +00:00
|
|
|
fail_test(
|
|
|
|
"if $false { let $x = 10 } else { let $x = 20 }; $x",
|
|
|
|
"VariableNotFound",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-31 04:04:42 +00:00
|
|
|
fn no_scope_leak2() -> TestResult {
|
2021-07-30 21:57:22 +00:00
|
|
|
fail_test(
|
|
|
|
"def foo [] { $x }; def bar [] { let $x = 10; foo }; bar",
|
|
|
|
"VariableNotFound",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-31 04:04:42 +00:00
|
|
|
fn no_scope_leak3() -> TestResult {
|
2021-07-30 21:57:22 +00:00
|
|
|
run_test(
|
|
|
|
"def foo [$x] { $x }; def bar [] { let $x = 10; foo 20}; bar",
|
|
|
|
"20",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-31 04:04:42 +00:00
|
|
|
fn no_scope_leak4() -> TestResult {
|
2021-07-30 21:57:22 +00:00
|
|
|
run_test(
|
|
|
|
"def foo [$x] { $x }; def bar [] { let $x = 10; (foo 20) + $x}; bar",
|
|
|
|
"30",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_var_closing() -> TestResult {
|
|
|
|
run_test("let $x = 10; def foo [] { $x }; foo", "10")
|
|
|
|
}
|
2021-07-31 04:04:42 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn predecl_check() -> TestResult {
|
|
|
|
run_test("def bob [] { sam }; def sam [] { 3 }; bob", "3")
|
|
|
|
}
|