7c60b83307a0e787d0310e2abf2fbe5a41bf9e99
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
21 private import realtime
23 redef class ToolContext
25 var opt_pattern
= new OptionString("Only run test case with name that match pattern", "-p", "--pattern")
27 var opt_autosav
= new OptionBool("Automatically create/update .res files for black box testing", "--autosav")
30 # Used to test nitunit test files.
33 # `ModelBuilder` used to parse test files.
34 var mbuilder
: ModelBuilder
36 # Compile and execute `mmodule` as a test suite.
37 fun test_module_unit
(mmodule
: MModule): TestSuite do
38 var toolcontext
= mbuilder
.toolcontext
39 var suite
= new TestSuite(mmodule
, toolcontext
)
40 # method to execute before all tests in the module
41 var before_module
= mmodule
.before_test
42 if before_module
!= null then
43 toolcontext
.modelbuilder
.total_tests
+= 1
44 suite
.before_module
= new TestCase(suite
, before_module
, toolcontext
)
46 # generate all test cases
47 for mclassdef
in mmodule
.mclassdefs
do
48 if not mclassdef
.is_test
then continue
49 if not suite_match_pattern
(mclassdef
) then continue
50 toolcontext
.modelbuilder
.total_classes
+= 1
51 for mpropdef
in mclassdef
.mpropdefs
do
52 if not mpropdef
isa MMethodDef or not mpropdef
.is_test
then continue
53 if not case_match_pattern
(mpropdef
) then continue
54 toolcontext
.modelbuilder
.total_tests
+= 1
55 var test
= new TestCase(suite
, mpropdef
, toolcontext
)
59 # method to execute after all tests in the module
60 var after_module
= mmodule
.after_test
61 if after_module
!= null then
62 toolcontext
.modelbuilder
.total_tests
+= 1
63 suite
.after_module
= new TestCase(suite
, after_module
, toolcontext
)
69 # Is the test suite name match the pattern option?
70 private fun suite_match_pattern
(suite
: MClassDef): Bool do
71 var pattern
= mbuilder
.toolcontext
.opt_pattern
.value
72 if pattern
== null then return true
73 var ps
= pattern
.split_with
("::")
75 if ps
.length
== 1 and p
.first
.is_lower
then return true
76 if ps
.length
== 2 and p
.first
.is_lower
then return false
77 if p
.has_suffix
("*") then
78 p
= p
.substring
(0, p
.length
- 1)
79 if suite
.name
.has_prefix
(p
) then return true
81 if suite
.name
== p
then return true
86 # Is the test case name match the pattern option?
87 private fun case_match_pattern
(case
: MPropDef): Bool do
88 var pattern
= mbuilder
.toolcontext
.opt_pattern
.value
89 if pattern
== null then return true
90 var ps
= pattern
.split_with
("::")
92 if ps
.length
== 1 and p
.first
.is_upper
then return true
93 if ps
.length
== 2 and p
.first
.is_upper
then return false
94 if p
.has_suffix
("*") then
95 p
= p
.substring
(0, p
.length
- 1)
96 if case
.name
.has_prefix
(p
) then return true
98 if case
.name
== p
then return true
104 # A test suite contains all the test cases for a `MModule`.
107 # `MModule` under test
110 # `ToolContext` to use to display messages.
111 var toolcontext
: ToolContext
113 # List of `TestCase` to be executed in this suite.
114 var test_cases
= new Array[TestCase]
116 # Add a `TestCase` to the suite.
117 fun add_test
(case
: TestCase) do test_cases
.add case
119 # Test to be executed before the whole test suite.
120 var before_module
: nullable TestCase = null
122 # Test to be executed after the whole test suite.
123 var after_module
: nullable TestCase = null
127 toolcontext
.show_unit_status
("Test-suite of module " + mmodule
.full_name
, test_cases
)
130 # Execute the test suite
133 if not toolcontext
.test_dir
.file_exists
then
134 toolcontext
.test_dir
.mkdir
138 toolcontext
.info
("Execute test-suite {mmodule.name}", 1)
139 var before_module
= self.before_module
140 if not before_module
== null then before_module
.run
141 for case
in test_cases
do
143 toolcontext
.clear_progress_bar
144 toolcontext
.show_unit
(case
)
148 var after_module
= self.after_module
149 if not after_module
== null then after_module
.run
155 # Write the test unit for `self` in a nit compilable file.
157 var file
= new Template
158 file
.addn
"intrude import test_suite"
159 file
.addn
"import {mmodule.name}\n"
160 file
.addn
"var name = args.first"
161 for case
in test_cases
do
162 case
.write_to_nit
(file
)
164 file
.write_to_file
("{test_file}.nit")
167 # Return the test suite in XML format compatible with Jenkins.
168 # Contents depends on the `run` execution.
169 fun to_xml
: HTMLTag do
170 var n
= new HTMLTag("testsuite")
171 n
.attr
("package", mmodule
.name
)
172 var failure
= self.failure
173 if failure
!= null then
174 var f
= new HTMLTag("failure")
175 f
.attr
("message", failure
.to_s
)
178 for test
in test_cases
do n
.add test
.to_xml
183 # Generated test file name.
184 fun test_file
: String do
185 return toolcontext
.test_dir
/ "gen_{mmodule.name.escape_to_c}"
188 # Compile all `test_cases` cases in one file.
191 var nitc
= toolcontext
.find_nitc
194 var module_file
= mmodule
.location
.file
195 if module_file
== null then
196 toolcontext
.error
(null, "Error: cannot find module file for {mmodule.name}.")
197 toolcontext
.check_errors
200 var include_dir
= module_file
.filename
.dirname
201 var cmd
= "{nitc} --no-color -q '{file}.nit' -I {include_dir} -o '{file}.bin' > '{file}.out' 2>&1 </dev/null"
202 var res
= toolcontext
.safe_exec
(cmd
)
203 var f
= new FileReader.open
("{file}.out")
206 # set test case result
207 var loc
= mmodule
.location
210 toolcontext
.warning
(loc
, "failure", "FAILURE: {mmodule.name} (in file {file}.nit): {msg}")
211 toolcontext
.modelbuilder
.failed_tests
+= 1
213 toolcontext
.check_errors
216 # Error occured during test-suite compilation.
217 var failure
: nullable String = null
220 # A test case is a unit test considering only a `MMethodDef`.
224 # Test suite wich `self` belongs to.
225 var test_suite
: TestSuite
227 # Test method to be compiled and tested.
228 var test_method
: MMethodDef
230 redef fun full_name
do return test_method
.full_name
232 redef fun location
do return test_method
.location
234 # `ToolContext` to use to display messages and find `nitc` bin.
235 var toolcontext
: ToolContext
237 # Generate the test unit for `self` in `file`.
238 fun write_to_nit
(file
: Template) do
239 var name
= test_method
.name
240 file
.addn
"if name == \"{name}\
" then"
241 if test_method
.mproperty
.is_toplevel
then
244 file
.addn
"\tvar subject = new {test_method.mclassdef.name}.nitunit"
245 file
.addn
"\tsubject.before_test"
246 file
.addn
"\tsubject.{name}"
247 file
.addn
"\tsubject.after_test"
252 # Execute the test case.
254 toolcontext
.info
("Execute test-case {test_method.name}", 1)
256 if toolcontext
.opt_noact
.value
then return
258 var method_name
= test_method
.name
259 var test_file
= test_suite
.test_file
260 var res_name
= "{test_file}_{method_name.escape_to_c}"
261 var clock
= new Clock
262 var res
= toolcontext
.safe_exec
("{test_file}.bin {method_name} > '{res_name}.out1' 2>&1 </dev/null")
263 real_time
= clock
.total
265 var raw_output
= "{res_name}.out1".to_path
.read_all
266 self.raw_output
= raw_output
267 # set test case result
269 error
= "Runtime Error in file {test_file}.nit"
270 toolcontext
.modelbuilder
.failed_tests
+= 1
272 # no error, check with res file, if any.
273 var mmodule
= test_method
.mclassdef
.mmodule
274 var file
= mmodule
.filepath
276 var tries
= [ file
.dirname
/ mmodule
.name
+ ".sav" / test_method
.name
+ ".res",
277 file
.dirname
/ "sav" / test_method
.name
+ ".res" ,
278 file
.dirname
/ test_method
.name
+ ".res" ]
279 var savs
= [ for t
in tries
do if t
.file_exists
then t
]
280 if savs
.length
== 1 then
282 toolcontext
.info
("Diff output with {sav}", 1)
283 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")
286 else if toolcontext
.opt_autosav
.value
then
287 raw_output
.write_to_file
(sav
)
288 info
= "Expected output updated: {sav} (--autoupdate)"
290 self.raw_output
= "Diff\n" + "{res_name}.diff".to_path
.read_all
291 error
= "Difference with expected output: diff -u {sav} {res_name}.out1"
292 toolcontext
.modelbuilder
.failed_tests
+= 1
294 else if savs
.length
> 1 then
295 toolcontext
.info
("Conflicting diffs: {savs.join(", ")}", 1)
296 error
= "Conflicting expected output: {savs.join(", ", " and ")} all exist"
297 toolcontext
.modelbuilder
.failed_tests
+= 1
298 else if not raw_output
.is_empty
then
299 toolcontext
.info
("No diff: {tries.join(", ", " or ")} not found", 1)
300 if toolcontext
.opt_autosav
.value
then
301 var sav
= tries
.first
303 raw_output
.write_to_file
(sav
)
304 info
= "Expected output saved: {sav} (--autoupdate)"
312 redef fun xml_classname
do
313 var a
= test_method
.full_name
.split
("$")
314 return "nitunit.{a[0]}.{a[1]}"
317 redef fun xml_name
do
318 var a
= test_method
.full_name
.split
("$")
323 redef class MMethodDef
324 # TODO use annotations?
326 # Is the method a test_method?
327 # i.e. begins with "test_"
328 private fun is_test
: Bool do return name
.has_prefix
("test_")
330 # Is the method a "before_module"?
331 private fun is_before_module
: Bool do return mproperty
.is_toplevel
and name
== "before_module"
333 # Is the method a "after_module"?
334 private fun is_after_module
: Bool do return mproperty
.is_toplevel
and name
== "after_module"
337 redef class MClassDef
338 # Is the class a TestClass?
339 # i.e. is a subclass of `TestSuite`
340 private fun is_test
: Bool do
341 var in_hierarchy
= self.in_hierarchy
342 if in_hierarchy
== null then return false
343 for sup
in in_hierarchy
.greaters
do
344 if sup
.name
== "TestSuite" then return true
351 # "before_module" method for this module.
352 private fun before_test
: nullable MMethodDef do
353 for mclassdef
in mclassdefs
do
354 if not mclassdef
.name
== "Object" then continue
355 for mpropdef
in mclassdef
.mpropdefs
do
356 if mpropdef
isa MMethodDef and mpropdef
.is_before_module
then return mpropdef
362 # "after_module" method for this module.
363 private fun after_test
: nullable MMethodDef do
364 for mclassdef
in mclassdefs
do
365 if not mclassdef
.name
== "Object" then continue
366 for mpropdef
in mclassdef
.mpropdefs
do
367 if mpropdef
isa MMethodDef and mpropdef
.is_after_module
then return mpropdef
374 redef class ModelBuilder
375 # Number of test classes generated.
376 var total_classes
= 0
378 # Number of tests generated.
381 # Number of failed tests.
384 # Run NitUnit test suite for `mmodule` (if it is one).
385 fun test_unit
(mmodule
: MModule): nullable HTMLTag do
386 # is the module a test_suite?
387 if get_mmodule_annotation
("test_suite", mmodule
) == null then return null
388 toolcontext
.info
("nitunit: test-suite {mmodule}", 2)
390 var tester
= new NitUnitTester(self)
391 var res
= tester
.test_module_unit
(mmodule
)