File: testcase.vala

package info (click to toggle)
libfsoframework 0.12.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,640 kB
  • ctags: 8,717
  • sloc: ansic: 52,981; sh: 11,367; makefile: 819
file content (143 lines) | stat: -rw-r--r-- 4,478 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* testcase.vala
 *
 * Copyright (C) 2009-2012 Julien Peeters
 *
 * 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>
 *
 * Copied from libgee/tests/testcase.vala.
 */

public abstract class FsoFramework.Test.TestCase : Object
{
    private GLib.TestSuite _suite;
    private Adaptor[] _adaptors = new Adaptor[0];

    public delegate void TestMethod ();

    public TestCase (string name)
    {
        this._suite = new GLib.TestSuite (name);
    }

    public void add_test (string name, TestMethod test)
    {
        var adaptor = new Adaptor (name, test, this);
        this._adaptors += adaptor;

        this._suite.add (new GLib.TestCase (adaptor.name, adaptor.set_up, adaptor.run, adaptor.tear_down, sizeof(Adaptor)));
    }

    public void add_async_test (string name, AsyncBegin async_begin, AsyncFinish async_finish, int timeout = 200)
    {
        var adaptor = new Adaptor (name, () => { }, this);
        adaptor.is_async = true;
        adaptor.async_begin = async_begin;
        adaptor.async_finish = async_finish;
        adaptor.async_timeout = timeout;
        this._adaptors += adaptor;

        this._suite.add (new GLib.TestCase (adaptor.name, adaptor.set_up, adaptor.run, adaptor.tear_down, sizeof(Adaptor)));
    }

    public virtual void set_up ()
    {
    }

    public virtual void tear_down ()
    {
    }

    public GLib.TestSuite get_suite ()
    {
        return this._suite;
    }

    private class Adaptor
    {
        public string name { get; private set; }
        public int async_timeout { get; set; }

        private unowned TestMethod _test;
        private TestCase _test_case;

        public bool is_async = false;
        public unowned AsyncBegin async_begin;
        public unowned AsyncFinish async_finish;

        public Adaptor (string name, TestMethod test, TestCase test_case)
        {
            this._name = name;
            this._test = test;
            this._test_case = test_case;
        }

        public void set_up (void* fixture)
        {
            GLib.set_printerr_handler (this._printerr_func_stack_trace);
            Log.set_default_handler (this._log_func_stack_trace);
            this._test_case.set_up ();
        }

        private static void _printerr_func_stack_trace (string? text)
        {
            if (text == null || str_equal (text, ""))
                return;

            stderr.printf (text);

            /* Print a stack trace since we've hit some major issue */
            GLib.on_error_stack_trace ("libtool --mode=execute gdb");
        }

        private void _log_func_stack_trace (string? log_domain, LogLevelFlags log_levels, string message)
        {
            Log.default_handler (log_domain, log_levels, message);

            /* Print a stack trace for any message at the warning level or above */
            if ((log_levels & (LogLevelFlags.LEVEL_WARNING | LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL)) != 0)
            {
                GLib.on_error_stack_trace ("libtool --mode=execute gdb");
            }
        }

        public void run (void* fixture)
        {
            if (this.is_async)
            {
                try
                {
                    assert( wait_for_async (async_timeout, this.async_begin, this.async_finish) );
                }
                catch (GLib.Error err)
                {
                    message(@"Got exception while excuting asynchronous test: $(err.message)");
                    GLib.Test.fail();
                }
            }
            else
            {
                this._test ();
            }
        }

        public void tear_down (void* fixture)
        {
            this._test_case.tear_down ();
        }
    }
}