1 1.1 jmmv // Copyright 2010 Google Inc. 2 1.1 jmmv // All rights reserved. 3 1.1 jmmv // 4 1.1 jmmv // Redistribution and use in source and binary forms, with or without 5 1.1 jmmv // modification, are permitted provided that the following conditions are 6 1.1 jmmv // met: 7 1.1 jmmv // 8 1.1 jmmv // * Redistributions of source code must retain the above copyright 9 1.1 jmmv // notice, this list of conditions and the following disclaimer. 10 1.1 jmmv // * Redistributions in binary form must reproduce the above copyright 11 1.1 jmmv // notice, this list of conditions and the following disclaimer in the 12 1.1 jmmv // documentation and/or other materials provided with the distribution. 13 1.1 jmmv // * Neither the name of Google Inc. nor the names of its contributors 14 1.1 jmmv // may be used to endorse or promote products derived from this software 15 1.1 jmmv // without specific prior written permission. 16 1.1 jmmv // 17 1.1 jmmv // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 1.1 jmmv // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 1.1 jmmv // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 1.1 jmmv // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 1.1 jmmv // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 1.1 jmmv // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 1.1 jmmv // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 1.1 jmmv // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 1.1 jmmv // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 1.1 jmmv // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 1.1 jmmv // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 1.1 jmmv 29 1.1 jmmv #include "engine/test_result.hpp" 30 1.1 jmmv 31 1.1 jmmv #include "engine/exceptions.hpp" 32 1.1 jmmv #include "utils/format/macros.hpp" 33 1.1 jmmv #include "utils/sanity.hpp" 34 1.1 jmmv #include "utils/text/operations.ipp" 35 1.1 jmmv 36 1.1 jmmv namespace text = utils::text; 37 1.1 jmmv 38 1.1 jmmv 39 1.1 jmmv /// Constructs a base result. 40 1.1 jmmv /// 41 1.1 jmmv /// \param type_ The type of the result. 42 1.1 jmmv /// \param reason_ The reason explaining the result, if any. It is OK for this 43 1.1 jmmv /// to be empty, which is actually the default. 44 1.1 jmmv engine::test_result::test_result(const result_type type_, 45 1.1 jmmv const std::string& reason_) : 46 1.1 jmmv _type(type_), 47 1.1 jmmv _reason(reason_) 48 1.1 jmmv { 49 1.1 jmmv } 50 1.1 jmmv 51 1.1 jmmv 52 1.1 jmmv /// Parses a result from an input stream. 53 1.1 jmmv /// 54 1.1 jmmv /// The parsing of a results file is quite permissive in terms of file syntax 55 1.1 jmmv /// validation. We accept result files with or without trailing new lines, and 56 1.1 jmmv /// with descriptions that may span multiple lines. This is to avoid getting in 57 1.1 jmmv /// trouble when the result is generated from user code, in which case it is 58 1.1 jmmv /// hard to predict how newlines look like. Just swallow them; it's better for 59 1.1 jmmv /// the consumer. 60 1.1 jmmv /// 61 1.1 jmmv /// \param input The stream from which to read the result. 62 1.1 jmmv /// 63 1.1 jmmv /// \return The parsed result. If there is any problem during parsing, the 64 1.1 jmmv /// failure is encoded as a broken result. 65 1.1 jmmv engine::test_result 66 1.1 jmmv engine::test_result::parse(std::istream& input) 67 1.1 jmmv { 68 1.1 jmmv std::string line; 69 1.1 jmmv if (!std::getline(input, line).good() && line.empty()) 70 1.1 jmmv return test_result(broken, "Empty result file"); 71 1.1 jmmv 72 1.1 jmmv // Fast-path for the most common case. 73 1.1 jmmv if (line == "passed") 74 1.1 jmmv return test_result(passed); 75 1.1 jmmv 76 1.1 jmmv std::string type, reason; 77 1.1 jmmv const std::string::size_type pos = line.find(": "); 78 1.1 jmmv if (pos == std::string::npos) { 79 1.1 jmmv type = line; 80 1.1 jmmv reason = ""; 81 1.1 jmmv } else { 82 1.1 jmmv type = line.substr(0, pos); 83 1.1 jmmv reason = line.substr(pos + 2); 84 1.1 jmmv } 85 1.1 jmmv 86 1.1 jmmv if (input.good()) { 87 1.1 jmmv line.clear(); 88 1.1 jmmv while (std::getline(input, line).good() && !line.empty()) { 89 1.1 jmmv reason += "<<NEWLINE>>" + line; 90 1.1 jmmv line.clear(); 91 1.1 jmmv } 92 1.1 jmmv if (!line.empty()) 93 1.1 jmmv reason += "<<NEWLINE>>" + line; 94 1.1 jmmv } 95 1.1 jmmv 96 1.1 jmmv if (type == "broken") { 97 1.1 jmmv return test_result(broken, reason); 98 1.1 jmmv } else if (type == "expected_failure") { 99 1.1 jmmv return test_result(expected_failure, reason); 100 1.1 jmmv } else if (type == "failed") { 101 1.1 jmmv return test_result(failed, reason); 102 1.1 jmmv } else if (type == "passed") { 103 1.1 jmmv return test_result(passed, reason); 104 1.1 jmmv } else if (type == "skipped") { 105 1.1 jmmv return test_result(skipped, reason); 106 1.1 jmmv } else { 107 1.1 jmmv return test_result(broken, F("Unknown result type '%s'") % type); 108 1.1 jmmv } 109 1.1 jmmv } 110 1.1 jmmv 111 1.1 jmmv 112 1.1 jmmv /// Returns the type of the result. 113 1.1 jmmv /// 114 1.1 jmmv /// \return A result type. 115 1.1 jmmv engine::test_result::result_type 116 1.1 jmmv engine::test_result::type(void) const 117 1.1 jmmv { 118 1.1 jmmv return _type; 119 1.1 jmmv } 120 1.1 jmmv 121 1.1 jmmv 122 1.1 jmmv /// Returns the reason explaining the result. 123 1.1 jmmv /// 124 1.1 jmmv /// \return A textual reason, possibly empty. 125 1.1 jmmv const std::string& 126 1.1 jmmv engine::test_result::reason(void) const 127 1.1 jmmv { 128 1.1 jmmv return _reason; 129 1.1 jmmv } 130 1.1 jmmv 131 1.1 jmmv 132 1.1 jmmv /// True if the test case result has a positive connotation. 133 1.1 jmmv /// 134 1.1 jmmv /// \return Whether the test case is good or not. 135 1.1 jmmv bool 136 1.1 jmmv engine::test_result::good(void) const 137 1.1 jmmv { 138 1.1 jmmv switch (_type) { 139 1.1 jmmv case expected_failure: 140 1.1 jmmv case passed: 141 1.1 jmmv case skipped: 142 1.1 jmmv return true; 143 1.1 jmmv 144 1.1 jmmv case broken: 145 1.1 jmmv case failed: 146 1.1 jmmv return false; 147 1.1 jmmv } 148 1.1 jmmv UNREACHABLE; 149 1.1 jmmv } 150 1.1 jmmv 151 1.1 jmmv 152 1.1 jmmv /// Equality comparator. 153 1.1 jmmv /// 154 1.1 jmmv /// \param other The test result to compare to. 155 1.1 jmmv /// 156 1.1 jmmv /// \return True if the other object is equal to this one, false otherwise. 157 1.1 jmmv bool 158 1.1 jmmv engine::test_result::operator==(const test_result& other) const 159 1.1 jmmv { 160 1.1 jmmv return _type == other._type && _reason == other._reason; 161 1.1 jmmv } 162 1.1 jmmv 163 1.1 jmmv 164 1.1 jmmv /// Inequality comparator. 165 1.1 jmmv /// 166 1.1 jmmv /// \param other The test result to compare to. 167 1.1 jmmv /// 168 1.1 jmmv /// \return True if the other object is different from this one, false 169 1.1 jmmv /// otherwise. 170 1.1 jmmv bool 171 1.1 jmmv engine::test_result::operator!=(const test_result& other) const 172 1.1 jmmv { 173 1.1 jmmv return !(*this == other); 174 1.1 jmmv } 175 1.1 jmmv 176 1.1 jmmv 177 1.1 jmmv /// Injects the object into a stream. 178 1.1 jmmv /// 179 1.1 jmmv /// \param output The stream into which to inject the object. 180 1.1 jmmv /// \param object The object to format. 181 1.1 jmmv /// 182 1.1 jmmv /// \return The output stream. 183 1.1 jmmv std::ostream& 184 1.1 jmmv engine::operator<<(std::ostream& output, const test_result& object) 185 1.1 jmmv { 186 1.1 jmmv std::string result_name; 187 1.1 jmmv switch (object.type()) { 188 1.1 jmmv case test_result::broken: result_name = "broken"; break; 189 1.1 jmmv case test_result::expected_failure: result_name = "expected_failure"; break; 190 1.1 jmmv case test_result::failed: result_name = "failed"; break; 191 1.1 jmmv case test_result::passed: result_name = "passed"; break; 192 1.1 jmmv case test_result::skipped: result_name = "skipped"; break; 193 1.1 jmmv } 194 1.1 jmmv const std::string& reason = object.reason(); 195 1.1 jmmv if (reason.empty()) { 196 1.1 jmmv output << F("test_result{type=%s}") % text::quote(result_name, '\''); 197 1.1 jmmv } else { 198 1.1 jmmv output << F("test_result{type=%s, reason=%s}") 199 1.1 jmmv % text::quote(result_name, '\'') % text::quote(reason, '\''); 200 1.1 jmmv } 201 1.1 jmmv return output; 202 1.1 jmmv } 203