summaryrefslogtreecommitdiff
path: root/external/bsd/kyua-cli/dist/engine/test_result.cpp
blob: 342eff86eee73f6b4faa4ed5f2890b7648e5b0e8 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2010 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "engine/test_result.hpp"

#include "engine/exceptions.hpp"
#include "utils/format/macros.hpp"
#include "utils/sanity.hpp"
#include "utils/text/operations.ipp"

namespace text = utils::text;


/// Constructs a base result.
///
/// \param type_ The type of the result.
/// \param reason_ The reason explaining the result, if any.  It is OK for this
///     to be empty, which is actually the default.
engine::test_result::test_result(const result_type type_,
                                 const std::string& reason_) :
    _type(type_),
    _reason(reason_)
{
}


/// Parses a result from an input stream.
///
/// The parsing of a results file is quite permissive in terms of file syntax
/// validation.  We accept result files with or without trailing new lines, and
/// with descriptions that may span multiple lines.  This is to avoid getting in
/// trouble when the result is generated from user code, in which case it is
/// hard to predict how newlines look like.  Just swallow them; it's better for
/// the consumer.
///
/// \param input The stream from which to read the result.
///
/// \return The parsed result.  If there is any problem during parsing, the
/// failure is encoded as a broken result.
engine::test_result
engine::test_result::parse(std::istream& input)
{
    std::string line;
    if (!std::getline(input, line).good() && line.empty())
        return test_result(broken, "Empty result file");

    // Fast-path for the most common case.
    if (line == "passed")
        return test_result(passed);

    std::string type, reason;
    const std::string::size_type pos = line.find(": ");
    if (pos == std::string::npos) {
        type = line;
        reason = "";
    } else {
        type = line.substr(0, pos);
        reason = line.substr(pos + 2);
    }

    if (input.good()) {
        line.clear();
        while (std::getline(input, line).good() && !line.empty()) {
            reason += "<<NEWLINE>>" + line;
            line.clear();
        }
        if (!line.empty())
            reason += "<<NEWLINE>>" + line;
    }

    if (type == "broken") {
        return test_result(broken, reason);
    } else if (type == "expected_failure") {
        return test_result(expected_failure, reason);
    } else if (type == "failed") {
        return test_result(failed, reason);
    } else if (type == "passed") {
        return test_result(passed, reason);
    } else if (type == "skipped") {
        return test_result(skipped, reason);
    } else {
        return test_result(broken, F("Unknown result type '%s'") % type);
    }
}


/// Returns the type of the result.
///
/// \return A result type.
engine::test_result::result_type
engine::test_result::type(void) const
{
    return _type;
}


/// Returns the reason explaining the result.
///
/// \return A textual reason, possibly empty.
const std::string&
engine::test_result::reason(void) const
{
    return _reason;
}


/// True if the test case result has a positive connotation.
///
/// \return Whether the test case is good or not.
bool
engine::test_result::good(void) const
{
    switch (_type) {
    case expected_failure:
    case passed:
    case skipped:
        return true;

    case broken:
    case failed:
        return false;
    }
    UNREACHABLE;
}


/// Equality comparator.
///
/// \param other The test result to compare to.
///
/// \return True if the other object is equal to this one, false otherwise.
bool
engine::test_result::operator==(const test_result& other) const
{
    return _type == other._type && _reason == other._reason;
}


/// Inequality comparator.
///
/// \param other The test result to compare to.
///
/// \return True if the other object is different from this one, false
/// otherwise.
bool
engine::test_result::operator!=(const test_result& other) const
{
    return !(*this == other);
}


/// Injects the object into a stream.
///
/// \param output The stream into which to inject the object.
/// \param object The object to format.
///
/// \return The output stream.
std::ostream&
engine::operator<<(std::ostream& output, const test_result& object)
{
    std::string result_name;
    switch (object.type()) {
    case test_result::broken: result_name = "broken"; break;
    case test_result::expected_failure: result_name = "expected_failure"; break;
    case test_result::failed: result_name = "failed"; break;
    case test_result::passed: result_name = "passed"; break;
    case test_result::skipped: result_name = "skipped"; break;
    }
    const std::string& reason = object.reason();
    if (reason.empty()) {
        output << F("test_result{type=%s}") % text::quote(result_name, '\'');
    } else {
        output << F("test_result{type=%s, reason=%s}")
            % text::quote(result_name, '\'') % text::quote(reason, '\'');
    }
    return output;
}