Debian Codegraph @

test/unit/expect.h

raw symbols SCIP
1 /*
2 * %injeqt copyright begin%
3 * Copyright 2014 Rafał Malinowski ([email protected])
4 * %injeqt copyright end%
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #pragma once
22
23 #include <QtTest/QTest>
24 #include <functional>
25
26 template<typename E>
27 void expect(const std::vector<std::string> &contents, std::function<void()> call)
28 {
29 try
30 {
31 call();
32 QFAIL("Exception not thrown");
33 }
34 catch (E &e)
35 {
36 for (auto &&content : contents)
37 if (std::string(e.what()).find(content) == std::string::npos)
38 QFAIL(qPrintable(QString{"Expected string '%1' not found in exception message '%2'"}
39 .arg(content.c_str())
40 .arg(e.what())));
41 }
42 catch (std::exception &e)
43 {
44 QFAIL(qPrintable(QString{"Unexpected exception thrown: %1 %2"}
45 .arg(typeid(e).name())
46 .arg(e.what())));
47 }
48 catch (...)
49 {
50 QFAIL("Unexpected exception thrown");
51 }
52 }
53
54 template<typename E>
55 void expect(std::function<void()> call)
56 {
57 expect<E>({}, call);
58 }