Skip to content

Commit

Permalink
Merge pull request uutils#5416 from Luv-Ray/fix-expr-issue5411
Browse files Browse the repository at this point in the history
`expr`: return 0 for `"" \| ""`
  • Loading branch information
cakebaker committed Oct 17, 2023
2 parents f180558 + d325a95 commit 895e136
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/uu/expr/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,15 @@ fn infix_operator_or(values: &[String]) -> String {
assert!(values.len() == 2);
if value_as_bool(&values[0]) {
values[0].clone()
} else {
} else if value_as_bool(&values[1]) {
values[1].clone()
} else {
0.to_string()
}
}

fn infix_operator_and(values: &[String]) -> String {
assert!(values.len() == 2);
if value_as_bool(&values[0]) && value_as_bool(&values[1]) {
values[0].clone()
} else {
Expand Down
31 changes: 22 additions & 9 deletions tests/by-util/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ fn test_or() {
.args(&["12", "|", "9a", "+", "1"])
.succeeds()
.stdout_only("12\n");

new_ucmd!().args(&["", "|", ""]).run().stdout_only("0\n");

new_ucmd!().args(&["", "|", "0"]).run().stdout_only("0\n");

new_ucmd!().args(&["", "|", "00"]).run().stdout_only("0\n");

new_ucmd!().args(&["", "|", "-0"]).run().stdout_only("0\n");
}

#[test]
Expand All @@ -152,14 +160,20 @@ fn test_and() {
.succeeds()
.stdout_only("foo\n");

new_ucmd!().args(&["", "&", "1"]).run().stdout_is("0\n");

new_ucmd!().args(&["14", "&", "1"]).run().stdout_is("14\n");
new_ucmd!()
.args(&["14", "&", "1"])
.succeeds()
.stdout_only("14\n");

new_ucmd!()
.args(&["-14", "&", "1"])
.run()
.stdout_is("-14\n");
.succeeds()
.stdout_only("-14\n");

new_ucmd!()
.args(&["-1", "&", "10", "/", "5"])
.succeeds()
.stdout_only("-1\n");

new_ucmd!()
.args(&["0", "&", "a", "/", "5"])
Expand All @@ -171,10 +185,9 @@ fn test_and() {
.run()
.stdout_only("0\n");

new_ucmd!()
.args(&["-1", "&", "10", "/", "5"])
.succeeds()
.stdout_only("-1\n");
new_ucmd!().args(&["", "&", "1"]).run().stdout_only("0\n");

new_ucmd!().args(&["", "&", ""]).run().stdout_only("0\n");
}

#[test]
Expand Down

0 comments on commit 895e136

Please sign in to comment.