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 # Program to extract and execute unit tests from nit source files
18 import modelize_property
19 intrude import markdown
22 # Extractor, Executor an Reporter for the tests in a module
26 # The name of the module to import
29 # The prefix of the generated Nit source-file
32 # The XML node associated to the module
33 var testsuite
: HTMLTag
36 init(toolcontext
: ToolContext, prefix
: String, modname
: String, testsuite
: HTMLTag)
40 self.modname
= modname
41 self.testsuite
= testsuite
44 # All blocks of code from a same `ADoc`
45 var block
= new Array[String]
47 redef fun process_code
(n
: HTMLTag, text
: String)
50 var ast
= toolcontext
.parse_something
(text
)
52 # We want executable code
53 if not (ast
isa AModule or ast
isa ABlockExpr or ast
isa AExpr) then return
55 # Search `assert` in the AST
56 var v
= new SearchAssertVisitor
58 if not v
.foundit
then return
64 # used to generate distinct names
67 # The entry point for a new `ndoc` node
68 # Fill the prepated `tc` (testcase) XTM node
69 fun extract
(ndoc
: ADoc, tc
: HTMLTag)
75 if block
.is_empty
then return
77 toolcontext
.modelbuilder
.unit_entities
+= 1
80 var file
= "{prefix}{cpt}.nit"
82 toolcontext
.info
("Execute {tc.attrs["classname"]}.{tc.attrs["name"]} in {file}", 2)
84 var dir
= file
.dirname
85 if dir
!= "" then dir
.mkdir
87 f
= new OFStream.open
(file
)
88 f
.write
("# GENERATED FILE\n")
89 f
.write
("# Example extracted from a documentation\n")
90 var modname
= self.modname
91 f
.write
("import {modname}\n")
98 var nit_dir
= toolcontext
.nit_dir
99 var nitg
= "{nit_dir}/bin/nitg"
100 if nit_dir
== null or not nitg
.file_exists
then
101 toolcontext
.error
(null, "Cannot find nitg. Set envvar NIT_DIR.")
102 toolcontext
.check_errors
104 var cmd
= "{nitg} --ignore-visibility --no-color '{file}' -I . >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
105 var res
= sys
.system
(cmd
)
108 res2
= sys
.system
("./{file}.bin >>'{file}.out1' 2>&1 </dev/null")
112 f
= new IFStream.open
("{file}.out1")
114 n2
= new HTMLTag("system-err")
119 n2
= new HTMLTag("system-out")
121 for text
in block
do n2
.append
(text
)
125 var ne
= new HTMLTag("failure")
126 ne
.attr
("message", msg
)
128 toolcontext
.warning
(ndoc
.location
, "FAILURE: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
129 toolcontext
.modelbuilder
.failed_entities
+= 1
130 else if res2
!= 0 then
131 var ne
= new HTMLTag("error")
132 ne
.attr
("message", msg
)
134 toolcontext
.warning
(ndoc
.location
, "ERROR: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
135 toolcontext
.modelbuilder
.failed_entities
+= 1
137 toolcontext
.check_errors
143 class SearchAssertVisitor
146 redef fun visit
(node
)
150 else if node
isa AAssertExpr then
159 redef class ModelBuilder
160 var total_entities
= 0
162 var unit_entities
= 0
163 var failed_entities
= 0
165 fun test_markdown
(mmodule
: MModule): HTMLTag
167 var ts
= new HTMLTag("testsuite")
168 toolcontext
.info
("nitunit: {mmodule}", 2)
169 if not mmodule2nmodule
.has_key
(mmodule
) then return ts
171 var nmodule
= mmodule2nmodule
[mmodule
]
172 assert nmodule
!= null
174 # what module to import in the unit test.
175 # try to detect the main module of the project
176 # TODO do things correctly once the importation of arbitraty nested module is legal
180 o
= get_mmodule_by_name
(nmodule
, g
, g
.mproject
.name
).as(not null)
183 ts
.attr
("package", mmodule
.full_name
)
185 var prefix
= toolcontext
.opt_dir
.value
186 if prefix
== null then prefix
= ".nitunit"
187 prefix
= prefix
.join_path
(mmodule
.to_s
)
188 var d2m
= new NitUnitExecutor(toolcontext
, prefix
, o
.name
, ts
)
194 var nmoduledecl
= nmodule
.n_moduledecl
195 if nmoduledecl
== null then break label x
196 var ndoc
= nmoduledecl
.n_doc
197 if ndoc
== null then break label x
199 tc
= new HTMLTag("testcase")
200 # NOTE: jenkins expects a '.' in the classname attr
201 tc
.attr
("classname", mmodule
.full_name
+ ".<module>")
202 tc
.attr
("name", "<module>")
203 d2m
.extract
(ndoc
, tc
)
205 for nclassdef
in nmodule
.n_classdefs
do
206 var mclassdef
= nclassdef
.mclassdef
.as(not null)
207 if nclassdef
isa AStdClassdef then
209 var ndoc
= nclassdef
.n_doc
212 tc
= new HTMLTag("testcase")
213 tc
.attr
("classname", mmodule
.full_name
+ "." + mclassdef
.mclass
.full_name
)
214 tc
.attr
("name", "<class>")
215 d2m
.extract
(ndoc
, tc
)
218 for npropdef
in nclassdef
.n_propdefs
do
219 var mpropdef
= npropdef
.mpropdef
.as(not null)
221 var ndoc
= npropdef
.n_doc
224 tc
= new HTMLTag("testcase")
225 tc
.attr
("classname", mmodule
.full_name
+ "." + mclassdef
.mclass
.full_name
)
226 tc
.attr
("name", mpropdef
.mproperty
.full_name
)
227 d2m
.extract
(ndoc
, tc
)
236 redef class ToolContext
237 var opt_full
= new OptionBool("Process also imported modules", "--full")
238 var opt_output
= new OptionString("Output name (default is 'nitunit.xml')", "-o", "--output")
239 var opt_dir
= new OptionString("Working directory (default is '.nitunit')", "--dir")
242 var toolcontext
= new ToolContext
244 toolcontext
.option_context
.add_option
(toolcontext
.opt_full
, toolcontext
.opt_output
, toolcontext
.opt_dir
)
245 toolcontext
.tooldescription
= "Usage: nitunit [OPTION]... <file.nit>...\nExecutes the unit tests from Nit source files."
247 toolcontext
.process_options
(args
)
248 var args
= toolcontext
.option_context
.rest
250 var model
= new Model
251 var modelbuilder
= new ModelBuilder(model
, toolcontext
)
253 var mmodules
= modelbuilder
.parse
(args
)
254 modelbuilder
.run_phases
256 var page
= new HTMLTag("testsuites")
258 if toolcontext
.opt_full
.value
then mmodules
= model
.mmodules
261 page
.add modelbuilder
.test_markdown
(m
)
264 var file
= toolcontext
.opt_output
.value
265 if file
== null then file
= "nitunit.xml"
266 page
.write_to_file
(file
)
267 print
"Results saved in {file}"
269 if modelbuilder
.unit_entities
== 0 then
270 print
"No nitunits found"
271 else if modelbuilder
.failed_entities
== 0 then
274 print
"Entities: {modelbuilder.total_entities}; Documented ones: {modelbuilder.doc_entities}; With nitunits: {modelbuilder.unit_entities}; Failures: {modelbuilder.failed_entities}"