2021-07-30 22:02:16 +02:00
|
|
|
use assert_cmd::prelude::*;
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::process::Command;
|
|
|
|
use tempfile::NamedTempFile;
|
2021-07-17 20:52:50 +02:00
|
|
|
|
2021-07-30 22:02:16 +02: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 23:57:22 +02: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 22:02:16 +02:00
|
|
|
|
2021-07-30 23:57:22 +02:00
|
|
|
assert!(output.status.success());
|
2021-07-30 22:02:16 +02:00
|
|
|
|
2021-07-30 23:57:22 +02:00
|
|
|
assert_eq!(stdout.trim(), expected);
|
2021-07-30 22:02:16 +02: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()?;
|
|
|
|
|
2021-08-10 07:08:10 +02: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 22:02:16 +02:00
|
|
|
|
2021-08-10 07:08:10 +02:00
|
|
|
assert!(stderr.contains(expected));
|
2021-07-30 22:02:16 +02:00
|
|
|
|
|
|
|
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 {
|
2021-08-10 07:08:10 +02:00
|
|
|
fail_test("3 + ", "incomplete")
|
2021-07-30 22:02:16 +02:00
|
|
|
}
|
2021-07-30 23:26:05 +02: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 23:57:22 +02:00
|
|
|
|
2021-08-26 23:48:27 +02:00
|
|
|
#[test]
|
|
|
|
fn simple_if() -> TestResult {
|
|
|
|
run_test("if $true { 10 } ", "10")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_if2() -> TestResult {
|
|
|
|
run_test("if $false { 10 } ", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_cond() -> TestResult {
|
|
|
|
run_test("if 2 < 3 { 3 } ", "3")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_cond2() -> TestResult {
|
|
|
|
run_test("if 2 > 3 { 3 } ", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_cond3() -> TestResult {
|
|
|
|
run_test("if 2 < 3 { 5 } else { 4 } ", "5")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_cond4() -> TestResult {
|
|
|
|
run_test("if 2 > 3 { 5 } else { 4 } ", "4")
|
|
|
|
}
|
|
|
|
|
2021-08-27 01:44:08 +02:00
|
|
|
#[test]
|
|
|
|
fn if_elseif1() -> TestResult {
|
|
|
|
run_test("if 2 > 3 { 5 } else if 6 < 7 { 4 } ", "4")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_elseif2() -> TestResult {
|
|
|
|
run_test("if 2 < 3 { 5 } else if 6 < 7 { 4 } else { 8 } ", "5")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_elseif3() -> TestResult {
|
|
|
|
run_test("if 2 > 3 { 5 } else if 6 > 7 { 4 } else { 8 } ", "8")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_elseif4() -> TestResult {
|
|
|
|
run_test("if 2 > 3 { 5 } else if 6 < 7 { 4 } else { 8 } ", "4")
|
|
|
|
}
|
|
|
|
|
2021-07-30 23:57:22 +02:00
|
|
|
#[test]
|
2021-07-31 06:04:42 +02:00
|
|
|
fn no_scope_leak1() -> TestResult {
|
2021-07-30 23:57:22 +02:00
|
|
|
fail_test(
|
|
|
|
"if $false { let $x = 10 } else { let $x = 20 }; $x",
|
2021-08-10 07:08:10 +02:00
|
|
|
"variable not found",
|
2021-07-30 23:57:22 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-31 06:04:42 +02:00
|
|
|
fn no_scope_leak2() -> TestResult {
|
2021-07-30 23:57:22 +02:00
|
|
|
fail_test(
|
|
|
|
"def foo [] { $x }; def bar [] { let $x = 10; foo }; bar",
|
2021-08-10 07:08:10 +02:00
|
|
|
"Variable not found",
|
2021-07-30 23:57:22 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-31 06:04:42 +02:00
|
|
|
fn no_scope_leak3() -> TestResult {
|
2021-07-30 23:57:22 +02:00
|
|
|
run_test(
|
|
|
|
"def foo [$x] { $x }; def bar [] { let $x = 10; foo 20}; bar",
|
|
|
|
"20",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-31 06:04:42 +02:00
|
|
|
fn no_scope_leak4() -> TestResult {
|
2021-07-30 23:57:22 +02: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 06:04:42 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn predecl_check() -> TestResult {
|
|
|
|
run_test("def bob [] { sam }; def sam [] { 3 }; bob", "3")
|
|
|
|
}
|
2021-07-31 06:25:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_with_no_dollar() -> TestResult {
|
|
|
|
run_test("def bob [x] { $x + 3 }; bob 4", "7")
|
|
|
|
}
|
2021-07-31 07:20:40 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn env_shorthand() -> TestResult {
|
|
|
|
run_test("FOO=BAR if $false { 3 } else { 4 }", "4")
|
|
|
|
}
|
2021-08-08 22:21:21 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn floating_add() -> TestResult {
|
|
|
|
run_test("10.1 + 0.8", "10.9")
|
|
|
|
}
|
2021-08-08 23:55:18 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subcommand() -> TestResult {
|
|
|
|
run_test("def foo [] {}; def \"foo bar\" [] {3}; foo bar", "3")
|
|
|
|
}
|
2021-08-09 09:53:06 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_1() -> TestResult {
|
|
|
|
run_test("def foo [$x] { $x + 10 }; alias f = foo; f 100", "110")
|
|
|
|
}
|
2021-08-09 09:55:06 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_2() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"def foo [$x $y] { $x + $y + 10 }; alias f = foo 33; f 100",
|
|
|
|
"143",
|
|
|
|
)
|
|
|
|
}
|
2021-09-06 04:20:02 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_param1() -> TestResult {
|
2021-09-06 06:07:48 +02:00
|
|
|
run_test("[3] | each { $it + 10 }", "[13]")
|
2021-09-06 04:20:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_param2() -> TestResult {
|
2021-09-06 06:07:48 +02:00
|
|
|
run_test("[3] | each { |y| $y + 10 }", "[13]")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_iteration1() -> TestResult {
|
|
|
|
run_test("1..4 | each { |y| $y + 10 }", "[11, 12, 13, 14]")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_iteration2() -> TestResult {
|
|
|
|
run_test("4..1 | each { |y| $y + 100 }", "[104, 103, 102, 101]")
|
2021-09-06 04:20:02 +02:00
|
|
|
}
|
2021-09-06 06:16:32 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_value_iteration() -> TestResult {
|
|
|
|
run_test("4 | each { $it + 10 }", "14")
|
|
|
|
}
|
2021-09-06 20:05:46 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn concrete_variable_assignment() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"let x = (1..100 | each { |y| $y + 100 }); $x | length; $x | length",
|
|
|
|
"100",
|
|
|
|
)
|
|
|
|
}
|
2021-09-06 20:07:41 +02:00
|
|
|
|
2021-09-06 18:05:53 +02:00
|
|
|
#[test]
|
|
|
|
fn build_string1() -> TestResult {
|
|
|
|
run_test("build-string 'nu' 'shell'", "nushell")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_string2() -> TestResult {
|
|
|
|
run_test("'nu' | each {build-string $it 'shell'}", "nushell")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn build_string3() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"build-string 'nu' 'shell' | each {build-string $it ' rocks'}",
|
|
|
|
"nushell rocks",
|
|
|
|
)
|
|
|
|
}
|
2021-09-07 00:02:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_path_subexpr1() -> TestResult {
|
|
|
|
run_test("([[lang, gems]; [nu, 100]]).lang", "[nu]")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_path_subexpr2() -> TestResult {
|
|
|
|
run_test("([[lang, gems]; [nu, 100]]).lang.0", "nu")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_path_var1() -> TestResult {
|
|
|
|
run_test("let x = [[lang, gems]; [nu, 100]]; $x.lang", "[nu]")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cell_path_var2() -> TestResult {
|
|
|
|
run_test("let x = [[lang, gems]; [nu, 100]]; $x.lang.0", "nu")
|
|
|
|
}
|
2021-09-07 05:37:02 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_rest_var() -> TestResult {
|
|
|
|
run_test("def foo [...x] { $x.0 + $x.1 }; foo 10 80", "90")
|
|
|
|
}
|
2021-09-07 09:09:49 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn row_iteration() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"[[name, size]; [tj, 100], [rl, 200]] | each { $it.size * 8 }",
|
|
|
|
"[800, 1600]",
|
|
|
|
)
|
|
|
|
}
|