The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

TestBirdFont.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/TestBirdFont.vala.
Select first character when overview tab is created
1 /* 2 Copyright (C) 2012, 2014 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 namespace BirdFont { 16 17 /** Class for executing tests cases. */ 18 class TestBirdFont : GLib.Object { 19 20 const int NOT_STARTED = 0; 21 const int RUNNING = 1; 22 const int PAUSED = 2; 23 const int DONE = 3; 24 25 static int state = NOT_STARTED; 26 27 TestCases tests; 28 29 unowned List<Test> test_cases; 30 unowned List<Test?>? current_case = null; 31 32 List<Test> passed; 33 List<Test> failed; 34 List<Test> skipped; 35 36 List<Test> bechmarks; 37 38 static TestBirdFont? singleton = null; 39 40 bool has_failed = false; 41 bool has_skipped = false; 42 43 static bool slow_test = false; 44 45 public string test_cases_to_run; // name of specific test case or all to run all test cases 46 47 public TestBirdFont () { 48 assert (singleton == null); 49 tests = new TestCases (); 50 test_cases = tests.get_test_functions (); 51 current_case = test_cases.first (); 52 test_cases_to_run = "All"; 53 from_command_line (); 54 } 55 56 public static bool is_slow_test () { 57 return slow_test; 58 } 59 60 public static void set_slow_test (bool s) { 61 slow_test = s; 62 } 63 64 public static void run_tests () { 65 TestBirdFont t = get_singleton (); 66 state = RUNNING; 67 t.run_all_tests (); 68 } 69 70 public static TestBirdFont get_singleton () { 71 if (singleton == null) { 72 singleton = new TestBirdFont (); 73 } 74 75 return (!) singleton; 76 } 77 78 private bool has_test_case (string s) { 79 foreach (var t in test_cases) { 80 if (t.name == s) return true; 81 } 82 83 if (s == "" || s == "All") { 84 print ("No specific tescase given run all test cases.\n"); 85 return true; 86 } 87 88 return false; 89 } 90 91 /** Run only test specified on the command line. */ 92 private void from_command_line () { 93 string? stn = BirdFont.get_argument ("--test"); 94 95 if (stn != null) { 96 string st = (!) stn; 97 98 if (!has_test_case (st)) { 99 stderr.printf (@"Test case \"$st\" does not exist.\n"); 100 stderr.printf ("\nAvaliable test cases:\n"); 101 102 foreach (var t in test_cases) { 103 stderr.printf (t.name); 104 stderr.printf ("\n"); 105 } 106 107 Process.exit(1); 108 } 109 110 if (st == "All" || st == "") { 111 return; 112 } else { 113 stderr.printf (@"Run test case \"$st\" \n"); 114 } 115 116 test_cases_to_run = st; 117 } 118 } 119 120 public static void log (string? log_domain, LogLevelFlags log_levels, string message) { 121 Test t = (!)((!)get_singleton ().current_case).data; 122 123 if (log_domain != null) { 124 stderr.printf ("%s: \n", (!) log_domain); 125 } 126 127 stderr.printf ("Testcase \"%s\" failed because:\n", t.name); 128 stderr.printf ("%s\n\n", message); 129 130 get_singleton ().has_failed = true; 131 132 assert (!BirdFont.fatal_wanings); 133 } 134 135 public static void @continue () { 136 int s = AtomicInt.get (ref state); 137 if (s == DONE) { 138 singleton = null; 139 } 140 141 TestBirdFont t = get_singleton (); 142 143 LogLevelFlags levels = LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL | LogLevelFlags.LEVEL_WARNING; 144 Log.set_handler (null, levels, log); 145 146 AtomicInt.set (ref state, RUNNING); 147 t.run_all_tests (); 148 } 149 150 public static bool is_running () { 151 int r = AtomicInt.get (ref state); 152 return (r == RUNNING); 153 } 154 155 public static void pause () { 156 AtomicInt.set (ref state, PAUSED); 157 Log.set_handler (null, LogLevelFlags.LEVEL_MASK, Log.default_handler); 158 } 159 160 private static void pad (int t) { 161 for (int i = 0; i < t; i++) { 162 stdout.printf (" "); 163 } 164 } 165 166 public void print_result () { 167 stdout.printf ("\n"); 168 stdout.printf ("Test case results:\n"); 169 170 foreach (Test t in skipped) { 171 stdout.printf ("%s", t.name); 172 pad (40 - t.name.char_count()); 173 stdout.printf ("Skipped\n"); 174 } 175 176 if (skipped.length () > 0) { 177 stdout.printf ("\n"); 178 } 179 180 foreach (Test t in passed) { 181 stdout.printf ("%s", t.name); 182 pad (40 - t.name.char_count()); 183 stdout.printf ("Passed\n"); 184 } 185 186 foreach (Test t in failed) { 187 stdout.printf ("%s", t.name); 188 pad (40 - t.name.char_count()); 189 stdout.printf ("Failed\n"); 190 } 191 192 foreach (Test t in bechmarks) { 193 stdout.printf ("%s", t.name); 194 pad (40 - t.name.char_count()); 195 stdout.printf (@"$(t.get_time ())s\n"); 196 } 197 198 stdout.printf ("\n"); 199 200 stdout.printf ("Total %u test cases executed, %u passed and %u failed.\n", (passed.length () + failed.length ()), passed.length (), failed.length ()); 201 } 202 203 /** Run tests in main loop. */ 204 public void run_all_tests () { 205 TimeoutSource idle = new TimeoutSource (20); 206 207 idle.set_callback(() => { 208 int s = AtomicInt.get (ref state); 209 210 if (s != RUNNING || current_case == null) { 211 return false; 212 } 213 214 Test test = (!)((!) current_case).data; 215 216 has_failed = false; 217 has_skipped = false; 218 219 if (test_cases_to_run != "All" && test_cases_to_run != test.name) { 220 has_skipped = true; 221 } else { 222 if (test.is_benchmark ()) { 223 test.timer_start (); 224 test.callback (); 225 test.print (); 226 } else { 227 test.callback (); 228 } 229 } 230 231 if (test.is_benchmark ()) { 232 bechmarks.append ((!) test); 233 } else if (has_failed) { 234 failed.append ((!) test); 235 236 if (BirdFont.has_argument ("--exit")) { 237 print_result (); 238 Process.exit (1); 239 } 240 241 } else if (has_skipped) { 242 skipped.append ((!) test); 243 } else { 244 passed.append ((!) test); 245 } 246 247 if (unlikely (current_case == test_cases.last ())) { 248 stdout.printf ("Finished running test suite.\n"); 249 250 AtomicInt.set (ref state, DONE); 251 Log.set_handler (null, LogLevelFlags.LEVEL_MASK, Log.default_handler); 252 253 print_result (); 254 255 if (BirdFont.has_argument ("--exit")) { 256 print_result (); 257 Process.exit ((failed.length () == 0) ? 0 : 1); 258 } 259 return false; 260 } 261 262 current_case = ((!) current_case).next; 263 264 return true; 265 }); 266 267 idle.attach (null); 268 } 269 270 } 271 272 } 273