nitunit: allow test-suite skeleton generation.
[nit.git] / src / nitunit.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Program to extract and execute unit tests from nit source files
16 module nitunit
17
18 import testing
19
20 var toolcontext = new ToolContext
21
22 toolcontext.option_context.add_option(toolcontext.opt_full, toolcontext.opt_output, toolcontext.opt_dir, toolcontext.opt_noact, toolcontext.opt_pattern, toolcontext.opt_file, toolcontext.opt_gen_unit, toolcontext.opt_gen_force, toolcontext.opt_gen_private, toolcontext.opt_gen_show)
23 toolcontext.tooldescription = "Usage: nitunit [OPTION]... <file.nit>...\nExecutes the unit tests from Nit source files."
24
25 toolcontext.process_options(args)
26 var args = toolcontext.option_context.rest
27
28 if toolcontext.opt_gen_unit.value then
29 if toolcontext.opt_pattern.value != null then
30 print "Option --pattern cannot be used with --gen-suite"
31 exit(0)
32 end
33 if toolcontext.opt_file.value != null then
34 print "Option --target-file cannot be used with --gen-suite"
35 exit(0)
36 end
37 else
38 if toolcontext.opt_gen_force.value then
39 print "Option --force must be used with --gen-suite"
40 exit(0)
41 end
42 if toolcontext.opt_gen_private.value then
43 print "Option --private must be used with --gen-suite"
44 exit(0)
45 end
46 if toolcontext.opt_gen_show.value then
47 print "Option --only-show must be used with --gen-suite"
48 exit(0)
49 end
50 end
51
52 var model = new Model
53 var modelbuilder = new ModelBuilder(model, toolcontext)
54
55 var mmodules = modelbuilder.parse(args)
56 modelbuilder.run_phases
57
58 if toolcontext.opt_gen_unit.value then
59 modelbuilder.gen_test_unit(mmodules.first)
60 exit(0)
61 end
62
63 var page = new HTMLTag("testsuites")
64
65 if toolcontext.opt_full.value then mmodules = model.mmodules
66
67 for m in mmodules do
68 page.add modelbuilder.test_markdown(m)
69 page.add modelbuilder.test_unit(m)
70 end
71
72 var file = toolcontext.opt_output.value
73 if file == null then file = "nitunit.xml"
74 page.write_to_file(file)
75 print "Results saved in {file}"
76 # print docunits results
77 print "\nDocUnits:"
78 if modelbuilder.unit_entities == 0 then
79 print "No doc units found"
80 else if modelbuilder.failed_entities == 0 and not toolcontext.opt_noact.value then
81 print "DocUnits Success"
82 end
83 print "Entities: {modelbuilder.total_entities}; Documented ones: {modelbuilder.doc_entities}; With nitunits: {modelbuilder.unit_entities}; Failures: {modelbuilder.failed_entities}"
84 # print testsuites results
85 print "\nTestSuites:"
86 if modelbuilder.total_tests == 0 then
87 print "No test cases found"
88 else if modelbuilder.failed_tests == 0 and not toolcontext.opt_noact.value then
89 print "TestSuites Success"
90 end
91 print "Class suites: {modelbuilder.total_classes}; Test Cases: {modelbuilder.total_tests}; Failures: {modelbuilder.failed_tests}"