-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.sh
executable file
·118 lines (91 loc) · 2.36 KB
/
test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/sh
FAILED_CASES=0
__do_test() {
VALID=$1
TEST=$2
JSON=$3
EXPT=$4
shift 4
PASSED=1
if [ "$VALID" -eq 1 ]; then
TEST="(VALID JSON) $TEST"
else
TEST="(INVALID JSON) $TEST"
fi
echo "========== RUN TEST - $TEST =========="
echo "JSON string:"
echo "$JSON"
OUTPUT=$(echo "$JSON" | ./poorjson.sh "$@" 2>&1)
RET=$?
if [ -n "$EXPT" ]; then
if [ "$OUTPUT" != "$EXPT" ]; then
echo "== ERROR: Output is not expected:"
echo "= JSON string:"
echo "$JSON"
echo "== Query:"
echo "$@"
echo "= Expected:"
echo "$EXPT"
echo "= Got:"
echo "$OUTPUT"
PASSED=0
fi
fi
if [ "$VALID" -eq 1 ]; then
if [ $RET -ne 0 ]; then
echo "== ERROR: Failed to parse valid JSON."
PASSED=0
fi
else
if [ $RET -eq 0 ]; then
echo "== ERROR: Invalid JSON didn't raise an error."
PASSED=0
fi
fi
if [ "$PASSED" -eq 1 ]; then
echo "========== TEST PASSED =========="
else
echo "========== TEST FAILED =========="
FAILED_CASES=$(( $FAILED_CASES + 1 ))
fi
echo
}
positive() {
__do_test 1 "$@"
}
negative() {
__do_test 0 "$@"
}
# Test case defination:
# positive/negative "<Test case name>" "<JSON string>" "<Expected output>" "[<Query parameters>, ...]"
positive "Empty Object" '{}' "" ""
positive "Object with one key" '{"key": ""}' "" ""
positive "String Empty" '{"key": ""}' "" ""
positive "String Simple" '{"key": "value"}' '"value"' '"key"'
positive "Array with no element" '{"key": []}' "" ""
positive "Array with one element" '{"key": ["element"]}' '"element"' '"key"' 0
positive "Array with multiple elements" '{"key": ["ele1", "ele2", "ele3", "ele4"]}' '"ele3"' '"key"' 2
positive "Complex JSON" \
'{
"key": ["ele1", "ele2", "ele3", "ele4"],
"key2-empty": [],
"key3-bool": true,
"key4-bool": false,
"key5-null": null,
"key6": ["1", 2, 3.0, -4, 0.5e5, -0.6, -0.7e-5, true, false, null]
}' 'null' '"key6"' 9
negative "Empty String" '' \
'Unexpected EOF after token: ""' -
negative "Invalid case 1" ',' \
'Unexpected invalid token: ","' -
negative "Invalid case 2" '{,}' \
'Unexpected token: ","' -
negative "Invalid case 3" '{"key": }' \
'Unexpected token: "}"' -
negative "Invalid case 4" '{"key": ,"key2": []}' \
'Unexpected token: ","' -
negative "Invalid case 5" '{"key":: ,"key2": []}' \
'Unexpected token: ":"' -
negative "Invalid case 6" '{"key": true, "key2": [, true]}' \
'Unexpected token: ","' -
exit $FAILED_CASES