xref: /freebsd/contrib/kyua/model/test_result_test.cpp (revision f4f56ff43dbd30930f4b018e39ba2b9abf84551f)
1 // Copyright 2014 The Kyua Authors.
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 "model/test_result.hpp"
30 
31 #include <sstream>
32 
33 #include <atf-c++.hpp>
34 
35 
36 /// Creates a test case to validate the getters.
37 ///
38 /// \param name The name of the test case; "__getters" will be appended.
39 /// \param expected_type The expected type of the result.
40 /// \param expected_reason The expected reason for the result.
41 /// \param result The result to query.
42 #define GETTERS_TEST(name, expected_type, expected_reason, result) \
43     ATF_TEST_CASE_WITHOUT_HEAD(name ## __getters); \
44     ATF_TEST_CASE_BODY(name ## __getters) \
45     { \
46         ATF_REQUIRE(expected_type == result.type()); \
47         ATF_REQUIRE_EQ(expected_reason, result.reason());  \
48     }
49 
50 
51 /// Creates a test case to validate the good() method.
52 ///
53 /// \param name The name of the test case; "__good" will be appended.
54 /// \param expected The expected result of good().
55 /// \param result_type The result type to check.
56 #define GOOD_TEST(name, expected, result_type) \
57     ATF_TEST_CASE_WITHOUT_HEAD(name ## __good); \
58     ATF_TEST_CASE_BODY(name ## __good) \
59     { \
60         ATF_REQUIRE_EQ(expected, model::test_result(result_type).good()); \
61     }
62 
63 
64 /// Creates a test case to validate the operator<< method.
65 ///
66 /// \param name The name of the test case; "__output" will be appended.
67 /// \param expected The expected string in the output.
68 /// \param result The result to format.
69 #define OUTPUT_TEST(name, expected, result) \
70     ATF_TEST_CASE_WITHOUT_HEAD(name ## __output); \
71     ATF_TEST_CASE_BODY(name ## __output) \
72     { \
73         std::ostringstream output; \
74         output << "prefix" << result << "suffix"; \
75         ATF_REQUIRE_EQ("prefix" + std::string(expected) + "suffix", \
76                        output.str()); \
77     }
78 
79 
80 GETTERS_TEST(
81     broken,
82     model::test_result_broken,
83     "The reason",
84     model::test_result(model::test_result_broken, "The reason"));
85 GETTERS_TEST(
86     expected_failure,
87     model::test_result_expected_failure,
88     "The reason",
89     model::test_result(model::test_result_expected_failure, "The reason"));
90 GETTERS_TEST(
91     failed,
92     model::test_result_failed,
93     "The reason",
94     model::test_result(model::test_result_failed, "The reason"));
95 GETTERS_TEST(
96     passed,
97     model::test_result_passed,
98     "",
99     model::test_result(model::test_result_passed));
100 GETTERS_TEST(
101     skipped,
102     model::test_result_skipped,
103     "The reason",
104     model::test_result(model::test_result_skipped, "The reason"));
105 
106 
107 GOOD_TEST(broken, false, model::test_result_broken);
108 GOOD_TEST(expected_failure, true, model::test_result_expected_failure);
109 GOOD_TEST(failed, false, model::test_result_failed);
110 GOOD_TEST(passed, true, model::test_result_passed);
111 GOOD_TEST(skipped, true, model::test_result_skipped);
112 
113 
114 OUTPUT_TEST(
115     broken,
116     "model::test_result{type='broken', reason='foo'}",
117     model::test_result(model::test_result_broken, "foo"));
118 OUTPUT_TEST(
119     expected_failure,
120     "model::test_result{type='expected_failure', reason='abc def'}",
121     model::test_result(model::test_result_expected_failure, "abc def"));
122 OUTPUT_TEST(
123     failed,
124     "model::test_result{type='failed', reason='some \\'string'}",
125     model::test_result(model::test_result_failed, "some 'string"));
126 OUTPUT_TEST(
127     passed,
128     "model::test_result{type='passed'}",
129     model::test_result(model::test_result_passed, ""));
130 OUTPUT_TEST(
131     skipped,
132     "model::test_result{type='skipped', reason='last message'}",
133     model::test_result(model::test_result_skipped, "last message"));
134 
135 
136 ATF_TEST_CASE_WITHOUT_HEAD(operator_eq);
137 ATF_TEST_CASE_BODY(operator_eq)
138 {
139     const model::test_result result1(model::test_result_broken, "Foo");
140     const model::test_result result2(model::test_result_broken, "Foo");
141     const model::test_result result3(model::test_result_broken, "Bar");
142     const model::test_result result4(model::test_result_failed, "Foo");
143 
144     ATF_REQUIRE(  result1 == result1);
145     ATF_REQUIRE(  result1 == result2);
146     ATF_REQUIRE(!(result1 == result3));
147     ATF_REQUIRE(!(result1 == result4));
148 }
149 
150 
151 ATF_TEST_CASE_WITHOUT_HEAD(operator_ne);
152 ATF_TEST_CASE_BODY(operator_ne)
153 {
154     const model::test_result result1(model::test_result_broken, "Foo");
155     const model::test_result result2(model::test_result_broken, "Foo");
156     const model::test_result result3(model::test_result_broken, "Bar");
157     const model::test_result result4(model::test_result_failed, "Foo");
158 
159     ATF_REQUIRE(!(result1 != result1));
160     ATF_REQUIRE(!(result1 != result2));
161     ATF_REQUIRE(  result1 != result3);
162     ATF_REQUIRE(  result1 != result4);
163 }
164 
165 
166 ATF_INIT_TEST_CASES(tcs)
167 {
168     ATF_ADD_TEST_CASE(tcs, broken__getters);
169     ATF_ADD_TEST_CASE(tcs, broken__good);
170     ATF_ADD_TEST_CASE(tcs, broken__output);
171     ATF_ADD_TEST_CASE(tcs, expected_failure__getters);
172     ATF_ADD_TEST_CASE(tcs, expected_failure__good);
173     ATF_ADD_TEST_CASE(tcs, expected_failure__output);
174     ATF_ADD_TEST_CASE(tcs, failed__getters);
175     ATF_ADD_TEST_CASE(tcs, failed__good);
176     ATF_ADD_TEST_CASE(tcs, failed__output);
177     ATF_ADD_TEST_CASE(tcs, passed__getters);
178     ATF_ADD_TEST_CASE(tcs, passed__good);
179     ATF_ADD_TEST_CASE(tcs, passed__output);
180     ATF_ADD_TEST_CASE(tcs, skipped__getters);
181     ATF_ADD_TEST_CASE(tcs, skipped__good);
182     ATF_ADD_TEST_CASE(tcs, skipped__output);
183     ATF_ADD_TEST_CASE(tcs, operator_eq);
184     ATF_ADD_TEST_CASE(tcs, operator_ne);
185 }
186