From 8e7dbb0bf6d9a28d3e2975c3bc1085b119322962 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Tue, 27 Dec 2022 20:31:39 -0600 Subject: [PATCH 1/6] test/expr: add 'index' operator tests --- tests/by-util/test_expr.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index 3a753aa1b..a286f2cf4 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -97,6 +97,31 @@ 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", "α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(); From be37e033be1998edc8d8b05839c9fda635b3ed2f Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Wed, 28 Dec 2022 02:19:39 -0600 Subject: [PATCH 2/6] test/expr: add more index testing of unicode strings --- tests/by-util/test_expr.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index a286f2cf4..6ff37075e 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -108,6 +108,14 @@ fn test_index() { .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() From fe954e582503bb49c1a1ced48390cfa7e3477876 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Tue, 27 Dec 2022 20:32:24 -0600 Subject: [PATCH 3/6] fix/expr: repair off-by-1 index operator error --- src/uu/expr/src/syntax_tree.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(); } } } From 812d811ff62b394e333febc65bbeaafbc55e345a Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Wed, 28 Dec 2022 02:20:57 -0600 Subject: [PATCH 4/6] test/expr: add simple value and EXIT_CODE tests --- tests/by-util/test_expr.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index 6ff37075e..483f2fa2c 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -2,6 +2,34 @@ 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!() From 8753fb95c396afe26ca074dcfd4c227618e48b07 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Wed, 28 Dec 2022 02:48:20 -0600 Subject: [PATCH 5/6] fix/expr: EXIT_CODE=1 for any return output parsable to 0 (or empty) --- src/uu/expr/src/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(()) From 737271de9630d488eac4518317851d90639f997d Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Wed, 28 Dec 2022 03:20:41 -0600 Subject: [PATCH 6/6] test/expr: add regex tests --- tests/by-util/test_expr.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index 483f2fa2c..17362bcae 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -1,4 +1,4 @@ -// spell-checker:ignore αbcdef +// spell-checker:ignore αbcdef ; (people) kkos use crate::common::util::*; @@ -179,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!()