nitunit: use `before_all`, `after_all` annotations for propdefs in Sys
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 15 Aug 2017 17:00:59 +0000 (13:00 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 15 Aug 2017 18:12:16 +0000 (14:12 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/testing/testing_suite.nit

index 1948b3a..0271224 100644 (file)
@@ -39,10 +39,9 @@ class NitUnitTester
                var toolcontext = mbuilder.toolcontext
                var suite = new TestSuite(mmodule, toolcontext)
                # method to execute before all tests in the module
-               var before_module = mmodule.before_test
-               if before_module != null then
+               for mmethod in mmodule.before_all do
                        toolcontext.modelbuilder.total_tests += 1
-                       suite.before_module = new TestCase(suite, before_module, toolcontext)
+                       suite.before_all.add new TestCase(suite, mmethod, toolcontext)
                end
                # generate all test cases
                for mclassdef in mmodule.mclassdefs do
@@ -58,10 +57,9 @@ class NitUnitTester
                        end
                end
                # method to execute after all tests in the module
-               var after_module = mmodule.after_test
-               if after_module != null then
+               for mmethod in mmodule.after_all do
                        toolcontext.modelbuilder.total_tests += 1
-                       suite.after_module = new TestCase(suite, after_module, toolcontext)
+                       suite.after_all.add new TestCase(suite, mmethod, toolcontext)
                end
                suite.run
                return suite
@@ -114,17 +112,17 @@ class TestSuite
        # List of `TestCase` to be executed in this suite.
        var test_cases = new Array[TestCase]
 
-       # Test to be executed before the whole test suite.
-       var before_module: nullable TestCase = null
+       # Tests to be executed before the whole test suite.
+       var before_all = new Array[TestCase]
 
-       # Test to be executed after the whole test suite.
-       var after_module: nullable TestCase = null
+       # Tests to be executed after the whole test suite.
+       var after_all = new Array[TestCase]
 
        # Display test suite status in std-out.
        fun show_status do
                var test_cases = self.test_cases.to_a
-               if before_module != null then test_cases.add before_module.as(not null)
-               if after_module != null then test_cases.add after_module.as(not null)
+               test_cases.add_all before_all
+               test_cases.add_all after_all
                toolcontext.show_unit_status("Test-suite of module " + mmodule.full_name, test_cases)
        end
 
@@ -150,10 +148,7 @@ class TestSuite
                end
                toolcontext.info("Execute test-suite {mmodule.name}", 1)
 
-               var before_module = self.before_module
-               var after_module = self.after_module
-
-               if before_module != null then
+               for before_module in before_all do
                        before_module.run
                        toolcontext.clear_progress_bar
                        toolcontext.show_unit(before_module)
@@ -163,7 +158,7 @@ class TestSuite
                                        toolcontext.clear_progress_bar
                                        toolcontext.show_unit(case)
                                end
-                               if after_module != null then
+                               for after_module in after_all do
                                        after_module.fail "Nitunit Error: before_module test failed"
                                        toolcontext.clear_progress_bar
                                        toolcontext.show_unit(after_module)
@@ -181,7 +176,7 @@ class TestSuite
                        show_status
                end
 
-               if not after_module == null then
+               for after_module in after_all do
                        after_module.run
                        toolcontext.clear_progress_bar
                        toolcontext.show_unit(after_module)
@@ -198,15 +193,13 @@ class TestSuite
                file.addn "intrude import core"
                file.addn "import {mmodule.name}\n"
                file.addn "var name = args.first"
-               var before_module = self.before_module
-               if before_module != null then
+               for before_module in before_all do
                        before_module.write_to_nit(file)
                end
                for case in test_cases do
                        case.write_to_nit(file)
                end
-               var after_module = self.after_module
-               if after_module != null then
+               for after_module in after_all do
                        after_module.write_to_nit(file)
                end
                file.write_to_file("{test_file}.nit")
@@ -372,35 +365,55 @@ class TestCase
        end
 end
 
-redef class MMethodDef
-       # Is the method a "before_module"?
-       private fun is_before_module: Bool do return name == "before_module"
+redef class MClassDef
+       # Methods tagged with `before_all` in this class definition
+       private fun before_all: Array[MMethodDef] do
+               var res = new Array[MMethodDef]
+               for mpropdef in mpropdefs do
+                       if mpropdef isa MMethodDef and mpropdef.is_before_all then
+                               res.add mpropdef
+                       end
+               end
+               var in_hierarchy = self.in_hierarchy
+               if in_hierarchy == null then return res
+               for mclassdef in in_hierarchy.direct_greaters do
+                       res.add_all mclassdef.before_all
+               end
+               return res
+       end
 
-       # Is the method a "after_module"?
-       private fun is_after_module: Bool do return name == "after_module"
+       # Methods tagged with `after_all` in this class definition
+       private fun after_all: Array[MMethodDef] do
+               var res = new Array[MMethodDef]
+               for mpropdef in mpropdefs do
+                       if mpropdef isa MMethodDef and mpropdef.is_after_all then
+                               res.add mpropdef
+                       end
+               end
+               var in_hierarchy = self.in_hierarchy
+               if in_hierarchy == null then return res
+               for mclassdef in in_hierarchy.direct_greaters do
+                       res.add_all mclassdef.after_all
+               end
+               return res
+       end
 end
 
 redef class MModule
-       # "before_module" method for this module.
-       private fun before_test: nullable MMethodDef do
+       # Methods tagged with `before_all` at the module level (in `Sys`)
+       private fun before_all: Array[MMethodDef] do
                for mclassdef in mclassdefs do
-                       if not mclassdef.name == "Sys" then continue
-                       for mpropdef in mclassdef.mpropdefs do
-                               if mpropdef isa MMethodDef and mpropdef.is_before_module then return mpropdef
-                       end
+                       if mclassdef.name == "Sys" then return mclassdef.before_all
                end
-               return null
+               return new Array[MMethodDef]
        end
 
-       # "after_module" method for this module.
-       private fun after_test: nullable MMethodDef do
+       # Methods tagged with `after_all` at the module level (in `Sys`)
+       private fun after_all: Array[MMethodDef] do
                for mclassdef in mclassdefs do
-                       if not mclassdef.name == "Sys" then continue
-                       for mpropdef in mclassdef.mpropdefs do
-                               if mpropdef isa MMethodDef and mpropdef.is_after_module then return mpropdef
-                       end
+                       if mclassdef.name == "Sys" then return mclassdef.after_all
                end
-               return null
+               return new Array[MMethodDef]
        end
 end