0c6d686e32b51748e73394a006d22b291cefd491
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.
18 private import parser_util
23 # Extractor, Executor and Reporter for the tests in a module
27 # Toolcontext used to parse Nit code blocks.
28 var toolcontext
: ToolContext
30 # The prefix of the generated Nit source-file
33 # The module to import, if any
34 var mmodule
: nullable MModule
36 # The XML node associated to the module
37 var testsuite
: HTMLTag
39 # The name of the suite
42 # Markdown processor used to parse markdown comments and extract code.
43 var mdproc
= new MarkdownProcessor
46 mdproc
.emitter
.decorator
= new NitunitDecorator(self)
49 # The associated documentation object
50 var mdoc
: nullable MDoc = null
52 # used to generate distinct names
55 # The last docunit extracted from a mdoc.
57 # Is used because a new code-block might just be added to it.
58 var last_docunit
: nullable DocUnit = null
60 var xml_classname
: String is noautoinit
62 var xml_name
: String is noautoinit
64 # The entry point for a new `ndoc` node
65 # Fill `docunits` with new discovered unit of tests.
66 fun extract
(mdoc
: MDoc, xml_classname
, xml_name
: String)
69 self.xml_classname
= xml_classname
70 self.xml_name
= xml_name
74 # Populate `blocks` from the markdown decorator
75 mdproc
.process
(mdoc
.content
.join
("\n"))
78 # All extracted docunits
79 var docunits
= new Array[DocUnit]
83 toolcontext
.show_unit_status
(name
, docunits
)
86 fun mark_done
(du
: DocUnit)
92 # Execute all the docunits
95 if docunits
.is_empty
then
99 var simple_du
= new Array[DocUnit]
101 for du
in docunits
do
102 # Skip existing errors
103 if du
.error
!= null then
108 var ast
= toolcontext
.parse_something
(du
.block
)
109 if ast
isa AExpr then
112 test_single_docunit
(du
)
116 test_simple_docunits
(simple_du
)
121 for du
in docunits
do
122 toolcontext
.show_unit
(du
)
125 for du
in docunits
do
126 testsuite
.add du
.to_xml
130 # Executes multiples doc-units in a shared program.
131 # Used for docunits simple block of code (without modules, classes, functions etc.)
133 # In case of compilation error, the method fallbacks to `test_single_docunit` to
134 # * locate exactly the compilation problem in the problematic docunit.
135 # * permit the execution of the other docunits that may be correct.
136 fun test_simple_docunits
(dus
: Array[DocUnit])
138 if dus
.is_empty
then return
140 var file
= "{prefix}-0.nit"
142 var dir
= file
.dirname
143 if dir
!= "" then dir
.mkdir
145 f
= create_unitfile
(file
)
150 f
.write
("fun run_{i} do\n")
151 f
.write
("# {du.full_name}\n")
155 f
.write
("var a = args.first.to_i\n")
157 f
.write
("if a == {j} then run_{j}\n")
161 if toolcontext
.opt_noact
.value
then return
163 var res
= compile_unitfile
(file
)
167 # Fall-back to individual modes:
169 test_single_docunit
(du
)
177 toolcontext
.info
("Execute doc-unit {du.full_name} in {file} {i}", 1)
178 var res2
= toolcontext
.safe_exec
("{file.to_program_name}.bin {i} >'{file}.out1' 2>&1 </dev/null")
181 var content
= "{file}.out1".to_path
.read_all
182 du
.raw_output
= content
185 du
.error
= "Runtime error in {file} with argument {i}"
186 toolcontext
.modelbuilder
.failed_entities
+= 1
192 # Executes a single doc-unit in its own program.
193 # Used for docunits larger than a single block of code (with modules, classes, functions etc.)
194 fun test_single_docunit
(du
: DocUnit)
197 var file
= "{prefix}-{cpt}.nit"
199 toolcontext
.info
("Execute doc-unit {du.full_name} in {file}", 1)
202 f
= create_unitfile
(file
)
206 if toolcontext
.opt_noact
.value
then return
208 var res
= compile_unitfile
(file
)
211 res2
= toolcontext
.safe_exec
("{file.to_program_name}.bin >'{file}.out1' 2>&1 </dev/null")
215 var content
= "{file}.out1".to_path
.read_all
216 du
.raw_output
= content
219 du
.error
= "Compilation error in {file}"
220 toolcontext
.modelbuilder
.failed_entities
+= 1
221 else if res2
!= 0 then
222 du
.error
= "Runtime error in {file}"
223 toolcontext
.modelbuilder
.failed_entities
+= 1
228 # Create and fill the header of a unit file `file`.
230 # A unit file is a Nit source file generated from one
231 # or more docunits that will be compiled and executed.
233 # The handled on the file is returned and must be completed and closed.
235 # `file` should be a valid filepath for a Nit source file.
236 private fun create_unitfile
(file
: String): Writer
238 var dir
= file
.dirname
239 if dir
!= "" then dir
.mkdir
241 f
= new FileWriter.open
(file
)
242 f
.write
("# GENERATED FILE\n")
243 f
.write
("# Docunits extracted from comments\n")
244 if mmodule
!= null then
245 f
.write
("import {mmodule.name}\n")
251 # Compile an unit file and return the compiler return code
253 # Can terminate the program if the compiler is not found
254 private fun compile_unitfile
(file
: String): Int
256 var nitc
= toolcontext
.find_nitc
257 var opts
= new Array[String]
258 if mmodule
!= null then
259 opts
.add
"-I {mmodule.filepath.dirname}"
261 var cmd
= "{nitc} --ignore-visibility --no-color '{file}' {opts.join(" ")} >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
262 var res
= toolcontext
.safe_exec
(cmd
)
267 private class NitunitDecorator
270 var executor
: NitUnitExecutor
272 redef fun add_code
(v
, block
) do
273 var code
= block
.raw_content
274 var meta
= block
.meta
or else "nit"
275 # Do not try to test non-nit code.
276 if meta
!= "nit" then return
277 # Try to parse code blocks
278 var ast
= executor
.toolcontext
.parse_something
(code
)
280 var mdoc
= executor
.mdoc
284 if ast
isa TComment then return
286 # The location is computed according to the starts of the mdoc and the block
287 # Note, the following assumes that all the comments of the mdoc are correctly aligned.
288 var loc
= block
.block
.location
289 var line_offset
= loc
.line_start
+ mdoc
.location
.line_start
- 2
290 var column_offset
= loc
.column_start
+ mdoc
.location
.column_start
291 # Hack to handle precise location in blocks
292 # TODO remove when markdown is more reliable
293 if block
isa BlockFence then
294 # Skip the starting fence
297 # Account a standard 4 space indentation
301 # We want executable code
302 if not (ast
isa AModule or ast
isa ABlockExpr or ast
isa AExpr) then
305 # Get real location of the node (or error)
306 var location
= new Location(mdoc
.location
.file
,
307 l
.line_start
+ line_offset
,
308 l
.line_end
+ line_offset
,
309 l
.column_start
+ column_offset
,
310 l
.column_end
+ column_offset
)
311 if ast
isa AError then
312 message
= ast
.message
314 message
= "Error: Invalid Nit code."
319 du
.error_location
= location
321 executor
.toolcontext
.modelbuilder
.failed_entities
+= 1
325 # Create a first block
326 # Or create a new block for modules that are more than a main part
327 var last_docunit
= executor
.last_docunit
328 if last_docunit
== null or ast
isa AModule then
329 last_docunit
= new_docunit
330 executor
.last_docunit
= last_docunit
334 last_docunit
.block
+= code
336 # In order to retrieve precise positions,
337 # the real position of each line of the raw_content is stored.
338 # See `DocUnit::real_location`
339 line_offset
-= loc
.line_start
- 1
340 for i
in [loc
.line_start
..loc
.line_end
] do
341 last_docunit
.lines
.add i
+ line_offset
342 last_docunit
.columns
.add column_offset
346 # Return and register a new empty docunit
347 fun new_docunit
: DocUnit
349 var mdoc
= executor
.mdoc
353 var name
= executor
.xml_name
354 if executor
.docunits
.not_empty
and executor
.docunits
.last
.mdoc
== mdoc
then
355 next_number
= executor
.docunits
.last
.number
+ 1
356 name
+= "#" + next_number
.to_s
359 var res
= new DocUnit(mdoc
, next_number
, "", executor
.xml_classname
, name
)
360 executor
.docunits
.add res
361 executor
.toolcontext
.modelbuilder
.unit_entities
+= 1
366 # A unit-test extracted from some documentation.
368 # A docunit is extracted from the code-blocks of mdocs.
369 # Each mdoc can contains more than one docunit, and a single docunit can be made of more that a single code-block.
373 # The doc that contains self
376 # The numbering of self in mdoc (starting with 0)
379 redef fun full_name
do
380 var mentity
= mdoc
.original_mentity
381 if mentity
!= null then
382 var res
= mentity
.full_name
388 return xml_classname
+ "." + xml_name
392 # The text of the code to execute.
394 # This is the verbatim content on one, or more, code-blocks from `mdoc`
397 # For each line in `block`, the associated line in the mdoc
399 # Is used to give precise locations
400 var lines
= new Array[Int]
402 # For each line in `block`, the associated column in the mdoc
404 # Is used to give precise locations
405 var columns
= new Array[Int]
407 # The location of the whole docunit.
409 # If `self` is made of multiple code-blocks, then the location
410 # starts at the first code-books and finish at the last one, thus includes anything between.
411 redef var location
is lazy
do
412 return new Location(mdoc
.location
.file
, lines
.first
, lines
.last
+1, columns
.first
+1, 0)
415 # Compute the real location of a node on the `ast` based on `mdoc.location`
417 # The result is basically: ast_location + markdown location of the piece + mdoc.location
419 # The fun is that a single docunit can be made of various pieces of code blocks.
420 fun real_location
(ast_location
: Location): Location
423 var res
= new Location(mdoc
.location
.file
, lines
[ast_location
.line_start-1
],
424 lines
[ast_location
.line_end-1
],
425 columns
[ast_location
.line_start-1
] + ast_location
.column_start
,
426 columns
[ast_location
.line_end-1
] + ast_location
.column_end
)
433 res
.open
("system-out").append
(block
)
437 redef var xml_classname
441 redef class ModelBuilder
442 # Total number analyzed `MEntity`
443 var total_entities
= 0
445 # The number of `MEntity` that have some documentation
448 # The total number of executed docunits
449 var unit_entities
= 0
451 # The number failed docunits
452 var failed_entities
= 0
454 # Extracts and executes all the docunits in the `mmodule`
455 # Returns a JUnit-compatible `<testsuite>` XML element that contains the results of the executions.
456 fun test_markdown
(mmodule
: MModule): HTMLTag
458 var ts
= new HTMLTag("testsuite")
459 toolcontext
.info
("nitunit: doc-unit {mmodule}", 2)
461 var nmodule
= mmodule2node
(mmodule
)
462 if nmodule
== null then return ts
464 # usualy, only the original module must be imported in the unit test.
467 if g
!= null and g
.mpackage
.name
== "core" then
468 # except for a unit test in a module of `core`
469 # in this case, the whole `core` must be imported
470 o
= get_mmodule_by_name
(nmodule
, g
, g
.mpackage
.name
).as(not null)
473 ts
.attr
("package", mmodule
.full_name
)
475 var prefix
= toolcontext
.test_dir
476 prefix
= prefix
.join_path
(mmodule
.to_s
)
477 var d2m
= new NitUnitExecutor(toolcontext
, prefix
, o
, ts
, "Docunits of module {mmodule.full_name}")
481 var nmoduledecl
= nmodule
.n_moduledecl
482 if nmoduledecl
== null then break label x
483 var ndoc
= nmoduledecl
.n_doc
484 if ndoc
== null then break label x
486 # NOTE: jenkins expects a '.' in the classname attr
487 d2m
.extract
(ndoc
.to_mdoc
, "nitunit." + mmodule
.full_name
+ ".<module>", "<module>")
489 for nclassdef
in nmodule
.n_classdefs
do
490 var mclassdef
= nclassdef
.mclassdef
491 if mclassdef
== null then continue
492 if nclassdef
isa AStdClassdef then
494 var ndoc
= nclassdef
.n_doc
497 d2m
.extract
(ndoc
.to_mdoc
, "nitunit." + mmodule
.full_name
+ "." + mclassdef
.mclass
.full_name
, "<class>")
500 for npropdef
in nclassdef
.n_propdefs
do
501 var mpropdef
= npropdef
.mpropdef
502 if mpropdef
== null then continue
504 var ndoc
= npropdef
.n_doc
507 d2m
.extract
(ndoc
.to_mdoc
, "nitunit." + mmodule
.full_name
+ "." + mclassdef
.mclass
.full_name
, mpropdef
.mproperty
.full_name
)
517 # Extracts and executes all the docunits in the readme of the `mgroup`
518 # Returns a JUnit-compatible `<testsuite>` XML element that contains the results of the executions.
519 fun test_group
(mgroup
: MGroup): HTMLTag
521 var ts
= new HTMLTag("testsuite")
522 toolcontext
.info
("nitunit: doc-unit group {mgroup}", 2)
524 # usually, only the default module must be imported in the unit test.
525 var o
= mgroup
.default_mmodule
527 ts
.attr
("package", mgroup
.full_name
)
529 var prefix
= toolcontext
.test_dir
530 prefix
= prefix
.join_path
(mgroup
.to_s
)
531 var d2m
= new NitUnitExecutor(toolcontext
, prefix
, o
, ts
, "Docunits of group {mgroup.full_name}")
534 var mdoc
= mgroup
.mdoc
535 if mdoc
== null then return ts
538 # NOTE: jenkins expects a '.' in the classname attr
539 d2m
.extract
(mdoc
, "nitunit." + mgroup
.full_name
, "<group>")
546 # Test a document object unrelated to a Nit entity
547 fun test_mdoc
(mdoc
: MDoc): HTMLTag
549 var ts
= new HTMLTag("testsuite")
550 var file
= mdoc
.location
.to_s
552 toolcontext
.info
("nitunit: doc-unit file {file}", 2)
554 ts
.attr
("package", file
)
556 var prefix
= toolcontext
.test_dir
/ "file"
557 var d2m
= new NitUnitExecutor(toolcontext
, prefix
, null, ts
, "Docunits of file {file}")
562 # NOTE: jenkins expects a '.' in the classname attr
563 d2m
.extract
(mdoc
, "nitunit.<file>", file
)