2022-07-27 17:36:56 +02:00
|
|
|
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
use nu_test_support::{nu, pipeline};
|
2023-04-08 20:52:37 +02:00
|
|
|
use pretty_assertions::assert_eq;
|
2023-09-24 11:01:21 +02:00
|
|
|
use rstest::rstest;
|
2022-03-05 14:20:13 +01:00
|
|
|
|
2022-11-15 00:05:27 +01:00
|
|
|
#[test]
|
|
|
|
fn source_file_relative_to_file() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/parsing/samples", "
|
2022-11-15 00:05:27 +01:00
|
|
|
nu source_file_relative.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2022-11-15 00:05:27 +01:00
|
|
|
|
2023-01-09 06:53:52 +01:00
|
|
|
assert_eq!(actual.out, "5");
|
2022-11-15 00:05:27 +01:00
|
|
|
}
|
|
|
|
|
2022-12-21 23:21:03 +01:00
|
|
|
#[test]
|
|
|
|
fn source_const_file() {
|
|
|
|
let actual = nu!(cwd: "tests/parsing/samples",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-12-21 23:21:03 +01:00
|
|
|
const file = 'single_line.nu'
|
|
|
|
source $file
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "5");
|
|
|
|
}
|
|
|
|
|
2022-03-05 14:20:13 +01:00
|
|
|
#[test]
|
|
|
|
fn run_nu_script_single_line() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/parsing/samples", "
|
2022-03-05 14:20:13 +01:00
|
|
|
nu single_line.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2022-03-05 14:20:13 +01:00
|
|
|
|
2023-01-09 06:53:52 +01:00
|
|
|
assert_eq!(actual.out, "5");
|
2022-03-05 14:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_nu_script_multiline_start_pipe() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/parsing/samples", "
|
2022-03-05 14:20:13 +01:00
|
|
|
nu multiline_start_pipe.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2022-03-05 14:20:13 +01:00
|
|
|
|
2023-01-09 06:53:52 +01:00
|
|
|
assert_eq!(actual.out, "4");
|
2022-03-05 14:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_nu_script_multiline_start_pipe_win() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/parsing/samples", "
|
2022-03-05 14:20:13 +01:00
|
|
|
nu multiline_start_pipe_win.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2022-03-05 14:20:13 +01:00
|
|
|
|
2023-01-09 06:53:52 +01:00
|
|
|
assert_eq!(actual.out, "3");
|
2022-03-05 14:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_nu_script_multiline_end_pipe() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/parsing/samples", "
|
2022-03-05 14:20:13 +01:00
|
|
|
nu multiline_end_pipe.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2022-03-05 14:20:13 +01:00
|
|
|
|
2023-01-09 06:53:52 +01:00
|
|
|
assert_eq!(actual.out, "2");
|
2022-03-05 14:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn run_nu_script_multiline_end_pipe_win() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(cwd: "tests/parsing/samples", "
|
2022-03-05 14:20:13 +01:00
|
|
|
nu multiline_end_pipe_win.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2022-03-05 14:20:13 +01:00
|
|
|
|
2023-01-09 06:53:52 +01:00
|
|
|
assert_eq!(actual.out, "3");
|
2022-03-05 14:20:13 +01:00
|
|
|
}
|
2022-07-27 17:36:56 +02:00
|
|
|
|
2022-08-31 22:32:56 +02:00
|
|
|
#[test]
|
|
|
|
fn parse_file_relative_to_parsed_file_simple() {
|
|
|
|
Playground::setup("relative_files_simple", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("lol")
|
|
|
|
.mkdir("lol/lol")
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"lol/lol/lol.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
use ../lol_shell.nu
|
|
|
|
|
2023-06-30 21:57:51 +02:00
|
|
|
$env.LOL = (lol_shell ls)
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-08-31 22:32:56 +02:00
|
|
|
)])
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"lol/lol_shell.nu",
|
|
|
|
r#"
|
|
|
|
export def ls [] { "lol" }
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
source-env lol/lol/lol.nu;
|
|
|
|
$env.LOL
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "lol");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-07 15:42:09 +02:00
|
|
|
#[test]
|
2023-10-08 12:58:26 +02:00
|
|
|
fn predecl_signature_single_inp_out_type() {
|
|
|
|
Playground::setup("predecl_signature_single_inp_out_type", |dirs, sandbox| {
|
2023-10-07 15:42:09 +02:00
|
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
2023-10-08 12:58:26 +02:00
|
|
|
"spam1.nu",
|
2023-10-07 15:42:09 +02:00
|
|
|
"
|
|
|
|
def main [] { foo }
|
|
|
|
|
|
|
|
def foo []: nothing -> nothing { print 'foo' }
|
|
|
|
",
|
|
|
|
)]);
|
|
|
|
|
2023-10-08 12:58:26 +02:00
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline("nu spam1.nu"));
|
2023-10-07 15:42:09 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
})
|
|
|
|
}
|
2023-10-08 12:58:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn predecl_signature_multiple_inp_out_types() {
|
|
|
|
Playground::setup(
|
|
|
|
"predecl_signature_multiple_inp_out_types",
|
|
|
|
|dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"spam2.nu",
|
|
|
|
"
|
|
|
|
def main [] { foo }
|
|
|
|
|
|
|
|
def foo []: [nothing -> string, string -> string] { 'foo' }
|
|
|
|
",
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline("nu spam2.nu"));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-31 22:32:56 +02:00
|
|
|
#[ignore]
|
2022-07-27 17:36:56 +02:00
|
|
|
#[test]
|
|
|
|
fn parse_file_relative_to_parsed_file() {
|
|
|
|
Playground::setup("relative_files", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("lol")
|
|
|
|
.mkdir("lol/lol")
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"lol/lol/lol.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
source-env ../../foo.nu
|
2022-07-27 17:36:56 +02:00
|
|
|
use ../lol_shell.nu
|
2022-08-21 16:27:56 +02:00
|
|
|
overlay use ../../lol/lol_shell.nu
|
2022-07-27 17:36:56 +02:00
|
|
|
|
2023-06-30 21:57:51 +02:00
|
|
|
$env.LOL = $'($env.FOO) (lol_shell ls) (ls)'
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-07-27 17:36:56 +02:00
|
|
|
)])
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"lol/lol_shell.nu",
|
|
|
|
r#"
|
|
|
|
export def ls [] { "lol" }
|
|
|
|
"#,
|
|
|
|
)])
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"foo.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2023-06-30 21:57:51 +02:00
|
|
|
$env.FOO = 'foo'
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-07-27 17:36:56 +02:00
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
source-env lol/lol/lol.nu;
|
|
|
|
$env.LOL
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-07-27 17:36:56 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "foo lol lol");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_file_relative_to_parsed_file_dont_use_cwd_1() {
|
|
|
|
Playground::setup("relative_files", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("lol")
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"lol/lol.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
source-env foo.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-07-27 17:36:56 +02:00
|
|
|
)])
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"lol/foo.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2023-06-30 21:57:51 +02:00
|
|
|
$env.FOO = 'good'
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-07-27 17:36:56 +02:00
|
|
|
)])
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"foo.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2023-06-30 21:57:51 +02:00
|
|
|
$env.FOO = 'bad'
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-07-27 17:36:56 +02:00
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
source-env lol/lol.nu;
|
2022-07-27 17:36:56 +02:00
|
|
|
$env.FOO
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-07-27 17:36:56 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "good");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_file_relative_to_parsed_file_dont_use_cwd_2() {
|
|
|
|
Playground::setup("relative_files", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.mkdir("lol")
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"lol/lol.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
source-env foo.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-07-27 17:36:56 +02:00
|
|
|
)])
|
|
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
|
|
"foo.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2023-06-30 21:57:51 +02:00
|
|
|
$env.FOO = 'bad'
|
2023-07-12 19:07:20 +02:00
|
|
|
",
|
2022-07-27 17:36:56 +02:00
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-08-31 22:32:56 +02:00
|
|
|
source-env lol/lol.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-07-27 17:36:56 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("File not found"));
|
|
|
|
})
|
|
|
|
}
|
2022-08-23 09:45:17 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_export_env_in_module() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!("
|
2022-08-23 09:45:17 +02:00
|
|
|
module spam { export-env { } }
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2022-08-23 09:45:17 +02:00
|
|
|
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_export_env_missing_block() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!("
|
2022-08-23 09:45:17 +02:00
|
|
|
module spam { export-env }
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2022-08-23 09:45:17 +02:00
|
|
|
|
|
|
|
assert!(actual.err.contains("missing block"));
|
|
|
|
}
|
2023-04-01 21:48:45 +02:00
|
|
|
|
2023-04-10 23:52:51 +02:00
|
|
|
#[test]
|
|
|
|
fn call_command_with_non_ascii_argument() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!("
|
2023-04-10 23:52:51 +02:00
|
|
|
def nu-arg [--umlaut(-ö): int] {}
|
|
|
|
nu-arg -ö 42
|
2023-07-12 19:07:20 +02:00
|
|
|
");
|
2023-04-10 23:52:51 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.err.len(), 0);
|
|
|
|
}
|
2023-05-12 18:57:50 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_long_duration() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(r#"
|
2023-05-12 18:57:50 +02:00
|
|
|
"78.797877879789789sec" | into duration
|
|
|
|
"#);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1min 18sec 797ms");
|
|
|
|
}
|
2023-09-24 11:01:21 +02:00
|
|
|
|
|
|
|
#[rstest]
|
2023-09-24 11:30:58 +02:00
|
|
|
#[case("def test [ --a: any = 32 ] {}")]
|
|
|
|
#[case("def test [ --a: number = 32 ] {}")]
|
|
|
|
#[case("def test [ --a: number = 32.0 ] {}")]
|
|
|
|
#[case("def test [ --a: list<any> = [ 1 2 3 ] ] {}")]
|
|
|
|
#[case("def test [ --a: record<a: int b: string> = { a: 32 b: 'qwe' c: 'wqe' } ] {}")]
|
|
|
|
#[case("def test [ --a: record<a: any b: any> = { a: 32 b: 'qwe'} ] {}")]
|
2023-09-24 11:01:21 +02:00
|
|
|
#[case("def test []: int -> int { 1 }")]
|
|
|
|
#[case("def test []: string -> string { 'qwe' }")]
|
|
|
|
#[case("def test []: nothing -> nothing { null }")]
|
|
|
|
#[case("def test []: list<string> -> list<string> { [] }")]
|
|
|
|
#[case("def test []: record<a: int b: int> -> record<c: int e: int> { {c: 1 e: 1} }")]
|
|
|
|
#[case("def test []: table<a: int b: int> -> table<c: int e: int> { [ {c: 1 e: 1} ] }")]
|
|
|
|
#[case("def test []: nothing -> record<c: int e: int> { {c: 1 e: 1} }")]
|
|
|
|
fn parse_function_signature(#[case] phrase: &str) {
|
|
|
|
let actual = nu!(phrase);
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|
|
|
|
|
2023-11-29 18:29:07 +01:00
|
|
|
#[rstest]
|
|
|
|
#[case("def test [ in ] {}")]
|
|
|
|
#[case("def test [ in: string ] {}")]
|
|
|
|
#[case("def test [ nu: int ] {}")]
|
|
|
|
#[case("def test [ env: record<> ] {}")]
|
|
|
|
#[case("def test [ --env ] {}")]
|
|
|
|
#[case("def test [ --nu: int ] {}")]
|
|
|
|
#[case("def test [ --in (-i): list<any> ] {}")]
|
|
|
|
#[case("def test [ a: string, b: int, in: table<a: int b: int> ] {}")]
|
|
|
|
#[case("def test [ env, in, nu ] {}")]
|
|
|
|
fn parse_function_signature_name_is_builtin_var(#[case] phrase: &str) {
|
|
|
|
let actual = nu!(phrase);
|
|
|
|
assert!(actual.err.contains("nu::parser::name_is_builtin_var"))
|
|
|
|
}
|
|
|
|
|
2023-09-24 11:01:21 +02:00
|
|
|
#[rstest]
|
|
|
|
#[case("let a: int = 1")]
|
|
|
|
#[case("let a: string = 'qwe'")]
|
|
|
|
#[case("let a: nothing = null")]
|
|
|
|
#[case("let a: list<string> = []")]
|
|
|
|
#[case("let a: record<a: int b: int> = {a: 1 b: 1}")]
|
|
|
|
#[case("let a: table<a: int b: int> = [[a b]; [1 2] [3 4]]")]
|
|
|
|
#[case("let a: record<a: record<name: string> b: int> = {a: {name: bob} b: 1}")]
|
|
|
|
fn parse_let_signature(#[case] phrase: &str) {
|
|
|
|
let actual = nu!(phrase);
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|
2024-03-21 16:37:52 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_let_signature_missing_colon() {
|
|
|
|
let actual = nu!("let a int = 1");
|
|
|
|
assert!(actual.err.contains("nu::parser::extra_tokens"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_mut_signature_missing_colon() {
|
|
|
|
let actual = nu!("mut a record<a: int b: int> = {a: 1 b: 1}");
|
|
|
|
assert!(actual.err.contains("nu::parser::extra_tokens"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_const_signature_missing_colon() {
|
|
|
|
let actual = nu!("const a string = 'Hello World\n'");
|
|
|
|
assert!(actual.err.contains("nu::parser::extra_tokens"));
|
|
|
|
}
|