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 code comments.
19 intrude import markdown
21 # Extractor, Executor and Reporter for the tests in a module
25 # The prefix of the generated Nit source-file
28 # The module to import
31 # The XML node associated to the module
32 var testsuite
: HTMLTag
34 # All blocks of code from a same `ADoc`
35 var blocks
= new Array[Array[String]]
37 redef fun process_code
(n
: HTMLTag, text
: String)
40 if n
.tag
!= "pre" then return
43 var ast
= toolcontext
.parse_something
(text
)
45 # We want executable code
46 if not (ast
isa AModule or ast
isa ABlockExpr or ast
isa AExpr) then
47 if ndoc
!= null and n
.tag
== "pre" and toolcontext
.opt_warn
.value
> 1 then
48 toolcontext
.warning
(ndoc
.location
, "invalid-block", "Warning: There is a block of code that is not valid Nit, thus not considered a nitunit")
49 if ast
isa AError then toolcontext
.warning
(ast
.location
, "syntax-error", ast
.message
)
50 ndoc
= null # To avoid multiple warning in the same node
55 # Search `assert` in the AST
56 var v
= new SearchAssertVisitor
59 if ndoc
!= null and n
.tag
== "pre" and toolcontext
.opt_warn
.value
> 1 then
60 toolcontext
.warning
(ndoc
.location
, "invalid-block", "Warning: There is a block of Nit code without `assert`, thus not considered a nitunit")
61 ndoc
= null # To avoid multiple warning in the same node
66 # Create a first block
67 # Or create a new block for modules that are more than a main part
68 if blocks
.is_empty
or ast
isa AModule then
69 blocks
.add
(new Array[String])
76 # The associated node to localize warnings
77 var ndoc
: nullable ADoc = null
79 # used to generate distinct names
82 # The entry point for a new `ndoc` node
83 # Fill the prepated `tc` (testcase) XTM node
84 fun extract
(ndoc
: ADoc, tc
: HTMLTag)
91 toolcontext
.check_errors
93 if blocks
.is_empty
then return
95 for block
in blocks
do test_block
(ndoc
, tc
, block
)
99 fun test_block
(ndoc
: ADoc, tc
: HTMLTag, block
: Array[String])
101 toolcontext
.modelbuilder
.unit_entities
+= 1
104 var file
= "{prefix}-{cpt}.nit"
106 toolcontext
.info
("Execute doc-unit {tc.attrs["name"]} in {file}", 1)
108 var dir
= file
.dirname
109 if dir
!= "" then dir
.mkdir
111 f
= new OFStream.open
(file
)
112 f
.write
("# GENERATED FILE\n")
113 f
.write
("# Example extracted from a documentation\n")
114 f
.write
("import {mmodule.name}\n")
121 if toolcontext
.opt_noact
.value
then return
123 var nit_dir
= toolcontext
.nit_dir
124 var nitg
= nit_dir
/"bin/nitg"
125 if not nitg
.file_exists
then
126 toolcontext
.error
(null, "Cannot find nitg. Set envvar NIT_DIR.")
127 toolcontext
.check_errors
129 var cmd
= "{nitg} --ignore-visibility --no-color '{file}' -I {mmodule.location.file.filename.dirname} >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
130 var res
= sys
.system
(cmd
)
133 res2
= sys
.system
("{file.to_program_name}.bin >>'{file}.out1' 2>&1 </dev/null")
137 f
= new IFStream.open
("{file}.out1")
139 n2
= new HTMLTag("system-err")
144 n2
= new HTMLTag("system-out")
146 for text
in block
do n2
.append
(text
)
150 var ne
= new HTMLTag("failure")
151 ne
.attr
("message", msg
)
153 toolcontext
.warning
(ndoc
.location
, "failure", "FAILURE: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
154 toolcontext
.modelbuilder
.failed_entities
+= 1
155 else if res2
!= 0 then
156 var ne
= new HTMLTag("error")
157 ne
.attr
("message", msg
)
159 toolcontext
.warning
(ndoc
.location
, "error", "ERROR: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
160 toolcontext
.modelbuilder
.failed_entities
+= 1
162 toolcontext
.check_errors
168 class SearchAssertVisitor
171 redef fun visit
(node
)
175 else if node
isa AAssertExpr then
184 redef class ModelBuilder
185 var total_entities
= 0
187 var unit_entities
= 0
188 var failed_entities
= 0
190 fun test_markdown
(mmodule
: MModule): HTMLTag
192 var ts
= new HTMLTag("testsuite")
193 toolcontext
.info
("nitunit: doc-unit {mmodule}", 2)
194 if not mmodule2nmodule
.has_key
(mmodule
) then return ts
196 var nmodule
= mmodule2nmodule
[mmodule
]
198 # usualy, only the original module must be imported in the unit test.
201 if g
!= null and g
.mproject
.name
== "standard" then
202 # except for a unit test in a module of standard
203 # in this case, the whole standard must be imported
204 o
= get_mmodule_by_name
(nmodule
, g
, g
.mproject
.name
).as(not null)
207 ts
.attr
("package", mmodule
.full_name
)
209 var prefix
= toolcontext
.test_dir
210 prefix
= prefix
.join_path
(mmodule
.to_s
)
211 var d2m
= new NitUnitExecutor(toolcontext
, prefix
, o
, ts
)
217 var nmoduledecl
= nmodule
.n_moduledecl
218 if nmoduledecl
== null then break label x
219 var ndoc
= nmoduledecl
.n_doc
220 if ndoc
== null then break label x
222 tc
= new HTMLTag("testcase")
223 # NOTE: jenkins expects a '.' in the classname attr
224 tc
.attr
("classname", "nitunit." + mmodule
.full_name
+ ".<module>")
225 tc
.attr
("name", "<module>")
226 d2m
.extract
(ndoc
, tc
)
228 for nclassdef
in nmodule
.n_classdefs
do
229 var mclassdef
= nclassdef
.mclassdef
230 if mclassdef
== null then continue
231 if nclassdef
isa AStdClassdef then
233 var ndoc
= nclassdef
.n_doc
236 tc
= new HTMLTag("testcase")
237 tc
.attr
("classname", "nitunit." + mmodule
.full_name
+ "." + mclassdef
.mclass
.full_name
)
238 tc
.attr
("name", "<class>")
239 d2m
.extract
(ndoc
, tc
)
242 for npropdef
in nclassdef
.n_propdefs
do
243 var mpropdef
= npropdef
.mpropdef
244 if mpropdef
== null then continue
246 var ndoc
= npropdef
.n_doc
249 tc
= new HTMLTag("testcase")
250 tc
.attr
("classname", "nitunit." + mmodule
.full_name
+ "." + mclassdef
.mclass
.full_name
)
251 tc
.attr
("name", mpropdef
.mproperty
.full_name
)
252 d2m
.extract
(ndoc
, tc
)