Add test fixture helper
authorJulien Peeters <contact@julienpeeters.fr>
Mon, 27 Jul 2009 20:09:21 +0000 (22:09 +0200)
committerDidier 'Ptitjes <ptitjes@free.fr>
Mon, 27 Jul 2009 20:09:21 +0000 (22:09 +0200)
tests/testfixture.vala [new file with mode: 0644]

diff --git a/tests/testfixture.vala b/tests/testfixture.vala
new file mode 100644 (file)
index 0000000..1ff7f51
--- /dev/null
@@ -0,0 +1,82 @@
+/* testfixture.vala
+ *
+ * Copyright (C) 2009 Julien Peeters
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Julien Peeters <contact@julienpeeters.fr>
+ */
+
+using GLib;
+
+public abstract class TestFixture: Object {
+
+       private TestSuite suite;
+       private TestAdaptor[] adaptors = new TestAdaptor[0];
+
+       public delegate void TestMethod ();
+
+       public TestFixture (string name) {
+               this.suite = new TestSuite (name);
+       }
+
+       public void add_test (string name, TestMethod test) {
+               var adaptor = new TestAdaptor (name, test, this);
+               this.adaptors += adaptor;
+
+               this.suite.add (new TestCase (adaptor.name,
+                                             0,
+                                             adaptor.setup,
+                                             adaptor.run,
+                                             adaptor.teardown ));
+       }
+
+       public abstract void setup ();
+       public abstract void teardown ();
+
+       public TestSuite get_suite () {
+               return this.suite;
+       }
+
+       public void add_to_suite (TestSuite suite) {
+               suite.add_suite (this.suite);
+       }
+
+       private class TestAdaptor {
+
+               public string name { set; get; }
+               private TestMethod test;
+               private TestFixture fixture;
+
+               public TestAdaptor (string name, TestMethod test, TestFixture fixture) {
+                       this.name = name;
+                       this.test = test;
+                       this.fixture = fixture;
+               }
+
+               public void setup (void* fixture) {
+                       this.fixture.setup ();
+               }
+
+               public void run (void* fixture) {
+                       this.test ();
+               }
+
+               public void teardown (void* fixture) {
+                       this.fixture.teardown ();
+               }
+       }
+}