yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Jay KwakImprove slang-test output verbosity control (#7625)a68e2635c

master
5.0 KiB165 linesraw
1// test-reporter.h
2
3#ifndef TEST_REPORTER_H_INCLUDED
4#define TEST_REPORTER_H_INCLUDED
5
6#include "../../source/core/slang-dictionary.h"
7#include "../../source/core/slang-platform.h"
8#include "../../source/core/slang-std-writers.h"
9#include "../../source/core/slang-string-util.h"
10#include "unit-test/slang-unit-test.h"
11
12#include <mutex>
13
14// Forward declaration
15enum class VerbosityLevel;
16
17enum class TestOutputMode
18{
19    Default = 0, ///< Default mode is to write test results to the console
20    AppVeyor,    ///< For AppVeyor continuous integration
21    Travis,      ///< We currently don't specialize for Travis, but maybe we should.
22    XUnit,    ///< xUnit original format  https://nose.readthedocs.io/en/latest/plugins/xunit.html
23    XUnit2,   ///< https://xunit.github.io/docs/format-xml-v2
24    TeamCity, ///< Output suitable for teamcity
25};
26
27class TestReporter : public ITestReporter
28{
29public:
30    struct TestInfo
31    {
32        TestResult testResult = TestResult::Ignored;
33        Slang::String name;
34        Slang::String message;      ///< Message that is specific for the testResult
35        double executionTime = 0.0; ///< <= 0.0 if not defined. Time is in seconds.
36    };
37
38    class TestScope
39    {
40    public:
41        TestScope(TestReporter* reporter, const Slang::String& testName)
42            : m_reporter(reporter)
43        {
44            reporter->startTest(testName.getBuffer());
45        }
46        ~TestScope() { m_reporter->endTest(); }
47
48    protected:
49        TestReporter* m_reporter;
50    };
51
52    class SuiteScope
53    {
54    public:
55        SuiteScope(TestReporter* reporter, const Slang::String& suiteName)
56            : m_reporter(reporter)
57        {
58            reporter->startSuite(suiteName);
59        }
60        ~SuiteScope() { m_reporter->endSuite(); }
61
62    protected:
63        TestReporter* m_reporter;
64    };
65
66    void startSuite(const Slang::String& name);
67    void endSuite();
68
69    TestResult adjustResult(Slang::UnownedStringSlice testName, TestResult result);
70
71    virtual SLANG_NO_THROW void SLANG_MCALL startTest(const char* testName) override;
72    virtual SLANG_NO_THROW void SLANG_MCALL addResult(TestResult result) override;
73    virtual SLANG_NO_THROW void SLANG_MCALL addResultWithLocation(
74        TestResult result,
75        const char* testText,
76        const char* file,
77        int line) override;
78    virtual SLANG_NO_THROW void SLANG_MCALL addResultWithLocation(
79        bool testSucceeded,
80        const char* testText,
81        const char* file,
82        int line) override;
83    virtual SLANG_NO_THROW void SLANG_MCALL addExecutionTime(double time) override;
84    virtual SLANG_NO_THROW void SLANG_MCALL endTest() override;
85
86    /// Runs start/endTest and outputs the result
87    TestResult addTest(const Slang::String& testName, bool isPass);
88    /// Effectively runs start/endTest (so cannot be called inside start/endTest).
89    void addTest(const Slang::String& testName, TestResult testResult);
90
91    // Called for an error in the test-runner (not for an error involving a test itself).
92    void message(TestMessageType type, const Slang::String& errorText);
93    SLANG_ATTR_PRINTF(3, 4)
94    void messageFormat(TestMessageType type, char const* message, ...);
95    virtual SLANG_NO_THROW void SLANG_MCALL
96    message(TestMessageType type, char const* message) override;
97
98    void dumpOutputDifference(
99        const Slang::String& expectedOutput,
100        const Slang::String& actualOutput);
101
102    void consolidateWith(TestReporter* other);
103
104    /// True if can write output directly to stderr
105    bool canWriteStdError() const;
106
107
108    /// Returns true if all run tests succeeded
109    bool didAllSucceed() const;
110
111    /// Returns a result from the current test
112    TestResult getResult() const;
113
114    void outputSummary();
115
116    SlangResult init(
117        TestOutputMode outputMode,
118        const Slang::HashSet<Slang::String>& expectedFailureList,
119        bool isSubReporter = false);
120
121    /// Ctor
122    TestReporter();
123    /// Dtor
124    ~TestReporter();
125
126    static TestResult combine(TestResult a, TestResult b) { return (a > b) ? a : b; }
127
128    static TestReporter* get() { return s_reporter; }
129    static void set(TestReporter* reporter) { s_reporter = reporter; }
130
131    Slang::List<TestInfo> m_testInfos;
132
133    Slang::List<Slang::String> m_suiteStack;
134
135    int m_totalTestCount;
136    int m_passedTestCount;
137    int m_failedTestCount;
138    int m_ignoredTestCount;
139    int m_expectedFailedTestCount;
140
141    int m_maxFailTestResults; ///< Maximum amount of results per test. If 0 it's infinite.
142
143    TestOutputMode m_outputMode = TestOutputMode::Default;
144    bool m_dumpOutputOnFailure;
145    VerbosityLevel m_verbosity;
146    bool m_hideIgnored = false;
147    bool m_isSubReporter = false;
148    Slang::HashSet<Slang::String> m_expectedFailureList;
149
150protected:
151    void _addResult(TestInfo info);
152
153    Slang::StringBuilder m_currentMessage;
154    TestInfo m_currentInfo;
155    int m_numCurrentResults;
156    int m_numFailResults;
157
158    bool m_inTest;
159
160    std::recursive_mutex m_mutex;
161
162    static TestReporter* s_reporter;
163};
164
165#endif // TEST_REPORTER_H_INCLUDED