geary/test/test-case.vala
Michael James Gratton 15748cef03 Tidy up unit test infrastructure and mock classes.
* test/api/*.vala: Renamed files that contained mock objects to *-mock.vala,
  not *-test.vala.

* test/testcase.vala: Renamed to test-case.vala for consistency, remove
  TestCase class from Gee package since that's really not true. Clean up
  code for consistency.

* test/meson.build, test/CMakeLists.txt: Split TestCase compilation out
  into a separate test lib.
2018-04-07 09:41:18 +10:00

116 lines
3.1 KiB
Vala

/*
* Copyright (C) 2009 Julien Peeters
* Copyright (C) 2017 Michael Gratton
*
* 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>
* Michael Gratton <mike@vee.net>
*/
public abstract class TestCase : Object {
protected MainContext main_loop = MainContext.default();
private GLib.TestSuite suite;
private Adaptor[] adaptors = new Adaptor[0];
private AsyncQueue<AsyncResult> async_results = new AsyncQueue<AsyncResult>();
public delegate void TestMethod() throws Error;
public TestCase(string name) {
this.suite = new GLib.TestSuite(name);
}
public void add_test(string name, owned TestMethod test) {
var adaptor = new Adaptor(name, (owned) test, this);
this.adaptors += adaptor;
this.suite.add(
new GLib.TestCase(
adaptor.name,
adaptor.set_up,
adaptor.run,
adaptor.tear_down
)
);
}
public virtual void set_up() throws Error {
}
public virtual void tear_down() throws Error {
}
public GLib.TestSuite get_suite() {
return this.suite;
}
protected void async_complete(AsyncResult result) {
this.async_results.push(result);
// notify the loop so that if async_result() has already been
// called, that method won't block
this.main_loop.wakeup();
}
protected AsyncResult async_result() {
AsyncResult? result = null;
while (result == null) {
this.main_loop.iteration(true);
result = this.async_results.try_pop();
}
return result;
}
private class Adaptor {
public string name { get; private set; }
private TestMethod test;
private TestCase test_case;
public Adaptor(string name,
owned TestMethod test,
TestCase test_case) {
this.name = name;
this.test = (owned) test;
this.test_case = test_case;
}
public void set_up(void* fixture) {
try {
this.test_case.set_up();
} catch (Error err) {
assert_no_error(err);
}
}
public void run(void* fixture) {
try {
this.test();
} catch (Error err) {
assert_no_error(err);
}
}
public void tear_down(void* fixture) {
try {
this.test_case.tear_down();
} catch (Error err) {
assert_no_error(err);
}
}
}
}