blob: c7d865665843af0fe3c8e476e8fc57c9bf839cf6 [file] [log] [blame]
Nigel Tao569a2942020-02-23 23:13:51 +11001#!/bin/bash -eu
2# Copyright 2020 The Wuffs Authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# ----------------
17
18# This script runs a JSON parsing program (e.g. example/jsonptr) over a JSON
19# test suite (e.g. https://github.com/nst/JSONTestSuite checked out on local
20# disk as $HOME/JSONTestSuite).
21#
22# It runs that program over every command line argument: if a file then itself,
23# if a directory then every [iny]_*.json child. It checks for a 0 exit code for
24# y_*.json files, 1 for n_*.json files and either 0 or 1 for i_*.json files.
25
26jsonparser=${JSONPARSER:-gen/bin/example-jsonptr}
27if [ ! "$(command -v $jsonparser)" ]; then
28 if [ ${JSONPARSER:-} ]; then
29 echo "Could not run \$JSONPARSER ($jsonparser)."
30 else
31 echo "Could not run the default JSON parser (gen/bin/example-jsonptr)."
32 echo "Run \"./build-example.sh example/jsonptr\" from the Wuffs root directory."
33 fi
34 exit 1
35fi
36
37sources=$@
38if [ $# -eq 0 ]; then
39 sources=$HOME/JSONTestSuite/test_parsing
40fi
41
42num_fail=0
43num_ok=0
44num_total=0
45
46# ----
47
48test1() {
49 set +e
50
51 $jsonparser < $1 >/dev/null 2>/dev/null
52 local x=$?
53
54 local basename=${1##*/}
55 local ok=
56 if [[ $basename = i_*.json ]]; then
57 if [[ ($x == 0 || $x == 1) ]]; then
58 ok=1
59 fi
60 elif [[ $basename = n_*.json ]]; then
61 if [[ ($x == 1) ]]; then
62 ok=1
63 fi
64 elif [[ $basename = y_*.json ]]; then
65 if [[ ($x == 0) ]]; then
66 ok=1
67 fi
68 fi
69
70 if [ $ok ]; then
71 echo "ok $1"
72 ((num_ok++))
73 else
74 echo "fail $1"
75 ((num_fail++))
76 fi
77 ((num_total++))
78
79 set -e
80}
81
82# ----
83
84for f in $sources; do
85 if [ -f $f ]; then
86 test1 $f
87 elif [ -d $f ]; then
88 for g in ${f%/}/[iny]_*.json; do
89 test1 $g
90 done
91 else
92 echo "Could not open $f"
93 if [ $# -eq 0 ]; then
94 echo "Run \"git clone https://github.com/nst/JSONTestSuite.git\" from your home dir."
95 fi
96 exit 1
97 fi
98done
99
100echo "$num_fail fail, $num_ok ok, $num_total total"
101if [ $num_fail -ne 0 ]; then
102 exit 1
103fi