mirror of
https://github.com/nushell/nushell.git
synced 2026-11-24 10:27:07 +08:00
## Description This is a follow up to #18796, regarding to this: ``` def g [--x: int = 5] { $x } g --x=(null) # 5 (omitted → default) note: in this example null is wrapped in parens because this syntax would fail with `g --x=null` as you're typing due to parse checking. ``` If we allow passing `(null)` to `g`, I think it's better to allow passing `null` without parentheses. ## User-facing changes (Release notes) Users can pass `null` directly to flag value. ``` def g [--x: int = 5] { $x } g --x=null # 5 ```
679 lines
19 KiB
Rust
679 lines
19 KiB
Rust
use miette::Diagnostic;
|
|
use nu_protocol::{ParseError, ShellError};
|
|
use nu_test_support::prelude::*;
|
|
use pretty_assertions::assert_matches;
|
|
use rstest::rstest;
|
|
|
|
#[test]
|
|
fn spread_in_list() -> Result {
|
|
test().run("[...[]]").expect_value_eq(test_value!([]))?;
|
|
test()
|
|
.run("[1 2 ...[[3] {x: 1}] 5]")
|
|
.expect_value_eq(test_value!([1, 2, [3], { x: 1 }, 5]))?;
|
|
test()
|
|
.run(r#"[...("foo" | split chars) 10]"#)
|
|
.expect_value_eq(test_value!(["f", "o", "o", 10]))?;
|
|
test()
|
|
.run("let l = [1, 2, [3]]; [...$l $l]")
|
|
.expect_value_eq(test_value!([1, 2, [3], [1, 2, [3]]]))?;
|
|
test()
|
|
.run("[ ...[ ...[ ...[ a ] b ] c ] d ]")
|
|
.expect_value_eq(["a", "b", "c", "d"])
|
|
}
|
|
|
|
#[test]
|
|
fn not_spread() -> Result {
|
|
test()
|
|
.run("def ... [x] { $x }; ... ...")
|
|
.expect_value_eq("...")?;
|
|
test()
|
|
.run("let a = 4; [... $a ... [1] ... (5) ...bare ...]")
|
|
.expect_value_eq(test_value!([
|
|
"...",
|
|
4,
|
|
"...",
|
|
[1],
|
|
"...",
|
|
5,
|
|
"...bare",
|
|
"..."
|
|
]))
|
|
}
|
|
|
|
#[test]
|
|
fn bad_spread_on_non_list() -> Result {
|
|
let err = test().run("let x = 5; [...$x]").expect_shell_error()?;
|
|
assert_matches!(err, ShellError::CannotSpreadAsList { .. });
|
|
|
|
let err = test().run("[...({ x: 1 })]").expect_shell_error()?;
|
|
assert_matches!(err, ShellError::CannotSpreadAsList { .. });
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn spread_type_list() -> Result {
|
|
test()
|
|
.run("def f [a: list<int>] { $a | describe }; f [1 ...[]]")
|
|
.expect_value_eq("list<int>")?;
|
|
test()
|
|
.run("def f [a: list<int>] { $a | describe }; f [1 ...[2]]")
|
|
.expect_value_eq("list<int>")?;
|
|
|
|
let err = test()
|
|
.run(r#"def f [a: list<int>] { }; f ["foo" ...[4 5 6]]"#)
|
|
.expect_parse_error()?;
|
|
assert_matches!(err, ParseError::Expected(expected, _) if expected == "int");
|
|
|
|
let err = test()
|
|
.run(r#"def f [a: list<int>] { }; f [1 2 ...["misfit"] 4]"#)
|
|
.expect_parse_error()?;
|
|
assert_matches!(err, ParseError::Expected(expected, _) if expected == "int");
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn spread_in_record() -> Result {
|
|
test()
|
|
.run("{...{} ...{}, a: 1}")
|
|
.expect_value_eq(test_record! { "a" => 1 })?;
|
|
test()
|
|
.run("{...{...{...{}}}}")
|
|
.expect_value_eq(test_record! {})?;
|
|
test()
|
|
.run("{foo: bar ...{a: {x: 1}} b: 3}")
|
|
.expect_value_eq(test_value!({ foo: "bar", a: { x: 1 }, b: 3 }))
|
|
}
|
|
|
|
/// `second` and `first` are the source text the "field redefined here" and "field first defined
|
|
/// here" labels should cover, `help` whether the spread help should be shown.
|
|
#[rstest]
|
|
#[case::spread_after_pair("{a: 1, ...{a: 3}}", "...{a: 3}", "a: 1", true)]
|
|
#[case::pair_after_spread("{...{a: 4, x: 3}, x: 1}", "x: 1", "...{a: 4, x: 3}", true)]
|
|
#[case::spread_after_spread("{...{a: 0, x: 2}, ...{x: 5}}", "...{x: 5}", "...{a: 0, x: 2}", true)]
|
|
#[case::variable_spread("let a = {s: 1}; {...$a, s: 3}", "s: 3", "...$a", true)]
|
|
#[case::nested_def(
|
|
"
|
|
def outer [] {
|
|
let args = {s: 1}
|
|
def recurse [--s: int] {
|
|
recurse ...{...$args, s: 3}
|
|
}
|
|
recurse ...$args
|
|
}
|
|
outer
|
|
",
|
|
"s: 3",
|
|
"...$args",
|
|
true
|
|
)]
|
|
#[case::spread_after_pair_with_value_from_elsewhere(
|
|
"def g [] { {b: 9} }; let r = {a: 3}; {a: (g).b, ...$r}",
|
|
"...$r",
|
|
"a: (g).b",
|
|
true
|
|
)]
|
|
#[case::spread_between_pairs("{x: 1, ...{a: 2}, y: 3, a: 4}", "a: 4", "...{a: 2}", true)]
|
|
#[case::empty_spread_before_pair("{...{}, a: 1, ...{a: 2}}", "...{a: 2}", "a: 1", true)]
|
|
#[case::pair_after_unrelated_spread(
|
|
"let k = 'x'; {...{a: 1}, $k: 2, $k: 3}",
|
|
"$k: 3",
|
|
"$k: 2",
|
|
false
|
|
)]
|
|
#[case::dynamic_key("let k = 'a'; {$k: 1, $k: 2}", "$k: 2", "$k: 1", false)]
|
|
#[case::table_header("let k = 'a'; [[$k $k]; [1 2]]", "2", "1", false)]
|
|
fn duplicate_cols(
|
|
#[case] code: &str,
|
|
#[case] second: &str,
|
|
#[case] first: &str,
|
|
#[case] help: bool,
|
|
) -> Result {
|
|
let err = test().run(code).expect_shell_error()?;
|
|
assert_matches!(err, ShellError::ColumnDefinedTwice { .. });
|
|
assert_eq!(
|
|
err.help().map(|help| help.to_string()).as_deref(),
|
|
help.then_some(ShellError::RECORD_SPREAD_DUPLICATE_HELP)
|
|
);
|
|
|
|
// Label offsets are relative to wherever the test source starts, so compare each label to
|
|
// where its text sits in `code`.
|
|
let labels: Vec<_> = err.labels().unwrap().collect();
|
|
assert_eq!(labels.len(), 2);
|
|
assert_eq!(labels[0].label(), Some("field redefined here"));
|
|
assert_eq!(labels[1].label(), Some("field first defined here"));
|
|
let second_at = code.find(second).unwrap();
|
|
let first_at = code.find(first).unwrap();
|
|
assert_eq!(labels[0].len(), second.len(), "second use in {code:?}");
|
|
assert_eq!(labels[1].len(), first.len(), "first use in {code:?}");
|
|
assert_eq!(
|
|
labels[0].offset() - second_at,
|
|
labels[1].offset() - first_at,
|
|
"label positions in {code:?}"
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn bad_spread_on_non_record() -> Result {
|
|
let err = test().run("let x = 5; { ...$x }").expect_shell_error()?;
|
|
assert_matches!(err, ShellError::CannotSpreadAsRecord { .. });
|
|
|
|
let err = test().run("{...([1, 2])}").expect_shell_error()?;
|
|
assert_matches!(err, ShellError::CannotSpreadAsRecord { .. });
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn spread_type_record() -> Result {
|
|
test()
|
|
.run("def f [a: record<x: int>] { $a.x }; f { ...{x: 0} }")
|
|
.expect_value_eq(0)?;
|
|
|
|
let err = test()
|
|
.run(r#"def f [a: record<x: int>] {}; f { ...{x: "not an int"} }"#)
|
|
.expect_parse_error()?;
|
|
assert_matches!(err, ParseError::TypeMismatch(..));
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
#[deps(NU, TESTBIN_COCOCO)]
|
|
fn spread_external_args() -> Result {
|
|
test()
|
|
.run(r#"cococo ...[1 "foo"] 2 ...[3 "bar"]"#)
|
|
.expect_value_eq("1 foo 2 3 bar")?;
|
|
|
|
// exec doesn't have rest parameters but allows unknown arguments
|
|
test()
|
|
.run(r#"nu -n -c 'exec cococo "foo" ...[5 6]'"#)
|
|
.expect_value_eq("foo 5 6")
|
|
}
|
|
|
|
#[test]
|
|
fn spread_internal_args() -> Result {
|
|
let code = r#"
|
|
let list = ["foo" 4]
|
|
def f [a b c? d? ...x] { [$a $b $c $d $x] }
|
|
f 1 2 ...[5 6] 7 ...$list
|
|
"#;
|
|
test()
|
|
.run(code)
|
|
.expect_value_eq(test_value!([1, 2, (), (), [5, 6, 7, "foo", 4]]))?;
|
|
|
|
let code = "
|
|
def f [a b c? d? ...x] { [$a $b $c $d $x] }
|
|
f 1 2 3 ...[5 6]
|
|
";
|
|
test()
|
|
.run(code)
|
|
.expect_value_eq(test_value!([1, 2, 3, (), [5, 6]]))?;
|
|
|
|
let code = "
|
|
def f [--flag: int ...x] { [$flag $x] }
|
|
f 2 ...[foo] 4 --flag 5 6 ...[7 8]
|
|
";
|
|
test()
|
|
.run(code)
|
|
.expect_value_eq(test_value!([5, [2, "foo", 4, 6, 7, 8]]))?;
|
|
|
|
let code = "
|
|
def f [a b? --flag: int ...x] { [$a $b $flag $x] }
|
|
f 1 ...[foo] 4 --flag 5 6 ...[7 8]
|
|
";
|
|
test()
|
|
.run(code)
|
|
.expect_value_eq(test_value!([1, (), 5, ["foo", 4, 6, 7, 8]]))
|
|
}
|
|
|
|
#[test]
|
|
fn bad_spread_internal_args() -> Result {
|
|
let code = "
|
|
def f [a b c? d? ...x] { echo $a $b $c $d $x }
|
|
f 1 ...[5 6]
|
|
";
|
|
let err = test().run(code).expect_parse_error()?;
|
|
assert_matches!(err, ParseError::MissingPositional(name, _, _) if name == "b");
|
|
|
|
let code = "
|
|
def f [a b?] { echo a b c d }
|
|
f ...[5 6]
|
|
";
|
|
let err = test().run(code).expect_parse_error()?;
|
|
assert_matches!(err, ParseError::UnexpectedSpreadArg(_, _));
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
#[deps(TESTBIN_COCOCO)]
|
|
fn spread_non_list_args() -> Result {
|
|
let err = test().run("echo ...(1)").expect_shell_error()?;
|
|
assert_matches!(err, ShellError::CannotSpreadAsList { .. });
|
|
|
|
let err = test().run("cococo ...(1)").expect_shell_error()?;
|
|
assert_matches!(err, ShellError::CannotSpreadAsList { .. });
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn spread_args_type() -> Result {
|
|
let err = test()
|
|
.run(r#"def f [...x: int] {}; f ...["abc"]"#)
|
|
.expect_parse_error()?;
|
|
assert_matches!(err, ParseError::Expected(expected, _) if expected == "int");
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn explain_spread_args() -> Result {
|
|
test()
|
|
.run("(explain { || echo ...[1 2] }).cmd_args.0 | select arg_type name type")
|
|
.expect_value_eq(test_table![
|
|
["arg_type", "name", "type"];
|
|
["spread", "[1 2]", "list<int>"],
|
|
])
|
|
}
|
|
|
|
#[test]
|
|
#[deps(TESTBIN_MEOW)]
|
|
fn disallow_implicit_spread_for_externals() -> Result {
|
|
let err = test().run("^meow [1 2]").expect_shell_error()?;
|
|
assert_matches!(err, ShellError::CannotPassListToExternal { .. });
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn respect_shape() -> Result {
|
|
let err = test()
|
|
.run("def foo [...rest] { ...$rest }; foo bar baz")
|
|
.expect_shell_error()?;
|
|
assert_matches!(err, ShellError::ExternalCommand { .. });
|
|
|
|
let err = test().run("module foo { ...$bar }").expect_parse_error()?;
|
|
assert_matches!(err, ParseError::ExpectedKeyword(_, _));
|
|
|
|
test()
|
|
.run(r#"def "...$foo" [] {2}; do { ...$foo }"#)
|
|
.expect_value_eq(2)?;
|
|
test()
|
|
.run(r#"match "...$foo" { ...$foo => 5 }"#)
|
|
.expect_value_eq(5)
|
|
}
|
|
|
|
#[test]
|
|
#[deps(TESTBIN_COCOCO)]
|
|
fn spread_null() -> Result {
|
|
// Spread in list
|
|
test().run("[1, 2, ...(null)]").expect_value_eq([1, 2])?;
|
|
|
|
// Spread in record
|
|
test()
|
|
.run("{a: 1, b: 2, ...(null)}")
|
|
.expect_value_eq(test_record! { "a" => 1, "b" => 2 })?;
|
|
|
|
// Spread to built-in command's ...rest
|
|
test().run("echo 1 2 ...(null)").expect_value_eq([1, 2])?;
|
|
|
|
// Spread to custom command's ...rest
|
|
let code = "
|
|
def foo [...rest] { $rest }
|
|
foo ...(null) 1 2 ...(null) 3
|
|
";
|
|
test().run(code).expect_value_eq([1, 2, 3])?;
|
|
|
|
// Spread to external command's arguments
|
|
test().run("cococo 1 ...(null) 2").expect_value_eq("1 2")?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_null_is_omitted() -> Result {
|
|
// Bare null named values use the signature default in every supported flag syntax
|
|
let code = "
|
|
def f [--x (-x): int = 5] { $x }
|
|
f --x=null
|
|
";
|
|
test().run(code).expect_value_eq(5)?;
|
|
|
|
let code = "
|
|
def f [--x (-x): int = 5] { $x }
|
|
f --x null
|
|
";
|
|
test().run(code).expect_value_eq(5)?;
|
|
|
|
let code = "
|
|
def f [--x (-x): int = 5] { $x }
|
|
f -x null
|
|
";
|
|
test().run(code).expect_value_eq(5)?;
|
|
|
|
let code = "
|
|
def f [--switch] { $switch }
|
|
f --switch=null
|
|
";
|
|
test().run(code).expect_value_eq(false)?;
|
|
|
|
// A parenthesized null named value also uses the signature default
|
|
let code = "
|
|
def f [--x: int = 5] { $x }
|
|
f --x=(null)
|
|
";
|
|
test().run(code).expect_value_eq(5)?;
|
|
|
|
// Null named value without default is same as omitting the flag
|
|
let code = "
|
|
def f [--x: int] { $x }
|
|
f --x=(null)
|
|
";
|
|
test().run(code).expect_value_eq(())?;
|
|
|
|
// Forwarding an unbound optional flag (null) into another command
|
|
let code = "
|
|
def outer [--preserve: list<string>] {
|
|
inner --preserve=$preserve
|
|
}
|
|
def inner [--preserve: list<string>] {
|
|
$preserve
|
|
}
|
|
outer
|
|
";
|
|
test().run(code).expect_value_eq(())?;
|
|
|
|
// Explicit empty list is distinct from null/omit
|
|
let code = "
|
|
def outer [--preserve: list<string>] {
|
|
inner --preserve=$preserve
|
|
}
|
|
def inner [--preserve: list<string>] {
|
|
$preserve
|
|
}
|
|
outer --preserve=[]
|
|
";
|
|
test().run(code).expect_value_eq(test_value!([]))?;
|
|
|
|
// Null switch value is treated as omitted (false)
|
|
let code = "
|
|
def f [--verbose] { $verbose }
|
|
f --verbose=(null)
|
|
";
|
|
test().run(code).expect_value_eq(false)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[rstest]
|
|
#[case::short("-x null")]
|
|
#[case::long("--x null")]
|
|
#[case::equals("--x=null")]
|
|
fn custom_command_alias_omits_bare_null_flag_value(#[case] flag: &str) -> Result {
|
|
test()
|
|
.run(format!(
|
|
"def f [--x (-x): int = 5] {{ $x }}; alias a = f; a {flag}"
|
|
))
|
|
.expect_value_eq(5)
|
|
}
|
|
|
|
#[rstest]
|
|
#[case::custom("def f [--x: int] { $x }; f --x abc", "int")]
|
|
#[case::builtin(r#""10" | into int --radix abc"#, "number")]
|
|
fn invalid_flag_value_keeps_shape_error(#[case] code: &str, #[case] expected: &str) -> Result {
|
|
let err = test().run(code).expect_parse_error()?;
|
|
assert_matches!(
|
|
err,
|
|
ParseError::Expected(actual, _) if actual == expected
|
|
);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn bare_null_keeps_flag_shape_behavior() -> Result {
|
|
test()
|
|
.run("def f [--c: cell-path] { $c | describe }; f --c null")
|
|
.expect_value_eq("cell-path")
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_null_passed_when_type_allows_nothing() -> Result {
|
|
// oneof with nothing: a bare explicit null is bound
|
|
let code = "
|
|
def f [--x: oneof<int, nothing> = 5] { $x }
|
|
f --x=null
|
|
";
|
|
test().run(code).expect_value_eq(())?;
|
|
|
|
// A parenthesized explicit null is also bound
|
|
let code = "
|
|
def f [--x: oneof<int, nothing> = 5] { $x }
|
|
f --x=(null)
|
|
";
|
|
test().run(code).expect_value_eq(())?;
|
|
|
|
// Omitting the flag still uses its default
|
|
let code = "
|
|
def f [--x: oneof<int, nothing> = 5] { $x }
|
|
f
|
|
";
|
|
test().run(code).expect_value_eq(5)?;
|
|
|
|
let code = "
|
|
def f [--x: oneof<int, nothing> = 5] { $x }
|
|
f --x=3
|
|
";
|
|
test().run(code).expect_value_eq(3)?;
|
|
|
|
// `any` accepts nothing, so null is passed through (not the default)
|
|
let code = "
|
|
def f [--x: any = 5] { $x }
|
|
f --x=(null)
|
|
";
|
|
test().run(code).expect_value_eq(())?;
|
|
|
|
// `nothing` type: null is passed through
|
|
let code = "
|
|
def f [--x: nothing] { $x }
|
|
f --x=(null)
|
|
";
|
|
test().run(code).expect_value_eq(())?;
|
|
|
|
// Record spread: null passes when type allows nothing
|
|
let code = "
|
|
def f [--x: oneof<int, nothing> = 5] { $x }
|
|
f ...{x: null}
|
|
";
|
|
test().run(code).expect_value_eq(())?;
|
|
|
|
// Record spread: null still omits when type does not allow nothing
|
|
let code = "
|
|
def f [--x: int = 5] { $x }
|
|
f ...{x: null}
|
|
";
|
|
test().run(code).expect_value_eq(5)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_record_spread() -> Result {
|
|
let code = r#"
|
|
def f [--x: int, --y: string, --verbose] {
|
|
{x: $x, y: $y, verbose: $verbose}
|
|
}
|
|
f ...{x: 1, y: "a", verbose: true}
|
|
"#;
|
|
test().run(code).expect_value_eq(test_value!({
|
|
x: 1,
|
|
y: "a",
|
|
verbose: true
|
|
}))?;
|
|
|
|
// Null fields are omitted (defaults / null apply)
|
|
let code = r#"
|
|
def f [--x: int = 9, --y: string, --verbose] {
|
|
{x: $x, y: $y, verbose: $verbose}
|
|
}
|
|
f ...{x: null, y: "hi", verbose: false}
|
|
"#;
|
|
test().run(code).expect_value_eq(test_value!({
|
|
x: 9,
|
|
y: "hi",
|
|
verbose: false
|
|
}))?;
|
|
|
|
// Dynamic record variable
|
|
let code = r#"
|
|
def f [--x: int, --y: string] { [$x $y] }
|
|
let flags = {x: 3, y: "z"}
|
|
f ...$flags
|
|
"#;
|
|
test().run(code).expect_value_eq(test_value!([3, "z"]))?;
|
|
|
|
// Combine named spread with rest positionals
|
|
let code = "
|
|
def f [--flag: int, ...rest] { {flag: $flag, rest: $rest} }
|
|
f ...{flag: 7} a b
|
|
";
|
|
test().run(code).expect_value_eq(test_value!({
|
|
flag: 7,
|
|
rest: ["a", "b"]
|
|
}))?;
|
|
|
|
// Shadowing-style call: flags record + rest paths
|
|
let code = "
|
|
def wrap [--preserve: list<string>, --recursive, ...rest] {
|
|
inner ...{
|
|
preserve: $preserve
|
|
recursive: $recursive
|
|
} ...$rest
|
|
}
|
|
def inner [--preserve: list<string>, --recursive, ...rest] {
|
|
{preserve: $preserve, recursive: $recursive, rest: $rest}
|
|
}
|
|
wrap src dest
|
|
";
|
|
test().run(code).expect_value_eq(test_value!({
|
|
preserve: (),
|
|
recursive: false,
|
|
rest: ["src", "dest"]
|
|
}))?;
|
|
|
|
let code = "
|
|
def wrap [--preserve: list<string>, --recursive, ...rest] {
|
|
inner ...{
|
|
preserve: $preserve
|
|
recursive: $recursive
|
|
} ...$rest
|
|
}
|
|
def inner [--preserve: list<string>, --recursive, ...rest] {
|
|
{preserve: $preserve, recursive: $recursive, rest: $rest}
|
|
}
|
|
wrap --preserve=[mode] --recursive src dest
|
|
";
|
|
test().run(code).expect_value_eq(test_value!({
|
|
preserve: ["mode"],
|
|
recursive: true,
|
|
rest: ["src", "dest"]
|
|
}))?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_record_spread_unknown_flag_errors() -> Result {
|
|
let code = "
|
|
def f [--x: int] { $x }
|
|
f ...{x: 1, nope: true}
|
|
";
|
|
let err = test().run(code).expect_shell_error()?;
|
|
assert_matches!(err, ShellError::Generic(_));
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_list_spread_before_required_errors() -> Result {
|
|
// Dual-purpose: dynamic list before required positionals must error (not leave them unbound)
|
|
let code = "
|
|
def f [a: string, --x: int, ...rest] { {a: $a, x: $x, rest: $rest} }
|
|
let list = [1]
|
|
f ...$list hello
|
|
";
|
|
let err = test().run(code).expect_shell_error()?;
|
|
assert_matches!(err, ShellError::Generic(_));
|
|
|
|
// Null rest-mode before required positionals must also error
|
|
let code = "
|
|
def f [a: string, --x: int, ...rest] { {a: $a, x: $x, rest: $rest} }
|
|
f ...(null) hello
|
|
";
|
|
let err = test().run(code).expect_shell_error()?;
|
|
assert_matches!(err, ShellError::Generic(_));
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_record_spread_required_named_deferred() -> Result {
|
|
// Bare required named still parse-errors
|
|
let err = test().run("stor create").expect_parse_error()?;
|
|
assert_matches!(err, ParseError::MissingRequiredFlag(..));
|
|
|
|
// With a record spread, required named is deferred to runtime (no MissingRequiredFlag)
|
|
test()
|
|
.run(r#"stor create ...{table-name: "t_spread_ok", columns: {id: int}} | describe"#)
|
|
.expect_value_eq("SQLiteDatabase")?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_list_spread_without_rest_errors() -> Result {
|
|
// Dynamic list on a named-only command must not silently drop the list.
|
|
let code = "
|
|
def f [--x: int] { $x }
|
|
let list = [1]
|
|
f ...$list
|
|
";
|
|
let err = test().run(code).expect_shell_error()?;
|
|
assert_matches!(err, ShellError::Generic(_));
|
|
|
|
// Explicit list spread is still a parse error (no rest).
|
|
let err = test()
|
|
.run("def f [--x: int] { $x }; f ...[1]")
|
|
.expect_parse_error()?;
|
|
assert_matches!(err, ParseError::UnexpectedSpreadArg(_, _));
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_record_spread_type_mismatch_errors() -> Result {
|
|
let code = r#"
|
|
def f [--x: int] { $x }
|
|
f ...{x: "hi"}
|
|
"#;
|
|
let err = test().run(code).expect_shell_error()?;
|
|
assert_matches!(err, ShellError::CantConvert { .. });
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_dynamic_record_before_required_positional() -> Result {
|
|
// Dual-purpose commands: flag record before required positionals is allowed.
|
|
let code = "
|
|
def f [a: string, --x: int, ...rest] { {a: $a, x: $x, rest: $rest} }
|
|
let flags = {x: 7}
|
|
f ...$flags hello more
|
|
";
|
|
test().run(code).expect_value_eq(test_value!({
|
|
a: "hello",
|
|
x: 7,
|
|
rest: ["more"]
|
|
}))
|
|
}
|
|
|
|
#[test]
|
|
fn named_flag_record_spread_without_named_params_is_parse_error() -> Result {
|
|
let err = test()
|
|
.run("def f [] { 1 }; f ...{x: 1}")
|
|
.expect_parse_error()?;
|
|
assert_matches!(err, ParseError::UnexpectedSpreadArg(_, _));
|
|
Ok(())
|
|
}
|