src: add some asserts on `super` related things
[nit.git] / src / nitunit.nit
index 688f8cf..627e223 100644 (file)
@@ -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,11 +33,11 @@ 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
 
@@ -50,12 +50,25 @@ 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
@@ -67,6 +80,9 @@ class NitUnitExecutor
                blocks.last.add(text)
        end
 
+       # The associated node to localize warnings
+       var ndoc: nullable ADoc
+
        # used to generate distinct names
        var cpt = 0
 
@@ -76,7 +92,10 @@ class NitUnitExecutor
        do
                blocks.clear
 
+               self.ndoc = ndoc
+
                work(ndoc.to_mdoc)
+               toolcontext.check_errors
 
                if blocks.is_empty then return
 
@@ -91,7 +110,7 @@ class NitUnitExecutor
                cpt += 1
                var file = "{prefix}{cpt}.nit"
 
-               toolcontext.info("Execute {tc.attrs["classname"]}.{tc.attrs["name"]} in {file}", 1)
+               toolcontext.info("Execute {tc.attrs["name"]} in {file}", 1)
 
                var dir = file.dirname
                if dir != "" then dir.mkdir
@@ -99,8 +118,7 @@ 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)
@@ -110,12 +128,12 @@ class NitUnitExecutor
                if toolcontext.opt_noact.value then return
 
                var nit_dir = toolcontext.nit_dir
-               var nitg = "{nit_dir}/bin/nitg"
+               var nitg = "{nit_dir or else ""}/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 </dev/null -o '{file}.bin'"
+               var cmd = "{nitg} --ignore-visibility --no-color '{file}' -I {mmodule.location.file.filename.dirname} >'{file}.out1' 2>&1 </dev/null -o '{file}.bin'"
                var res = sys.system(cmd)
                var res2 = 0
                if res == 0 then
@@ -185,12 +203,12 @@ redef class ModelBuilder
                var nmodule = mmodule2nmodule[mmodule]
                assert nmodule != null
 
-               # what module to import in the unit test.
-               # try to detect the main module of the project
-               # TODO do things correctly once the importation of arbitraty nested module is legal
+               # usualy, only the original module must be imported in the unit test.
                var o = mmodule
                var g = o.mgroup
-               if g != null then
+               if g != null and g.mproject.name == "standard" then
+                       # except for a unit test in a module of standard
+                       # in this case, the whole standard must be imported
                        o = get_mmodule_by_name(nmodule, g, g.mproject.name).as(not null)
                end
 
@@ -199,7 +217,7 @@ redef class ModelBuilder
                var prefix = toolcontext.opt_dir.value
                if prefix == null then prefix = ".nitunit"
                prefix = prefix.join_path(mmodule.to_s)
-               var d2m = new NitUnitExecutor(toolcontext, prefix, o.name, ts)
+               var d2m = new NitUnitExecutor(toolcontext, prefix, o, ts)
 
                var tc