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