aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/unittest/junit.h
blob: f5113c290edc4d2d6836862aeb079e92c51fded3 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "registar.h"

namespace NUnitTest {

class TJUnitProcessor : public ITestSuiteProcessor {
    struct TTestCase {
        TString Name;
        bool Success;
        TVector<TString> Failures;

        size_t GetFailuresCount() const {
            return Failures.size();
        }
    };

    struct TTestSuite {
        TMap<TString, TTestCase> Cases;

        size_t GetTestsCount() const {
            return Cases.size();
        }

        size_t GetFailuresCount() const {
            size_t sum = 0;
            for (const auto& [name, testCase] : Cases) {
                sum += testCase.GetFailuresCount();
            }
            return sum;
        }
    };

public:
    TJUnitProcessor(TString file, TString exec)
        : FileName(file)
        , ExecName(exec)
    {
    }

    ~TJUnitProcessor() {
        Save();
    }

    void OnError(const TError* descr) override {
        auto* testCase = GetTestCase(descr->test);
        testCase->Failures.emplace_back(descr->msg);
    }

    void OnFinish(const TFinish* descr) override {
        GetTestCase(descr->test)->Success = descr->Success;
    }

private:
    TTestCase* GetTestCase(const TTest* test) {
        auto& suite = Suites[test->unit->name];
        return &suite.Cases[test->name];
    }

    void Save();

    size_t GetTestsCount() const {
        size_t sum = 0;
        for (const auto& [name, suite] : Suites) {
            sum += suite.GetTestsCount();
        }
        return sum;
    }

    size_t GetFailuresCount() const {
        size_t sum = 0;
        for (const auto& [name, suite] : Suites) {
            sum += suite.GetFailuresCount();
        }
        return sum;
    }

private:
    TString FileName;
    TString ExecName;
    TMap<TString, TTestSuite> Suites;
};

} // namespace NUnitTest