summaryrefslogtreecommitdiffstats
path: root/tools/unit-test/slang-unit-test.cpp
blob: 8b57b375b48ca6a0cec360d09bea979c39a559c7 (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
60
61
62
63
64
65
#include "slang-unit-test.h"
#include "slang.h"
#include "source/core/slang-basic.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 });
}