Fix async tests
This commit is contained in:
parent
717d0b7fca
commit
8f8018ec81
|
@ -72,14 +72,14 @@ bool fail_if_not_eq_str(string? left, string? right, string? reason = null) {
|
||||||
bool nullcheck = (left == null || right == null) && (left != null && right != null);
|
bool nullcheck = (left == null || right == null) && (left != null && right != null);
|
||||||
if (left == null) left = "(null)";
|
if (left == null) left = "(null)";
|
||||||
if (right == null) right = "(null)";
|
if (right == null) right = "(null)";
|
||||||
return fail_if_not(!nullcheck && left == right, @"$(reason + ": " ?? "")$left != $right");
|
return fail_if_not(!nullcheck && left == right, @"$(reason + ": " ?? "")'$left' != '$right'");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fail_if_eq_str(string? left, string? right, string? reason = null) {
|
bool fail_if_eq_str(string? left, string? right, string? reason = null) {
|
||||||
bool nullcheck = (left == null && right != null) || (left != null && right == null);
|
bool nullcheck = (left == null && right != null) || (left != null && right == null);
|
||||||
if (left == null) left = "(null)";
|
if (left == null) left = "(null)";
|
||||||
if (right == null) right = "(null)";
|
if (right == null) right = "(null)";
|
||||||
return fail_if(!nullcheck && left == right, @"$(reason + ": " ?? "")$left == $right");
|
return fail_if(!nullcheck && left == right, @"$(reason + ": " ?? "")'$left' == '$right'");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fail_if_not_eq_uint8_arr(uint8[] left, uint8[] right, string? reason = null) {
|
bool fail_if_not_eq_uint8_arr(uint8[] left, uint8[] right, string? reason = null) {
|
||||||
|
|
|
@ -4,12 +4,12 @@ class StanzaTest : Gee.TestCase {
|
||||||
public StanzaTest() {
|
public StanzaTest() {
|
||||||
base("Stanza");
|
base("Stanza");
|
||||||
|
|
||||||
add_test("node_one", () => { test_node_one.begin(); });
|
add_async_test("node_one", (cb) => { test_node_one.begin(cb); });
|
||||||
add_test("typical_stream", () => { test_typical_stream.begin(); });
|
add_async_test("typical_stream", (cb) => { test_typical_stream.begin(cb); });
|
||||||
add_test("ack_stream", () => { test_ack_stream.begin(); });
|
add_async_test("ack_stream", (cb) => { test_ack_stream.begin(cb); });
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void test_node_one() {
|
private async void test_node_one(Gee.TestCase.FinishCallback cb) {
|
||||||
var node1 = new StanzaNode.build("test", "ns1_uri")
|
var node1 = new StanzaNode.build("test", "ns1_uri")
|
||||||
.add_self_xmlns()
|
.add_self_xmlns()
|
||||||
.put_attribute("ns2", "ns2_uri", XMLNS_URI)
|
.put_attribute("ns2", "ns2_uri", XMLNS_URI)
|
||||||
|
@ -23,9 +23,10 @@ class StanzaTest : Gee.TestCase {
|
||||||
var node2 = yield new StanzaReader.for_string(xml1).read_node();
|
var node2 = yield new StanzaReader.for_string(xml1).read_node();
|
||||||
fail_if_not(node1.equals(node2));
|
fail_if_not(node1.equals(node2));
|
||||||
fail_if_not_eq_str(node1.to_string(), node2.to_string());
|
fail_if_not_eq_str(node1.to_string(), node2.to_string());
|
||||||
|
cb();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void test_typical_stream() {
|
private async void test_typical_stream(Gee.TestCase.FinishCallback cb) {
|
||||||
var stream = """
|
var stream = """
|
||||||
<?xml version='1.0' encoding='UTF-8'?>
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
<stream:stream
|
<stream:stream
|
||||||
|
@ -50,16 +51,28 @@ class StanzaTest : Gee.TestCase {
|
||||||
.put_attribute("to", "juliet@example.com")
|
.put_attribute("to", "juliet@example.com")
|
||||||
.put_attribute("lang", "en", XML_URI)
|
.put_attribute("lang", "en", XML_URI)
|
||||||
.put_node(new StanzaNode.build("body")
|
.put_node(new StanzaNode.build("body")
|
||||||
.put_node(new StanzaNode.text("I'll send a friar with speed, to Mantua, with my letters to thy lord.")));
|
.put_node(new StanzaNode.text(" I'll send a friar with speed, to Mantua, with my letters to thy lord.")));
|
||||||
|
|
||||||
var reader = new StanzaReader.for_string(stream);
|
var reader = new StanzaReader.for_string(stream);
|
||||||
fail_if_not_eq_node(root_node_cmp, yield reader.read_root_node());
|
fail_if_not_eq_node(root_node_cmp, yield reader.read_root_node());
|
||||||
fail_if_not_eq_node(node_cmp, yield reader.read_node());
|
fail_if_not_eq_node(node_cmp, yield reader.read_node());
|
||||||
yield reader.read_node();
|
yield reader.read_node();
|
||||||
fail_if_not_error_code(() => reader.read_node(), 3, "end of stream should be reached");
|
yield fail_if_not_end_of_stream(reader);
|
||||||
|
cb();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void test_ack_stream() {
|
private async void fail_if_not_end_of_stream(StanzaReader reader) {
|
||||||
|
try {
|
||||||
|
yield reader.read_node();
|
||||||
|
fail_if_reached("end of stream should be reached");
|
||||||
|
} catch (XmlError.EOF e) {
|
||||||
|
return;
|
||||||
|
} catch (Error e) {
|
||||||
|
fail_if_reached("Unexpected error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void test_ack_stream(Gee.TestCase.FinishCallback cb) {
|
||||||
var stream = """
|
var stream = """
|
||||||
<?xml version='1.0' encoding='UTF-8'?>
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
<stream:stream
|
<stream:stream
|
||||||
|
@ -95,7 +108,8 @@ class StanzaTest : Gee.TestCase {
|
||||||
fail_if_not_eq_node(node_cmp, yield reader.read_node());
|
fail_if_not_eq_node(node_cmp, yield reader.read_node());
|
||||||
fail_if_not_eq_node(node2_cmp, yield reader.read_node());
|
fail_if_not_eq_node(node2_cmp, yield reader.read_node());
|
||||||
yield reader.read_node();
|
yield reader.read_node();
|
||||||
fail_if_not_error_code(() => reader.read_node(), 3, "end of stream should be reached");
|
yield fail_if_not_end_of_stream(reader);
|
||||||
|
cb();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,13 +26,25 @@ public abstract class Gee.TestCase : Object {
|
||||||
private Adaptor[] adaptors = new Adaptor[0];
|
private Adaptor[] adaptors = new Adaptor[0];
|
||||||
|
|
||||||
public delegate void TestMethod ();
|
public delegate void TestMethod ();
|
||||||
|
public delegate void FinishCallback ();
|
||||||
|
public delegate void AsyncTestMethod (FinishCallback cb);
|
||||||
|
|
||||||
protected TestCase (string name) {
|
protected TestCase (string name) {
|
||||||
this.suite = new GLib.TestSuite (name);
|
this.suite = new GLib.TestSuite (name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add_test (string name, owned TestMethod test) {
|
public void add_test (string name, owned TestMethod test) {
|
||||||
var adaptor = new Adaptor (name, (owned)test, this);
|
var adaptor = new DefaultAdaptor (name, (owned)test, this);
|
||||||
|
this.adaptors += adaptor;
|
||||||
|
|
||||||
|
this.suite.add (new GLib.TestCase (adaptor.name,
|
||||||
|
adaptor.set_up,
|
||||||
|
adaptor.run,
|
||||||
|
adaptor.tear_down ));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add_async_test (string name, owned AsyncTestMethod test, int timeout = 10000) {
|
||||||
|
var adaptor = new AsyncAdaptor (name, (owned)test, this, timeout);
|
||||||
this.adaptors += adaptor;
|
this.adaptors += adaptor;
|
||||||
|
|
||||||
this.suite.add (new GLib.TestCase (adaptor.name,
|
this.suite.add (new GLib.TestCase (adaptor.name,
|
||||||
|
@ -51,13 +63,19 @@ public abstract class Gee.TestCase : Object {
|
||||||
return this.suite;
|
return this.suite;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Adaptor {
|
private interface Adaptor : Object {
|
||||||
|
public abstract void set_up (void* fixture);
|
||||||
|
public abstract void run (void* fixture);
|
||||||
|
public abstract void tear_down (void* fixture);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DefaultAdaptor : Object, Adaptor {
|
||||||
[CCode (notify = false)]
|
[CCode (notify = false)]
|
||||||
public string name { get; private set; }
|
public string name { get; private set; }
|
||||||
private TestMethod test;
|
private TestMethod test;
|
||||||
private TestCase test_case;
|
private TestCase test_case;
|
||||||
|
|
||||||
public Adaptor (string name,
|
public DefaultAdaptor (string name,
|
||||||
owned TestMethod test,
|
owned TestMethod test,
|
||||||
TestCase test_case) {
|
TestCase test_case) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -77,4 +95,49 @@ public abstract class Gee.TestCase : Object {
|
||||||
this.test_case.tear_down ();
|
this.test_case.tear_down ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class AsyncAdaptor : Object, Adaptor {
|
||||||
|
[CCode (notify = false)]
|
||||||
|
public string name { get; private set; }
|
||||||
|
private AsyncTestMethod test;
|
||||||
|
private TestCase test_case;
|
||||||
|
private MainLoop main_loop;
|
||||||
|
private int timeout;
|
||||||
|
|
||||||
|
public AsyncAdaptor (string name,
|
||||||
|
owned AsyncTestMethod test,
|
||||||
|
TestCase test_case,
|
||||||
|
int timeout) {
|
||||||
|
this.name = name;
|
||||||
|
this.test = (owned)test;
|
||||||
|
this.test_case = test_case;
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set_up (void* fixture) {
|
||||||
|
this.test_case.set_up ();
|
||||||
|
main_loop = new MainLoop ();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run (void* fixture) {
|
||||||
|
this.test (finish);
|
||||||
|
Timeout.add (timeout, finish_timeout);
|
||||||
|
main_loop.run ();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void finish () {
|
||||||
|
Idle.add (() => { main_loop.quit (); return false; });
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool finish_timeout () {
|
||||||
|
Test.fail ();
|
||||||
|
Test.message (@"Timeout of $(timeout)ms reached.");
|
||||||
|
main_loop.quit ();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tear_down (void* fixture) {
|
||||||
|
this.test_case.tear_down ();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue