2023-04-07 23:09:55 +02:00
|
|
|
use nu_test_support::nu;
|
2021-01-07 18:14:51 +01:00
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
use std::fs;
|
2022-02-04 03:01:45 +01:00
|
|
|
|
2021-01-07 18:14:51 +01:00
|
|
|
#[test]
|
|
|
|
fn def_with_comment() {
|
|
|
|
Playground::setup("def_with_comment", |dirs, _| {
|
|
|
|
let data = r#"
|
|
|
|
#My echo
|
2022-08-31 22:32:56 +02:00
|
|
|
export def e [arg] {echo $arg}
|
2021-01-07 18:14:51 +01:00
|
|
|
"#;
|
|
|
|
fs::write(dirs.root().join("def_test"), data).expect("Unable to write file");
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.root(),
|
2022-08-31 22:32:56 +02:00
|
|
|
"use def_test e; help e | to json -r"
|
2021-01-07 18:14:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.out.contains("My echo\\n\\n"));
|
|
|
|
});
|
|
|
|
}
|
2022-07-10 10:32:52 +02:00
|
|
|
|
2022-12-31 12:18:53 +01:00
|
|
|
#[test]
|
|
|
|
fn def_with_param_comment() {
|
|
|
|
Playground::setup("def_with_param_comment", |dirs, _| {
|
|
|
|
let data = r#"
|
|
|
|
export def e [
|
|
|
|
param:string #My cool attractive param
|
|
|
|
] {echo $param};
|
|
|
|
"#;
|
|
|
|
fs::write(dirs.root().join("def_test"), data).expect("Unable to write file");
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.root(),
|
|
|
|
"use def_test e; help e"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.out.contains(r#"My cool attractive param"#));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-12 16:10:40 +02:00
|
|
|
#[test]
|
|
|
|
fn def_errors_with_no_space_between_params_and_name_1() {
|
|
|
|
let actual = nu!("def test-command[] {}");
|
|
|
|
|
|
|
|
assert!(actual.err.contains("expected space"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_no_space_between_params_and_name_2() {
|
|
|
|
let actual = nu!("def-env test-command() {}");
|
|
|
|
|
|
|
|
assert!(actual.err.contains("expected space"));
|
|
|
|
}
|
|
|
|
|
2022-07-10 10:32:52 +02:00
|
|
|
#[test]
|
|
|
|
fn def_errors_with_multiple_short_flags() {
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!("def test-command [ --long(-l)(-o) ] {}");
|
2022-07-10 10:32:52 +02:00
|
|
|
|
2022-12-28 00:00:44 +01:00
|
|
|
assert!(actual.err.contains("expected only one short flag"));
|
2022-07-10 10:32:52 +02:00
|
|
|
}
|
2022-12-22 21:31:34 +01:00
|
|
|
|
2022-12-31 12:18:53 +01:00
|
|
|
#[test]
|
|
|
|
fn def_errors_with_comma_before_alternative_short_flag() {
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!("def test-command [ --long, (-l) ] {}");
|
2022-12-31 12:18:53 +01:00
|
|
|
|
|
|
|
assert!(actual.err.contains("expected parameter"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_comma_before_equals() {
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!("def test-command [ foo, = 1 ] {}");
|
2022-12-31 12:18:53 +01:00
|
|
|
|
|
|
|
assert!(actual.err.contains("expected parameter"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_comma_before_colon() {
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!("def test-command [ foo, : int ] {}");
|
2022-12-31 12:18:53 +01:00
|
|
|
|
|
|
|
assert!(actual.err.contains("expected parameter"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_multiple_colons() {
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!("def test-command [ foo::int ] {}");
|
2022-12-31 12:18:53 +01:00
|
|
|
assert!(actual.err.contains("expected type"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ignore = "This error condition is not implemented yet"]
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_multiple_types() {
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!("def test-command [ foo:int:string ] {}");
|
2022-12-31 12:18:53 +01:00
|
|
|
|
|
|
|
assert!(actual.err.contains("expected parameter"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_multiple_commas() {
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!("def test-command [ foo,,bar ] {}");
|
2022-12-31 12:18:53 +01:00
|
|
|
|
|
|
|
assert!(actual.err.contains("expected parameter"));
|
|
|
|
}
|
|
|
|
|
2022-12-22 21:31:34 +01:00
|
|
|
#[test]
|
|
|
|
fn def_fails_with_invalid_name() {
|
2022-12-28 00:00:44 +01:00
|
|
|
let err_msg = "command name can't be a number, a filesize, or contain a hash # or caret ^";
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!(r#"def 1234 = echo "test""#);
|
2022-12-28 00:00:44 +01:00
|
|
|
assert!(actual.err.contains(err_msg));
|
2022-12-22 21:31:34 +01:00
|
|
|
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!(r#"def 5gib = echo "test""#);
|
2022-12-28 00:00:44 +01:00
|
|
|
assert!(actual.err.contains(err_msg));
|
2022-12-22 21:31:34 +01:00
|
|
|
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!(r#"def ^foo [] {}"#);
|
2022-12-28 00:00:44 +01:00
|
|
|
assert!(actual.err.contains(err_msg));
|
2022-12-22 21:31:34 +01:00
|
|
|
}
|
2023-02-22 13:53:11 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_with_list() {
|
|
|
|
Playground::setup("def_with_list", |dirs, _| {
|
|
|
|
let data = r#"
|
|
|
|
def e [
|
|
|
|
param: list
|
|
|
|
] {echo $param};
|
|
|
|
"#;
|
|
|
|
fs::write(dirs.root().join("def_test"), data).expect("Unable to write file");
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.root(),
|
|
|
|
"source def_test; e [one] | to json -r"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.out.contains(r#"one"#));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_with_default_list() {
|
|
|
|
Playground::setup("def_with_default_list", |dirs, _| {
|
|
|
|
let data = r#"
|
|
|
|
def f [
|
|
|
|
param: list = [one]
|
|
|
|
] {echo $param};
|
|
|
|
"#;
|
|
|
|
fs::write(dirs.root().join("def_test"), data).expect("Unable to write file");
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.root(),
|
|
|
|
"source def_test; f | to json -r"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.out.contains(r#"["one"]"#));
|
|
|
|
})
|
|
|
|
}
|
2023-03-16 21:08:41 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_with_paren_params() {
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!("def foo (x: int, y: int) { $x + $y }; foo 1 2");
|
2023-03-16 21:08:41 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "3");
|
|
|
|
}
|
2023-04-28 09:06:43 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extern_with_block() {
|
2023-07-14 23:51:28 +02:00
|
|
|
let actual = nu!(
|
|
|
|
"extern-wrapped foo [...rest] { print ($rest | str join ',' ) }; foo --bar baz -- -q -u -x"
|
|
|
|
);
|
2023-04-28 09:06:43 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "--bar,baz,--,-q,-u,-x");
|
|
|
|
}
|