Default the values of named params (#695)

This commit is contained in:
JT 2022-01-07 07:32:47 +11:00 committed by GitHub
parent eab6b322bb
commit 3478f35330
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 8 deletions

View File

@ -111,14 +111,18 @@ fn eval_call(
} }
} }
if !found && named.arg.is_none() { if !found {
callee_stack.add_var( if named.arg.is_none() {
var_id, callee_stack.add_var(
Value::Bool { var_id,
val: false, Value::Bool {
span: call.head, val: false,
}, span: call.head,
) },
)
} else {
callee_stack.add_var(var_id, Value::Nothing { span: call.head })
}
} }
} }
} }

View File

@ -87,3 +87,35 @@ fn earlier_errors() -> TestResult {
"int", "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",
)
}