2021-12-25 20:39:42 +01:00
|
|
|
use crate::tests::{fail_test, run_test, TestResult};
|
|
|
|
|
2022-01-22 19:24:47 +01:00
|
|
|
use super::run_test_contains;
|
|
|
|
|
2021-12-25 20:39:42 +01:00
|
|
|
#[test]
|
|
|
|
fn env_shorthand() -> TestResult {
|
2022-03-03 01:55:03 +01:00
|
|
|
run_test("FOO=BAR if false { 3 } else { 4 }", "4")
|
2021-12-25 20:39:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subcommand() -> TestResult {
|
|
|
|
run_test("def foo [] {}; def \"foo bar\" [] {3}; foo bar", "3")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_1() -> TestResult {
|
|
|
|
run_test("def foo [$x] { $x + 10 }; alias f = foo; f 100", "110")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_2() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
"def foo [$x $y] { $x + $y + 10 }; alias f = foo 33; f 100",
|
|
|
|
"143",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_2_multi_word() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def "foo bar" [$x $y] { $x + $y + 10 }; alias f = foo bar 33; f 100"#,
|
|
|
|
"143",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-15 23:36:24 +01:00
|
|
|
#[test]
|
|
|
|
fn alias_recursion() -> TestResult {
|
|
|
|
run_test_contains(r#"alias ls = (ls | sort-by type name -i); ls"#, " ")
|
|
|
|
}
|
|
|
|
|
2021-12-25 20:39:42 +01:00
|
|
|
#[test]
|
|
|
|
fn block_param1() -> TestResult {
|
2022-02-17 12:40:24 +01:00
|
|
|
run_test("[3] | each { |it| $it + 10 } | get 0", "13")
|
2021-12-25 20:39:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_param2() -> TestResult {
|
|
|
|
run_test("[3] | each { |y| $y + 10 } | get 0", "13")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_param3_list_iteration() -> TestResult {
|
2022-02-17 12:40:24 +01:00
|
|
|
run_test("[1,2,3] | each { |it| $it + 10 } | get 1", "12")
|
2021-12-25 20:39:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_param4_list_iteration() -> TestResult {
|
|
|
|
run_test("[1,2,3] | each { |y| $y + 10 } | get 2", "13")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_iteration1() -> TestResult {
|
|
|
|
run_test("1..4 | each { |y| $y + 10 } | get 0", "11")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_iteration2() -> TestResult {
|
|
|
|
run_test("4..1 | each { |y| $y + 100 } | get 3", "101")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_value_iteration() -> TestResult {
|
2022-02-17 12:40:24 +01:00
|
|
|
run_test("4 | each { |it| $it + 10 }", "14")
|
2021-12-25 20:39:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comment_multiline() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"def foo [] {
|
|
|
|
let x = 1 + 2 # comment
|
|
|
|
let y = 3 + 4 # another comment
|
|
|
|
$x + $y
|
|
|
|
}; foo"#,
|
|
|
|
"10",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comment_skipping_1() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"let x = {
|
|
|
|
y: 20
|
|
|
|
# foo
|
|
|
|
}; $x.y"#,
|
|
|
|
"20",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn comment_skipping_2() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"let x = {
|
|
|
|
y: 20
|
|
|
|
# foo
|
|
|
|
z: 40
|
|
|
|
}; $x.z"#,
|
|
|
|
"40",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_var_name() -> TestResult {
|
|
|
|
fail_test(r#"let $"foo bar" = 4"#, "can't contain")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn long_flag() -> TestResult {
|
|
|
|
run_test(
|
2022-02-17 12:40:24 +01:00
|
|
|
r#"([a, b, c] | each --numbered { |it| if $it.index == 1 { 100 } else { 0 } }).1"#,
|
2021-12-25 20:39:42 +01:00
|
|
|
"100",
|
|
|
|
)
|
|
|
|
}
|
2021-12-27 04:04:22 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_not_statement() -> TestResult {
|
2022-01-15 16:26:52 +01:00
|
|
|
fail_test(r#"let x = "hello" | str length"#, "used in pipeline")
|
2021-12-27 04:04:22 +01:00
|
|
|
}
|
2021-12-27 21:04:48 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn for_in_missing_var_name() -> TestResult {
|
|
|
|
fail_test("for in", "missing")
|
|
|
|
}
|
2022-01-02 06:27:58 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiline_pipe_in_block() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"do {
|
|
|
|
echo hello |
|
|
|
|
str length
|
|
|
|
}"#,
|
|
|
|
"5",
|
|
|
|
)
|
|
|
|
}
|
2022-01-06 22:06:54 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_short_flag() -> TestResult {
|
|
|
|
fail_test(r#"def foo3 [-l?:int] { $l }"#, "short flag")
|
|
|
|
}
|
2022-01-10 03:52:01 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn alias_with_error_doesnt_panic() -> TestResult {
|
|
|
|
fail_test(
|
|
|
|
r#"alias s = shells
|
|
|
|
s ."#,
|
|
|
|
"extra positional",
|
|
|
|
)
|
|
|
|
}
|
2022-01-19 15:58:12 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn quotes_with_equals() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"let query_prefix = "https://api.github.com/search/issues?q=repo:nushell/"; $query_prefix"#,
|
|
|
|
"https://api.github.com/search/issues?q=repo:nushell/",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_interp_with_equals() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"let query_prefix = $"https://api.github.com/search/issues?q=repo:nushell/"; $query_prefix"#,
|
|
|
|
"https://api.github.com/search/issues?q=repo:nushell/",
|
|
|
|
)
|
|
|
|
}
|
2022-01-21 17:39:55 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_parse() -> TestResult {
|
|
|
|
run_test(r#"def c [] { c }; echo done"#, "done")
|
|
|
|
}
|
2022-01-22 19:24:47 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn commands_have_usage() -> TestResult {
|
|
|
|
run_test_contains(
|
|
|
|
r#"
|
|
|
|
# This is a test
|
|
|
|
#
|
|
|
|
# To see if I have cool usage
|
|
|
|
def foo [] {}
|
|
|
|
help foo"#,
|
|
|
|
"cool usage",
|
|
|
|
)
|
|
|
|
}
|
2022-01-27 02:20:12 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn equals_separates_long_flag() -> TestResult {
|
|
|
|
run_test(r#"seq 1 4 --separator='+'"#, "1+2+3+4")
|
|
|
|
}
|
2022-02-09 19:41:41 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_env_expressions() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"let-env PATH = if (env | any? name == VENV_OLD_PATH) { $env.VENV_OLD_PATH } else { $env.PATH }; echo done"#,
|
|
|
|
"done",
|
|
|
|
)
|
|
|
|
}
|
2022-02-10 17:09:08 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_interpolation_paren_test() -> TestResult {
|
|
|
|
run_test(r#"$"('(')(')')""#, "()")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_interpolation_paren_test2() -> TestResult {
|
|
|
|
run_test(r#"$"('(')test(')')""#, "(test)")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_interpolation_paren_test3() -> TestResult {
|
|
|
|
run_test(r#"$"('(')("test")test(')')""#, "(testtest)")
|
|
|
|
}
|
2022-02-11 00:15:15 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn capture_multiple_commands() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"
|
|
|
|
let CONST_A = 'Hello'
|
|
|
|
|
|
|
|
def 'say-hi' [] {
|
|
|
|
echo (call-me)
|
|
|
|
}
|
|
|
|
|
|
|
|
def 'call-me' [] {
|
|
|
|
echo $CONST_A
|
|
|
|
}
|
|
|
|
|
2022-02-21 14:38:15 +01:00
|
|
|
[(say-hi) (call-me)] | str collect
|
|
|
|
|
2022-02-11 00:15:15 +01:00
|
|
|
"#,
|
|
|
|
"HelloHello",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn capture_multiple_commands2() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"
|
|
|
|
let CONST_A = 'Hello'
|
|
|
|
|
|
|
|
def 'call-me' [] {
|
|
|
|
echo $CONST_A
|
|
|
|
}
|
|
|
|
|
|
|
|
def 'say-hi' [] {
|
|
|
|
echo (call-me)
|
|
|
|
}
|
|
|
|
|
2022-02-21 14:38:15 +01:00
|
|
|
[(say-hi) (call-me)] | str collect
|
|
|
|
|
2022-02-11 00:15:15 +01:00
|
|
|
"#,
|
|
|
|
"HelloHello",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn capture_multiple_commands3() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"
|
|
|
|
let CONST_A = 'Hello'
|
|
|
|
|
|
|
|
def 'say-hi' [] {
|
|
|
|
echo (call-me)
|
|
|
|
}
|
|
|
|
|
|
|
|
def 'call-me' [] {
|
|
|
|
echo $CONST_A
|
|
|
|
}
|
|
|
|
|
2022-02-21 14:38:15 +01:00
|
|
|
[(call-me) (say-hi)] | str collect
|
|
|
|
|
2022-02-11 00:15:15 +01:00
|
|
|
"#,
|
|
|
|
"HelloHello",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn capture_multiple_commands4() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"
|
|
|
|
let CONST_A = 'Hello'
|
|
|
|
|
|
|
|
def 'call-me' [] {
|
|
|
|
echo $CONST_A
|
|
|
|
}
|
|
|
|
|
|
|
|
def 'say-hi' [] {
|
|
|
|
echo (call-me)
|
|
|
|
}
|
|
|
|
|
2022-02-21 14:38:15 +01:00
|
|
|
[(call-me) (say-hi)] | str collect
|
|
|
|
|
2022-02-11 00:15:15 +01:00
|
|
|
"#,
|
|
|
|
"HelloHello",
|
|
|
|
)
|
|
|
|
}
|
2022-02-11 13:37:10 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn capture_row_condition() -> TestResult {
|
|
|
|
run_test(
|
|
|
|
r#"let name = "foo"; [foo] | where $'($name)' =~ $it | str collect"#,
|
|
|
|
"foo",
|
|
|
|
)
|
|
|
|
}
|
2022-02-14 18:33:47 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proper_missing_param() -> TestResult {
|
|
|
|
fail_test(r#"def foo [x y z w] { }; foo a b c"#, "missing w")
|
|
|
|
}
|
2022-02-17 12:40:24 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_arity_check1() -> TestResult {
|
|
|
|
fail_test(r#"ls | each { 1 }"#, "expected 1 block parameter")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_arity_check2() -> TestResult {
|
|
|
|
fail_test(r#"ls | reduce { 1 }"#, "expected 2 block parameters")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_arity_check3() -> TestResult {
|
|
|
|
fail_test(r#"ls | each { |x, y| 1}"#, "expected 1 block parameter")
|
|
|
|
}
|
2022-03-03 19:14:03 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_escape() -> TestResult {
|
|
|
|
run_test(r#""\u015B""#, "ś")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string_escape_interpolation() -> TestResult {
|
|
|
|
run_test(r#"$"\u015B(char hamburger)abc""#, "ś≡abc")
|
|
|
|
}
|