summaryrefslogtreecommitdiff
path: root/vendor/bandit/bandit/reporters/xunit_reporter.h
blob: 15f6ea299df31441dc77fe27b74c9792cdb80381 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef BANDIT_REPORTERS_XUNIT_REPORTER_H
#define BANDIT_REPORTERS_XUNIT_REPORTER_H

namespace bandit { namespace detail {

  struct xunit_reporter : public progress_reporter
  {
    xunit_reporter(std::ostream& stm, const failure_formatter& formatter)
      : progress_reporter(formatter), stm_(stm)
    {
    }

    xunit_reporter(const failure_formatter& formatter)
      : progress_reporter(formatter), stm_(std::cout)
    {
    }

    void it_starting(const char* desc)
    {
      progress_reporter::it_starting(desc);
      work_stm_ << "\t<testcase classname=\"" << escape(current_context_name()) << "\" ";
      work_stm_ << "name=\"" << escape(desc) << "\" time=\"0\">\n";
    }

    void it_succeeded(const char* desc)
    {
      progress_reporter::it_succeeded(desc);
      work_stm_ << "\t</testcase>\n";
    }

    void it_failed(const char* desc, const assertion_exception& ex)
    {
      progress_reporter::it_failed(desc, ex);
      work_stm_ << "\t\t<failure message=\"" << escape(failure_formatter_.format(ex)) << "\" />\n";
      work_stm_ << "\t</testcase>\n";
    }

    void it_unknown_error(const char* desc)
    {
      progress_reporter::it_unknown_error(desc);
      work_stm_ << "\t\t<failure message=\"Unknown exception\" />\n";
      work_stm_ << "\t</testcase>\n";
    }

    void it_skip(const char* desc)
    {
      progress_reporter::it_skip(desc);
      work_stm_ << "\t<testcase classname=\"" << escape(current_context_name()) << "\" ";
      work_stm_ << "name=\"" << escape(desc) << "\" time=\"0\">\n";
      work_stm_ << "\t\t<skipped />\n";
      work_stm_ << "\t</testcase>\n";
    }

    void test_run_complete()
    {
      stm_ << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
      stm_ << "<testsuite name=\"bandit\" tests=\"" << specs_run_ << "\" errors=\"0\" failures=\"" 
           << specs_failed_ << "\"";
      
      if(specs_skipped_ > 0)
      {
        stm_ << " skipped=\"" << specs_skipped_ << "\"";
      }
      
      stm_ << ">\n";

      stm_ << work_stm_.str();

      stm_ << "</testsuite>\n";
    }

    private:
    std::string escape(const std::string& str)
    {
      std::stringstream stm;

      std::for_each(str.begin(), str.end(), [&](char c){
            switch(c)
            {
              case '&':
                stm << "&amp;";
                break;
              case '<':
                stm << "&lt;";
                break;
              case '>':
                stm << "&gt;";
                break;
              case '\\':
                stm << "&apos;";
                break;
              case '\"':
                stm << "&quot;";
                break;
              default:
                stm << c;
            }
          });

      return stm.str();
    }

    private:
    std::ostream& stm_;
    std::stringstream work_stm_;
  };
}}

#endif