]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
test: simplify redundant code
authorBernhard Voelker <mail@bernhard-voelker.de>
Sun, 21 Oct 2018 19:56:43 +0000 (21:56 +0200)
committerBernhard Voelker <mail@bernhard-voelker.de>
Fri, 26 Oct 2018 08:54:11 +0000 (10:54 +0200)
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
tests/misc/test-diag.pl

index 339a840752b584418ed22be13e1ef1d7b8e920f1..9005b196223abe50e08c9f5854cd4f6f29bd65b0 100644 (file)
@@ -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 ();
index 91356ef569d0c7c8681503d95c04853588314daf..f543f9f0982632c6940cc3eda448dd84ccb90728 100755 (executable)
@@ -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}],
     );