nit: Added link to `CONTRIBUTING.md` from the README
[nit.git] / lib / popcorn / tests / tests.sh
1 #!/bin/bash
2
3 # This file is part of NIT ( http://www.nitlanguage.org ).
4 #
5 # Copyright 2016 Alexandre Terrasa <alexandre@moz-code.org>.
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18
19 BIN=../bin
20 OUT=./out
21 RES=./res
22
23 NITC=../../../bin/nitc
24
25 compile() {
26 local test="$1"
27 $NITC $test.nit -o $OUT/$test.bin 1>&2 2> $OUT/$test.cmp_err
28 }
29
30 test_prog()
31 {
32 local test="$1"
33
34 chmod +x $OUT/$test.bin 2> $OUT/$test.err
35 $OUT/$test.bin > $OUT/$test.res 2> $OUT/$test.err
36
37 diff $OUT/$test.res $RES/$test.res > $OUT/$test.diff 2> /dev/null
38 }
39
40 # return
41 # 0 if the sav not exists
42 # 1 if the file does match
43 # 2 if the file does not match
44 check_result() {
45 local test="$1"
46
47 if [ -s "$OUT/$test.cmp_err" ]; then
48 return 0
49 elif [ -s "$OUT/$test.err" ]; then
50 return 1
51 elif [ ! -r "$RES/$test.res" ]; then
52 return 2
53 elif [ -s "$OUT/$test.diff" ]; then
54 return 3
55 else
56 return 4
57 fi
58 }
59
60 echo "Testing..."
61 echo ""
62
63 rm -rf $OUT 2>/dev/null
64 mkdir $OUT 2>/dev/null
65
66 all=0
67 ok=0
68 ko=0
69 sk=0
70
71 for file in `ls test_*.nit`; do
72 ((all++))
73 test="${file%.*}"
74 echo -n "* $test: "
75
76 compile $test
77 test_prog $test
78 check_result $test
79
80 case "$?" in
81 0)
82 echo "compile error (cat $OUT/$test.cmp_err)"
83 ((ko++))
84 ;;
85 1)
86 echo "error (cat $OUT/$test.cmp_err)"
87 ((ko++))
88 ;;
89 2)
90 echo "skip ($test.res not found)"
91 ((sk++))
92 continue;;
93 3)
94 echo "error (diff $OUT/$test.res $RES/$test.res)"
95 ((ko++))
96 ;;
97 4)
98 echo "success"
99 ((ok++))
100 ;;
101
102 esac
103 done
104 echo ""
105 echo "==> success $ok/$all ($ko tests failed, $sk skipped)"
106
107 # return result
108 test "$ok" == "$all"