diff options
| author | Yong He <yonghe@outlook.com> | 2021-09-24 11:33:44 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-24 11:33:44 -0700 |
| commit | bec8e6aec85b6e3f875c58bdd59eb15613978358 (patch) | |
| tree | 0791fb2ce1be786c17e5a6ee489ed3065fc07332 /tools/unit-test | |
| parent | f2a3c933bc11a498c622fa18694c84beca8ca031 (diff) | |
Move existing unit tests to a standalone dll. (#1945)
Diffstat (limited to 'tools/unit-test')
| -rw-r--r-- | tools/unit-test/slang-unit-test.cpp | 27 | ||||
| -rw-r--r-- | tools/unit-test/slang-unit-test.h | 97 |
2 files changed, 89 insertions, 35 deletions
diff --git a/tools/unit-test/slang-unit-test.cpp b/tools/unit-test/slang-unit-test.cpp index a31614c05..28eba3a1f 100644 --- a/tools/unit-test/slang-unit-test.cpp +++ b/tools/unit-test/slang-unit-test.cpp @@ -5,13 +5,14 @@ struct SlangUnitTest { const char* name; - slang::UnitTestFunc func; + UnitTestFunc func; }; -class SlangUnitTestModule : public slang::IUnitTestModule +class SlangUnitTestModule : public IUnitTestModule { public: Slang::List<SlangUnitTest> tests; + ITestReporter* testReporter = nullptr; virtual SlangInt getTestCount() override { @@ -22,10 +23,21 @@ public: return tests[index].name; } - virtual slang::UnitTestFunc getTestFunc(SlangInt index) override + virtual UnitTestFunc getTestFunc(SlangInt index) override { return tests[index].func; } + + virtual void setTestReporter(ITestReporter* reporter) override + { + testReporter = reporter; + } + + virtual void destroy() override + { + tests = decltype(tests)(); + } + }; SlangUnitTestModule* _getTestModule() @@ -34,15 +46,20 @@ SlangUnitTestModule* _getTestModule() return &testModule; } +ITestReporter* getTestReporter() +{ + return _getTestModule()->testReporter; +} + extern "C" { -SLANG_DLL_EXPORT slang::IUnitTestModule* slangUnitTestGetModule() +SLANG_DLL_EXPORT IUnitTestModule* slangUnitTestGetModule() { return _getTestModule(); } } -slang::UnitTestRegisterHelper::UnitTestRegisterHelper(const char* name, UnitTestFunc testFunc) +UnitTestRegisterHelper::UnitTestRegisterHelper(const char* name, UnitTestFunc testFunc) { _getTestModule()->tests.add(SlangUnitTest{ name, testFunc }); } diff --git a/tools/unit-test/slang-unit-test.h b/tools/unit-test/slang-unit-test.h index 7651e6b46..033fab395 100644 --- a/tools/unit-test/slang-unit-test.h +++ b/tools/unit-test/slang-unit-test.h @@ -3,36 +3,73 @@ #include "slang.h" #include "source/core/slang-render-api-util.h" -namespace slang +enum class TestResult { - struct UnitTestContext - { - slang::IGlobalSession* slangGlobalSession; - const char* workDirectory; - ISlangWriter* outputWriter; - Slang::RenderApiFlags enabledApis; - }; - - typedef SlangResult (*UnitTestFunc)(UnitTestContext*); - - class IUnitTestModule - { - public: - virtual SlangInt getTestCount() = 0; - virtual const char* getTestName(SlangInt index) = 0; - virtual UnitTestFunc getTestFunc(SlangInt index) = 0; - }; - - class UnitTestRegisterHelper - { - public: - UnitTestRegisterHelper(const char* name, UnitTestFunc testFunc); - }; - - typedef slang::IUnitTestModule* (*UnitTestGetModuleFunc)(); + // NOTE! Must keep in order such that combine is meaningful. That is larger values are higher precident - and a series of tests that has lots of passes + // and a fail, is still a fail overall. + Ignored, + Pass, + Fail, +}; + +enum class TestMessageType +{ + Info, ///< General info (may not be shown depending on verbosity setting) + TestFailure, ///< Describes how a test failure took place + RunError, ///< Describes an error that caused a test not to actually correctly run +}; + +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; +}; + +ITestReporter* getTestReporter(); + +struct UnitTestContext +{ + slang::IGlobalSession* slangGlobalSession; + const char* workDirectory; + Slang::RenderApiFlags enabledApis; +}; + +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; +}; + +class UnitTestRegisterHelper +{ +public: + UnitTestRegisterHelper(const char* name, UnitTestFunc testFunc); +}; + +typedef IUnitTestModule* (*UnitTestGetModuleFunc)(); #define SLANG_UNIT_TEST(name) \ - SlangResult name(slang::UnitTestContext* context); \ - slang::UnitTestRegisterHelper _##name##RegisterHelper(#name, name); \ - SlangResult name(slang::UnitTestContext* context) -} +void name(UnitTestContext* unitTestContext); \ +UnitTestRegisterHelper _##name##RegisterHelper(#name, name); \ +void name(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; \ + } +#define SLANG_IGNORE_TEST getTestReporter()->addResult(TestResult::Ignored); return; |
