From 6088beb33c64c1fad7f1ec607335bf227fd831d0 Mon Sep 17 00:00:00 2001 From: Bernhard Voelker Date: Sun, 21 Oct 2018 21:56:43 +0200 Subject: [PATCH 2/3] test: simplify redundant code Remove the function 'test_unop', as the cases therein are redundant to those handled by 'unary_operator'; exception: the cases 'o' and 'N': they had been present in test_unop and handling the commands test -N STR test -o STR and test x = x -a -N STR test x = x -a -o STR which ran into an error later on anyway. With this commit, the error diagnostic will change from ... $ /usr/bin/test -N STR /usr/bin/test: extra argument '-N' $ /usr/bin/test -o STR /usr/bin/test: extra argument '-o' ... to ... $ src/test -N STR src/test: '-N': unary operator expected $ src/test -o STR src/test: '-o': unary operator expected * src/test.c (test_unop): Remove. (unary_operator): Fail with test_syntax_error in the default case. (term): Directly call unary_operator. (two_arguments): Likewise. * tests/misc/test-diag.pl: Adjust error diagnostic. --- src/test.c | 34 +++------------------------------- tests/misc/test-diag.pl | 4 ++-- 2 files changed, 5 insertions(+), 33 deletions(-) diff --git a/src/test.c b/src/test.c index 339a84075..9005b1962 100644 --- a/src/test.c +++ b/src/test.c @@ -72,7 +72,6 @@ static int pos; /* The offset of the current argument in ARGV. */ static int argc; /* The number of arguments present in ARGV. */ static char **argv; /* The argument list. */ -static bool test_unop (char const *s); static bool unary_operator (void); static bool binary_operator (bool); static bool two_arguments (void); @@ -258,12 +257,7 @@ term (void) /* It might be a switch type argument. */ else if (argv[pos][0] == '-' && argv[pos][1] && argv[pos][2] == '\0') - { - if (test_unop (argv[pos])) - value = unary_operator (); - else - test_syntax_error (_("%s: unary operator expected"), quote (argv[pos])); - } + value = unary_operator (); else { value = (argv[pos][0] != '\0'); @@ -399,6 +393,7 @@ unary_operator (void) switch (argv[pos][1]) { default: + test_syntax_error (_("%s: unary operator expected"), quote (argv[pos])); return false; /* All of the following unary operators use unary_advance (), which @@ -576,26 +571,6 @@ expr (void) return or (); /* Same with this. */ } -/* Return true if OP is one of the test command's unary operators. */ -static bool -test_unop (char const *op) -{ - if (op[0] != '-') - return false; - - switch (op[1]) - { - case 'b': case 'c': case 'd': case 'e': - case 'f': case 'g': case 'h': case 'k': case 'n': - case 'o': case 'p': case 'r': case 's': case 't': - case 'u': case 'w': case 'x': case 'z': - case 'G': case 'L': case 'O': case 'S': case 'N': - return true; - default: - return false; - } -} - static bool one_argument (void) { @@ -616,10 +591,7 @@ two_arguments (void) && argv[pos][1] != '\0' && argv[pos][2] == '\0') { - if (test_unop (argv[pos])) - value = unary_operator (); - else - test_syntax_error (_("%s: unary operator expected"), quote (argv[pos])); + value = unary_operator (); } else beyond (); diff --git a/tests/misc/test-diag.pl b/tests/misc/test-diag.pl index 91356ef56..f543f9f09 100755 --- a/tests/misc/test-diag.pl +++ b/tests/misc/test-diag.pl @@ -26,8 +26,8 @@ use strict; my @Tests = ( # In coreutils-5.93, this diagnostic lacked the newline. - ['o', '-o arg', {ERR => "test: extra argument '-o'\n"}, - {ERR_SUBST => 's!^.*:!test:!'}, + ['o', '-o arg', {ERR => "test: '-o': unary operator expected\n"}, + {ERR_SUBST => 's!^.*test:!test:!'}, {EXIT => 2}], ); -- 2.19.1