[vala/wip/valadate: 44/60] more work on testrunner



commit 9f8299499c9b76319400dc982f6e4acbf46c8616
Author: chebizarro gmail com <chebizarro gmail com>
Date:   Thu Jul 21 09:09:54 2016 -0700

    more work on testrunner

 valadate/test.vala         |   42 +++++++++++++
 valadate/testcase.vala     |   70 ++++++++++++++++++++--
 valadate/testconfig.vala   |   25 +++-----
 valadate/testexplorer.vala |  140 +++++++++++++++++++++++++++----------------
 valadate/testrunner.vala   |   57 ++++++++++--------
 valadate/testsuite.vala    |   83 +++++++++-----------------
 6 files changed, 264 insertions(+), 153 deletions(-)
---
diff --git a/valadate/test.vala b/valadate/test.vala
index 356a1e7..66201ae 100644
--- a/valadate/test.vala
+++ b/valadate/test.vala
@@ -33,8 +33,50 @@ public interface Valadate.Test : Object {
        public abstract void run (TestResult result);
 
        /**
+        * The name of the test
+        */
+       public abstract string name { get; set; }
+
+       /**
         * Returns the number of tests that will be run by this test
         */
        public abstract int count {get;}
 
+       public abstract Test get_test(int index);
+
+       public virtual TestIterator iterator() {
+               return new TestIterator(this);
+       }
+       
+       public class TestIterator {
+               
+               private Test test;
+               private Test current;
+               private int index = 0;
+               
+               public TestIterator(Test test) {
+                       this.test = test;
+               }
+               
+               public Test get() {
+                       current = this.test.get_test(index);
+                       index++;
+                       return current;
+               }
+
+               public bool next() {
+                       if (index >= this.test.count)
+                               return false;
+                       return true;
+               }
+
+               
+               public int size {
+                       get {
+                               return this.test.count;
+                       }
+               }
+               
+       }
+
 }
