Mercurial > hg > svcore
comparison test/TestHelper.h @ 1430:b5283878cca2 streaming-csv-writer
Introduce a TestHelper which contains boiler plate for running a suite of QtTest style objects. Stub CSVStreamWriter and test to integrate into build.
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Tue, 17 Apr 2018 10:03:49 +0100 |
parents | |
children | 7decc74b0fd0 |
comparison
equal
deleted
inserted
replaced
1425:bd73a689c8af | 1430:b5283878cca2 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Sonic Visualiser | |
5 An audio file viewer and annotation editor. | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2017 Lucas Thompson. | |
8 | |
9 This program is free software; you can redistribute it and/or | |
10 modify it under the terms of the GNU General Public License as | |
11 published by the Free Software Foundation; either version 2 of the | |
12 License, or (at your option) any later version. See the file | |
13 COPYING included with this distribution for more information. | |
14 */ | |
15 | |
16 #ifndef _TEST_HELPER_H_ | |
17 #define _TEST_HELPER_H_ | |
18 | |
19 #include <initializer_list> | |
20 #include <memory> | |
21 #include <iostream> | |
22 #include <functional> | |
23 | |
24 #include <QtTest> | |
25 | |
26 namespace Test | |
27 { | |
28 | |
29 template <class T> | |
30 using Factory = std::function<std::unique_ptr<T>()>; | |
31 | |
32 template <class T, typename... Args> | |
33 auto createFactory(Args... FArgs) -> Factory<T> | |
34 { | |
35 return [&]() { return std::unique_ptr<T> { new T {FArgs...} }; }; | |
36 } | |
37 | |
38 using TestStatus = int; | |
39 | |
40 auto startTestRunner( | |
41 std::initializer_list<Factory<QObject>> tests, | |
42 int argc, | |
43 char *argv[], | |
44 QString testName, | |
45 QString orgName = "sonic-visualiser" | |
46 ) -> TestStatus | |
47 { | |
48 int good = 0, bad = 0; | |
49 | |
50 QCoreApplication app(argc, argv); | |
51 app.setOrganizationName(orgName); | |
52 app.setApplicationName(testName); | |
53 auto executeTest = [&](std::unique_ptr<QObject> t) { | |
54 if (QTest::qExec(t.get(), argc, argv) == 0) ++good; | |
55 else ++bad; | |
56 }; | |
57 | |
58 for (const auto& test : tests) { | |
59 executeTest(test()); | |
60 } | |
61 | |
62 if (bad > 0) { | |
63 cerr << "\n********* " << bad << " test suite(s) failed!\n" << endl; | |
64 return 1; | |
65 } else { | |
66 cerr << "All tests passed" << endl; | |
67 return 0; | |
68 } | |
69 } | |
70 | |
71 } // namespace | |
72 | |
73 #endif |