2022-05-29 13:14:15 +00:00
|
|
|
use crate::tests::{fail_test, run_test, run_test_contains, TestResult};
|
2022-07-26 01:11:46 +00:00
|
|
|
use nu_test_support::nu;
|
2021-12-25 19:39:42 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_scope_leak1() -> TestResult {
|
|
|
|
fail_test(
|
2022-03-03 00:55:03 +00:00
|
|
|
"if false { let $x = 10 } else { let $x = 20 }; $x",
|
2021-12-25 19:39:42 +00:00
|
|
|
"Variable not found",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_scope_leak2() -> TestResult {
|
|
|
|
fail_test(
|
|
|
|
"def foo [] { $x }; def bar [] { let $x = 10; foo }; bar",
|
|
|
|
"Variable not found",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_scope_leak3() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"def foo [$x] { $x }; def bar [] { let $x = 10; foo 20}; bar",
|
|
|
|
"20",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_scope_leak4() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"def foo [$x] { $x }; def bar [] { let $x = 10; (foo 20) + $x}; bar",
|
|
|
|
"30",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_rest_var() -> TestResult {
|
|
|
|
run_test("def foo [...x] { $x.0 + $x.1 }; foo 10 80", "90")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_twice_should_fail() -> TestResult {
|
|
|
|
fail_test(
|
|
|
|
r#"def foo [] { "foo" }; def foo [] { "bar" }"#,
|
|
|
|
"defined more than once",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_parameters() -> TestResult {
|
2021-12-27 19:13:52 +00:00
|
|
|
fail_test(r#"def foo {}"#, "Missing required positional")
|
2021-12-25 19:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn flag_param_value() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def foo [--bob: int] { $bob + 100 }; foo --bob 55"#,
|
|
|
|
"155",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn do_rest_args() -> TestResult {
|
|
|
|
run_test(r#"(do { |...rest| $rest } 1 2).1 + 10"#, "12")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_switch1() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def florb [ --dry-run: bool ] { if ($dry-run) { "foo" } else { "bar" } }; florb --dry-run"#,
|
|
|
|
"foo",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_switch2() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def florb [ --dry-run: bool ] { if ($dry-run) { "foo" } else { "bar" } }; florb"#,
|
|
|
|
"bar",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_switch3() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def florb [ --dry-run ] { if ($dry-run) { "foo" } else { "bar" } }; florb --dry-run"#,
|
|
|
|
"foo",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_switch4() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def florb [ --dry-run ] { if ($dry-run) { "foo" } else { "bar" } }; florb"#,
|
|
|
|
"bar",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_var_closing() -> TestResult {
|
|
|
|
run_test("let $x = 10; def foo [] { $x }; foo", "10")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn predecl_check() -> TestResult {
|
|
|
|
run_test("def bob [] { sam }; def sam [] { 3 }; bob", "3")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_with_no_dollar() -> TestResult {
|
|
|
|
run_test("def bob [x] { $x + 3 }; bob 4", "7")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn allow_missing_optional_params() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"def foo [x?:int] { if $x != $nothing { $x + 10 } else { 5 } }; foo",
|
|
|
|
"5",
|
|
|
|
)
|
|
|
|
}
|
2022-05-29 13:14:15 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_present_in_def() -> TestResult {
|
|
|
|
run_test_contains("def foo [] {}; help foo;", "Display this help message")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_not_present_in_extern() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"module test {export extern \"git fetch\" []}; use test; help git fetch",
|
|
|
|
"Usage:\n > git fetch",
|
|
|
|
)
|
|
|
|
}
|
2022-07-25 23:41:30 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_table() -> TestResult {
|
|
|
|
run_test(r#"def table [] { "hi" }; table"#, "hi")
|
|
|
|
}
|
2022-07-26 01:11:46 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_table_eval_file() {
|
|
|
|
let actual = nu!(cwd: ".", r#"def table [] { "hi" }; table"#);
|
|
|
|
assert_eq!(actual.out, "hi");
|
|
|
|
}
|