5 #ifndef vstgui_unittests_h 6 #define vstgui_unittests_h 10 #include "../../lib/vstguifwd.h" 61 class error :
public std::logic_error
64 error (
const char* str) : std::logic_error (str) {}
67 #define VSTGUI_UNITTEST_MAKE_STRING_PRIVATE_DONT_USE(x) # x 68 #define VSTGUI_UNITTEST_MAKE_STRING(x) VSTGUI_UNITTEST_MAKE_STRING_PRIVATE_DONT_USE(x) 71 #define TESTCASE(name,function) static VSTGUI::UnitTest::TestCaseRegistrar name##TestCaseRegistrar (VSTGUI_UNITTEST_MAKE_STRING(name), [](VSTGUI::UnitTest::TestCase* testCase) { function }) 72 #define TEST(name,function) testCase->registerTest (VSTGUI_UNITTEST_MAKE_STRING(name), [](VSTGUI::UnitTest::Context* context) { { function } return true; }); 73 #define EXPECT(condition) if (!(condition)) { throw VSTGUI::UnitTest::error (__FILE__ ":" VSTGUI_UNITTEST_MAKE_STRING(__LINE__) ": Expected: " VSTGUI_UNITTEST_MAKE_STRING(condition)); } 74 #define FAIL(reason) { context->print (__FILE__ ":" VSTGUI_UNITTEST_MAKE_STRING(__LINE__) ": Failure: " reason); return false; } 76 #define EXPECT_EXCEPTION(call, name) \ 81 } catch (const std::exception& error) {\ 82 EXPECT(error.what() == std::string(name));\ 88 #define SETUP(function) testCase->setSetupFunction ([](VSTGUI::UnitTest::Context* context) { function } ) 89 #define TEARDOWN(function) testCase->setTeardownFunction ([](VSTGUI::UnitTest::Context* context) { function } ) 95 using TestFunction = std::function<bool(Context*)>;
96 using SetupFunction = std::function<void(Context*)>;
97 using TeardownFunction = std::function<void(Context*)>;
98 using TestCaseFunction = std::function<void(TestCase*)>;
101 class UnitTestRegistry
103 using TestCases = std::list<TestCase>;
104 using Iterator = TestCases::const_iterator;
106 static UnitTestRegistry& instance ();
108 void registerTestCase (TestCase&& testCase);
110 Iterator begin ()
const {
return testCases.begin (); }
111 Iterator end ()
const {
return testCases.end (); }
119 using TestPair = std::pair<std::string, TestFunction>;
120 using Tests = std::list<TestPair>;
121 using Iterator = Tests::const_iterator;
123 TestCase (std::string&& name, TestCaseFunction&& testCase);
124 TestCase (TestCase&& tc) noexcept;
126 void setSetupFunction (SetupFunction&& setupFunction);
127 void setTeardownFunction (TeardownFunction&& teardownFunction);
128 void registerTest (std::string&& name, TestFunction&&
function);
130 const std::string& getName ()
const {
return name; }
132 Iterator begin ()
const {
return tests.begin (); }
133 Iterator end ()
const {
return tests.end (); }
135 const SetupFunction& setup ()
const {
return setupFunction; }
136 const TeardownFunction& teardown ()
const {
return teardownFunction; }
138 TestCase& operator= (TestCase&& tc) noexcept;
142 TestCaseFunction tcf;
143 SetupFunction setupFunction;
144 TeardownFunction teardownFunction;
148 class TestCaseRegistrar
151 TestCaseRegistrar (std::string&& name, TestCaseFunction&& testCase)
153 UnitTestRegistry::instance().registerTestCase (TestCase (std::move (name), std::move (testCase)));
161 void print (
const char* fmt, ...);
162 virtual void printRaw (
const char* str) = 0;
169 #define TESTCASE(x,y) Context
Definition: iasync.h:22
Definition: customcontrols.cpp:8