~people-project/people-core/deep-refactoring

« back to all changes in this revision

Viewing changes to libs/test-it/suite.vala

  • Committer: Ali Sabil
  • Date: 2009-01-29 22:35:23 UTC
  • Revision ID: ali.sabil@gmail.com-20090129223523-f834u01bufze5r0e
- Removed the libs/ folder, depending now on libcore to provide these features

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* suite.vala
2
 
 *
3
 
 * Copyright (C) 2008 Ali Sabil <ali.sabil@gmail.com>
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
 
 */
20
 
 
21
 
 
22
 
namespace TestIt {
23
 
 
24
 
        public class Suite : Object {
25
 
 
26
 
                protected GLib.List<Case> cases = new GLib.List<Case> ();
27
 
                protected Case current_test_case = null;
28
 
 
29
 
                private bool break_mainloop = false;
30
 
                private uint timeout_source_id = 0;
31
 
 
32
 
                public uint total {
33
 
                        get;
34
 
                        private set;
35
 
                        default = 0;
36
 
                }
37
 
 
38
 
                public uint succeeded {
39
 
                        get;
40
 
                        private set;
41
 
                        default = 0;
42
 
                }
43
 
 
44
 
                public string name {
45
 
                        get;
46
 
                        protected set;
47
 
                }
48
 
 
49
 
                public Suite () {
50
 
                }
51
 
 
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);
56
 
                }
57
 
 
58
 
                public int run () {
59
 
                        initialize ();
60
 
 
61
 
                        total = 0;
62
 
                        succeeded = 0;
63
 
 
64
 
                        stdout.printf (">> Test suite: %s\n", name);
65
 
 
66
 
                        Timer timer = new Timer();
67
 
                        timer.start();
68
 
 
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;
73
 
 
74
 
                                break_mainloop = false;
75
 
                                timeout_source_id = 0;
76
 
                                set_up ();
77
 
                                if (current_test_case.func != null)
78
 
                                        current_test_case.func ();
79
 
                                tear_down ();
80
 
 
81
 
                                stdout.printf ("  > check #%02u:  checking %s ",
82
 
                                                total++,
83
 
                                                ellipsize(test_case.name));
84
 
                                if (test_case.total_assertions == test_case.succeeded_assertions) {
85
 
                                        succeeded++;
86
 
                                        stdout.printf ("OK\n");
87
 
                                } else {
88
 
                                        stdout.printf ("FAILED\n");
89
 
                                }
90
 
                        }
91
 
                        timer.stop();
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());
95
 
 
96
 
                        return (int) (total - succeeded);
97
 
                }
98
 
 
99
 
                protected virtual void initialize () {
100
 
                }
101
 
 
102
 
                protected virtual void set_up () {
103
 
                }
104
 
 
105
 
                protected virtual void tear_down () {
106
 
                }
107
 
 
108
 
                protected void success (string? message=null) {
109
 
                        var res = new AssertionResult (AssertionStatus.SUCCEEDED, message);
110
 
 
111
 
                        current_test_case.total_assertions++;
112
 
                        current_test_case.succeeded_assertions++;
113
 
                        current_test_case.results.prepend (res);
114
 
                }
115
 
 
116
 
                protected void skip (string? message=null) {
117
 
                        var res = new AssertionResult (AssertionStatus.SKIPPED, message);
118
 
 
119
 
                        current_test_case.results.prepend (res);
120
 
                }
121
 
 
122
 
                protected void failure (string? message=null) {
123
 
                        var res = new AssertionResult (AssertionStatus.FAILED, message);
124
 
 
125
 
                        current_test_case.total_assertions++;
126
 
                        current_test_case.results.prepend (res);
127
 
                }
128
 
 
129
 
                protected void timeout (string? message=null) {
130
 
                        var res = new AssertionResult (AssertionStatus.TIMED_OUT, message);
131
 
 
132
 
                        current_test_case.total_assertions++;
133
 
                        current_test_case.results.prepend (res);
134
 
                }
135
 
 
136
 
                protected void assert_true (bool cond, string? message=null) {
137
 
                        if (cond)
138
 
                                success (message);
139
 
                        else
140
 
                                failure (message);
141
 
                }
142
 
 
143
 
                protected void assert_false (bool cond, string? message=null) {
144
 
                        if (! cond)
145
 
                                success (message);
146
 
                        else
147
 
                                failure (message);
148
 
                }
149
 
 
150
 
                protected void async_wait (uint timeout=0) {
151
 
                        var loop = new MainLoop (null, false);
152
 
                        var context = loop.get_context ();
153
 
 
154
 
                        if (timeout > 0 && !break_mainloop) {
155
 
                                Timeout.add (timeout, timeout_callback);
156
 
                        }
157
 
 
158
 
                        while (!break_mainloop) {
159
 
                                context.iteration (false);
160
 
                        }
161
 
                        break_mainloop = false;
162
 
                }
163
 
 
164
 
                protected void async_break () {
165
 
                        if (timeout_source_id > 0) {
166
 
                                Source.remove (timeout_source_id);
167
 
                        }
168
 
                        break_mainloop = true;
169
 
                }
170
 
 
171
 
                protected void async_success (string? message=null) {
172
 
                        async_break ();
173
 
                        success (message);
174
 
                }
175
 
 
176
 
                protected void async_skip (string? message=null) {
177
 
                        async_break ();
178
 
                        skip (message);
179
 
                }
180
 
 
181
 
                protected void async_failure (string? message=null) {
182
 
                        async_break ();
183
 
                        failure (message);
184
 
                }
185
 
 
186
 
                private bool timeout_callback () {
187
 
                        timeout_source_id = 0;
188
 
                        async_break ();
189
 
                        timeout ();
190
 
                        return false;
191
 
                }
192
 
 
193
 
                private static string ellipsize (string s) {
194
 
                        if (s == null)
195
 
                                return "";
196
 
                        string result;
197
 
                        if (s.len() > 48)
198
 
                                result = s.substring (0, 48);
199
 
                        else
200
 
                                result = s;
201
 
                        result +=  " " + string.nfill (52 - result.len(), '.');
202
 
                        return result;
203
 
                }
204
 
        }
205
 
}