diff --git a/src/uu/expr/src/expr.rs b/src/uu/expr/src/expr.rs index 508359e52..7c262da87 100644 --- a/src/uu/expr/src/expr.rs +++ b/src/uu/expr/src/expr.rs @@ -74,7 +74,7 @@ fn process_expr(token_strings: &[&str]) -> Result { fn print_expr_ok(expr_result: &str) -> UResult<()> { println!("{}", expr_result); - if expr_result == "0" || expr_result.is_empty() { + if expr_result.parse::() == Ok(0) || expr_result.is_empty() { Err(1.into()) } else { Ok(()) diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index dbd197017..6a112df75 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -478,7 +478,7 @@ fn prefix_operator_index(values: &[String]) -> String { for (current_idx, ch_h) in haystack.chars().enumerate() { for ch_n in needles.chars() { if ch_n == ch_h { - return current_idx.to_string(); + return (current_idx + 1).to_string(); } } } diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index 3a753aa1b..17362bcae 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -1,7 +1,35 @@ -// spell-checker:ignore αbcdef +// spell-checker:ignore αbcdef ; (people) kkos use crate::common::util::*; +#[test] +fn test_simple_values() { + // null or 0 => EXIT_VALUE == 1 + new_ucmd!() + .args(&[""]) + .fails() + .status_code(1) + .stdout_only("\n"); + new_ucmd!() + .args(&["0"]) + .fails() + .status_code(1) + .stdout_only("0\n"); + new_ucmd!() + .args(&["00"]) + .fails() + .status_code(1) + .stdout_only("00\n"); + new_ucmd!() + .args(&["-0"]) + .fails() + .status_code(1) + .stdout_only("-0\n"); + + // non-null and non-0 => EXIT_VALUE = 0 + new_ucmd!().args(&["1"]).succeeds().stdout_only("1\n"); +} + #[test] fn test_simple_arithmetic() { new_ucmd!() @@ -97,6 +125,39 @@ fn test_and() { new_ucmd!().args(&["", "&", "1"]).run().stdout_is("0\n"); } +#[test] +fn test_index() { + new_ucmd!() + .args(&["index", "αbcdef", "x"]) + .fails() + .status_code(1) + .stdout_only("0\n"); + new_ucmd!() + .args(&["index", "αbcdef", "α"]) + .succeeds() + .stdout_only("1\n"); + new_ucmd!() + .args(&["index", "αbc_δef", "δ"]) + .succeeds() + .stdout_only("5\n"); + new_ucmd!() + .args(&["index", "αbc_δef", "δf"]) + .succeeds() + .stdout_only("5\n"); + new_ucmd!() + .args(&["index", "αbcdef", "fb"]) + .succeeds() + .stdout_only("2\n"); + new_ucmd!() + .args(&["index", "αbcdef", "f"]) + .succeeds() + .stdout_only("6\n"); + new_ucmd!() + .args(&["index", "αbcdef_f", "f"]) + .succeeds() + .stdout_only("6\n"); +} + #[test] fn test_length_fail() { new_ucmd!().args(&["length", "αbcdef", "1"]).fails(); @@ -118,6 +179,27 @@ fn test_length_mb() { .stdout_only("6\n"); } +#[test] +fn test_regex() { + // FixME: [2022-12-19; rivy] test disabled as it currently fails due to 'oniguruma' bug (see GH:kkos/oniguruma/issues/279) + // new_ucmd!() + // .args(&["a^b", ":", "a^b"]) + // .succeeds() + // .stdout_only("3\n"); + new_ucmd!() + .args(&["a^b", ":", "a\\^b"]) + .succeeds() + .stdout_only("3\n"); + new_ucmd!() + .args(&["a$b", ":", "a\\$b"]) + .succeeds() + .stdout_only("3\n"); + new_ucmd!() + .args(&["-5", ":", "-\\{0,1\\}[0-9]*$"]) + .succeeds() + .stdout_only("2\n"); +} + #[test] fn test_substr() { new_ucmd!()