1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "model/test_case.hpp"
31 #include "model/metadata.hpp"
32 #include "model/test_result.hpp"
33 #include "utils/format/macros.hpp"
34 #include "utils/noncopyable.hpp"
35 #include "utils/optional.ipp"
36 #include "utils/text/operations.ipp"
38 namespace text = utils::text;
41 using utils::optional;
44 /// Internal implementation for a test_case.
45 struct model::test_case::impl : utils::noncopyable {
46 /// Name of the test case; must be unique within the test program.
49 /// Metadata of the container test program.
51 /// Yes, this is a pointer. Yes, we do not own the object pointed to.
52 /// However, because this is only intended to point to the metadata object
53 /// of test programs _containing_ this test case, we can assume that the
54 /// referenced object will be alive for the lifetime of this test case.
55 const model::metadata* md_defaults;
57 /// Test case metadata.
60 /// Fake result to return instead of running the test case.
61 optional< model::test_result > fake_result;
65 /// \param name_ The name of the test case within the test program.
66 /// \param md_defaults_ Metadata of the container test program.
67 /// \param md_ Metadata of the test case.
68 /// \param fake_result_ Fake result to return instead of running the test
70 impl(const std::string& name_,
71 const model::metadata* md_defaults_,
72 const model::metadata& md_,
73 const optional< model::test_result >& fake_result_) :
75 md_defaults(md_defaults_),
77 fake_result(fake_result_)
81 /// Gets the test case metadata.
83 /// This combines the test case's metadata with any possible test program
84 /// metadata, using the latter as defaults.
86 /// \return The test case metadata.
88 get_metadata(void) const
90 if (md_defaults != NULL) {
91 return md_defaults->apply_overrides(md);
97 /// Equality comparator.
99 /// \param other The other object to compare this one to.
101 /// \return True if this object and other are equal; false otherwise.
103 operator==(const impl& other) const
105 return (name == other.name &&
106 get_metadata() == other.get_metadata() &&
107 fake_result == other.fake_result);
112 /// Constructs a new test case from an already-built impl oject.
114 /// \param pimpl_ The internal representation of the test case.
115 model::test_case::test_case(std::shared_ptr< impl > pimpl_) :
121 /// Constructs a new test case.
123 /// \param name_ The name of the test case within the test program.
124 /// \param md_ Metadata of the test case.
125 model::test_case::test_case(const std::string& name_,
126 const model::metadata& md_) :
127 _pimpl(new impl(name_, NULL, md_, none))
133 /// Constructs a new fake test case.
135 /// A fake test case is a test case that is not really defined by the test
136 /// program. Such test cases have a name surrounded by '__' and, when executed,
137 /// they return a fixed, pre-recorded result.
139 /// This is necessary for the cases where listing the test cases of a test
140 /// program fails. In this scenario, we generate a single test case within
141 /// the test program that unconditionally returns a failure.
143 /// TODO(jmmv): Need to get rid of this. We should be able to report the
144 /// status of test programs independently of test cases, as some interfaces
145 /// don't know about the latter at all.
147 /// \param name_ The name to give to this fake test case. This name has to be
148 /// prefixed and suffixed by '__' to clearly denote that this is internal.
149 /// \param description_ The description of the test case, if any.
150 /// \param test_result_ The fake result to return when this test case is run.
151 model::test_case::test_case(
152 const std::string& name_,
153 const std::string& description_,
154 const model::test_result& test_result_) :
158 model::metadata_builder().set_description(description_).build(),
159 utils::make_optional(test_result_)))
161 PRE_MSG(name_.length() > 4 && name_.substr(0, 2) == "__" &&
162 name_.substr(name_.length() - 2) == "__",
163 "Invalid fake name provided to fake test case");
167 /// Destroys a test case.
168 model::test_case::~test_case(void)
173 /// Constructs a new test case applying metadata defaults.
175 /// This method is intended to be used by the container test program when
176 /// ownership of the test is given to it. At that point, the test case receives
177 /// the default metadata properties of the test program, not the global
180 /// \param defaults The metadata properties to use as defaults. The provided
181 /// object's lifetime MUST extend the lifetime of the test case. Because
182 /// this is only intended to point at the metadata of the test program
183 /// containing this test case, this assumption should hold.
185 /// \return A new test case.
187 model::test_case::apply_metadata_defaults(const metadata* defaults) const
189 return test_case(std::shared_ptr< impl >(new impl(
193 _pimpl->fake_result)));
197 /// Gets the test case name.
199 /// \return The test case name, relative to the test program.
201 model::test_case::name(void) const
207 /// Gets the test case metadata.
209 /// This combines the test case's metadata with any possible test program
210 /// metadata, using the latter as defaults. You should use this method in
211 /// generaland not get_raw_metadata().
213 /// \return The test case metadata.
215 model::test_case::get_metadata(void) const
217 return _pimpl->get_metadata();
221 /// Gets the original test case metadata without test program overrides.
223 /// This method should be used for storage purposes as serialized test cases
224 /// should record exactly whatever the test case reported and not what the test
225 /// program may have provided. The final values will be reconstructed at load
228 /// \return The test case metadata.
229 const model::metadata&
230 model::test_case::get_raw_metadata(void) const
236 /// Gets the fake result pre-stored for this test case.
238 /// \return A fake result, or none if not defined.
239 optional< model::test_result >
240 model::test_case::fake_result(void) const
242 return _pimpl->fake_result;
246 /// Equality comparator.
248 /// \warning Because test cases reference their container test programs, and
249 /// test programs include test cases, we cannot perform a full comparison here:
250 /// otherwise, we'd enter an inifinte loop. Therefore, out of necessity, this
251 /// does NOT compare whether the container test programs of the affected test
252 /// cases are the same.
254 /// \param other The other object to compare this one to.
256 /// \return True if this object and other are equal; false otherwise.
258 model::test_case::operator==(const test_case& other) const
260 return _pimpl == other._pimpl || *_pimpl == *other._pimpl;
264 /// Inequality comparator.
266 /// \param other The other object to compare this one to.
268 /// \return True if this object and other are different; false otherwise.
270 model::test_case::operator!=(const test_case& other) const
272 return !(*this == other);
276 /// Injects the object into a stream.
278 /// \param output The stream into which to inject the object.
279 /// \param object The object to format.
281 /// \return The output stream.
283 model::operator<<(std::ostream& output, const test_case& object)
285 output << F("test_case{name=%s, metadata=%s}")
286 % text::quote(object.name(), '\'')
287 % object.get_metadata();
292 /// Adds an already-constructed test case.
294 /// \param test_case The test case to add.
296 /// \return A reference to this builder.
297 model::test_cases_map_builder&
298 model::test_cases_map_builder::add(const test_case& test_case)
301 test_cases_map::value_type(test_case.name(), test_case));
306 /// Constructs and adds a new test case with default metadata.
308 /// \param test_case_name The name of the test case to add.
310 /// \return A reference to this builder.
311 model::test_cases_map_builder&
312 model::test_cases_map_builder::add(const std::string& test_case_name)
314 return add(test_case(test_case_name, metadata_builder().build()));
318 /// Constructs and adds a new test case with explicit metadata.
320 /// \param test_case_name The name of the test case to add.
321 /// \param metadata The metadata of the test case.
323 /// \return A reference to this builder.
324 model::test_cases_map_builder&
325 model::test_cases_map_builder::add(const std::string& test_case_name,
326 const metadata& metadata)
328 return add(test_case(test_case_name, metadata));
332 /// Creates a new test_cases_map.
334 /// \return The constructed test_cases_map.
335 model::test_cases_map
336 model::test_cases_map_builder::build(void) const