2021-01-07 18:14:51 +01:00
|
|
|
use nu_test_support::playground::Playground;
|
2022-07-10 10:32:52 +02:00
|
|
|
use nu_test_support::{nu, pipeline};
|
2021-01-07 18:14:51 +01:00
|
|
|
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"#));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-10 10:32:52 +02:00
|
|
|
#[test]
|
|
|
|
fn def_errors_with_multiple_short_flags() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def test-command [ --long(-l)(-o) ] {}
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
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() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def test-command [ --long, (-l) ] {}
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("expected parameter"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_comma_before_equals() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def test-command [ foo, = 1 ] {}
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("expected parameter"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_comma_before_colon() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def test-command [ foo, : int ] {}
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("expected parameter"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_multiple_colons() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def test-command [ foo::int ] {}
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("expected type"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ignore = "This error condition is not implemented yet"]
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_multiple_types() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def test-command [ foo:int:string ] {}
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("expected parameter"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn def_errors_with_multiple_commas() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def test-command [ foo,,bar ] {}
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
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 ^";
|
2022-12-22 21:31:34 +01:00
|
|
|
let actual = nu!(
|
2022-12-28 00:00:44 +01:00
|
|
|
cwd: ".", pipeline(
|
2022-12-22 21:31:34 +01:00
|
|
|
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
|
|
|
|
|
|
|
let actual = nu!(
|
2022-12-28 00:00:44 +01:00
|
|
|
cwd: ".", pipeline(
|
2022-12-22 21:31:34 +01:00
|
|
|
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
|
|
|
|
|
|
|
let actual = nu!(
|
2022-12-28 00:00:44 +01:00
|
|
|
cwd: ".", pipeline(
|
2022-12-22 21:31:34 +01:00
|
|
|
r#"
|
2022-12-28 00:00:44 +01:00
|
|
|
def ^foo [] {}
|
2022-12-22 21:31:34 +01:00
|
|
|
"#
|
|
|
|
));
|
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_errors_with_specified_list_type() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: ".", pipeline(
|
|
|
|
r#"
|
|
|
|
def test-command [ foo: list<any> ] {}
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("unknown type"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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"]"#));
|
|
|
|
})
|
|
|
|
}
|