Merge: doc: fixed some typos and other misc. corrections
[nit.git] / src / testing / testing_doc.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 from code comments.
16 module testing_doc
17
18 private import parser_util
19 import testing_base
20 import markdown2
21 import html
22 import realtime
23
24 # Extractor, Executor and Reporter for the tests in a module
25 class NitUnitExecutor
26
27 # Toolcontext used to parse Nit code blocks.
28 var toolcontext: ToolContext
29
30 # The prefix of the generated Nit source-file
31 var prefix: String
32
33 # The module to import, if any
34 var mmodule: nullable MModule
35
36 # The XML node associated to the module
37 var testsuite: HTMLTag
38
39 # The name of the suite
40 var name: String
41
42 # Markdown parse used to parse markdown comments and extract code
43 private var md_parser = new MdParser
44
45 # Markdown visitor used to extract markdown code blocks
46 private var md_visitor = new NitunitMdVisitor(self) is lazy
47
48 # The associated documentation object
49 var mdoc: nullable MDoc = null
50
51 # used to generate distinct names
52 var cpt = 0
53
54 # The last docunit extracted from a mdoc.
55 #
56 # Is used because a new code-block might just be added to it.
57 var last_docunit: nullable DocUnit = null
58
59 # Unit class name in XML output
60 var xml_classname: String is noautoinit
61
62 # Unit name in xml output
63 var xml_name: String is noautoinit
64
65 # The entry point for a new `ndoc` node
66 # Fill `docunits` with new discovered unit of tests.
67 fun extract(mdoc: MDoc, xml_classname, xml_name: String)
68 do
69 last_docunit = null
70 self.xml_classname = xml_classname
71 self.xml_name = xml_name
72
73 self.mdoc = mdoc
74
75 # Populate `blocks` from the markdown decorator
76 var md_node = md_parser.parse(mdoc.content.join("\n"))
77 md_visitor.enter_visit(md_node)
78 end
79
80 # All extracted docunits
81 var docunits = new Array[DocUnit]
82
83 # Display current testing status
84 fun show_status do toolcontext.show_unit_status(name, docunits)
85
86 # Update display when a test case is done
87 fun mark_done(du: DocUnit)
88 do
89 du.is_done = true
90 toolcontext.clear_progress_bar
91 toolcontext.show_unit(du)
92 show_status
93 end
94
95 # Execute all the docunits
96 fun run_tests
97 do
98 if docunits.is_empty then
99 return
100 end
101
102 # Try to group each nitunit into a single source file to fasten the compilation
103 var simple_du = new Array[DocUnit] # du that are simple statements
104 var single_du = new Array[DocUnit] # du that are modules or include classes
105 show_status
106 for du in docunits do
107 # Skip existing errors
108 if du.error != null then
109 continue
110 end
111
112 var ast = toolcontext.parse_something(du.block)
113 if ast isa AExpr then
114 simple_du.add du
115 else
116 single_du.add du
117 end
118 end
119
120 # Try to mass compile all the simple du as a single nit module
121 compile_simple_docunits(simple_du)
122 # Try to mass compile all the single du in a single nitc invocation with many modules
123 compile_single_docunits(single_du)
124 # If the mass compilation fail, then each one will be compiled individually
125
126 # Now test them in order
127 for du in docunits do
128 if du.error != null then
129 # Nothing to execute. Conclude
130 else if du.is_compiled then
131 # Already compiled. Execute it.
132 execute_simple_docunit(du)
133 else
134 # A mass compilation failed
135 # Need to try to recompile it, then execute it
136 test_single_docunit(du)
137 end
138 mark_done(du)
139 end
140
141 # Final status
142 show_status
143 print ""
144
145 for du in docunits do
146 testsuite.add du.to_xml
147 end
148 end
149
150 # Compiles multiples doc-units in a shared program.
151 # Used for docunits simple block of code (without modules, classes, functions etc.)
152 #
153 # In case of success, the docunits are compiled and the caller can call `execute_simple_docunit`.
154 #
155 # In case of compilation error, the docunits are let uncompiled.
156 # The caller should fallbacks to `test_single_docunit` to
157 # * locate exactly the compilation problem in the problematic docunit.
158 # * permit the execution of the other docunits that may be correct.
159 fun compile_simple_docunits(dus: Array[DocUnit])
160 do
161 if dus.is_empty then return
162
163 var file = "{prefix}-0.nit"
164
165 toolcontext.info("Compile {dus.length} simple(s) doc-unit(s) in {file}", 1)
166
167 var dir = file.dirname
168 if dir != "" then dir.mkdir
169 var f
170 f = create_unitfile(file)
171 var i = 0
172 for du in dus do
173 i += 1
174 f.write("fun run_{i} do\n")
175 f.write("# {du.full_name}\n")
176 f.write(du.block)
177 f.write("end\n")
178 end
179 f.write("var a = args.first.to_i\n")
180 for j in [1..i] do
181 f.write("if a == {j} then run_{j}\n")
182 end
183 f.close
184
185 if toolcontext.opt_noact.value then return
186
187 var res = compile_unitfile(file)
188
189 if res != 0 then
190 # Compilation error.
191 # They should be generated and compiled independently
192 return
193 end
194
195 # Compilation was a success.
196 # Store what need to be executed for each one.
197 i = 0
198 for du in dus do
199 i += 1
200 du.test_file = file
201 du.test_arg = i
202 du.is_compiled = true
203 end
204 end
205
206 # Execute a docunit compiled by `test_single_docunit`
207 fun execute_simple_docunit(du: DocUnit)
208 do
209 var file = du.test_file.as(not null)
210 var i = du.test_arg or else 0
211 toolcontext.info("Execute doc-unit {du.full_name} in {file} {i}", 1)
212 var clock = new Clock
213 var res2 = toolcontext.safe_exec("{file.to_program_name}.bin {i} >'{file}.out1' 2>&1 </dev/null")
214 if not toolcontext.opt_no_time.value then du.real_time = clock.total
215 du.was_exec = true
216
217 var content = "{file}.out1".to_path.read_all
218 du.raw_output = content
219
220 if res2 != 0 then
221 du.error = "Runtime error in {file} with argument {i}"
222 toolcontext.modelbuilder.failed_entities += 1
223 end
224 end
225
226 # Produce a single unit file for the docunit `du`.
227 fun generate_single_docunit(du: DocUnit): String
228 do
229 cpt += 1
230 var file = "{prefix}-{cpt}.nit"
231
232 var f
233 f = create_unitfile(file)
234 f.write(du.block)
235 f.close
236
237 du.test_file = file
238 return file
239 end
240
241 # Executes a single doc-unit in its own program.
242 # Used for docunits larger than a single block of code (with modules, classes, functions etc.)
243 fun test_single_docunit(du: DocUnit)
244 do
245 var file = generate_single_docunit(du)
246
247 toolcontext.info("Compile doc-unit {du.full_name} in {file}", 1)
248
249 if toolcontext.opt_noact.value then return
250
251 var res = compile_unitfile(file)
252 var content = "{file}.out1".to_path.read_all
253 du.raw_output = content
254
255 du.test_file = file
256
257 if res != 0 then
258 du.error = "Compilation error in {file}"
259 toolcontext.modelbuilder.failed_entities += 1
260 return
261 end
262
263 du.is_compiled = true
264 execute_simple_docunit(du)
265 end
266
267 # Create and fill the header of a unit file `file`.
268 #
269 # A unit file is a Nit source file generated from one
270 # or more docunits that will be compiled and executed.
271 #
272 # The handled on the file is returned and must be completed and closed.
273 #
274 # `file` should be a valid filepath for a Nit source file.
275 private fun create_unitfile(file: String): Writer
276 do
277 var mmodule = self.mmodule
278 var dir = file.dirname
279 if dir != "" then dir.mkdir
280 var f
281 f = new FileWriter.open(file)
282 f.write("# GENERATED FILE\n")
283 f.write("# Docunits extracted from comments\n")
284 if mmodule != null then
285 f.write("intrude import {mmodule.name}\n")
286 end
287 f.write("\n")
288 return f
289 end
290
291 # Compile a unit file and return the compiler return code
292 #
293 # Can terminate the program if the compiler is not found
294 private fun compile_unitfile(file: String): Int
295 do
296 var mmodule = self.mmodule
297 var nitc = toolcontext.find_nitc
298 var opts = new Array[String]
299 if mmodule != null then
300 # FIXME playing this way with the include dir is not safe nor robust
301 opts.add "-I {mmodule.filepath.as(not null).dirname}"
302 end
303 var cmd = "{nitc} --ignore-visibility --no-color -q '{file}' {opts.join(" ")} >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
304 var res = toolcontext.safe_exec(cmd)
305 return res
306 end
307
308 # Compile a unit file and return the compiler return code
309 #
310 # Can terminate the program if the compiler is not found
311 private fun compile_single_docunits(dus: Array[DocUnit]): Int
312 do
313 # Generate all unitfiles
314 var files = new Array[String]
315 for du in dus do
316 files.add generate_single_docunit(du)
317 end
318
319 if files.is_empty then return 0
320
321 toolcontext.info("Compile {dus.length} single(s) doc-unit(s) at once", 1)
322
323 # Mass compile them
324 var nitc = toolcontext.find_nitc
325 var opts = new Array[String]
326 if mmodule != null then
327 # FIXME playing this way with the include dir is not safe nor robust
328 opts.add "-I {mmodule.filepath.dirname}"
329 end
330 var cmd = "{nitc} --ignore-visibility --no-color -q '{files.join("' '")}' {opts.join(" ")} > '{prefix}.out1' 2>&1 </dev/null --dir {prefix.dirname}"
331 var res = toolcontext.safe_exec(cmd)
332 if res != 0 then
333 # Mass compilation failure
334 return res
335 end
336
337 # Rename each file into it expected binary name
338 for du in dus do
339 var f = du.test_file.as(not null)
340 toolcontext.safe_exec("mv '{f.strip_extension(".nit")}' '{f}.bin'")
341 du.is_compiled = true
342 end
343
344 return res
345 end
346 end
347
348 private class NitunitMdVisitor
349 super MdVisitor
350
351 var executor: NitUnitExecutor
352
353 redef fun visit(node) do node.accept_nitunit(self)
354
355 fun parse_code(block: MdCodeBlock) do
356 var code = block.literal
357 if code == null then return
358
359 var meta = block.info or else "nit"
360 # Do not try to test non-nit code.
361 if meta != "nit" then return
362
363 # Try to parse code blocks
364 var executor = self.executor
365 var ast = executor.toolcontext.parse_something(code)
366
367 var mdoc = executor.mdoc
368 assert mdoc != null
369
370 # Skip pure comments
371 if ast isa TComment then return
372
373 # The location is computed according to the starts of the mdoc and the block
374 # Note, the following assumes that all the comments of the mdoc are correctly aligned.
375 var loc = block.location
376 var line_offset = loc.line_start + mdoc.location.line_start - 2
377 var column_offset = loc.column_start + mdoc.location.column_start
378 # Hack to handle precise location in blocks
379 # TODO remove when markdown is more reliable
380 if block isa MdFencedCodeBlock then
381 # Skip the starting fence
382 line_offset += 1
383 else
384 # Account a standard 4 space indentation
385 column_offset += 4
386 end
387
388 # We want executable code
389 if not (ast isa AModule or ast isa ABlockExpr or ast isa AExpr) then
390 var message
391 var l = ast.location
392 # Get real location of the node (or error)
393 var location = new Location(mdoc.location.file,
394 l.line_start + line_offset,
395 l.line_end + line_offset,
396 l.column_start + column_offset,
397 l.column_end + column_offset)
398 if ast isa AError then
399 message = ast.message
400 else
401 message = "Error: Invalid Nit code."
402 end
403
404 var du = new_docunit
405 du.block += code
406 du.error_location = location
407 du.error = message
408 executor.toolcontext.modelbuilder.failed_entities += 1
409 return
410 end
411
412 # Create a first block
413 # Or create a new block for modules that are more than a main part
414 var last_docunit = executor.last_docunit
415 if last_docunit == null or ast isa AModule then
416 last_docunit = new_docunit
417 executor.last_docunit = last_docunit
418 end
419
420 # Add it to the file
421 last_docunit.block += code
422
423 # In order to retrieve precise positions,
424 # the real position of each line of the raw_content is stored.
425 # See `DocUnit::real_location`
426 line_offset -= loc.line_start - 1
427 for i in [loc.line_start..loc.line_end] do
428 last_docunit.lines.add i + line_offset
429 last_docunit.columns.add column_offset
430 end
431 end
432
433 # Return and register a new empty docunit
434 fun new_docunit: DocUnit do
435 var mdoc = executor.mdoc
436 assert mdoc != null
437
438 var next_number = 1
439 var name = executor.xml_name
440 if executor.docunits.not_empty and executor.docunits.last.mdoc == mdoc then
441 next_number = executor.docunits.last.number + 1
442 name += "#" + next_number.to_s
443 end
444
445 var res = new DocUnit(mdoc, next_number, "", executor.xml_classname, name)
446 executor.docunits.add res
447 executor.toolcontext.modelbuilder.unit_entities += 1
448 return res
449 end
450 end
451
452 redef class MdNode
453 private fun accept_nitunit(v: NitunitMdVisitor) do visit_all(v)
454 end
455
456 redef class MdCodeBlock
457 redef fun accept_nitunit(v) do v.parse_code(self)
458 end
459
460 # A unit-test extracted from some documentation.
461 #
462 # A docunit is extracted from the code-blocks of mdocs.
463 # Each mdoc can contains more than one docunit, and a single docunit can be made of more that a single code-block.
464 class DocUnit
465 super UnitTest
466
467 # The doc that contains self
468 var mdoc: MDoc
469
470 # The numbering of self in mdoc (starting with 0)
471 var number: Int
472
473 # The generated Nit source file that contains the unit-test
474 #
475 # Note that a same generated file can be used for multiple tests.
476 # See `test_arg` that is used to distinguish them
477 var test_file: nullable String = null
478
479 # Was `test_file` successfully compiled?
480 var is_compiled = false
481
482 # The command-line argument to use when executing the test, if any.
483 var test_arg: nullable Int = null
484
485 redef fun full_name do
486 var mentity = mdoc.original_mentity
487 if mentity != null then
488 var res = mentity.full_name
489 if number > 1 then
490 res += "#{number}"
491 end
492 return res
493 else
494 return xml_classname + "." + xml_name
495 end
496 end
497
498 # The text of the code to execute.
499 #
500 # This is the verbatim content on one, or more, code-blocks from `mdoc`
501 var block: String
502
503 # For each line in `block`, the associated line in the mdoc
504 #
505 # Is used to give precise locations
506 var lines = new Array[Int]
507
508 # For each line in `block`, the associated column in the mdoc
509 #
510 # Is used to give precise locations
511 var columns = new Array[Int]
512
513 # The location of the whole docunit.
514 #
515 # If `self` is made of multiple code-blocks, then the location
516 # starts at the first code-books and finish at the last one, thus includes anything between.
517 redef var location is lazy do
518 return new Location(mdoc.location.file, lines.first, lines.last+1, columns.first+1, 0)
519 end
520
521 # Compute the real location of a node on the `ast` based on `mdoc.location`
522 #
523 # The result is basically: ast_location + markdown location of the piece + mdoc.location
524 #
525 # The fun is that a single docunit can be made of various pieces of code blocks.
526 fun real_location(ast_location: Location): Location
527 do
528 var mdoc = self.mdoc
529
530 var res = new Location(mdoc.location.file,
531 lines[ast_location.line_start-1],
532 lines[ast_location.line_end-1],
533 columns[ast_location.line_start-1] + ast_location.column_start,
534 columns[ast_location.line_end-1] + ast_location.column_end)
535
536 return res
537 end
538
539 redef fun to_xml
540 do
541 var res = super
542 res.open("system-out").append(block)
543 return res
544 end
545
546 redef var xml_classname
547 redef var xml_name
548 end
549
550 redef class ModelBuilder
551 # Total number analyzed `MEntity`
552 var total_entities = 0
553
554 # The number of `MEntity` that have some documentation
555 var doc_entities = 0
556
557 # The total number of executed docunits
558 var unit_entities = 0
559
560 # The number failed docunits
561 var failed_entities = 0
562
563 # Extracts and executes all the docunits in the `mmodule`
564 # Returns a JUnit-compatible `<testsuite>` XML element that contains the results of the executions.
565 fun test_markdown(mmodule: MModule): HTMLTag
566 do
567 var ts = new HTMLTag("testsuite")
568 toolcontext.info("nitunit: doc-unit {mmodule}", 2)
569
570 var nmodule = mmodule2node(mmodule)
571 if nmodule == null then return ts
572
573 # usualy, only the original module must be imported in the unit test.
574 var o = mmodule
575 var g = o.mgroup
576 if g != null and g.mpackage.name == "core" then
577 # except for a unit test in a module of `core`
578 # in this case, the whole `core` must be imported
579 o = get_mmodule_by_name(nmodule, g, g.mpackage.name).as(not null)
580 end
581
582 ts.attr("package", mmodule.full_name)
583
584 var prefix = toolcontext.test_dir
585 prefix = prefix.join_path(mmodule.to_s)
586 var d2m = new NitUnitExecutor(toolcontext, prefix, o, ts, "Docunits of module {mmodule.full_name}")
587
588 do
589 total_entities += 1
590 var nmoduledecl = nmodule.n_moduledecl
591 if nmoduledecl == null then break label x
592 var ndoc = nmoduledecl.n_doc
593 if ndoc == null then break label x
594 doc_entities += 1
595 # NOTE: jenkins expects a '.' in the classname attr
596 d2m.extract(ndoc.to_mdoc, "nitunit." + mmodule.full_name + ".<module>", "<module>")
597 end label x
598 for nclassdef in nmodule.n_classdefs do
599 var mclassdef = nclassdef.mclassdef
600 if mclassdef == null then continue
601 if nclassdef isa AStdClassdef then
602 total_entities += 1
603 var ndoc = nclassdef.n_doc
604 if ndoc != null then
605 doc_entities += 1
606 d2m.extract(ndoc.to_mdoc, "nitunit." + mclassdef.full_name.replace("$", "."), "<class>")
607 end
608 end
609 for npropdef in nclassdef.n_propdefs do
610 var mpropdef = npropdef.mpropdef
611 if mpropdef == null then continue
612 total_entities += 1
613 var ndoc = npropdef.n_doc
614 if ndoc != null then
615 doc_entities += 1
616 var a = mpropdef.full_name.split("$")
617 d2m.extract(ndoc.to_mdoc, "nitunit." + a[0] + "." + a[1], a[2])
618 end
619 end
620 end
621
622 d2m.run_tests
623
624 return ts
625 end
626
627 # Extracts and executes all the docunits in the readme of the `mgroup`
628 # Returns a JUnit-compatible `<testsuite>` XML element that contains the results of the executions.
629 fun test_group(mgroup: MGroup): HTMLTag
630 do
631 var ts = new HTMLTag("testsuite")
632 toolcontext.info("nitunit: doc-unit group {mgroup}", 2)
633
634 # usually, only the default module must be imported in the unit test.
635 var o = mgroup.default_mmodule
636
637 ts.attr("package", mgroup.full_name)
638
639 var prefix = toolcontext.test_dir
640 prefix = prefix.join_path(mgroup.to_s)
641 var d2m = new NitUnitExecutor(toolcontext, prefix, o, ts, "Docunits of group {mgroup.full_name}")
642
643 total_entities += 1
644 var mdoc = mgroup.mdoc
645 if mdoc == null then return ts
646
647 doc_entities += 1
648 # NOTE: jenkins expects a '.' in the classname attr
649 d2m.extract(mdoc, "nitunit." + mgroup.mpackage.name + "." + mgroup.name + ".<group>", "<group>")
650
651 d2m.run_tests
652
653 return ts
654 end
655
656 # Test a document object unrelated to a Nit entity
657 fun test_mdoc(mdoc: MDoc): HTMLTag
658 do
659 var ts = new HTMLTag("testsuite")
660 var file = mdoc.location.file.as(not null).filename
661
662 toolcontext.info("nitunit: doc-unit file {file}", 2)
663
664 ts.attr("package", file)
665
666 var prefix = toolcontext.test_dir / "file"
667 var d2m = new NitUnitExecutor(toolcontext, prefix, null, ts, "Docunits of file {file}")
668
669 total_entities += 1
670 doc_entities += 1
671
672 # NOTE: jenkins expects a '.' in the classname attr
673 d2m.extract(mdoc, "nitunit.<file>", file)
674 d2m.run_tests
675
676 return ts
677 end
678 end