Git fork
at reftables-rust 332 lines 6.0 kB view raw
1#!/bin/sh 2 3test_description='test json-writer JSON generation' 4 5. ./test-lib.sh 6 7test_expect_success 'unit test of json-writer routines' ' 8 test-tool json-writer -u 9' 10 11test_expect_success 'trivial object' ' 12 cat >expect <<-\EOF && 13 {} 14 EOF 15 cat >input <<-\EOF && 16 object 17 end 18 EOF 19 test-tool json-writer <input >actual && 20 test_cmp expect actual 21' 22 23test_expect_success 'trivial array' ' 24 cat >expect <<-\EOF && 25 [] 26 EOF 27 cat >input <<-\EOF && 28 array 29 end 30 EOF 31 test-tool json-writer <input >actual && 32 test_cmp expect actual 33' 34 35test_expect_success 'simple object' ' 36 cat >expect <<-\EOF && 37 {"a":"abc","b":42,"c":3.14,"d":true,"e":false,"f":null} 38 EOF 39 cat >input <<-\EOF && 40 object 41 object-string a abc 42 object-int b 42 43 object-double c 2 3.140 44 object-true d 45 object-false e 46 object-null f 47 end 48 EOF 49 test-tool json-writer <input >actual && 50 test_cmp expect actual 51' 52 53test_expect_success 'simple array' ' 54 cat >expect <<-\EOF && 55 ["abc",42,3.14,true,false,null] 56 EOF 57 cat >input <<-\EOF && 58 array 59 array-string abc 60 array-int 42 61 array-double 2 3.140 62 array-true 63 array-false 64 array-null 65 end 66 EOF 67 test-tool json-writer <input >actual && 68 test_cmp expect actual 69' 70 71test_expect_success 'escape quoting string' ' 72 cat >expect <<-\EOF && 73 {"a":"abc\\def"} 74 EOF 75 cat >input <<-\EOF && 76 object 77 object-string a abc\def 78 end 79 EOF 80 test-tool json-writer <input >actual && 81 test_cmp expect actual 82' 83 84test_expect_success 'escape quoting string 2' ' 85 cat >expect <<-\EOF && 86 {"a":"abc\"def"} 87 EOF 88 cat >input <<-\EOF && 89 object 90 object-string a abc"def 91 end 92 EOF 93 test-tool json-writer <input >actual && 94 test_cmp expect actual 95' 96 97test_expect_success 'nested inline object' ' 98 cat >expect <<-\EOF && 99 {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":{"e":false,"f":null}}} 100 EOF 101 cat >input <<-\EOF && 102 object 103 object-string a abc 104 object-int b 42 105 object-object sub1 106 object-double c 2 3.140 107 object-true d 108 object-object sub2 109 object-false e 110 object-null f 111 end 112 end 113 end 114 EOF 115 test-tool json-writer <input >actual && 116 test_cmp expect actual 117' 118 119test_expect_success 'nested inline array' ' 120 cat >expect <<-\EOF && 121 ["abc",42,[3.14,true,[false,null]]] 122 EOF 123 cat >input <<-\EOF && 124 array 125 array-string abc 126 array-int 42 127 array-array 128 array-double 2 3.140 129 array-true 130 array-array 131 array-false 132 array-null 133 end 134 end 135 end 136 EOF 137 test-tool json-writer <input >actual && 138 test_cmp expect actual 139' 140 141test_expect_success 'nested inline object and array' ' 142 cat >expect <<-\EOF && 143 {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,null]}} 144 EOF 145 cat >input <<-\EOF && 146 object 147 object-string a abc 148 object-int b 42 149 object-object sub1 150 object-double c 2 3.140 151 object-true d 152 object-array sub2 153 array-false 154 array-null 155 end 156 end 157 end 158 EOF 159 test-tool json-writer <input >actual && 160 test_cmp expect actual 161' 162 163test_expect_success 'nested inline object and array 2' ' 164 cat >expect <<-\EOF && 165 {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,{"g":0,"h":1},null]}} 166 EOF 167 cat >input <<-\EOF && 168 object 169 object-string a abc 170 object-int b 42 171 object-object sub1 172 object-double c 2 3.140 173 object-true d 174 object-array sub2 175 array-false 176 array-object 177 object-int g 0 178 object-int h 1 179 end 180 array-null 181 end 182 end 183 end 184 EOF 185 test-tool json-writer <input >actual && 186 test_cmp expect actual 187' 188 189test_expect_success 'pretty nested inline object and array 2' ' 190 sed -e "s/^|//" >expect <<-\EOF && 191 |{ 192 | "a": "abc", 193 | "b": 42, 194 | "sub1": { 195 | "c": 3.14, 196 | "d": true, 197 | "sub2": [ 198 | false, 199 | { 200 | "g": 0, 201 | "h": 1 202 | }, 203 | null 204 | ] 205 | } 206 |} 207 EOF 208 cat >input <<-\EOF && 209 object 210 object-string a abc 211 object-int b 42 212 object-object sub1 213 object-double c 2 3.140 214 object-true d 215 object-array sub2 216 array-false 217 array-object 218 object-int g 0 219 object-int h 1 220 end 221 array-null 222 end 223 end 224 end 225 EOF 226 test-tool json-writer -p <input >actual && 227 test_cmp expect actual 228' 229 230test_expect_success 'inline object with no members' ' 231 cat >expect <<-\EOF && 232 {"a":"abc","empty":{},"b":42} 233 EOF 234 cat >input <<-\EOF && 235 object 236 object-string a abc 237 object-object empty 238 end 239 object-int b 42 240 end 241 EOF 242 test-tool json-writer <input >actual && 243 test_cmp expect actual 244' 245 246test_expect_success 'inline array with no members' ' 247 cat >expect <<-\EOF && 248 {"a":"abc","empty":[],"b":42} 249 EOF 250 cat >input <<-\EOF && 251 object 252 object-string a abc 253 object-array empty 254 end 255 object-int b 42 256 end 257 EOF 258 test-tool json-writer <input >actual && 259 test_cmp expect actual 260' 261 262test_expect_success 'larger empty example' ' 263 cat >expect <<-\EOF && 264 {"a":"abc","empty":[{},{},{},[],{}],"b":42} 265 EOF 266 cat >input <<-\EOF && 267 object 268 object-string a abc 269 object-array empty 270 array-object 271 end 272 array-object 273 end 274 array-object 275 end 276 array-array 277 end 278 array-object 279 end 280 end 281 object-int b 42 282 end 283 EOF 284 test-tool json-writer <input >actual && 285 test_cmp expect actual 286' 287 288test_lazy_prereq PERLJSON ' 289 perl -MJSON -e "exit 0" 290' 291 292# As a sanity check, ask Perl to parse our generated JSON and recursively 293# dump the resulting data in sorted order. Confirm that that matches our 294# expectations. 295test_expect_success PERLJSON 'parse JSON using Perl' ' 296 cat >expect <<-\EOF && 297 row[0].a abc 298 row[0].b 42 299 row[0].sub1 hash 300 row[0].sub1.c 3.14 301 row[0].sub1.d 1 302 row[0].sub1.sub2 array 303 row[0].sub1.sub2[0] 0 304 row[0].sub1.sub2[1] hash 305 row[0].sub1.sub2[1].g 0 306 row[0].sub1.sub2[1].h 1 307 row[0].sub1.sub2[2] null 308 EOF 309 cat >input <<-\EOF && 310 object 311 object-string a abc 312 object-int b 42 313 object-object sub1 314 object-double c 2 3.140 315 object-true d 316 object-array sub2 317 array-false 318 array-object 319 object-int g 0 320 object-int h 1 321 end 322 array-null 323 end 324 end 325 end 326 EOF 327 test-tool json-writer <input >output.json && 328 perl "$TEST_DIRECTORY"/t0019/parse_json.perl <output.json >actual && 329 test_cmp expect actual 330' 331 332test_done