summaryrefslogtreecommitdiffstats
path: root/tools/unit-test/slang-unit-test.cpp
blob: c5a50613b399b1ffe928243b8ce9fb99865c195d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "slang-unit-test.h"

#include "core/slang-basic.h"
#include "slang.h"

struct SlangUnitTest
{
    const char* name;
    UnitTestFunc func;
};

class SlangUnitTestModule : public IUnitTestModule
{
public:
    Slang::List<SlangUnitTest> tests;
    ITestReporter* testReporter = nullptr;

    virtual SLANG_NO_THROW SlangInt SLANG_MCALL getTestCount() override { return tests.getCount(); }
    virtual SLANG_NO_THROW const char* SLANG_MCALL getTestName(SlangInt index) override
    {
        return tests[index].name;
    }

    virtual SLANG_NO_THROW UnitTestFunc SLANG_MCALL getTestFunc(SlangInt index) override
    {
        return tests[index].func;
    }

    virtual SLANG_NO_THROW void SLANG_MCALL setTestReporter(ITestReporter* reporter) override
    {
        testReporter = reporter;
    }

    virtual SLANG_NO_THROW void SLANG_MCALL destroy() override { tests = decltype(tests)(); }
};

SlangUnitTestModule* _getTestModule()
{
    static SlangUnitTestModule testModule;
    return &testModule;
}

ITestReporter* getTestReporter()
{
    return _getTestModule()->testReporter;
}

extern "C"
{
    SLANG_DLL_EXPORT IUnitTestModule* slangUnitTestGetModule()
    {
        return _getTestModule();
    }
}

UnitTestRegisterHelper::UnitTestRegisterHelper(const char* name, UnitTestFunc testFunc)
{
    _getTestModule()->tests.add(SlangUnitTest{name, testFunc});
}