3
* Copyright (C) 2008 Ali Sabil <ali.sabil@gmail.com>
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.
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.
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
24
public class Suite : Object {
26
protected GLib.List<Case> cases = new GLib.List<Case> ();
27
protected Case current_test_case = null;
29
private bool break_mainloop = false;
30
private uint timeout_source_id = 0;
38
public uint succeeded {
52
protected void add_test_case(string name, CaseFunc case_func) {
53
var test_case = new Case(name);
54
test_case.func = case_func;
55
cases.append (test_case);
64
stdout.printf (">> Test suite: %s\n", name);
66
Timer timer = new Timer();
69
foreach (Case test_case in cases) {
70
current_test_case = test_case;
71
current_test_case.total_assertions = 0;
72
current_test_case.succeeded_assertions = 0;
74
break_mainloop = false;
75
timeout_source_id = 0;
77
if (current_test_case.func != null)
78
current_test_case.func ();
81
stdout.printf (" > check #%02u: checking %s ",
83
ellipsize(test_case.name));
84
if (test_case.total_assertions == test_case.succeeded_assertions) {
86
stdout.printf ("OK\n");
88
stdout.printf ("FAILED\n");
92
stdout.printf (" >>> Summary: %s\n", name);
93
stdout.printf (" %u/%u test(s) succeeded\n", succeeded, total);
94
stdout.printf (" test(s) executed in %f second(s)\n\n", timer.elapsed());
96
return (int) (total - succeeded);
99
protected virtual void initialize () {
102
protected virtual void set_up () {
105
protected virtual void tear_down () {
108
protected void success (string? message=null) {
109
var res = new AssertionResult (AssertionStatus.SUCCEEDED, message);
111
current_test_case.total_assertions++;
112
current_test_case.succeeded_assertions++;
113
current_test_case.results.prepend (res);
116
protected void skip (string? message=null) {
117
var res = new AssertionResult (AssertionStatus.SKIPPED, message);
119
current_test_case.results.prepend (res);
122
protected void failure (string? message=null) {
123
var res = new AssertionResult (AssertionStatus.FAILED, message);
125
current_test_case.total_assertions++;
126
current_test_case.results.prepend (res);
129
protected void timeout (string? message=null) {
130
var res = new AssertionResult (AssertionStatus.TIMED_OUT, message);
132
current_test_case.total_assertions++;
133
current_test_case.results.prepend (res);
136
protected void assert_true (bool cond, string? message=null) {
143
protected void assert_false (bool cond, string? message=null) {
150
protected void async_wait (uint timeout=0) {
151
var loop = new MainLoop (null, false);
152
var context = loop.get_context ();
154
if (timeout > 0 && !break_mainloop) {
155
Timeout.add (timeout, timeout_callback);
158
while (!break_mainloop) {
159
context.iteration (false);
161
break_mainloop = false;
164
protected void async_break () {
165
if (timeout_source_id > 0) {
166
Source.remove (timeout_source_id);
168
break_mainloop = true;
171
protected void async_success (string? message=null) {
176
protected void async_skip (string? message=null) {
181
protected void async_failure (string? message=null) {
186
private bool timeout_callback () {
187
timeout_source_id = 0;
193
private static string ellipsize (string s) {
198
result = s.substring (0, 48);
201
result += " " + string.nfill (52 - result.len(), '.');