1 // Copyright 2010 Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above copyright 11 // notice, this list of conditions and the following disclaimer in the 12 // documentation and/or other materials provided with the distribution. 13 // * Neither the name of Google Inc. nor the names of its contributors 14 // may be used to endorse or promote products derived from this software 15 // without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #include "engine/test_result.hpp" 30 31 #include <sstream> 32 33 #include <atf-c++.hpp> 34 35 #include "engine/exceptions.hpp" 36 37 using engine::test_result; 38 39 40 namespace { 41 42 43 /// Creates a test case to validate the getters. 44 /// 45 /// \param name The name of the test case; "__getters" will be appended. 46 /// \param expected_type The expected type of the result. 47 /// \param expected_reason The expected reason for the result. 48 /// \param result The result to query. 49 #define GETTERS_TEST(name, expected_type, expected_reason, result) \ 50 ATF_TEST_CASE_WITHOUT_HEAD(name ## __getters); \ 51 ATF_TEST_CASE_BODY(name ## __getters) \ 52 { \ 53 ATF_REQUIRE(expected_type == result.type()); \ 54 ATF_REQUIRE_EQ(expected_reason, result.reason()); \ 55 } 56 57 58 /// Creates a test case to validate the good() method. 59 /// 60 /// \param name The name of the test case; "__good" will be appended. 61 /// \param expected The expected result of good(). 62 /// \param result_type The result type to check. 63 #define GOOD_TEST(name, expected, result_type) \ 64 ATF_TEST_CASE_WITHOUT_HEAD(name ## __good); \ 65 ATF_TEST_CASE_BODY(name ## __good) \ 66 { \ 67 ATF_REQUIRE_EQ(expected, test_result(result_type).good()); \ 68 } 69 70 71 /// Creates a test case to validate the operator<< method. 72 /// 73 /// \param name The name of the test case; "__output" will be appended. 74 /// \param expected The expected string in the output. 75 /// \param result The result to format. 76 #define OUTPUT_TEST(name, expected, result) \ 77 ATF_TEST_CASE_WITHOUT_HEAD(name ## __output); \ 78 ATF_TEST_CASE_BODY(name ## __output) \ 79 { \ 80 std::ostringstream output; \ 81 output << "prefix" << result << "suffix"; \ 82 ATF_REQUIRE_EQ("prefix" + std::string(expected) + "suffix", \ 83 output.str()); \ 84 } 85 86 87 /// Validates the parse() method on a particular test result type. 88 /// 89 /// \param result_name Textual representation of the type, to be written to the 90 /// input data. 91 /// \param result_type Expected result type. 92 static void 93 parse_test(const std::string& result_name, 94 const test_result::result_type result_type) 95 { 96 std::istringstream input(result_name); 97 ATF_REQUIRE(test_result(result_type) == test_result::parse(input)); 98 99 input.clear(); 100 input.str(result_name + ": Some message"); 101 ATF_REQUIRE(test_result(result_type, "Some message") == 102 test_result::parse(input)); 103 104 input.clear(); 105 input.str(result_name + ": Some message\n"); 106 ATF_REQUIRE(test_result(result_type, "Some message") == 107 test_result::parse(input)); 108 109 input.clear(); 110 input.str(result_name + ": foo\nbar"); 111 ATF_REQUIRE(test_result(result_type, "foo<<NEWLINE>>bar") == 112 test_result::parse(input)); 113 114 input.clear(); 115 input.str(result_name + ": foo\nbar\n"); 116 ATF_REQUIRE(test_result(result_type, "foo<<NEWLINE>>bar") == 117 test_result::parse(input)); 118 } 119 120 121 /// Creates a test case to validate the parse() method for a given type. 122 /// 123 /// \param name The name of the test case; "parse__" will be prepended. 124 #define PARSE_TEST(name) \ 125 ATF_TEST_CASE_WITHOUT_HEAD(parse__ ## name); \ 126 ATF_TEST_CASE_BODY(parse__ ## name) \ 127 { \ 128 parse_test(#name, test_result:: name); \ 129 } 130 131 132 } // anonymous namespace 133 134 135 PARSE_TEST(broken); 136 PARSE_TEST(expected_failure); 137 PARSE_TEST(failed); 138 PARSE_TEST(passed); 139 PARSE_TEST(skipped); 140 141 142 ATF_TEST_CASE_WITHOUT_HEAD(parse__empty); 143 ATF_TEST_CASE_BODY(parse__empty) 144 { 145 std::istringstream input(""); 146 ATF_REQUIRE(test_result(test_result::broken, "Empty result file") == 147 test_result::parse(input)); 148 } 149 150 151 ATF_TEST_CASE_WITHOUT_HEAD(parse__unknown_type); 152 ATF_TEST_CASE_BODY(parse__unknown_type) 153 { 154 std::istringstream input("passed "); 155 ATF_REQUIRE( 156 test_result(test_result::broken, "Unknown result type 'passed '") == 157 test_result::parse(input)); 158 159 input.clear(); 160 input.str("fail"); 161 ATF_REQUIRE( 162 test_result(test_result::broken, "Unknown result type 'fail'") == 163 test_result::parse(input)); 164 165 input.clear(); 166 input.str("a b"); 167 ATF_REQUIRE( 168 test_result(test_result::broken, "Unknown result type 'a b'") == 169 test_result::parse(input)); 170 } 171 172 173 GETTERS_TEST(broken, test_result::broken, "The reason", 174 test_result(test_result::broken, "The reason")); 175 GETTERS_TEST(expected_failure, test_result::expected_failure, "The reason", 176 test_result(test_result::expected_failure, "The reason")); 177 GETTERS_TEST(failed, test_result::failed, "The reason", 178 test_result(test_result::failed, "The reason")); 179 GETTERS_TEST(passed, test_result::passed, "", 180 test_result(test_result::passed)); 181 GETTERS_TEST(skipped, test_result::skipped, "The reason", 182 test_result(test_result::skipped, "The reason")); 183 184 185 GOOD_TEST(broken, false, test_result::broken); 186 GOOD_TEST(expected_failure, true, test_result::expected_failure); 187 GOOD_TEST(failed, false, test_result::failed); 188 GOOD_TEST(passed, true, test_result::passed); 189 GOOD_TEST(skipped, true, test_result::skipped); 190 191 192 OUTPUT_TEST(broken, "test_result{type='broken', reason='foo'}", 193 test_result(test_result::broken, "foo")); 194 OUTPUT_TEST(expected_failure, 195 "test_result{type='expected_failure', reason='abc def'}", 196 test_result(test_result::expected_failure, "abc def")); 197 OUTPUT_TEST(failed, "test_result{type='failed', reason='some \\'string'}", 198 test_result(test_result::failed, "some 'string")); 199 OUTPUT_TEST(passed, "test_result{type='passed'}", 200 test_result(test_result::passed, "")); 201 OUTPUT_TEST(skipped, "test_result{type='skipped', reason='last message'}", 202 test_result(test_result::skipped, "last message")); 203 204 205 ATF_TEST_CASE_WITHOUT_HEAD(operator_eq); 206 ATF_TEST_CASE_BODY(operator_eq) 207 { 208 const test_result result1(test_result::broken, "Foo"); 209 const test_result result2(test_result::broken, "Foo"); 210 const test_result result3(test_result::broken, "Bar"); 211 const test_result result4(test_result::failed, "Foo"); 212 213 ATF_REQUIRE( result1 == result1); 214 ATF_REQUIRE( result1 == result2); 215 ATF_REQUIRE(!(result1 == result3)); 216 ATF_REQUIRE(!(result1 == result4)); 217 } 218 219 220 ATF_TEST_CASE_WITHOUT_HEAD(operator_ne); 221 ATF_TEST_CASE_BODY(operator_ne) 222 { 223 const test_result result1(test_result::broken, "Foo"); 224 const test_result result2(test_result::broken, "Foo"); 225 const test_result result3(test_result::broken, "Bar"); 226 const test_result result4(test_result::failed, "Foo"); 227 228 ATF_REQUIRE(!(result1 != result1)); 229 ATF_REQUIRE(!(result1 != result2)); 230 ATF_REQUIRE( result1 != result3); 231 ATF_REQUIRE( result1 != result4); 232 } 233 234 235 ATF_INIT_TEST_CASES(tcs) 236 { 237 ATF_ADD_TEST_CASE(tcs, parse__broken); 238 ATF_ADD_TEST_CASE(tcs, parse__expected_failure); 239 ATF_ADD_TEST_CASE(tcs, parse__failed); 240 ATF_ADD_TEST_CASE(tcs, parse__passed); 241 ATF_ADD_TEST_CASE(tcs, parse__skipped); 242 ATF_ADD_TEST_CASE(tcs, parse__empty); 243 ATF_ADD_TEST_CASE(tcs, parse__unknown_type); 244 245 ATF_ADD_TEST_CASE(tcs, broken__getters); 246 ATF_ADD_TEST_CASE(tcs, broken__good); 247 ATF_ADD_TEST_CASE(tcs, broken__output); 248 ATF_ADD_TEST_CASE(tcs, expected_failure__getters); 249 ATF_ADD_TEST_CASE(tcs, expected_failure__good); 250 ATF_ADD_TEST_CASE(tcs, expected_failure__output); 251 ATF_ADD_TEST_CASE(tcs, failed__getters); 252 ATF_ADD_TEST_CASE(tcs, failed__good); 253 ATF_ADD_TEST_CASE(tcs, failed__output); 254 ATF_ADD_TEST_CASE(tcs, passed__getters); 255 ATF_ADD_TEST_CASE(tcs, passed__good); 256 ATF_ADD_TEST_CASE(tcs, passed__output); 257 ATF_ADD_TEST_CASE(tcs, skipped__getters); 258 ATF_ADD_TEST_CASE(tcs, skipped__good); 259 ATF_ADD_TEST_CASE(tcs, skipped__output); 260 ATF_ADD_TEST_CASE(tcs, operator_eq); 261 ATF_ADD_TEST_CASE(tcs, operator_ne); 262 } 263