news: update
[bison.git] / examples / test
bloba537062408493be45bd79446d15ae99a32faf499
1 #! /bin/sh
3 # Copyright (C) 2005-2015, 2018-2021 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 SHELL=/bin/sh
19 export SHELL
21 me=$(basename "$1" .test)
22 medir=$(dirname "$1" | sed -e 's,.*examples/,,')
24 # Number of the current test.
25 number=1
27 # Exit status of this script.
28 status=0
30 # top_builddir.
31 cwd=$(pwd)
33 # Whether to strip '> ...' lines from the expected output.
34 # See bistromathic.test.
35 strip_prompt=false
37 # If diff supports --strip-trailing-cr, use it, to avoid EOL issues
38 # when testing Java programs on Windows.
39 echo "checking for diff --strip-trailing-cr..."
40 diff_opts=
41 if diff --strip-trailing-cr "$1" "$1"; then
42 diff_opts=--strip-trailing-cr
44 echo "checking for diff --strip-trailing-cr... $diff_opts"
46 # The exercised program.
47 abs_medir=$cwd/examples/$medir
48 if test -x "$abs_medir/$me"; then
49 prog ()
51 "$abs_medir/$me" "$@"
53 elif test -f "$abs_medir/$me.class"; then
54 prog ()
56 "$SHELL" "$cwd/javaexec.sh" -cp "$abs_medir" "$me" "$@"
58 else
59 echo "$me: ERROR: cannot find program to exercise in:"
60 echo "$me: ERROR: $cwd/examples/$medir/$me"
61 exit 1
65 # cleanup
66 # -------
67 cleanup ()
69 status=$?
70 if test -z "$DEBUG"; then
71 cd "$cwd"
72 rm -rf $$.dir
74 exit $status
76 trap cleanup 0 1 2 13 15
77 mkdir $$.dir
78 cd $$.dir
81 # skip [MSG]
82 # ----------
83 # Skip this test.
84 skip ()
86 if test x"$1" != x; then
87 echo "SKIP: $1"
89 # See Autoconf's doc on 'trap'.
90 (exit 77); exit 77
94 # run [-n, -noerr, -t] EXPECTED-EXIT-STATUS EXPECTED-OUTPUT [PARSER-OPTIONS]
95 # --------------------------------------------------------------------------
96 # -n: no final end-of-line in expected-output
97 # -noerr: ignore stderr, otherwise merge it into effective output.
98 # -t: nuke the possible trailing white spaces in the effective output.
99 run ()
101 echo=echo
102 noerr=false
103 rstrip=false
104 while true; do
105 case $1 in
106 (-n) echo=printf; shift;;
107 (-noerr) noerr=true; shift;;
108 (-t) rstrip=true; shift;;
109 (*) break;;
110 esac
111 done
113 # Expected exit status.
114 sta_exp=$1
115 shift
117 # Expected output.
118 $echo "$1" |
119 sed -e 's/Reducing stack by rule .* (line .*):/Reducing stack by rule XX (line XXX):/g' |
120 if $strip_prompt; then
121 # An extra EOL added in bistromathic's main. Keep that empty line.
122 sed -e '/^> ./d;s/^> $//g'
123 else
125 fi >exp
126 shift
128 # Effective exit status.
129 sta_eff=0
131 prog "$@" - <input >out_eff 2>err_eff || sta_eff=$?
133 # Combine effective output and error streams.
135 if $rstrip; then
136 sed -e 's/ *$//g' out_eff
137 else
138 cat out_eff
140 if ! $noerr; then
141 sed -e 's/^/err: /g' \
142 -e 's/Reducing stack by rule .* (line .*):/Reducing stack by rule XX (line XXX):/g' \
143 err_eff
145 } >eff
147 if test $sta_eff -eq $sta_exp; then
148 if diff $diff_opts eff exp >/dev/null 2>&1; then
149 echo "$me: PASS: $number"
150 else
151 echo "$me: FAIL: $number"
152 echo "$me: input:"
153 sed -e 's/^/ /' input
154 echo "$me: expected output:"
155 sed -e 's/^/ /' exp
156 echo "$me: effective output:"
157 sed -e 's/^/ /' eff
158 echo "$me: diff:"
159 diff $diff_opts -u exp eff | sed -e 's/^/ /'
160 status=1
162 else
163 echo "$me: FAIL: $number (expected status: $sta_exp, effective: $sta_eff)"
164 cat err_eff
165 status=1
167 number=$(expr $number + 1)
170 # We have cd'd one level deeper.
171 case $1 in
172 /*) . "$1" || status=2;;
173 *) . "../$1" || status=2;;
174 esac
176 exit $status