Bash · 3649 bytes Raw Blame History
1 #!/bin/sh
2 TEST_PREFIX="[test]"
3 . "$(cd "$(dirname "$0")/.." && pwd)/test_harness.sh"
4
5 section "1. test string operators"
6 compare_exit "test -z empty string" 'test -z ""'
7 compare_exit "test -z nonempty fails" 'test -z "hello"'
8 compare_exit "test -n nonempty string" 'test -n "hello"'
9 compare_exit "test -n empty fails" 'test -n ""'
10 compare_exit "test string equality" 'test "abc" = "abc"'
11 compare_exit "test string inequality" 'test "abc" != "def"'
12 compare_exit "test equal strings =" 'test "same" = "same"'
13 compare_exit "test unequal strings !=" 'test "a" != "b"'
14
15 section "2. test numeric operators"
16 compare_exit "test -eq equal" 'test 5 -eq 5'
17 compare_exit "test -ne not equal" 'test 5 -ne 3'
18 compare_exit "test -lt less than" 'test 3 -lt 5'
19 compare_exit "test -gt greater than" 'test 5 -gt 3'
20 compare_exit "test -le less or equal" 'test 5 -le 5'
21 compare_exit "test -le strictly less" 'test 4 -le 5'
22 compare_exit "test -ge greater or equal" 'test 5 -ge 5'
23 compare_exit "test -ge strictly greater" 'test 6 -ge 5'
24 compare_exit "test -eq fail" 'test 5 -eq 3'
25 compare_exit "test -lt fail" 'test 5 -lt 3'
26
27 section "3. test file operators"
28 compare_exit "test -f regular file" "touch $TEST_TMPDIR/testfile; test -f $TEST_TMPDIR/testfile"
29 compare_exit "test -f nonexistent" "test -f $TEST_TMPDIR/no_such_file"
30 compare_exit "test -d directory" "test -d $TEST_TMPDIR"
31 compare_exit "test -d on file fails" "touch $TEST_TMPDIR/testfile; test -d $TEST_TMPDIR/testfile"
32 compare_exit "test -e file exists" "touch $TEST_TMPDIR/testfile; test -e $TEST_TMPDIR/testfile"
33 compare_exit "test -e nonexistent" "test -e $TEST_TMPDIR/no_such_file"
34 compare_exit "test -s nonempty file" "echo data > $TEST_TMPDIR/nonempty; test -s $TEST_TMPDIR/nonempty"
35 compare_exit "test -s empty file" "touch $TEST_TMPDIR/emptyfile; test -s $TEST_TMPDIR/emptyfile"
36 compare_exit "test -r readable file" "touch $TEST_TMPDIR/testfile; test -r $TEST_TMPDIR/testfile"
37 compare_exit "test -w writable file" "touch $TEST_TMPDIR/testfile; test -w $TEST_TMPDIR/testfile"
38 compare_exit "test -x not executable" "touch $TEST_TMPDIR/testfile; test -x $TEST_TMPDIR/testfile"
39 compare_exit "test -x executable" "touch $TEST_TMPDIR/exefile; chmod +x $TEST_TMPDIR/exefile; test -x $TEST_TMPDIR/exefile"
40
41 section "4. test logical operators"
42 compare_exit "test logical NOT true" 'test ! -z "hello"'
43 compare_exit "test logical NOT false" 'test ! -z ""'
44 compare_exit "test logical AND -a both true" 'test 1 -eq 1 -a 2 -eq 2'
45 compare_exit "test logical AND -a one false" 'test 1 -eq 1 -a 2 -eq 3'
46 compare_exit "test logical OR -o both false" 'test 1 -eq 2 -o 3 -eq 4'
47 compare_exit "test logical OR -o one true" 'test 1 -eq 2 -o 2 -eq 2'
48
49 section "5. [ bracket form"
50 compare_exit "[ string equality ]" '[ "abc" = "abc" ]'
51 compare_exit "[ numeric test ]" '[ 5 -gt 3 ]'
52 compare_exit "[ -z empty ]" '[ -z "" ]'
53 compare_exit "[ file exists ]" "touch $TEST_TMPDIR/testfile; [ -f $TEST_TMPDIR/testfile ]"
54
55 section "6. [[ extended test"
56 compare_exit "[[ pattern match glob ]]" '[[ "hello" == h* ]]'
57 compare_exit "[[ pattern no match ]]" '[[ "hello" == x* ]]'
58 compare_exit "[[ regex match =~ ]]" '[[ "hello123" =~ [0-9]+ ]]'
59 compare_exit "[[ regex no match ]]" '[[ "hello" =~ ^[0-9]+$ ]]'
60 compare_exit "[[ logical AND && ]]" '[[ 1 -eq 1 && 2 -eq 2 ]]'
61 compare_exit "[[ logical OR || ]]" '[[ 1 -eq 2 || 2 -eq 2 ]]'
62 compare_exit "[[ negation ! ]]" '[[ ! "hello" == "world" ]]'
63 compare_exit "[[ string comparison < ]]" '[[ "abc" < "def" ]]'
64 compare_exit "[[ string comparison > ]]" '[[ "def" > "abc" ]]'
65 compare_exit "[[ -z in extended ]]" '[[ -z "" ]]'
66 compare_exit "[[ -n in extended ]]" '[[ -n "hello" ]]'
67
68 print_summary