diff options
| author | Yong He <yonghe@outlook.com> | 2021-09-28 11:54:24 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-28 11:54:24 -0700 |
| commit | cdf1b2c007fefdca128584d2a9f63dec3d350e16 (patch) | |
| tree | 9a7dbe0c78794e6d2d8c215017119ef06e9f8ab6 /tools/unit-test | |
| parent | af788b62e18bbd55cd748ad60400a74cf1bc93ee (diff) | |
Improvements to the unit test framework. (#1948)
Diffstat (limited to 'tools/unit-test')
| -rw-r--r-- | tools/unit-test/slang-unit-test.cpp | 10 | ||||
| -rw-r--r-- | tools/unit-test/slang-unit-test.h | 40 |
2 files changed, 28 insertions, 22 deletions
diff --git a/tools/unit-test/slang-unit-test.cpp b/tools/unit-test/slang-unit-test.cpp index 28eba3a1f..8b57b375b 100644 --- a/tools/unit-test/slang-unit-test.cpp +++ b/tools/unit-test/slang-unit-test.cpp @@ -14,26 +14,26 @@ public: Slang::List<SlangUnitTest> tests; ITestReporter* testReporter = nullptr; - virtual SlangInt getTestCount() override + virtual SLANG_NO_THROW SlangInt SLANG_MCALL getTestCount() override { return tests.getCount(); } - virtual const char* getTestName(SlangInt index) override + virtual SLANG_NO_THROW const char* SLANG_MCALL getTestName(SlangInt index) override { return tests[index].name; } - virtual UnitTestFunc getTestFunc(SlangInt index) override + virtual SLANG_NO_THROW UnitTestFunc SLANG_MCALL getTestFunc(SlangInt index) override { return tests[index].func; } - virtual void setTestReporter(ITestReporter* reporter) override + virtual SLANG_NO_THROW void SLANG_MCALL setTestReporter(ITestReporter* reporter) override { testReporter = reporter; } - virtual void destroy() override + virtual SLANG_NO_THROW void SLANG_MCALL destroy() override { tests = decltype(tests)(); } diff --git a/tools/unit-test/slang-unit-test.h b/tools/unit-test/slang-unit-test.h index 033fab395..9e437f072 100644 --- a/tools/unit-test/slang-unit-test.h +++ b/tools/unit-test/slang-unit-test.h @@ -22,13 +22,13 @@ enum class TestMessageType class ITestReporter { public: - virtual void startTest(const char* testName) = 0; - virtual void addResult(TestResult result) = 0; - virtual void addResultWithLocation(TestResult result, const char* testText, const char* file, int line) = 0; - virtual void addResultWithLocation(bool testSucceeded, const char* testText, const char* file, int line) = 0; - virtual void addExecutionTime(double time) = 0; - virtual void message(TestMessageType type, const char* message) = 0; - virtual void endTest() = 0; + virtual SLANG_NO_THROW void SLANG_MCALL startTest(const char* testName) = 0; + virtual SLANG_NO_THROW void SLANG_MCALL addResult(TestResult result) = 0; + virtual SLANG_NO_THROW void SLANG_MCALL addResultWithLocation(TestResult result, const char* testText, const char* file, int line) = 0; + virtual SLANG_NO_THROW void SLANG_MCALL addResultWithLocation(bool testSucceeded, const char* testText, const char* file, int line) = 0; + virtual SLANG_NO_THROW void SLANG_MCALL addExecutionTime(double time) = 0; + virtual SLANG_NO_THROW void SLANG_MCALL message(TestMessageType type, const char* message) = 0; + virtual SLANG_NO_THROW void SLANG_MCALL endTest() = 0; }; ITestReporter* getTestReporter(); @@ -45,11 +45,11 @@ typedef void (*UnitTestFunc)(UnitTestContext*); class IUnitTestModule { public: - virtual SlangInt getTestCount() = 0; - virtual const char* getTestName(SlangInt index) = 0; - virtual UnitTestFunc getTestFunc(SlangInt index) = 0; - virtual void setTestReporter(ITestReporter* reporter) = 0; - virtual void destroy() = 0; + virtual SLANG_NO_THROW SlangInt SLANG_MCALL getTestCount() = 0; + virtual SLANG_NO_THROW const char* SLANG_MCALL getTestName(SlangInt index) = 0; + virtual SLANG_NO_THROW UnitTestFunc SLANG_MCALL getTestFunc(SlangInt index) = 0; + virtual SLANG_NO_THROW void SLANG_MCALL setTestReporter(ITestReporter* reporter) = 0; + virtual SLANG_NO_THROW void SLANG_MCALL destroy() = 0; }; class UnitTestRegisterHelper @@ -58,18 +58,24 @@ public: UnitTestRegisterHelper(const char* name, UnitTestFunc testFunc); }; +class AbortTestException {}; + typedef IUnitTestModule* (*UnitTestGetModuleFunc)(); #define SLANG_UNIT_TEST(name) \ -void name(UnitTestContext* unitTestContext); \ +void _##name##_impl(UnitTestContext* unitTestContext); \ +void name(UnitTestContext* unitTestContext)\ +{\ + try { _##name##_impl(unitTestContext); } catch (AbortTestException){} \ +}\ UnitTestRegisterHelper _##name##RegisterHelper(#name, name); \ -void name(UnitTestContext* unitTestContext) +void _##name##_impl(UnitTestContext* unitTestContext) #define SLANG_CHECK(x) getTestReporter()->addResultWithLocation((x), #x, __FILE__, __LINE__); #define SLANG_CHECK_ABORT(x) \ { \ bool _slang_check_result = (x); \ - getTestReporter()->addResultWithLocation(_slang_check_result, #x, __FILE__, __LINE__); \ - if (!_slang_check_result) return; \ + getTestReporter()->addResultWithLocation(_slang_check_result, #x, __FILE__, __LINE__); \ + if (!_slang_check_result) throw AbortTestException(); \ } -#define SLANG_IGNORE_TEST getTestReporter()->addResult(TestResult::Ignored); return; +#define SLANG_IGNORE_TEST getTestReporter()->addResult(TestResult::Ignored); throw AbortTestException(); |
