mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 15:22:38 +00:00
Merge pull request #4248 from rivy/fix.expr
Multiple fixes and tests added for `expr`
This commit is contained in:
commit
87da0c0a5f
3 changed files with 85 additions and 3 deletions
|
@ -74,7 +74,7 @@ fn process_expr(token_strings: &[&str]) -> Result<String, String> {
|
|||
|
||||
fn print_expr_ok(expr_result: &str) -> UResult<()> {
|
||||
println!("{}", expr_result);
|
||||
if expr_result == "0" || expr_result.is_empty() {
|
||||
if expr_result.parse::<i32>() == Ok(0) || expr_result.is_empty() {
|
||||
Err(1.into())
|
||||
} else {
|
||||
Ok(())
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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!()
|
||||
|
|
Loading…
Reference in a new issue