Phil Nash | fd58d48 | 2011-01-07 19:57:32 +0000 | [diff] [blame] | 1 | /* |
Phil Nash | ddfe963 | 2012-08-14 19:30:30 +0100 | [diff] [blame] | 2 | * Created by Phil on 14/08/2012. |
| 3 | * Copyright 2012 Two Blue Cubes Ltd. All rights reserved. |
Phil Nash | fd58d48 | 2011-01-07 19:57:32 +0000 | [diff] [blame] | 4 | * |
| 5 | * Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
Phil Nash | fd58d48 | 2011-01-07 19:57:32 +0000 | [diff] [blame] | 7 | */ |
Matt Wozniski | f29c898 | 2012-09-17 01:42:29 -0400 | [diff] [blame] | 8 | #ifndef TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED |
| 9 | #define TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED |
Phil Nash | fd58d48 | 2011-01-07 19:57:32 +0000 | [diff] [blame] | 10 | |
Phil Nash | b1e7d16 | 2014-05-16 18:28:58 +0100 | [diff] [blame] | 11 | #include "catch_test_spec.hpp" |
Phil Nash | ddfe963 | 2012-08-14 19:30:30 +0100 | [diff] [blame] | 12 | #include "catch_test_case_info.h" |
Phil Nash | fd58d48 | 2011-01-07 19:57:32 +0000 | [diff] [blame] | 13 | #include "catch_interfaces_testcase.h" |
Phil Nash | 6ba2057 | 2013-03-22 19:00:42 +0000 | [diff] [blame] | 14 | #include "catch_common.h" |
Phil Nash | fd58d48 | 2011-01-07 19:57:32 +0000 | [diff] [blame] | 15 | |
Martin Hořeňovský | 9012f95 | 2017-02-12 12:17:07 +0100 | [diff] [blame] | 16 | #include <cctype> |
| 17 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 18 | namespace Catch { |
| 19 | |
Phil Nash | 9c1f9a8 | 2014-07-03 08:09:57 +0100 | [diff] [blame] | 20 | inline TestCaseInfo::SpecialProperties parseSpecialTag( std::string const& tag ) { |
Martin Hořeňovský | 3b98a01 | 2017-01-15 09:41:33 +0100 | [diff] [blame] | 21 | if( startsWith( tag, '.' ) || |
Phil Nash | 9c1f9a8 | 2014-07-03 08:09:57 +0100 | [diff] [blame] | 22 | tag == "hide" || |
| 23 | tag == "!hide" ) |
| 24 | return TestCaseInfo::IsHidden; |
| 25 | else if( tag == "!throws" ) |
| 26 | return TestCaseInfo::Throws; |
| 27 | else if( tag == "!shouldfail" ) |
| 28 | return TestCaseInfo::ShouldFail; |
| 29 | else if( tag == "!mayfail" ) |
| 30 | return TestCaseInfo::MayFail; |
Phil Nash | 31c23b9 | 2017-01-23 17:44:55 +0000 | [diff] [blame] | 31 | else if( tag == "!nonportable" ) |
| 32 | return TestCaseInfo::NonPortable; |
Phil Nash | 9c1f9a8 | 2014-07-03 08:09:57 +0100 | [diff] [blame] | 33 | else |
| 34 | return TestCaseInfo::None; |
Phil Nash | 20cad7c | 2014-04-15 18:44:37 +0100 | [diff] [blame] | 35 | } |
| 36 | inline bool isReservedTag( std::string const& tag ) { |
Martin Hořeňovský | d0620c3 | 2017-01-26 19:11:20 +0100 | [diff] [blame] | 37 | return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( tag[0] ); |
Phil Nash | 20cad7c | 2014-04-15 18:44:37 +0100 | [diff] [blame] | 38 | } |
Phil Nash | 9bf43e7 | 2014-05-16 18:52:55 +0100 | [diff] [blame] | 39 | inline void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) { |
| 40 | if( isReservedTag( tag ) ) { |
Martin Hořeňovský | f64d914 | 2017-03-22 17:53:22 +0100 | [diff] [blame^] | 41 | std::ostringstream ss; |
| 42 | ss << Colour(Colour::Red) |
| 43 | << "Tag name [" << tag << "] not allowed.\n" |
| 44 | << "Tag names starting with non alpha-numeric characters are reserved\n" |
| 45 | << Colour(Colour::FileName) |
| 46 | << _lineInfo << '\n'; |
| 47 | throw std::runtime_error(ss.str()); |
Phil Nash | 9bf43e7 | 2014-05-16 18:52:55 +0100 | [diff] [blame] | 48 | } |
| 49 | } |
Phil Nash | 20cad7c | 2014-04-15 18:44:37 +0100 | [diff] [blame] | 50 | |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 51 | TestCase makeTestCase( ITestCase* _testCase, |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 52 | std::string const& _className, |
| 53 | std::string const& _name, |
| 54 | std::string const& _descOrTags, |
| 55 | SourceLineInfo const& _lineInfo ) |
Phil Nash | fc1baac | 2012-09-15 17:53:27 +0100 | [diff] [blame] | 56 | { |
Phil Nash | 337dc25 | 2013-11-19 07:21:03 +0000 | [diff] [blame] | 57 | bool isHidden( startsWith( _name, "./" ) ); // Legacy support |
Phil Nash | 9bf43e7 | 2014-05-16 18:52:55 +0100 | [diff] [blame] | 58 | |
| 59 | // Parse out tags |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 60 | std::set<std::string> tags; |
Phil Nash | 9bf43e7 | 2014-05-16 18:52:55 +0100 | [diff] [blame] | 61 | std::string desc, tag; |
| 62 | bool inTag = false; |
| 63 | for( std::size_t i = 0; i < _descOrTags.size(); ++i ) { |
| 64 | char c = _descOrTags[i]; |
| 65 | if( !inTag ) { |
| 66 | if( c == '[' ) |
| 67 | inTag = true; |
| 68 | else |
| 69 | desc += c; |
Phil Nash | 20cad7c | 2014-04-15 18:44:37 +0100 | [diff] [blame] | 70 | } |
Phil Nash | 9bf43e7 | 2014-05-16 18:52:55 +0100 | [diff] [blame] | 71 | else { |
| 72 | if( c == ']' ) { |
Phil Nash | 0dd214f | 2014-12-15 07:25:34 +0000 | [diff] [blame] | 73 | TestCaseInfo::SpecialProperties prop = parseSpecialTag( tag ); |
| 74 | if( prop == TestCaseInfo::IsHidden ) |
Phil Nash | ee956bc | 2014-05-19 17:50:58 +0100 | [diff] [blame] | 75 | isHidden = true; |
Phil Nash | 0dd214f | 2014-12-15 07:25:34 +0000 | [diff] [blame] | 76 | else if( prop == TestCaseInfo::None ) |
| 77 | enforceNotReservedTag( tag, _lineInfo ); |
| 78 | |
| 79 | tags.insert( tag ); |
Phil Nash | 9bf43e7 | 2014-05-16 18:52:55 +0100 | [diff] [blame] | 80 | tag.clear(); |
Phil Nash | 0dd214f | 2014-12-15 07:25:34 +0000 | [diff] [blame] | 81 | inTag = false; |
Phil Nash | 9bf43e7 | 2014-05-16 18:52:55 +0100 | [diff] [blame] | 82 | } |
| 83 | else |
| 84 | tag += c; |
| 85 | } |
Phil Nash | 47a5ad3 | 2013-12-04 07:58:39 +0000 | [diff] [blame] | 86 | } |
Phil Nash | b43d770 | 2014-05-20 18:11:23 +0100 | [diff] [blame] | 87 | if( isHidden ) { |
| 88 | tags.insert( "hide" ); |
| 89 | tags.insert( "." ); |
| 90 | } |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 91 | |
Phil Nash | 9c1f9a8 | 2014-07-03 08:09:57 +0100 | [diff] [blame] | 92 | TestCaseInfo info( _name, _className, desc, tags, _lineInfo ); |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 93 | return TestCase( _testCase, info ); |
Phil Nash | fc1baac | 2012-09-15 17:53:27 +0100 | [diff] [blame] | 94 | } |
Phil Nash | fd58d48 | 2011-01-07 19:57:32 +0000 | [diff] [blame] | 95 | |
Phil Nash | 088c5bc | 2015-07-02 08:20:18 +0100 | [diff] [blame] | 96 | void setTags( TestCaseInfo& testCaseInfo, std::set<std::string> const& tags ) |
| 97 | { |
| 98 | testCaseInfo.tags = tags; |
| 99 | testCaseInfo.lcaseTags.clear(); |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 100 | |
Phil Nash | 088c5bc | 2015-07-02 08:20:18 +0100 | [diff] [blame] | 101 | std::ostringstream oss; |
| 102 | for( std::set<std::string>::const_iterator it = tags.begin(), itEnd = tags.end(); it != itEnd; ++it ) { |
Martin Hořeňovský | bcaa2f9 | 2017-01-29 23:07:15 +0100 | [diff] [blame] | 103 | oss << '[' << *it << ']'; |
Phil Nash | 088c5bc | 2015-07-02 08:20:18 +0100 | [diff] [blame] | 104 | std::string lcaseTag = toLower( *it ); |
| 105 | testCaseInfo.properties = static_cast<TestCaseInfo::SpecialProperties>( testCaseInfo.properties | parseSpecialTag( lcaseTag ) ); |
| 106 | testCaseInfo.lcaseTags.insert( lcaseTag ); |
| 107 | } |
| 108 | testCaseInfo.tagsAsString = oss.str(); |
| 109 | } |
Phil Nash | e917381 | 2015-11-04 18:01:28 +0000 | [diff] [blame] | 110 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 111 | TestCaseInfo::TestCaseInfo( std::string const& _name, |
| 112 | std::string const& _className, |
| 113 | std::string const& _description, |
| 114 | std::set<std::string> const& _tags, |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 115 | SourceLineInfo const& _lineInfo ) |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 116 | : name( _name ), |
| 117 | className( _className ), |
| 118 | description( _description ), |
Phil Nash | fe98123 | 2012-12-05 08:40:53 +0000 | [diff] [blame] | 119 | lineInfo( _lineInfo ), |
Phil Nash | 9c1f9a8 | 2014-07-03 08:09:57 +0100 | [diff] [blame] | 120 | properties( None ) |
Phil Nash | 15fd032 | 2013-03-28 22:13:31 +0000 | [diff] [blame] | 121 | { |
Phil Nash | 088c5bc | 2015-07-02 08:20:18 +0100 | [diff] [blame] | 122 | setTags( *this, _tags ); |
Phil Nash | 15fd032 | 2013-03-28 22:13:31 +0000 | [diff] [blame] | 123 | } |
Phil Nash | 9c6ce97 | 2012-08-14 08:38:22 +0100 | [diff] [blame] | 124 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 125 | TestCaseInfo::TestCaseInfo( TestCaseInfo const& other ) |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 126 | : name( other.name ), |
| 127 | className( other.className ), |
| 128 | description( other.description ), |
| 129 | tags( other.tags ), |
Phil Nash | fbf3f6f | 2014-05-20 18:03:54 +0100 | [diff] [blame] | 130 | lcaseTags( other.lcaseTags ), |
Phil Nash | 15fd032 | 2013-03-28 22:13:31 +0000 | [diff] [blame] | 131 | tagsAsString( other.tagsAsString ), |
Phil Nash | fe98123 | 2012-12-05 08:40:53 +0000 | [diff] [blame] | 132 | lineInfo( other.lineInfo ), |
Phil Nash | 9c1f9a8 | 2014-07-03 08:09:57 +0100 | [diff] [blame] | 133 | properties( other.properties ) |
Phil Nash | ddfe963 | 2012-08-14 19:30:30 +0100 | [diff] [blame] | 134 | {} |
Phil Nash | fd58d48 | 2011-01-07 19:57:32 +0000 | [diff] [blame] | 135 | |
Phil Nash | 9c1f9a8 | 2014-07-03 08:09:57 +0100 | [diff] [blame] | 136 | bool TestCaseInfo::isHidden() const { |
| 137 | return ( properties & IsHidden ) != 0; |
| 138 | } |
| 139 | bool TestCaseInfo::throws() const { |
| 140 | return ( properties & Throws ) != 0; |
| 141 | } |
| 142 | bool TestCaseInfo::okToFail() const { |
| 143 | return ( properties & (ShouldFail | MayFail ) ) != 0; |
| 144 | } |
| 145 | bool TestCaseInfo::expectedToFail() const { |
| 146 | return ( properties & (ShouldFail ) ) != 0; |
| 147 | } |
| 148 | |
| 149 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 150 | TestCase::TestCase( ITestCase* testCase, TestCaseInfo const& info ) : TestCaseInfo( info ), test( testCase ) {} |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 151 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 152 | TestCase::TestCase( TestCase const& other ) |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 153 | : TestCaseInfo( other ), |
| 154 | test( other.test ) |
Phil Nash | c2675b5 | 2012-08-23 08:38:27 +0100 | [diff] [blame] | 155 | {} |
| 156 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 157 | TestCase TestCase::withName( std::string const& _newName ) const { |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 158 | TestCase other( *this ); |
| 159 | other.name = _newName; |
| 160 | return other; |
Phil Nash | ddfe963 | 2012-08-14 19:30:30 +0100 | [diff] [blame] | 161 | } |
Phil Nash | 684baf1 | 2011-01-14 08:47:43 +0000 | [diff] [blame] | 162 | |
Phil Nash | fbf3f6f | 2014-05-20 18:03:54 +0100 | [diff] [blame] | 163 | void TestCase::swap( TestCase& other ) { |
| 164 | test.swap( other.test ); |
| 165 | name.swap( other.name ); |
| 166 | className.swap( other.className ); |
| 167 | description.swap( other.description ); |
| 168 | tags.swap( other.tags ); |
| 169 | lcaseTags.swap( other.lcaseTags ); |
| 170 | tagsAsString.swap( other.tagsAsString ); |
Phil Nash | 9c1f9a8 | 2014-07-03 08:09:57 +0100 | [diff] [blame] | 171 | std::swap( TestCaseInfo::properties, static_cast<TestCaseInfo&>( other ).properties ); |
Phil Nash | fbf3f6f | 2014-05-20 18:03:54 +0100 | [diff] [blame] | 172 | std::swap( lineInfo, other.lineInfo ); |
| 173 | } |
| 174 | |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 175 | void TestCase::invoke() const { |
| 176 | test->invoke(); |
Phil Nash | ddfe963 | 2012-08-14 19:30:30 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 179 | bool TestCase::operator == ( TestCase const& other ) const { |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 180 | return test.get() == other.test.get() && |
| 181 | name == other.name && |
| 182 | className == other.className; |
Phil Nash | ddfe963 | 2012-08-14 19:30:30 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 185 | bool TestCase::operator < ( TestCase const& other ) const { |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 186 | return name < other.name; |
Phil Nash | ddfe963 | 2012-08-14 19:30:30 +0100 | [diff] [blame] | 187 | } |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 188 | TestCase& TestCase::operator = ( TestCase const& other ) { |
Phil Nash | 06a671a | 2012-11-22 19:17:20 +0000 | [diff] [blame] | 189 | TestCase temp( other ); |
Phil Nash | c2675b5 | 2012-08-23 08:38:27 +0100 | [diff] [blame] | 190 | swap( temp ); |
| 191 | return *this; |
| 192 | } |
Phil Nash | 85c0e3d | 2012-09-21 07:48:03 +0100 | [diff] [blame] | 193 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 194 | TestCaseInfo const& TestCase::getTestCaseInfo() const |
Phil Nash | 8baa06c | 2012-11-25 11:19:55 +0000 | [diff] [blame] | 195 | { |
| 196 | return *this; |
| 197 | } |
| 198 | |
Phil Nash | 85c0e3d | 2012-09-21 07:48:03 +0100 | [diff] [blame] | 199 | } // end namespace Catch |
Phil Nash | fd58d48 | 2011-01-07 19:57:32 +0000 | [diff] [blame] | 200 | |
Matt Wozniski | f29c898 | 2012-09-17 01:42:29 -0400 | [diff] [blame] | 201 | #endif // TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED |