1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Testing from external files.
20 private import annotation
22 redef class ToolContext
24 var opt_file
= new OptionString("Specify test suite location", "-t", "--target-file")
26 var opt_pattern
= new OptionString("Only run test case with name that match pattern", "-p", "--pattern")
29 # Used to test nitunit test files.
32 # `ModelBuilder` used to parse test files.
33 var mbuilder
: ModelBuilder
35 # Parse a file and return the contained `MModule`.
36 private fun parse_module_unit
(file
: String): nullable MModule do
37 var mmodule
= mbuilder
.parse
([file
]).first
38 if mbuilder
.get_mmodule_annotation
("test_suite", mmodule
) == null then return null
43 # Compile and execute the test suite for a NitUnit `file`.
44 fun test_module_unit
(file
: String): nullable TestSuite do
45 var toolcontext
= mbuilder
.toolcontext
46 var mmodule
= parse_module_unit
(file
)
47 # is the module a test_suite?
48 if mmodule
== null then return null
49 var suite
= new TestSuite(mmodule
, toolcontext
)
50 # method to execute before all tests in the module
51 var before_module
= mmodule
.before_test
52 if before_module
!= null then
53 toolcontext
.modelbuilder
.total_tests
+= 1
54 suite
.before_module
= new TestCase(suite
, before_module
, toolcontext
)
56 # generate all test cases
57 for mclassdef
in mmodule
.mclassdefs
do
58 if not mclassdef
.is_test
then continue
59 if not suite_match_pattern
(mclassdef
) then continue
60 toolcontext
.modelbuilder
.total_classes
+= 1
61 for mpropdef
in mclassdef
.mpropdefs
do
62 if not mpropdef
isa MMethodDef or not mpropdef
.is_test
then continue
63 if not case_match_pattern
(mpropdef
) then continue
64 toolcontext
.modelbuilder
.total_tests
+= 1
65 var test
= new TestCase(suite
, mpropdef
, toolcontext
)
69 # method to execute after all tests in the module
70 var after_module
= mmodule
.after_test
71 if after_module
!= null then
72 toolcontext
.modelbuilder
.total_tests
+= 1
73 suite
.after_module
= new TestCase(suite
, after_module
, toolcontext
)
79 # Is the test suite name match the pattern option?
80 private fun suite_match_pattern
(suite
: MClassDef): Bool do
81 var pattern
= mbuilder
.toolcontext
.opt_pattern
.value
82 if pattern
== null then return true
83 var ps
= pattern
.split_with
("::")
85 if ps
.length
== 1 and p
.first
.is_lower
then return true
86 if ps
.length
== 2 and p
.first
.is_lower
then return false
87 if p
.has_suffix
("*") then
88 p
= p
.substring
(0, p
.length
- 1)
89 if suite
.name
.has_prefix
(p
) then return true
91 if suite
.name
== p
then return true
96 # Is the test case name match the pattern option?
97 private fun case_match_pattern
(case
: MPropDef): Bool do
98 var pattern
= mbuilder
.toolcontext
.opt_pattern
.value
99 if pattern
== null then return true
100 var ps
= pattern
.split_with
("::")
102 if ps
.length
== 1 and p
.first
.is_upper
then return true
103 if ps
.length
== 2 and p
.first
.is_upper
then return false
104 if p
.has_suffix
("*") then
105 p
= p
.substring
(0, p
.length
- 1)
106 if case
.name
.has_prefix
(p
) then return true
108 if case
.name
== p
then return true
114 # A test suite contains all the test cases for a `MModule`.
117 # `MModule` under test
120 # `ToolContext` to use to display messages.
121 var toolcontext
: ToolContext
123 # List of `TestCase` to be executed in this suite.
124 var test_cases
= new Array[TestCase]
126 # Add a `TestCase` to the suite.
127 fun add_test
(case
: TestCase) do test_cases
.add case
129 # Test to be executed before the whole test suite.
130 var before_module
: nullable TestCase = null
132 # Test to be executed after the whole test suite.
133 var after_module
: nullable TestCase = null
137 toolcontext
.show_unit_status
("Test-suite of module " + mmodule
.full_name
, test_cases
)
140 # Execute the test suite
143 if not toolcontext
.test_dir
.file_exists
then
144 toolcontext
.test_dir
.mkdir
148 toolcontext
.info
("Execute test-suite {mmodule.name}", 1)
149 var before_module
= self.before_module
150 if not before_module
== null then before_module
.run
151 for case
in test_cases
do
159 var after_module
= self.after_module
160 if not after_module
== null then after_module
.run
161 for case
in test_cases
do
162 toolcontext
.show_unit
(case
)
166 # Write the test unit for `self` in a nit compilable file.
168 var file
= new Template
169 file
.addn
"intrude import test_suite"
170 file
.addn
"import {mmodule.name}\n"
171 file
.addn
"var name = args.first"
172 for case
in test_cases
do
173 case
.write_to_nit
(file
)
175 file
.write_to_file
("{test_file}.nit")
178 # Return the test suite in XML format compatible with Jenkins.
179 # Contents depends on the `run` execution.
180 fun to_xml
: HTMLTag do
181 var n
= new HTMLTag("testsuite")
182 n
.attr
("package", mmodule
.name
)
183 var failure
= self.failure
184 if failure
!= null then
185 var f
= new HTMLTag("failure")
186 f
.attr
("message", failure
.to_s
)
189 for test
in test_cases
do n
.add test
.to_xml
194 # Generated test file name.
195 fun test_file
: String do
196 return toolcontext
.test_dir
/ "gen_{mmodule.name.escape_to_c}"
199 # Compile all `test_cases` cases in one file.
202 var nitc
= toolcontext
.find_nitc
205 var module_file
= mmodule
.location
.file
206 if module_file
== null then
207 toolcontext
.error
(null, "Error: cannot find module file for {mmodule.name}.")
208 toolcontext
.check_errors
211 var include_dir
= module_file
.filename
.dirname
212 var cmd
= "{nitc} --no-color '{file}.nit' -I {include_dir} -o '{file}.bin' > '{file}.out' 2>&1 </dev/null"
213 var res
= toolcontext
.safe_exec
(cmd
)
214 var f
= new FileReader.open
("{file}.out")
217 # set test case result
218 var loc
= mmodule
.location
221 toolcontext
.warning
(loc
, "failure", "FAILURE: {mmodule.name} (in file {file}.nit): {msg}")
222 toolcontext
.modelbuilder
.failed_tests
+= 1
224 toolcontext
.check_errors
227 # Error occured during test-suite compilation.
228 var failure
: nullable String = null
231 # A test case is a unit test considering only a `MMethodDef`.
235 # Test suite wich `self` belongs to.
236 var test_suite
: TestSuite
238 # Test method to be compiled and tested.
239 var test_method
: MMethodDef
241 redef fun full_name
do return test_method
.full_name
243 redef fun location
do return test_method
.location
245 # `ToolContext` to use to display messages and find `nitc` bin.
246 var toolcontext
: ToolContext
248 # Generate the test unit for `self` in `file`.
249 fun write_to_nit
(file
: Template) do
250 var name
= test_method
.name
251 file
.addn
"if name == \"{name}\
" then"
252 if test_method
.mproperty
.is_toplevel
then
255 file
.addn
"\tvar subject = new {test_method.mclassdef.name}.nitunit"
256 file
.addn
"\tsubject.before_test"
257 file
.addn
"\tsubject.{name}"
258 file
.addn
"\tsubject.after_test"
263 # Execute the test case.
265 toolcontext
.info
("Execute test-case {test_method.name}", 1)
267 if toolcontext
.opt_noact
.value
then return
269 var method_name
= test_method
.name
270 var test_file
= test_suite
.test_file
271 var res_name
= "{test_file}_{method_name.escape_to_c}"
272 var res
= toolcontext
.safe_exec
("{test_file}.bin {method_name} > '{res_name}.out1' 2>&1 </dev/null")
273 self.raw_output
= "{res_name}.out1".to_path
.read_all
274 # set test case result
276 error
= "Runtime Error in file {test_file}.nit"
277 toolcontext
.modelbuilder
.failed_tests
+= 1
279 # no error, check with res file, if any.
280 var mmodule
= test_method
.mclassdef
.mmodule
281 var file
= mmodule
.filepath
283 var sav
= file
.dirname
/ mmodule
.name
+ ".sav" / test_method
.name
+ ".res"
284 if sav
.file_exists
then
285 toolcontext
.info
("Diff output with {sav}", 1)
286 res
= toolcontext
.safe_exec
("diff -u --label 'expected:{sav}' --label 'got:{res_name}.out1' '{sav}' '{res_name}.out1' > '{res_name}.diff' 2>&1 </dev/null")
288 self.raw_output
= "Diff\n" + "{res_name}.diff".to_path
.read_all
289 error
= "Difference with expected output: diff -u {sav} {res_name}.out1"
290 toolcontext
.modelbuilder
.failed_tests
+= 1
293 toolcontext
.info
("No diff: {sav} not found", 2)
300 redef fun xml_classname
do
301 var mclassdef
= test_method
.mclassdef
302 return "nitunit." + mclassdef
.mmodule
.full_name
+ "." + mclassdef
.mclass
.full_name
305 redef fun xml_name
do
306 return test_method
.mproperty
.full_name
310 redef class MMethodDef
311 # TODO use annotations?
313 # Is the method a test_method?
314 # i.e. begins with "test_"
315 private fun is_test
: Bool do return name
.has_prefix
("test_")
317 # Is the method a "before_module"?
318 private fun is_before_module
: Bool do return mproperty
.is_toplevel
and name
== "before_module"
320 # Is the method a "after_module"?
321 private fun is_after_module
: Bool do return mproperty
.is_toplevel
and name
== "after_module"
324 redef class MClassDef
325 # Is the class a TestClass?
326 # i.e. begins with "Test"
327 private fun is_test
: Bool do
328 var in_hierarchy
= self.in_hierarchy
329 if in_hierarchy
== null then return false
330 for sup
in in_hierarchy
.greaters
do
331 if sup
.name
== "TestSuite" then return true
338 # "before_module" method for this module.
339 private fun before_test
: nullable MMethodDef do
340 for mclassdef
in mclassdefs
do
341 if not mclassdef
.name
== "Object" then continue
342 for mpropdef
in mclassdef
.mpropdefs
do
343 if mpropdef
isa MMethodDef and mpropdef
.is_before_module
then return mpropdef
349 # "after_module" method for this module.
350 private fun after_test
: nullable MMethodDef do
351 for mclassdef
in mclassdefs
do
352 if not mclassdef
.name
== "Object" then continue
353 for mpropdef
in mclassdef
.mpropdefs
do
354 if mpropdef
isa MMethodDef and mpropdef
.is_after_module
then return mpropdef
361 redef class ModelBuilder
362 # Number of test classes generated.
363 var total_classes
= 0
365 # Number of tests generated.
368 # Number of failed tests.
371 # Run NitUnit test file for mmodule (if exists).
372 fun test_unit
(mmodule
: MModule): HTMLTag do
373 var ts
= new HTMLTag("testsuite")
374 toolcontext
.info
("nitunit: test-suite test_{mmodule}", 2)
375 var f
= toolcontext
.opt_file
.value
376 var test_file
= "test_{mmodule.name}.nit"
379 else if not test_file
.file_exists
then
380 var module_file
= mmodule
.location
.file
381 if module_file
== null then
382 toolcontext
.info
("Skip test for {mmodule}, no file found", 2)
385 var include_dir
= module_file
.filename
.dirname
386 test_file
= "{include_dir}/{test_file}"
388 if not test_file
.file_exists
then
389 toolcontext
.info
("Skip test for {mmodule}, no file {test_file} found", 2)
392 var tester
= new NitUnitTester(self)
393 var res
= tester
.test_module_unit
(test_file
)
395 toolcontext
.info
("Skip test for {mmodule}, no test suite found", 2)