Debian Codegraph @

xmpp-vala/tests/common.vala

raw
1 namespace Xmpp.Test {
2
3 int main(string[] args) {
4 GLib.Test.init(ref args);
5 GLib.Test.set_nonfatal_assertions();
6 TestSuite.get_root().add_suite(new Xmpp.Test.StanzaTest().get_suite());
7 TestSuite.get_root().add_suite(new Xmpp.Test.UtilTest().get_suite());
8 TestSuite.get_root().add_suite(new Xmpp.Test.JidTest().get_suite());
9 TestSuite.get_root().add_suite(new Xmpp.Test.ColorTest().get_suite());
10 return GLib.Test.run();
11 }
12
13 bool fail_if(bool exp, string? reason = null) {
14 if (exp) {
15 if (reason != null) GLib.Test.message(reason);
16 GLib.Test.fail();
17 return true;
18 }
19 return false;
20 }
21
22 void fail_if_reached(string? reason = null) {
23 fail_if(true, reason);
24 }
25
26 delegate void ErrorFunc() throws Error;
27
28 void fail_if_not_error_code(ErrorFunc func, int expectedCode, string? reason = null) {
29 try {
30 func();
31 fail_if_reached(@"$(reason + ": " ?? "")no error thrown");
32 } catch (Error e) {
33 fail_if_not_eq_int(e.code, expectedCode, @"$(reason + ": " ?? "")caught unexpected error");
34 }
35 }
36
37 bool fail_if_not(bool exp, string? reason = null) {
38 return fail_if(!exp, reason);
39 }
40
41 bool fail_if_eq_int(int left, int right, string? reason = null) {
42 return fail_if(left == right, @"$(reason + ": " ?? "")$left == $right");
43 }
44
45 bool fail_if_not_eq_node(StanzaNode left, StanzaNode right, string? reason = null) {
46 if (fail_if_not_eq_str(left.name, right.name, @"$(reason + ": " ?? "")name mismatch")) return true;
47 if (fail_if_not_eq_str(left.val, right.val, @"$(reason + ": " ?? "")val mismatch")) return true;
48 if (left.name == "#text") return false;
49 if (fail_if_not_eq_str(left.ns_uri, right.ns_uri, @"$(reason + ": " ?? "")ns_uri mismatch")) return true;
50 if (fail_if_not_eq_int(left.sub_nodes.size, right.sub_nodes.size, @"$(reason + ": " ?? "")sub node count mismatch")) return true;
51 if (fail_if_not_eq_int(left.attributes.size, right.attributes.size, @"$(reason + ": " ?? "")attributes count mismatch")) return true;
52 for (var i = 0; i < left.sub_nodes.size; i++) {
53 if (fail_if_not_eq_node(left.sub_nodes[i], right.sub_nodes[i], @"$(reason + ": " ?? "")$(i+1)th subnode mismatch")) return true;
54 }
55 for (var i = 0; i < left.attributes.size; i++) {
56 if (fail_if_not_eq_attr(left.attributes[i], right.attributes[i], @"$(reason + ": " ?? "")$(i+1)th attribute mismatch")) return true;
57 }
58 return false;
59 }
60
61 bool fail_if_not_eq_attr(StanzaAttribute left, StanzaAttribute right, string? reason = null) {
62 if (fail_if_not_eq_str(left.name, right.name, @"$(reason + ": " ?? "")name mismatch")) return true;
63 if (fail_if_not_eq_str(left.val, right.val, @"$(reason + ": " ?? "")val mismatch")) return true;
64 if (fail_if_not_eq_str(left.ns_uri, right.ns_uri, @"$(reason + ": " ?? "")ns_uri mismatch")) return true;
65 return false;
66 }
67
68 bool fail_if_not_eq_int(int left, int right, string? reason = null) {
69 return fail_if_not(left == right, @"$(reason + ": " ?? "")$left != $right");
70 }
71
72 bool fail_if_not_eq_uint(uint left, uint right, string? reason = null) {
73 return fail_if_not(left == right, @"$(reason + ": " ?? "")$left != $right");
74 }
75
76 private float float_to_accuracy(float f, float accuracy) {
77 return (float) (Math.round(f * Math.pow(10, accuracy)) / Math.pow(10, accuracy));
78 }
79
80 private float double_to_accuracy(double f, float accuracy) {
81 return (float) (Math.round(f * Math.pow(10, accuracy)) / Math.pow(10, accuracy));
82 }
83
84 bool fail_if_not_eq_float(float left, float right, float accuracy = 3, string? reason = null) {
85 return fail_if_not(float_to_accuracy(left, accuracy) == float_to_accuracy(right, accuracy), @"$(reason + ": " ?? "")$left != $right");
86 }
87
88 bool fail_if_not_eq_double(double left, double right, float accuracy = 3, string? reason = null) {
89 return fail_if_not(double_to_accuracy(left, accuracy) == double_to_accuracy(right, accuracy), @"$(reason + ": " ?? "")$left != $right");
90 }
91
92 bool fail_if_not_eq_str(string? left, string? right, string? reason = null) {
93 bool nullcheck = (left == null || right == null) && (left != null && right != null);
94 if (left == null) left = "(null)";
95 if (right == null) right = "(null)";
96 return fail_if_not(!nullcheck && left == right, @"$(reason + ": " ?? "")'$left' != '$right'");
97 }
98
99 bool fail_if_eq_str(string? left, string? right, string? reason = null) {
100 bool nullcheck = (left == null && right != null) || (left != null && right == null);
101 if (left == null) left = "(null)";
102 if (right == null) right = "(null)";
103 return fail_if(!nullcheck && left == right, @"$(reason + ": " ?? "")'$left' == '$right'");
104 }
105
106 bool fail_if_not_eq_uint8_arr(uint8[] left, uint8[] right, string? reason = null) {
107 if (fail_if_not_eq_int(left.length, right.length, @"$(reason + ": " ?? "")array length not equal")) return true;
108 return fail_if_not_eq_str(Base64.encode(left), Base64.encode(right), reason);
109 }
110
111 bool fail_if_not_zero_int(int zero, string? reason = null) {
112 return fail_if_not_eq_int(zero, 0, reason);
113 }
114
115 bool fail_if_zero_int(int zero, string? reason = null) {
116 return fail_if_eq_int(zero, 0, reason);
117 }
118
119 bool fail_if_null(void* what, string? reason = null) {
120 return fail_if(what == null || (size_t)what == 0, reason);
121 }
122
123 }