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