nitunit: run nitunits for documentation of groups
[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 # Testing tool.
16 # see `testing/README`
17 module nitunit
18
19 import testing
20
21 var toolcontext = new ToolContext
22
23 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)
24 toolcontext.tooldescription = "Usage: nitunit [OPTION]... <file.nit>...\nExecutes the unit tests from Nit source files."
25
26 toolcontext.process_options(args)
27 var args = toolcontext.option_context.rest
28
29 if toolcontext.opt_gen_unit.value then
30 if toolcontext.opt_pattern.value != null then
31 print "Option --pattern cannot be used with --gen-suite"
32 exit(0)
33 end
34 if toolcontext.opt_file.value != null then
35 print "Option --target-file cannot be used with --gen-suite"
36 exit(0)
37 end
38 else
39 if toolcontext.opt_gen_force.value then
40 print "Option --force must be used with --gen-suite"
41 exit(0)
42 end
43 if toolcontext.opt_gen_private.value then
44 print "Option --private must be used with --gen-suite"
45 exit(0)
46 end
47 if toolcontext.opt_gen_show.value then
48 print "Option --only-show must be used with --gen-suite"
49 exit(0)
50 end
51 end
52
53 var model = new Model
54 var modelbuilder = new ModelBuilder(model, toolcontext)
55
56 var mmodules = modelbuilder.parse_full(args)
57 modelbuilder.run_phases
58
59 if toolcontext.opt_gen_unit.value then
60 modelbuilder.gen_test_unit(mmodules.first)
61 exit(0)
62 end
63
64 var page = new HTMLTag("testsuites")
65
66 if toolcontext.opt_full.value then mmodules = model.mmodules
67
68 for a in args do
69 var g = modelbuilder.get_mgroup(a)
70 if g == null then continue
71 page.add modelbuilder.test_group(g)
72 end
73
74 for m in mmodules do
75 page.add modelbuilder.test_markdown(m)
76 page.add modelbuilder.test_unit(m)
77 end
78
79 var file = toolcontext.opt_output.value
80 if file == null then file = "nitunit.xml"
81 page.write_to_file(file)
82 # print docunits results
83 print "DocUnits:"
84 if modelbuilder.unit_entities == 0 then
85 print "No doc units found"
86 else if modelbuilder.failed_entities == 0 and not toolcontext.opt_noact.value then
87 print "DocUnits Success"
88 end
89 print "Entities: {modelbuilder.total_entities}; Documented ones: {modelbuilder.doc_entities}; With nitunits: {modelbuilder.unit_entities}; Failures: {modelbuilder.failed_entities}"
90 # print testsuites results
91 print "\nTestSuites:"
92 if modelbuilder.total_tests == 0 then
93 print "No test cases found"
94 else if modelbuilder.failed_tests == 0 and not toolcontext.opt_noact.value then
95 print "TestSuites Success"
96 end
97 print "Class suites: {modelbuilder.total_classes}; Test Cases: {modelbuilder.total_tests}; Failures: {modelbuilder.failed_tests}"