| 1 |
/* testcase.vala |
| 2 |
* |
| 3 |
* Copyright (C) 2009 Julien Peeters |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
|
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
* |
| 19 |
* Author: |
| 20 |
* Julien Peeters <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
public abstract class Gee.TestCase : Object { |
| 24 |
|
| 25 |
private GLib.TestSuite suite; |
| 26 |
private TestAdaptor[] adaptors = new TestAdaptor[0]; |
| 27 |
|
| 28 |
protected TestCase (string name) { |
| 29 |
this.suite = new GLib.TestSuite (name); |
| 30 |
} |
| 31 |
|
| 32 |
public void add_test (string name, owned TestMethod test) { |
| 33 |
var adaptor = new TestDefaultAdaptor (name, (owned)test, this); |
| 34 |
this.adaptors += adaptor; |
| 35 |
|
| 36 |
this.suite.add (new GLib.TestCase (adaptor.name, |
| 37 |
adaptor.set_up, |
| 38 |
adaptor.run, |
| 39 |
adaptor.tear_down )); |
| 40 |
} |
| 41 |
|
| 42 |
public void add_async_test (string name, owned AsyncTestMethod test, int timeout = 10000) { |
| 43 |
var adaptor = new TestAsyncAdaptor (name, (owned)test, this, timeout); |
| 44 |
this.adaptors += adaptor; |
| 45 |
|
| 46 |
this.suite.add (new GLib.TestCase (adaptor.name, |
| 47 |
adaptor.set_up, |
| 48 |
adaptor.run, |
| 49 |
adaptor.tear_down )); |
| 50 |
} |
| 51 |
|
| 52 |
public virtual void set_up () { |
| 53 |
} |
| 54 |
|
| 55 |
public virtual void tear_down () { |
| 56 |
} |
| 57 |
|
| 58 |
public GLib.TestSuite get_suite () { |
| 59 |
return (owned) this.suite; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
namespace Gee { |
| 64 |
public delegate void TestMethod (); |
| 65 |
public delegate void TestFinishedCallback (); |
| 66 |
public delegate void AsyncTestMethod (TestFinishedCallback cb); |
| 67 |
} |
| 68 |
|
| 69 |
private interface Gee.TestAdaptor : Object { |
| 70 |
public abstract void set_up (void* fixture); |
| 71 |
public abstract void run (void* fixture); |
| 72 |
public abstract void tear_down (void* fixture); |
| 73 |
} |
| 74 |
|
| 75 |
private class Gee.TestDefaultAdaptor : Object, TestAdaptor { |
| 76 |
[CCode (notify = false)] |
| 77 |
public string name { get; private set; } |
| 78 |
private TestMethod test; |
| 79 |
private TestCase test_case; |
| 80 |
|
| 81 |
public TestDefaultAdaptor (string name, |
| 82 |
owned TestMethod test, |
| 83 |
TestCase test_case) { |
| 84 |
this.name = name; |
| 85 |
this.test = (owned)test; |
| 86 |
this.test_case = test_case; |
| 87 |
} |
| 88 |
|
| 89 |
public void set_up (void* fixture) { |
| 90 |
this.test_case.set_up (); |
| 91 |
} |
| 92 |
|
| 93 |
public void run (void* fixture) { |
| 94 |
this.test (); |
| 95 |
} |
| 96 |
|
| 97 |
public void tear_down (void* fixture) { |
| 98 |
this.test_case.tear_down (); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
private class Gee.TestAsyncAdaptor : Object, TestAdaptor { |
| 103 |
[CCode (notify = false)] |
| 104 |
public string name { get; private set; } |
| 105 |
private AsyncTestMethod test; |
| 106 |
private TestCase test_case; |
| 107 |
private MainLoop main_loop; |
| 108 |
private int timeout; |
| 109 |
|
| 110 |
public TestAsyncAdaptor (string name, |
| 111 |
owned AsyncTestMethod test, |
| 112 |
TestCase test_case, |
| 113 |
int timeout) { |
| 114 |
this.name = name; |
| 115 |
this.test = (owned)test; |
| 116 |
this.test_case = test_case; |
| 117 |
this.timeout = timeout; |
| 118 |
} |
| 119 |
|
| 120 |
public void set_up (void* fixture) { |
| 121 |
this.test_case.set_up (); |
| 122 |
main_loop = new MainLoop (); |
| 123 |
} |
| 124 |
|
| 125 |
public void run (void* fixture) { |
| 126 |
this.test (finish); |
| 127 |
Timeout.add (timeout, finish_timeout); |
| 128 |
main_loop.run (); |
| 129 |
} |
| 130 |
|
| 131 |
public void finish () { |
| 132 |
Idle.add (() => { main_loop.quit (); return false; }); |
| 133 |
} |
| 134 |
|
| 135 |
public bool finish_timeout () { |
| 136 |
Test.fail (); |
| 137 |
Test.message (@"Timeout of $(timeout)ms reached."); |
| 138 |
main_loop.quit (); |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
public void tear_down (void* fixture) { |
| 143 |
this.test_case.tear_down (); |
| 144 |
} |
| 145 |
} |