From 3478f35330d91d730258769f6666835714c0159a Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Fri, 7 Jan 2022 07:32:47 +1100 Subject: [PATCH] Default the values of named params (#695) --- crates/nu-engine/src/eval.rs | 20 ++++++++++++-------- src/tests/test_engine.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 8216e29792..ac81b21cc2 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -111,14 +111,18 @@ fn eval_call( } } - if !found && named.arg.is_none() { - callee_stack.add_var( - var_id, - Value::Bool { - val: false, - span: call.head, - }, - ) + if !found { + if named.arg.is_none() { + callee_stack.add_var( + var_id, + Value::Bool { + val: false, + span: call.head, + }, + ) + } else { + callee_stack.add_var(var_id, Value::Nothing { span: call.head }) + } } } } diff --git a/src/tests/test_engine.rs b/src/tests/test_engine.rs index 6f515d8ff0..240f2d1cb0 100644 --- a/src/tests/test_engine.rs +++ b/src/tests/test_engine.rs @@ -87,3 +87,35 @@ fn earlier_errors() -> TestResult { "int", ) } + +#[test] +fn missing_flags_are_nothing() -> TestResult { + run_test( + r#"def foo [--aaa(-a): int, --bbb(-b): int] { (if $aaa == $nothing { 10 } else { $aaa }) + (if $bbb == $nothing { 100 } else { $bbb }) }; foo"#, + "110", + ) +} + +#[test] +fn missing_flags_are_nothing2() -> TestResult { + run_test( + r#"def foo [--aaa(-a): int, --bbb(-b): int] { (if $aaa == $nothing { 10 } else { $aaa }) + (if $bbb == $nothing { 100 } else { $bbb }) }; foo -a 90"#, + "190", + ) +} + +#[test] +fn missing_flags_are_nothing3() -> TestResult { + run_test( + r#"def foo [--aaa(-a): int, --bbb(-b): int] { (if $aaa == $nothing { 10 } else { $aaa }) + (if $bbb == $nothing { 100 } else { $bbb }) }; foo -b 45"#, + "55", + ) +} + +#[test] +fn missing_flags_are_nothing4() -> TestResult { + run_test( + r#"def foo [--aaa(-a): int, --bbb(-b): int] { (if $aaa == $nothing { 10 } else { $aaa }) + (if $bbb == $nothing { 100 } else { $bbb }) }; foo -a 3 -b 10000"#, + "10003", + ) +}