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