1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

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 <daniel.hofstetter@42dh.com>

* Change test name to specify lt/gt operator

---------

Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
This commit is contained in:
jfinkels 2025-02-21 08:41:15 -05:00 committed by GitHub
parent d065e172f7
commit 4acc59d075
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 4 deletions

View file

@ -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)),

View file

@ -97,9 +97,14 @@ fn eval(stack: &mut Vec<Symbol>) -> ParseResult<bool> {
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!();

View file

@ -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();
}