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