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 docdown
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 # All failures from a same `ADoc`
38 var failures
= new Array[String]
40 redef fun process_code
(n
: HTMLTag, text
: String, tag
: nullable String)
43 if n
.tag
!= "pre" then return
46 if tag
!= null and tag
!= "nit" and tag
!= "" then
51 var ast
= toolcontext
.parse_something
(text
)
54 if ast
isa TComment then return
56 # We want executable code
57 if not (ast
isa AModule or ast
isa ABlockExpr or ast
isa AExpr) then
59 if ast
isa AError then message
= " At {ast.location}: {ast.message}."
60 toolcontext
.warning
(ndoc
.location
, "invalid-block", "Error: There is a block of code that is not valid Nit, thus not considered a nitunit. To suppress this warning, enclose the block with a fence tagged `nitish` or `raw` (see `man nitdoc`).{message}")
61 failures
.add
("{ndoc.location}: Invalid block of code.{message}")
65 # Create a first block
66 # Or create a new block for modules that are more than a main part
67 if blocks
.is_empty
or ast
isa AModule then
68 blocks
.add
(new Array[String])
75 # The associated node to localize warnings
76 var ndoc
: nullable ADoc = null
78 # used to generate distinct names
81 # The entry point for a new `ndoc` node
82 # Fill the prepated `tc` (testcase) XTM node
83 fun extract
(ndoc
: ADoc, tc
: HTMLTag)
92 toolcontext
.check_errors
94 if not failures
.is_empty
then
95 for msg
in failures
do
96 var ne
= new HTMLTag("failure")
97 ne
.attr
("message", msg
)
99 toolcontext
.modelbuilder
.failed_entities
+= 1
101 if blocks
.is_empty
then testsuite
.add
(tc
)
104 if blocks
.is_empty
then return
106 for block
in blocks
do test_block
(ndoc
, tc
, block
)
110 fun test_block
(ndoc
: ADoc, tc
: HTMLTag, block
: Array[String])
112 toolcontext
.modelbuilder
.unit_entities
+= 1
115 var file
= "{prefix}-{cpt}.nit"
117 toolcontext
.info
("Execute doc-unit {tc.attrs["name"]} in {file}", 1)
119 var dir
= file
.dirname
120 if dir
!= "" then dir
.mkdir
122 f
= new OFStream.open
(file
)
123 f
.write
("# GENERATED FILE\n")
124 f
.write
("# Example extracted from a documentation\n")
125 f
.write
("import {mmodule.name}\n")
132 if toolcontext
.opt_noact
.value
then return
134 var nit_dir
= toolcontext
.nit_dir
135 var nitg
= nit_dir
/"bin/nitg"
136 if not nitg
.file_exists
then
137 toolcontext
.error
(null, "Cannot find nitg. Set envvar NIT_DIR.")
138 toolcontext
.check_errors
140 var cmd
= "{nitg} --ignore-visibility --no-color '{file}' -I {mmodule.location.file.filename.dirname} >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
141 var res
= sys
.system
(cmd
)
144 res2
= sys
.system
("{file.to_program_name}.bin >>'{file}.out1' 2>&1 </dev/null")
148 f
= new IFStream.open
("{file}.out1")
150 n2
= new HTMLTag("system-err")
155 n2
= new HTMLTag("system-out")
157 for text
in block
do n2
.append
(text
)
161 var ne
= new HTMLTag("failure")
162 ne
.attr
("message", msg
)
164 toolcontext
.warning
(ndoc
.location
, "failure", "FAILURE: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
165 toolcontext
.modelbuilder
.failed_entities
+= 1
166 else if res2
!= 0 then
167 var ne
= new HTMLTag("error")
168 ne
.attr
("message", msg
)
170 toolcontext
.warning
(ndoc
.location
, "error", "ERROR: {tc.attrs["classname"]}.{tc.attrs["name"]} (in {file}): {msg}")
171 toolcontext
.modelbuilder
.failed_entities
+= 1
173 toolcontext
.check_errors
179 class SearchAssertVisitor
182 redef fun visit
(node
)
186 else if node
isa AAssertExpr then
195 redef class ModelBuilder
196 var total_entities
= 0
198 var unit_entities
= 0
199 var failed_entities
= 0
201 fun test_markdown
(mmodule
: MModule): HTMLTag
203 var ts
= new HTMLTag("testsuite")
204 toolcontext
.info
("nitunit: doc-unit {mmodule}", 2)
205 if not mmodule2nmodule
.has_key
(mmodule
) then return ts
207 var nmodule
= mmodule2nmodule
[mmodule
]
209 # usualy, only the original module must be imported in the unit test.
212 if g
!= null and g
.mproject
.name
== "standard" then
213 # except for a unit test in a module of standard
214 # in this case, the whole standard must be imported
215 o
= get_mmodule_by_name
(nmodule
, g
, g
.mproject
.name
).as(not null)
218 ts
.attr
("package", mmodule
.full_name
)
220 var prefix
= toolcontext
.test_dir
221 prefix
= prefix
.join_path
(mmodule
.to_s
)
222 var d2m
= new NitUnitExecutor(toolcontext
, prefix
, o
, ts
)
228 var nmoduledecl
= nmodule
.n_moduledecl
229 if nmoduledecl
== null then break label x
230 var ndoc
= nmoduledecl
.n_doc
231 if ndoc
== null then break label x
233 tc
= new HTMLTag("testcase")
234 # NOTE: jenkins expects a '.' in the classname attr
235 tc
.attr
("classname", "nitunit." + mmodule
.full_name
+ ".<module>")
236 tc
.attr
("name", "<module>")
237 d2m
.extract
(ndoc
, tc
)
239 for nclassdef
in nmodule
.n_classdefs
do
240 var mclassdef
= nclassdef
.mclassdef
241 if mclassdef
== null then continue
242 if nclassdef
isa AStdClassdef then
244 var ndoc
= nclassdef
.n_doc
247 tc
= new HTMLTag("testcase")
248 tc
.attr
("classname", "nitunit." + mmodule
.full_name
+ "." + mclassdef
.mclass
.full_name
)
249 tc
.attr
("name", "<class>")
250 d2m
.extract
(ndoc
, tc
)
253 for npropdef
in nclassdef
.n_propdefs
do
254 var mpropdef
= npropdef
.mpropdef
255 if mpropdef
== null then continue
257 var ndoc
= npropdef
.n_doc
260 tc
= new HTMLTag("testcase")
261 tc
.attr
("classname", "nitunit." + mmodule
.full_name
+ "." + mclassdef
.mclass
.full_name
)
262 tc
.attr
("name", mpropdef
.mproperty
.full_name
)
263 d2m
.extract
(ndoc
, tc
)