Skip to content

Commit

Permalink
Expand ${#unset_variable} to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
magicant committed Apr 23, 2024
1 parent 90c18ec commit 82704c2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 0 additions & 1 deletion yash-cli/tests/scripted_test/param-p.sh
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ __IN__
${#?} 1
__OUT__

: TODO yash is broken <<\__OUT__
test_oE -e 0 'length of unset variables, success'
unset u
echoraw ${#u}
Expand Down
2 changes: 2 additions & 0 deletions yash-semantics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `<TextUnit as Expand>::expand` now correctly expands an unset parameter with a
switch to an empty string regardless of the `Unset` shell option. Previously,
it would expand to an empty string only if the `Unset` shell option was on.
- The parameter expansion of an unset variable with a `Length` modifier now
correctly expands to `0` rather than an empty string.

## [0.1.0] - 2024-04-13

Expand Down
14 changes: 13 additions & 1 deletion yash-semantics/src/expansion/initial/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Expand for ParamRef<'_> {
Modifier::Length => {
// TODO Reject ${#*} and ${#@} in POSIX mode
match &mut value {
None => (),
None => value = Some(Value::scalar("0")),
Some(Value::Scalar(v)) => to_length(v),
Some(Value::Array(vs)) => vs.iter_mut().for_each(to_length),
}
Expand Down Expand Up @@ -189,6 +189,18 @@ pub mod tests {
assert_eq!(phrase, Phrase::Field(to_field("a1\u{30A4}")));
}

#[test]
fn length_of_unset() {
let mut env = yash_env::Env::new_virtual();
let mut env = Env::new(&mut env);
let mut param = param("foo");
param.modifier = Modifier::Length;
let param = ParamRef::from(&param);

let phrase = param.expand(&mut env).now_or_never().unwrap().unwrap();
assert_eq!(phrase, Phrase::Field(to_field("0")));
}

#[test]
fn length_of_scalar() {
let mut env = yash_env::Env::new_virtual();
Expand Down

0 comments on commit 82704c2

Please sign in to comment.