changeset 964:9927d1d1e7e2

Implement test
author Felix Janda <felix.janda@posteo.de>
date Fri, 19 Jul 2013 01:18:22 +0200
parents 86470ea03bc5
children 0d20619388b3
files scripts/test/test.test toys/pending/test.c
diffstat 2 files changed, 132 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/test/test.test	Fri Jul 19 01:18:22 2013 +0200
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+# TODO: Should also have device and socket files
+
+mkdir d
+touch f
+ln -s /dev/null L
+echo nonempty > s
+mkfifo p
+
+type_test()
+{
+  result=""
+  for i in d f L s p n
+  do
+    if test $* $i
+    then
+      result=$result$i
+    fi
+  done
+  printf "%s" $result
+}
+
+testing "test -b" "type_test -b" "" "" ""
+testing "test -c" "type_test -c" "L" "" ""
+testing "test -d" "type_test -d" "d" "" ""
+testing "test -f" "type_test -f" "fs" "" ""
+testing "test -h" "type_test -h" "L" "" ""
+testing "test -L" "type_test -L" "L" "" ""
+testing "test -s" "type_test -s" "ds" "" ""
+testing "test -S" "type_test -S" "" "" ""
+testing "test -p" "type_test -p" "p" "" ""
+testing "test -e" "type_test -e" "dfLsp" "" ""
+testing "test ! -e" "type_test ! -e" "n" "" ""
+
+rm f L s p
+rmdir d
+
+# TODO: Test rwx gu t
+
+testing "test" "test "" || test a && echo yes" "yes\n" "" ""
+testing "test -n" "test -n "" || test -n a && echo yes" "yes\n" "" ""
+testing "test -z" "test -n a || test -n "" && echo yes" "yes\n" "" ""
+testing "test a = b" "test a = b || test "" = "" && echo yes" "yes\n" "" ""
+testing "test a != b" "test "" != "" || test a = b && echo yes" "yes\n" "" ""
+
+arith_test()
+{
+  test -1 $1 1 && echo l
+  test 0 $1 0 && echo e
+  test -3 $1 -5 && echo g
+}
+
+testing "test -eq" "arith_test -eq" "e\n" "" ""
+testing "test -ne" "arith_test -ne" "l\ng\n" "" ""
+testing "test -gt" "arith_test -gt" "g\n" "" ""
+testing "test -ge" "arith_test -ge" "e\ng\n" "" ""
+testing "test -lt" "arith_test -lt" "l\n" "" ""
+testing "test -le" "arith_test -le" "l\ne\n" "" ""
--- a/toys/pending/test.c	Thu Jul 25 13:49:58 2013 -0500
+++ b/toys/pending/test.c	Fri Jul 19 01:18:22 2013 +0200
@@ -41,5 +41,74 @@
 
 void test_main(void)
 {
+  int id, not;
+  char *s, *err_fmt = "Bad flag '%s'";
+
+  toys.exitval = 2;
+  if (!strcmp("[", toys.which->name))
+    if (!strcmp("]", toys.optargs[--toys.optc])) error_exit("Missing ']'");
+  if (!strcmp("!", toys.optargs[0])) {
+    not = 1;
+    toys.optargs++;
+    toys.optc--;
+  }
+  if (!toys.optc) toys.exitval = 0;
+  else if (toys.optargs[0][0] == '-') {
+    id = stridx("bcdefghLpSsurwxznt", toys.optargs[0][1]);
+    if (id == -1 || toys.optargs[0][2]) error_exit(err_fmt, toys.optargs[0]);
+    if (id < 12) {
+      struct stat st;
+      int nolink;
+
+      toys.exitval = 1;
+      if (lstat(toys.optargs[1], &st) == -1) return;
+      nolink = !S_ISLNK(st.st_mode);
+      if (!nolink && (stat(toys.optargs[1], &st) == -1)) return;
+
+      if (id == 0) toys.exitval = !S_ISBLK(st.st_mode); // b
+      else if (id == 1) toys.exitval = !S_ISCHR(st.st_mode); // c
+      else if (id == 2) toys.exitval = !S_ISDIR(st.st_mode); // d
+      else if (id == 3) toys.exitval = 0; // e
+      else if (id == 4) toys.exitval = !S_ISREG(st.st_mode); // f
+      else if (id == 5) toys.exitval = !(st.st_mode & S_ISGID); // g
+      else if ((id == 6) || (id == 7)) toys.exitval = nolink; // hL
+      else if (id == 8) toys.exitval = !S_ISFIFO(st.st_mode); // p
+      else if (id == 9) toys.exitval = !S_ISSOCK(st.st_mode); // S
+      else if (id == 10) toys.exitval = st.st_size == 0; // s
+      else toys.exitval = !(st.st_mode & S_ISUID); // u
+    }
+    else if (id < 15) // rwx
+      toys.exitval = access(toys.optargs[1], 1 << (id - 12)) == -1;
+    else if (id < 17) // zn
+      toys.exitval = toys.optargs[1] && !*toys.optargs[1] ^ (id - 15);
+    else { // t
+      struct termios termios;
+      toys.exitval = tcgetattr(atoi(toys.optargs[1]), &termios) == -1;
+    }
+  }
+  else if (toys.optc == 1) toys.exitval = *toys.optargs[0] == 0;
+  else if (toys.optc == 3) {
+    if (*toys.optargs[1] == '-') {
+      long a = atol(toys.optargs[0]), b = atol(toys.optargs[2]);
+      
+      s = toys.optargs[1] + 1;
+      if (!strcmp("eq", s)) toys.exitval = a != b;
+      else if (!strcmp("ne", s)) toys.exitval = a == b;
+      else if (!strcmp("gt", s)) toys.exitval = a < b;
+      else if (!strcmp("ge", s)) toys.exitval = a <= b;
+      else if (!strcmp("lt", s)) toys.exitval = a > b;
+      else if (!strcmp("le", s)) toys.exitval = a >= b;
+      else error_exit(err_fmt, toys.optargs[1]);
+    }
+    else {
+      int result = strcmp(toys.optargs[0], toys.optargs[2]);
+
+      s = toys.optargs[1];
+      if (!strcmp("=", s)) toys.exitval = !!result;
+      else if (!strcmp("!=", s)) toys.exitval = !result;
+      else error_exit(err_fmt, toys.optargs[1]);
+    }
+  }
+  toys.exitval ^= not;
   return;
 }