X-Git-Url: http://nitlanguage.org diff --git a/src/nitunit.nit b/src/nitunit.nit index 1015621..e2404ca 100644 --- a/src/nitunit.nit +++ b/src/nitunit.nit @@ -23,8 +23,8 @@ import parser_util class NitUnitExecutor super Doc2Mdwn - # The name of the module to import - var modname: String + # The module to import + var mmodule: MModule # The prefix of the generated Nit source-file var prefix: String @@ -33,16 +33,16 @@ class NitUnitExecutor var testsuite: HTMLTag # Initialize a new e - init(toolcontext: ToolContext, prefix: String, modname: String, testsuite: HTMLTag) + init(toolcontext: ToolContext, prefix: String, mmodule: MModule, testsuite: HTMLTag) do super(toolcontext) self.prefix = prefix - self.modname = modname + self.mmodule = mmodule self.testsuite = testsuite end # All blocks of code from a same `ADoc` - var block = new Array[String] + var blocks = new Array[Array[String]] redef fun process_code(n: HTMLTag, text: String) do @@ -50,17 +50,39 @@ class NitUnitExecutor var ast = toolcontext.parse_something(text) # We want executable code - if not (ast isa AModule or ast isa ABlockExpr or ast isa AExpr) then return + if not (ast isa AModule or ast isa ABlockExpr or ast isa AExpr) then + if ndoc != null and n.tag == "pre" and toolcontext.opt_warn.value > 1 then + toolcontext.warning(ndoc.location, "Warning: There is a block of code that is not valid Nit, thus not considered a nitunit") + if ast isa AError then toolcontext.warning(ast.location, ast.message) + ndoc = null # To avoid multiple warning in the same node + end + return + end # Search `assert` in the AST var v = new SearchAssertVisitor v.enter_visit(ast) - if not v.foundit then return + if not v.foundit then + if ndoc != null and n.tag == "pre" and toolcontext.opt_warn.value > 1 then + toolcontext.warning(ndoc.location, "Warning: There is a block of Nit code without `assert`, thus not considered a nitunit") + ndoc = null # To avoid multiple warning in the same node + end + return + end + + # Create a first block + # Or create a new block for modules that are more than a main part + if blocks.is_empty or ast isa AModule then + blocks.add(new Array[String]) + end # Add it to the file - block.add(text) + blocks.last.add(text) end + # The associated node to localize warnings + var ndoc: nullable ADoc + # used to generate distinct names var cpt = 0 @@ -68,18 +90,27 @@ class NitUnitExecutor # Fill the prepated `tc` (testcase) XTM node fun extract(ndoc: ADoc, tc: HTMLTag) do - block.clear + blocks.clear + + self.ndoc = ndoc work(ndoc.to_mdoc) + toolcontext.check_errors - if block.is_empty then return + if blocks.is_empty then return + for block in blocks do test_block(ndoc, tc, block) + end + + # Execute a block + fun test_block(ndoc: ADoc, tc: HTMLTag, block: Array[String]) + do toolcontext.modelbuilder.unit_entities += 1 cpt += 1 var file = "{prefix}{cpt}.nit" - toolcontext.info("Execute {tc.attrs["classname"]}.{tc.attrs["name"]} in {file}", 2) + toolcontext.info("Execute {tc.attrs["name"]} in {file}", 1) var dir = file.dirname if dir != "" then dir.mkdir @@ -87,21 +118,22 @@ class NitUnitExecutor f = new OFStream.open(file) f.write("# GENERATED FILE\n") f.write("# Example extracted from a documentation\n") - var modname = self.modname - f.write("import {modname}\n") + f.write("import {mmodule.name}\n") f.write("\n") for text in block do f.write(text) end f.close + if toolcontext.opt_noact.value then return + var nit_dir = toolcontext.nit_dir var nitg = "{nit_dir}/bin/nitg" if nit_dir == null or not nitg.file_exists then toolcontext.error(null, "Cannot find nitg. Set envvar NIT_DIR.") toolcontext.check_errors end - var cmd = "{nitg} --ignore-visibility --no-color '{file}' -I . >'{file}.out1' 2>&1