From 4acc59d075c3e3aa3474538804e524b48553fbc7 Mon Sep 17 00:00:00 2001 From: jfinkels Date: Fri, 21 Feb 2025 08:41:15 -0500 Subject: [PATCH] test: add < and > operators for string comparison (#7315) * test: add < and > operators for string comparison Fixes #7254. * Add more tests for string inequality * Use match in place of if/elseif block Co-authored-by: Daniel Hofstetter * Change test name to specify lt/gt operator --------- Co-authored-by: Daniel Hofstetter --- src/uu/test/src/parser.rs | 2 +- src/uu/test/src/test.rs | 11 ++++++++--- tests/by-util/test_test.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/uu/test/src/parser.rs b/src/uu/test/src/parser.rs index 23a2d7cf6..f1c490bde 100644 --- a/src/uu/test/src/parser.rs +++ b/src/uu/test/src/parser.rs @@ -50,7 +50,7 @@ impl Symbol { "(" => Self::LParen, "!" => Self::Bang, "-a" | "-o" => Self::BoolOp(s), - "=" | "==" | "!=" => Self::Op(Operator::String(s)), + "=" | "==" | "!=" | "<" | ">" => Self::Op(Operator::String(s)), "-eq" | "-ge" | "-gt" | "-le" | "-lt" | "-ne" => Self::Op(Operator::Int(s)), "-ef" | "-nt" | "-ot" => Self::Op(Operator::File(s)), "-n" | "-z" => Self::UnaryOp(UnaryOperator::StrlenOp(s)), diff --git a/src/uu/test/src/test.rs b/src/uu/test/src/test.rs index 0ef70b1c3..354aa67dc 100644 --- a/src/uu/test/src/test.rs +++ b/src/uu/test/src/test.rs @@ -97,9 +97,14 @@ fn eval(stack: &mut Vec) -> ParseResult { Ok(!result) } Some(Symbol::Op(Operator::String(op))) => { - let b = stack.pop(); - let a = stack.pop(); - Ok(if op == "!=" { a != b } else { a == b }) + let b = pop_literal!(); + let a = pop_literal!(); + match op.to_string_lossy().as_ref() { + "!=" => Ok(a != b), + "<" => Ok(a < b), + ">" => Ok(a > b), + _ => Ok(a == b), + } } Some(Symbol::Op(Operator::Int(op))) => { let b = pop_literal!(); diff --git a/tests/by-util/test_test.rs b/tests/by-util/test_test.rs index db02346f1..cbd38b604 100644 --- a/tests/by-util/test_test.rs +++ b/tests/by-util/test_test.rs @@ -991,3 +991,40 @@ fn test_missing_argument_after() { "test: missing argument after 'foo'" ); } + +#[test] +fn test_string_lt_gt_operator() { + let items = [ + ("a", "b"), + ("a", "aa"), + ("a", "a "), + ("a", "a b"), + ("", "b"), + ("a", "รค"), + ]; + for (left, right) in items { + new_ucmd!().args(&[left, "<", right]).succeeds().no_output(); + new_ucmd!() + .args(&[right, "<", left]) + .fails() + .code_is(1) + .no_output(); + + new_ucmd!().args(&[right, ">", left]).succeeds().no_output(); + new_ucmd!() + .args(&[left, ">", right]) + .fails() + .code_is(1) + .no_output(); + } + new_ucmd!() + .args(&["", "<", ""]) + .fails() + .code_is(1) + .no_output(); + new_ucmd!() + .args(&["", ">", ""]) + .fails() + .code_is(1) + .no_output(); +}