Skip to content

Commit

Permalink
test: Show error when using a binary operator withut two operands
Browse files Browse the repository at this point in the history
  • Loading branch information
sargas authored and sylvestre committed Apr 27, 2024
1 parent 65b25c7 commit 921c411
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/uu/test/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum ParseError {
MissingArgument(String),
UnknownOperator(String),
InvalidInteger(String),
UnaryOperatorExpected(String),

Check warning on line 14 in src/uu/test/src/error.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/test/src/error.rs#L14

Added line #L14 was not covered by tests
}

/// A Result type for parsing test expressions
Expand All @@ -26,6 +27,7 @@ impl std::fmt::Display for ParseError {
Self::ExtraArgument(s) => write!(f, "extra argument {s}"),
Self::UnknownOperator(s) => write!(f, "unknown operator {s}"),
Self::InvalidInteger(s) => write!(f, "invalid integer {s}"),
Self::UnaryOperatorExpected(s) => write!(f, "{s}: unary operator expected"),
}
}
}
Expand Down
22 changes: 20 additions & 2 deletions src/uu/test/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use std::ffi::{OsStr, OsString};
use std::iter::Peekable;

use super::error::{ParseError, ParseResult};

use uucore::display::Quotable;

use super::error::{ParseError, ParseResult};

/// Represents one of the binary comparison operators for strings, integers, or files
#[derive(Debug, PartialEq, Eq)]
pub enum Operator {
Expand Down Expand Up @@ -438,3 +438,21 @@ pub fn parse(args: Vec<OsString>) -> ParseResult<Vec<Symbol>> {
p.parse()?;
Ok(p.stack)
}

#[cfg(test)]
mod tests {
use crate::error::ParseError::UnaryOperatorExpected;
use crate::eval;
use crate::parser::parse;

#[test]
fn unary_expected() {
let result = parse(vec!["-o".into(), "arg".into()]).map(|mut x| eval(&mut x));

if let Ok(Err(UnaryOperatorExpected(s))) = result {
assert_eq!(s, "'-o'".to_string());
} else {
panic!("expected unary operator expected error, not {result:?}")

Check warning on line 455 in src/uu/test/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/test/src/parser.rs#L455

Added line #L455 was not covered by tests
}
}
}
3 changes: 3 additions & 0 deletions src/uu/test/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ fn eval(stack: &mut Vec<Symbol>) -> ParseResult<bool> {
Some(Symbol::None) | None => Ok(false),
Some(Symbol::BoolOp(op)) => {
let b = eval(stack)?;
if stack.is_empty() {
return Err(ParseError::UnaryOperatorExpected(op.quote().to_string()));
}
let a = eval(stack)?;

Ok(if op == "-a" { a && b } else { a || b })
Expand Down
2 changes: 1 addition & 1 deletion tests/by-util/test_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn test_double_not_is_false() {

#[test]
fn test_and_not_is_false() {
new_ucmd!().args(&["-a", "!"]).run().code_is(1);
new_ucmd!().args(&["-a", "!"]).run().code_is(2);
}

#[test]
Expand Down

0 comments on commit 921c411

Please sign in to comment.