diff --git a/valadate/testcase.vala b/valadate/testcase.vala
index 1e2107c..8a4af80 100644
--- a/valadate/testcase.vala
+++ b/valadate/testcase.vala
@@ -31,6 +31,12 @@ public errordomain Valadate.TestError {
 public abstract class Valadate.TestCase : Object, Test, TestFixture {
 
        /**
+        * The TestMethod delegate represents a {@link Valadate.Test} method
+        * that can be added to a TestCase and run
+        */
+       public delegate void TestMethod ();
+
+       /**
         * the name of the TestCase
         */
        public string name { get; set; }
@@ -40,13 +46,28 @@ public abstract class Valadate.TestCase : Object, Test, TestFixture {
         * TestCase's name
         */
        public TestCase(string? name = null) {
-               this.name = name;
+               this.name = name ?? this.get_type().name();
        }
 
        /**
         * Returns the number of {@link Valadate.Test}s that will be run by this TestCase
         */
-       public int count {get;set;}
+       public int count {
+               get {
+                       int testcount = 0;
+                       _tests.foreach((t) => {
+                               testcount += t.count;
+                       });
+                       return testcount;
+               }
+       }
+
+       private List<Test> _tests = new List<Test>();
+
+       public void add_test(string testname, owned TestMethod test) {
+               var adaptor = new TestAdaptor (this.name + testname, (owned)test, this);
+               _tests.append(adaptor);
+       }
 
        
        public void run(TestResult result) {
@@ -55,12 +76,49 @@ public abstract class Valadate.TestCase : Object, Test, TestFixture {
                
        }
 
+       public Test get_test(int index) {
+
+               return _tests.nth_data(index);
+
+       }
+
 
        public virtual void set_up() {}
 
        public virtual void tear_down() {}
 
 
+       private class TestAdaptor : Object, Test {
+
+               private TestMethod test;
+               private unowned TestCase testcase;
+
+               public string name {get;set;}
+
+               public int count {
+                       get {
+                               return 1;
+                       }
+               }
+               
+               public Test get_test(int index) {
+                       return this;
+               }
+               
+               public TestAdaptor(string name, owned TestMethod test, TestCase testcase) {
+                       this.test = (owned)test;
+                       this.name = name;
+                       this.testcase = testcase;
+               }
+
+               public void run(TestResult test) {
+                       this.testcase.set_up();
+                       this.test();
+                       this.testcase.tear_down();
+               }
+
+       }
+
 
 
 
@@ -106,10 +164,10 @@ public abstract class Valadate.TestCase : Object, Test, TestFixture {
 
        
        construct {
-               name = this.get_type().name();
+               //name = this.get_type().name();
        }
 
-       public void add_testb (string name, owned TestSuite.TestMethod test)
+       public void add_testb (string name, owned TestMethod test)
                requires (name.contains("/") != true)
        {
                var adaptor = new Adaptor (name, (owned)test, this);
@@ -157,7 +215,7 @@ public abstract class Valadate.TestCase : Object, Test, TestFixture {
                public string name { get; private set; }
                public int async_timeout { get; set; }
 
-               private TestSuite.TestMethod test;
+               private TestMethod test;
                private TestCase test_case;
 
                public bool is_async = false;
@@ -165,7 +223,7 @@ public abstract class Valadate.TestCase : Object, Test, TestFixture {
                public AsyncFinish async_finish;
 
                public Adaptor (string name,
-                                               owned TestSuite.TestMethod test,
+                                               owned TestMethod test,
                                                TestCase test_case) {
                        this.name = name;
                        this.test = (owned)test;
diff --git a/valadate/testconfig.vala b/valadate/testconfig.vala
index e51b19d..db3b06c 100644
--- a/valadate/testconfig.vala
+++ b/valadate/testconfig.vala
@@ -22,11 +22,9 @@
 
 public class Valadate.TestConfig : Object {
 
-       public virtual signal void set_up() {}
-       public virtual signal void tear_up() {}
-
        public static string seed;
        public static string testplan;
+       public static string runtest;
        public static string format = "tap";
        public static bool fatal_warnings;
        public static bool list;
@@ -43,14 +41,15 @@ public class Valadate.TestConfig : Object {
        [CCode (array_length = false, array_null_terminated = true)]
        public static string[] testplans;
 
+
        public string binary {get;set;}
 
-       public TestResult result {get;set;}
+       public TestSuite root {get;set;}
 
        public OptionContext opt_context;
 
        private Vala.CodeContext context;
-       private Module module;
+       internal Module module;
 
 
        public const OptionEntry[] options = {
@@ -60,8 +59,9 @@ public class Valadate.TestConfig : Object {
                { "list", 'l', 0, OptionArg.NONE, ref list, "List test cases available in a test executable", 
null },
                { "skip", 's', 0, OptionArg.STRING_ARRAY, ref skip, "Skip all tests matching", "TESTPATH..." 
},
                { "quiet", 'q', 0, OptionArg.NONE, ref quiet, "Run tests quietly", null },
-               { "timed", 't', 0, OptionArg.NONE, ref timed, "Run timed tests", null },
+               { "timed", 0, 0, OptionArg.NONE, ref timed, "Run timed tests", null },
                { "testplan", 0, 0, OptionArg.STRING, ref testplan, "Run the specified TestPlan", "FILE" },
+               { "", 'r', 0, OptionArg.STRING, ref runtest, null, null },
                { "verbose", 0, 0, OptionArg.NONE, ref verbose, "Run tests verbosely", null },
                { "version", 0, 0, OptionArg.NONE, ref version, "Display version number", null },
                { "vala-version", 0, 0, OptionArg.NONE, ref vala_version, "Display Vala version number", null 
},
@@ -74,6 +74,7 @@ public class Valadate.TestConfig : Object {
                opt_context = new OptionContext ("- Valadate Testing Framework");
                opt_context.set_help_enabled (true);
                opt_context.add_main_entries (options, null);
+               root = new TestSuite("/");
                setup_context();
        }
 
@@ -102,6 +103,7 @@ public class Valadate.TestConfig : Object {
        public int parse(string[] args) {
                binary = args[0];
                GLib.Environment.set_prgname(binary);
+               root.name = binary;
 
                try {
                        opt_context.parse (ref args);
@@ -126,20 +128,12 @@ public class Valadate.TestConfig : Object {
                                GLib.Random.next_int(),
                                GLib.Random.next_int());
                
-               result = new TestResult();
-               
                try {
                        load();
                } catch (ConfigError e) {
                        stdout.printf ("%s\n", e.message);
                        return 1;
                }
-
-               // We are just listing the tests in the binary
-               if(paths == null && TestConfig.list) {
-                       result.report();
-                       return 0;
-               }
                
                return -1;
        }
@@ -178,9 +172,8 @@ public class Valadate.TestConfig : Object {
                if (context.report.get_errors () > 0)
                        throw new ConfigError.TESTPLAN("Error parsing testplan %s", path);
        
-               var testexplorer = new TestExplorer(module, result);
+               var testexplorer = new TestExplorer(this);
                context.accept(testexplorer);
-               //_tests = testexplorer.get_tests();
        }
 
 }
diff --git a/valadate/testexplorer.vala b/valadate/testexplorer.vala
index d51885f..9718dfc 100644
--- a/valadate/testexplorer.vala
+++ b/valadate/testexplorer.vala
@@ -21,26 +21,20 @@
  */
  
 internal class Valadate.TestExplorer : Vala.CodeVisitor {
-               
+
+       private TestConfig config;
        private Vala.Class testcase;
        private Vala.Class testsuite;
 
+       private TestSuite current;
 
-       private Test[] tests;
-       private TestCase current_test;
-       private weak Module module;
-       private weak TestResult result;
-
-       internal delegate void* Constructor(); 
+       internal delegate void* Constructor(string? name = null); 
        internal delegate void TestMethod(TestCase self);
        
-       public TestExplorer(Module module, TestResult result) {
-               this.module = module;
-               this.result = result;
-       }
-       
-       public Test[] get_tests() {
-               return tests;
+       public TestExplorer(TestConfig config) {
+
+               this.config = config;
+               this.current = config.root;
        }
        
        public override void visit_class(Vala.Class class) {
@@ -55,51 +49,93 @@ internal class Valadate.TestExplorer : Vala.CodeVisitor {
                        return;
                }
 
-               if (testcase != null &&
-                       class.is_subtype_of(testcase) &&
-                       class.is_abstract != true ) {
-
-                       string cname = Vala.Symbol.camel_case_to_lower_case(
-                               class.default_construction_method.get_full_name().replace(".","_"));
-
-                       try {
-                               unowned Constructor meth = (Constructor)module.get_method(cname);
-                               current_test = meth() as TestCase;
-                               current_test.name = class.get_full_name().replace("."," ");
-                               
-                               foreach(var method in class.get_methods()) {
-                                       if( method.name.has_prefix("test_") &&
-                                               method.has_result != true &&
-                                               method.get_parameters().size == 0
-                                       ) {
-                                               unowned TestMethod testmethod = null;
-                                               string mname = Vala.Symbol.camel_case_to_lower_case(
-                                                       method.get_full_name()
-                                                       .replace(".","_")
-                                               );
-
-                                               testmethod = (TestMethod)module.get_method(mname);
-
-                                               if (testmethod != null)
-                                                       
current_test.add_test(method.name.substring(5).replace("_"," "), ()=> {testmethod(current_test); });
-
-                                               
-                                       }
-                               }
-                               tests += current_test;
-
-                               class.accept_children(this);
-                       } catch (ModuleError e) {
-                               stderr.puts(e.message);
+               try {
+
+                       if (testcase != null &&
+                               class.is_subtype_of(testcase) &&
+                               class.is_abstract != true )
+                               current.add_test(visit_testcase(class));
+                       else if (testsuite != null &&
+                               class.is_subtype_of(testsuite) &&
+                               class.is_abstract != true )
+                               current.add_test(visit_testsuite(class));
+
+               } catch (ModuleError e) {
+                       stderr.puts(e.message);
+               }
+
+               class.accept_children(this);
+
+       }
+
+       public TestCase visit_testcase(Vala.Class testclass) throws ModuleError {
+               string cname = Vala.Symbol.camel_case_to_lower_case(
+                       testclass.default_construction_method.get_full_name().replace(".","_"));
+
+               string tname = Vala.Symbol.camel_case_to_lower_case(
+                       testclass.get_full_name().replace(".","/"));
+
+               unowned Constructor meth = (Constructor)config.module.get_method(cname);
+               var current_test = meth("%s%s/".printf(config.root.name, tname)) as TestCase;
+               
+               foreach(var method in testclass.get_methods()) {
+                       if( method.name.has_prefix("test_") &&
+                               method.has_result != true &&
+                               method.get_parameters().size == 0
+                       ) {
+                               unowned TestMethod testmethod = null;
+                               string mname = Vala.Symbol.camel_case_to_lower_case(
+                                       method.get_full_name()
+                                       .replace(".","_")
+                               );
+
+                               testmethod = (TestMethod)config.module.get_method(mname);
+
+                               if (testmethod != null)
+                                       current_test.add_test(method.name.substring(5), ()=> {
+                                               testmethod(current_test);
+                                       });
                        }
                }
+               return current_test;
+       }
+
+       public TestSuite visit_testsuite(Vala.Class testclass) throws ModuleError {
+               string cname = Vala.Symbol.camel_case_to_lower_case(
+                       testclass.default_construction_method.get_full_name().replace(".","_"));
+
+               string tname = Vala.Symbol.camel_case_to_lower_case(
+                       testclass.get_full_name().replace(".","/"));
+
+               unowned Constructor meth = (Constructor)config.module.get_method(cname);
+               var current_test = meth("%s%s/".printf(config.root.name, tname)) as TestSuite;
+               
+               return current_test;
        }
 
        public override void visit_namespace(Vala.Namespace ns) {
                
-               if (ns.name != "GLib")
+               if (ns.name == "GLib")
+                       return;
+
+               if (ns.name == "Valadate") {
                        ns.accept_children(this);
-               
+                       return;
+               }
+
+               if (ns.name != null) {
+
+                       string testname = Vala.Symbol.camel_case_to_lower_case(
+                               ns.get_full_name().replace(".","/"));
+
+                       var nstest = new TestSuite("%s%s/".printf(config.root.name, testname));
+
+                       if (current != null)
+                               current.add_test(nstest);
+
+                       current = nstest;
+               }
+
        }
        
        
diff --git a/valadate/testrunner.vala b/valadate/testrunner.vala
index 553f2c6..0256fa2 100644
--- a/valadate/testrunner.vala
+++ b/valadate/testrunner.vala
@@ -24,47 +24,52 @@ public class Valadate.TestRunner : Object {
 
        private TestConfig config;
 
+       private TestResult result;
+
        private SubprocessLauncher launcher =
                new SubprocessLauncher(GLib.SubprocessFlags.STDOUT_PIPE | GLib.SubprocessFlags.STDERR_PIPE);
-
-
-       private string path;
-       private Test[] _tests;
        
-       private TestResult result;
        
-       public Test[] tests {
-               get {
-                       return _tests;
-               }
-       }
-
        public TestRunner(TestConfig config) {
                this.config = config;
-               this.path = config.binary;
        }
 
-       public TestResult? run(TestResult? result = null) throws ConfigError {
+       public void run(TestResult result) {
+               
+               this.result = result;
                
+               if (TestConfig.runtest != null && config.root.count == 1) {
+                       config.root.run(result);
+                       return;
+               }
+               
+               run_test(config.root);
                
-               return result;
        }
 
 
-       private void run_test() {
-               string command = "";
+       private void run_test(Test test) {
+               
+               message(test.count.to_string());
+               
+               if (test.count > 1) {
+                       foreach(var subtest in test) {
+                               run_test(subtest);
+                       }
+               } else {
+                       string command = "%s -r ".printf(config.binary);
 
-               string[] args;
-               Shell.parse_argv(command, out args);
+                       string[] args;
+                       Shell.parse_argv(command, out args);
 
-               var process = launcher.spawnv(args);
-               var stderr_pipe = process.get_stderr_pipe();
+                       var process = launcher.spawnv(args);
+                       var stderr_pipe = process.get_stderr_pipe();
 
-               uint8 buffer[1028];
-               var err = stderr_pipe.read(buffer);
-               
-       }
+                       uint8 buffer[1028];
+                       var err = stderr_pipe.read(buffer);
 
+               }
+       }
 
 
        public static int main (string[] args) {
@@ -76,14 +81,14 @@ public class Valadate.TestRunner : Object {
                        return result;
 
                var runner = new TestRunner(config);
+               var testresult = new TestResult();
                
                try {
-                       runner.run();
+                       runner.run(testresult);
                } catch (ConfigError err) {
                        stderr.puts(err.message);
                        return 1;
                }
-               
 
                return 0;
        }
diff --git a/valadate/testsuite.vala b/valadate/testsuite.vala
index 8a7e85e..aaf4b72 100644
--- a/valadate/testsuite.vala
+++ b/valadate/testsuite.vala
@@ -22,17 +22,7 @@
 
 public class Valadate.TestSuite : Object, Test {
 
-       /**
-        * The TestMethod delegate represents a {@link Valadate.Test} method
-        * that can be added to a TestCase and run
-        */
-       public delegate void TestMethod ();
-
-
-       private HashTable<string, Test> _tests =
-               new HashTable<string, Test> (str_hash, str_equal);
-       
-       private List<weak Test> _values;
+       private List<Test> _tests = new List<Test>();
        
        /**
         * the name of the TestSuite
@@ -45,7 +35,11 @@ public class Valadate.TestSuite : Object, Test {
         */
        public int count {
                get {
-                       return (int)_tests.size();
+                       int testcount = 0;
+                       _tests.foreach((t) => {
+                               testcount += t.count;
+                       });
+                       return testcount;
                }
        }
 
@@ -53,10 +47,9 @@ public class Valadate.TestSuite : Object, Test {
         * Returns a {@link GLib.List} of {@link Valadate.Test}s that will be
         * run by this TestSuite
         */
-       public List<weak Test> tests {
+       public List<Test> tests {
                get {
-                       _values = _tests.get_values();
-                       return _values;
+                       return _tests;
                }
        }
 
@@ -65,57 +58,41 @@ public class Valadate.TestSuite : Object, Test {
         * TestSuite's name
         */
        public TestSuite(string? name = null) {
-               this.name = name;
+               this.name = name ?? this.get_type().name();
        }
 
        /**
         * Adds a test to the suite.
         */
-       public void add_test(string name, Test test) {
-               _tests.set(name, test);
-       }
-
-       public void add_test_method(string name, owned TestMethod test) {
-               var adaptor = new TestAdaptor ((owned)test);
-               _tests.set(name, adaptor);
+       public void add_test(Test test) {
+               _tests.append(test);
        }
 
 
        public void run(TestResult result) {
-               _tests.foreach((k,t) => { run_test(t, result); });
-       }
-
-       public void run_test(Test test, TestResult result) {
-               result.run(test);
-       }
-       
-       public new unowned Test? get(string name) {
-               return _tests.lookup(name);
+               _tests.foreach((t) => { t.run(result); });
        }
 
-       public new void set(string name, Test test) {
-               _tests.set(name, test);
-       }
-       
-       private class TestAdaptor : Object, Test {
-
-               private TestSuite.TestMethod test;
-
-               public int count {
-                       get {
-                               return 1;
+       public Test get_test(int index) {
+               int testcount = 0;
+               Test test = null;
+
+               _tests.foreach((t) => {
+                       
+                       if (t.count + testcount >= index) {
+                       
+                               int offset = index - testcount;
+                               
+                               test = t.get_test(offset);
+                       
+                       
+                       } else {
+                               testcount += t.count;
                        }
-               }
-               
-               public TestAdaptor(owned TestSuite.TestMethod test) {
-                       this.test = (owned)test;
-               }
-
-               public void run(TestResult test) {
-                       this.test();
-               }
+               });
 
+               return test;
        }
-       
+
        
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]