• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/libubox/tests/shunit2/tests.sh

  1 JSON_SCRIPT=tests.json
  2 JSON_SCRIPT_BIN=${TEST_JSON_SCRIPT=:-./json_script-example}
  3 FILE_STDOUT=tests.stdout
  4 FILE_STDERR=tests.stderr
  5 FILE_EXPECTED=tests.expected
  6 
  7 call_json_script() {
  8         #export LD_PRELOAD=../libjson_script.so
  9         $JSON_SCRIPT_BIN "$@" "$JSON_SCRIPT" >"$FILE_STDOUT" 2>"$FILE_STDERR"
 10 }
 11 
 12 assertStdioEquals() {
 13         local expected="$1"
 14         local file_stdio="$2"
 15 
 16         echo "$expected" >"$FILE_EXPECTED"
 17         if [ -z "$expected" ]; then
 18                 # we are expecting empty output, but we deliberately added a newline
 19                 # with echo above, so adding another echo to compensate for that
 20                 echo >>"$file_stdio"
 21         fi
 22         diff -up "$FILE_EXPECTED" "$file_stdio" >/dev/null 2>&1 || {
 23                 cat >&2 <<EOF
 24 |--- expecting
 25 $expected<
 26 |--- actual
 27 $(cat $file_stdio)<
 28 |--- END
 29 EOF
 30                 exit 1
 31         }
 32 }
 33 
 34 assertStdoutEquals() {
 35         assertStdioEquals "$1" "$FILE_STDOUT"
 36 }
 37 
 38 assertStderrEquals() {
 39         assertStdioEquals "$1" "$FILE_STDERR"
 40 }
 41 
 42 test_bad_json() {
 43         cat >"$JSON_SCRIPT" <<-EOF
 44         [
 45                 [ ]
 46                 [ ]
 47         ]
 48         EOF
 49         call_json_script
 50         assertStderrEquals "load JSON data from $JSON_SCRIPT failed."
 51 }
 52 
 53 test_expr_eq() {
 54         cat >"$JSON_SCRIPT" <<-EOF
 55         [
 56                 [ "if",
 57                         [ "eq", "VAR", "foo" ],
 58                         [ "echo", "bar" ],
 59                         [ "echo", "baz" ]
 60                 ]
 61         ]
 62         EOF
 63         call_json_script "VAR=foo"
 64         assertStdoutEquals "echo bar"
 65         call_json_script "VAR=xxx"
 66         assertStdoutEquals "echo baz"
 67 }
 68 
 69 test_expr_has() {
 70         cat >"$JSON_SCRIPT" <<-EOF
 71         [
 72                 [ "if",
 73                         [ "has", "VAR" ],
 74                         [ "echo", "bar" ],
 75                         [ "echo", "baz" ]
 76                 ]
 77         ]
 78         EOF
 79         call_json_script "VAR=foo"
 80         assertStdoutEquals "echo bar"
 81         call_json_script
 82         assertStdoutEquals "echo baz"
 83 }
 84 
 85 test_expr_regex_single() {
 86         cat >"$JSON_SCRIPT" <<-EOF
 87         [
 88                 [ "if",
 89                         [ "regex", "VAR", ".ell." ],
 90                         [ "echo", "bar" ],
 91                         [ "echo", "baz" ]
 92                 ]
 93         ]
 94         EOF
 95         call_json_script "VAR=hello"
 96         assertStdoutEquals "echo bar"
 97         call_json_script "VAR=.ell."
 98         assertStdoutEquals "echo bar"
 99         call_json_script
100         assertStdoutEquals "echo baz"
101         call_json_script "VAR="
102         assertStdoutEquals "echo baz"
103         call_json_script "VAR=hell"
104         assertStdoutEquals "echo baz"
105 }
106 
107 test_expr_regex_multi() {
108         cat >"$JSON_SCRIPT" <<-EOF
109         [
110                 [ "if",
111                         [ "regex", "VAR", [ ".ell.", "w.rld" ] ],
112                         [ "echo", "bar" ],
113                         [ "echo", "baz" ]
114                 ]
115         ]
116         EOF
117         call_json_script "VAR=hello"
118         assertStdoutEquals "echo bar"
119         call_json_script "VAR=world"
120         assertStdoutEquals "echo bar"
121         call_json_script "VAR=.ell."
122         assertStdoutEquals "echo bar"
123         call_json_script "VAR=w.rld"
124         assertStdoutEquals "echo bar"
125         call_json_script
126         assertStdoutEquals "echo baz"
127         call_json_script "VAR="
128         assertStdoutEquals "echo baz"
129         call_json_script "VAR=hell"
130         assertStdoutEquals "echo baz"
131 }
132 
133 test_expr_not() {
134         cat >"$JSON_SCRIPT" <<-EOF
135         [
136                 [ "if",
137                         [ "not", [ "has", "VAR" ] ],
138                         [ "echo", "bar" ],
139                         [ "echo", "baz" ]
140                 ]
141         ]
142         EOF
143         call_json_script "VAR=foo"
144         assertStdoutEquals "echo baz"
145         call_json_script
146         assertStdoutEquals "echo bar"
147 }
148 
149 test_expr_and() {
150         cat >"$JSON_SCRIPT" <<-EOF
151         [
152                 [ "if",
153                         [ "and", [ "eq", "EQVAR", "eqval" ],
154                                          [ "regex", "REGEXVAR", "regex..." ]
155                         ],
156                         [ "echo", "bar" ],
157                         [ "echo", "baz" ]
158                 ]
159         ]
160         EOF
161         call_json_script "EQVAR=eqval" "REGEXVAR=regexval"
162         assertStdoutEquals "echo bar"
163         call_json_script "EQVAR=foo"
164         assertStdoutEquals "echo baz"
165         call_json_script "REGEXVAR=regex***"
166         assertStdoutEquals "echo baz"
167         call_json_script
168         assertStdoutEquals "echo baz"
169 }
170 
171 test_expr_or() {
172         cat >"$JSON_SCRIPT" <<-EOF
173         [
174                 [ "if",
175                         [ "or", [ "not", [ "eq", "EQVAR", "eqval" ] ],
176                                         [ "regex", "REGEXVAR", [ "regexva.[0-9]", "regexva.[a-z]" ] ]
177                         ],
178                         [ "echo", "bar" ],
179                         [ "echo", "baz" ]
180                 ]
181         ]
182         EOF
183         call_json_script "EQVAR=eqval" "REGEXVAR=regexval1"
184         assertStdoutEquals "echo bar"
185         call_json_script "EQVAR=neq" "REGEXVAR=sxc"
186         assertStdoutEquals "echo bar"
187         call_json_script "REGEXVAR=sxc"
188         assertStdoutEquals "echo bar"
189         call_json_script "EQVAR=foo"
190         assertStdoutEquals "echo bar"
191         call_json_script
192         assertStdoutEquals "echo bar"
193         call_json_script "EQVAR=eqval" "REGEXVAR=regexval"
194         assertStdoutEquals "echo baz"
195 }
196 
197 test_expr_isdir() {
198         cat >"$JSON_SCRIPT" <<-EOF
199         [
200                 [ "if",
201                         [ "isdir", "%VAR%" ],
202                         [ "echo", "bar" ],
203                         [ "echo", "baz" ]
204                 ]
205         ]
206         EOF
207         call_json_script "VAR=/"
208         assertStdoutEquals "echo bar"
209         call_json_script "VAR=$(mktemp -u)"
210         assertStdoutEquals "echo baz"
211         call_json_script
212         assertStdoutEquals "echo baz"
213 }
214 
215 test_cmd_case() {
216         cat >"$JSON_SCRIPT" <<-EOF
217         [
218                 [ "case", "CASEVAR", {
219                         "0": [ "echo", "foo" ],
220                         "1": [
221                                 [ "echo", "bar" ],
222                                 [ "echo", "baz" ]
223                         ],
224                         "%VAR%": [ "echo", "quz" ]
225                 } ]
226         ]
227         EOF
228         call_json_script "CASEVAR=0"
229         assertStdoutEquals "echo foo"
230         call_json_script "CASEVAR=1"
231         assertStdoutEquals "echo bar
232 echo baz"
233         call_json_script "CASEVAR=%VAR%"
234         assertStdoutEquals "echo quz"
235         call_json_script "CASEVAR="
236         assertStdoutEquals ""
237         call_json_script
238         assertStdoutEquals ""
239         call_json_script "CASEVAR=xxx" "VAR=xxx"
240         assertStdoutEquals ""
241 }
242 
243 test_cmd_if() {
244         cat >"$JSON_SCRIPT" <<-EOF
245         [
246                 [ "if",
247                         [ "eq", "VAR", "foo" ],
248                         [ "echo", "bar" ],
249                         [ "echo", "baz" ]
250                 ]
251         ]
252         EOF
253         call_json_script "VAR=foo"
254         assertStdoutEquals "echo bar"
255         call_json_script "VAR=xxx"
256         assertStdoutEquals "echo baz"
257 }
258 
259 test_cmd_cb() {
260         cat >"$JSON_SCRIPT" <<-EOF
261         [
262                 [ "exec", "%VAR%", "/%VAS%%%/" ]
263         ]
264         EOF
265         call_json_script
266         assertStdoutEquals "exec  /%/"
267         call_json_script "VAR="
268         assertStdoutEquals "exec  /%/"
269         call_json_script "VAR=qux" "VAS=3"
270         assertStdoutEquals "exec qux /3%/"
271 }
272 
273 test_cmd_return() {
274         cat >"$JSON_SCRIPT" <<-EOF
275         [
276                 [ "heh", "%HEHVAR%" ],
277                 [ "%VAR%", "%VAR%" ],
278                 [ "return" ],
279                 [ "exec_non_reachable", "Arghhh" ]
280         ]
281         EOF
282         call_json_script "HEHVAR=dude" "VAR=ow"
283         assertStdoutEquals "heh dude
284 %VAR% ow"
285 }
286 
287 . ./shunit2/shunit2

This page was automatically generated by LXR 0.3.1.  •  OpenWrt