src: remove old metamodel and nitc
[nit.git] / tests / tests_icode.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 usage()
19 {
20 e=`basename "$0"`
21 cat<<END
22 Usage: $e modulenames
23 END
24 }
25
26 find_nitc()
27 {
28 recent=`ls -t ../src/nitc ../src/nitc_[0-9] ../bin/nitc ../c_src/nitc 2>/dev/null | head -1`
29 if [[ "x$recent" == "x" ]]; then
30 echo 'Could not find nitc, aborting'
31 exit 1
32 fi
33 echo 'Using nitc from: '$recent
34 NITC=$recent
35 }
36
37 # The default nitc compiler
38 [ -z "$NITC" ] && find_nitc
39
40 if [ $# = 0 ]; then
41 usage;
42 exit
43 fi
44
45 ok=""
46 nok=""
47
48 # Process each files given as arguments
49 for ii in "$@"; do
50 if [ ! -f $ii ]; then
51 echo "File '$ii' does not exist."
52 continue
53 fi
54
55 # Prepare the includes
56 tmp=${ii/../AA}
57 if [ "x$tmp" = "x$ii" ]; then
58 oincludes="-I . -I ../lib/standard -I ../lib/standard/collection"
59 else
60 oincludes=""
61 fi
62
63 # Process each alternatives in the current file
64 for alt in "$ii" `./alterner.pl --start '#' "$ii"`; do
65 f=`basename "$ii" .nit`
66 d=`dirname "$ii"`
67 ff="$f"
68 i="$ii"
69 includes="$oincludes -I alt"
70
71 echo -n "=> $i: "
72
73 # Clean-up before compile and tests
74 rm -rf .nit_compile 2> /dev/null
75
76 # Compile
77 # The point of ICode testing is to validate analysis/optimizations
78 # Force '--global' option !
79 $NITC $OPT --global --output-format icode "$i" $includes 2> "out/$ff.cmp.err" > "out/$ff.compile.log"
80 ERR=$?
81 if [ "$ERR" != 0 ]; then
82 # Could not compile
83 echo "! [======= fail: Compilation error =======]"
84 nok="$nok $ff"
85 else
86 TEST_FILE=$d/$ff.tests
87 if [ ! -f $TEST_FILE ]; then
88 # Could not find the .tests file associated with this test
89 echo ". [======= fail: Cannot open test file =======]"
90 nok="$nok $ff"
91 continue
92 fi
93 echo "."
94
95 # Execute tests
96 # Each lines in the .tests file describe a test to execute on the generated
97 # ICode. The file has 4 parameters:
98 # - The class
99 # - The method
100 # - The type of test
101 # - Arguments for the test
102 # We use the class to open the right icode file.
103 # We use a special method named "no-file" to ensure that the class file does not exist.
104 # If the method is different from "no-file" then we search in said method for the expression given
105 # with the _arguments_ field. This expression will be evaluated as a awk regexp.
106 # We have two types of tests :
107 # - has : the given argument must be found for the test to be successful
108 # - no : the given argument must NOT be found for the test to be successful
109 cptr=0
110 while IFS=, read CLASS METHOD TYPE ATTR
111 do
112 ICODE_FILE=$d/.nit_compile/$CLASS.icode
113 let cptr+=1
114 echo -n '==> Test #'$cptr' ...... '
115
116 if [ -f $ICODE_FILE ]; then
117 # Check if the file should exist
118 if [ "x$METHOD" = "xno-file" ]; then
119 # this is not normal, the file should not exist
120 echo "Failed"
121 nok="$nok $ff#$cptr"
122 continue
123 fi
124
125 # Use awk to get the method in the file and search in it for the argument
126 awk '
127 BEGIN{
128 process=0
129 }
130 /'"$METHOD"'/{
131 process=1
132 }
133 /^$/ {
134 process=0
135 }
136 /'"$ATTR"'/{
137 if(process){
138 exit -3
139 }
140 }
141 ' $ICODE_FILE
142 awkstatus=$?
143
144 if [ $awkstatus -ne 0 ] ; then
145 # Found the value !
146 if [ "x$TYPE" = "xno" ]; then
147 # Should not have beed found
148 echo "Failed"
149 nok="$nok $ff#$cptr"
150 elif [ "x$TYPE" = "xhas" ]; then
151 # Should have been found
152 echo "Ok"
153 ok="$ok $ff#$cptr"
154 else
155 echo "Test file format error !!!"
156 exit -1
157 fi
158 else
159 # Value not found
160 if [ "x$TYPE" = "xno" ]; then
161 # Should not have beed found
162 echo "Ok"
163 ok="$ok $ff#$cptr"
164 elif [ "x$TYPE" = "xhas" ]; then
165 # Should have been found
166 echo "Failed"
167 nok="$nok $ff#$cptr"
168 else
169 echo "Test file format error !!!"
170 exit -1
171 fi
172 fi
173 else
174 # The class file was not found
175 if [ "x$METHOD" = "xno-file" ]; then
176 # this is normal
177 echo "Ok"
178 ok="$ok $ff#$cptr"
179 else
180 # Should have been found
181 echo "Failed"
182 nok="$nok $ff#$cptr"
183 fi
184 fi
185 done < $TEST_FILE
186 fi
187 done
188 done
189
190 echo "ok: " `echo $ok | wc -w` "/" `echo $ok $nok $nos | wc -w`
191
192 if [ -n "$nok" ]; then
193 echo "fail: $nok"
194 echo "There were $(echo $nok | wc -w) errors !"
195 fi
196 if [ -n "$nos" ]; then
197 echo "no sav: $nos"
198 fi
199
200 if [ -n "$nok" ]; then
201 exit 1
202 else
203 exit 0
204 fi