File: test-case.vala

package info (click to toggle)
libkkc 0.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,928 kB
  • ctags: 15,126
  • sloc: ansic: 76,279; sh: 11,501; makefile: 918; cpp: 435; python: 233
file content (118 lines) | stat: -rw-r--r-- 3,292 bytes parent folder | download | duplicates (5)
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
/* testcase.vala
 *
 * Copyright (C) 2009 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 Kkc.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));
    }

  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; }
      private unowned TestMethod _test;
      private TestCase _test_case;

      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 (Adaptor._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)
        {
          this._test ();
        }

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