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