Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
catch_test_case_info.cpp
Go to the documentation of this file.
1/*
2 * Created by Phil on 14/08/2012.
3 * Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
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)
7 */
8
10#include "catch_enforce.h"
11#include "catch_test_spec.h"
13#include "catch_string_manip.h"
14
15#include <cctype>
16#include <exception>
17#include <algorithm>
18#include <sstream>
19
20namespace Catch {
21
22 namespace {
23 TestCaseInfo::SpecialProperties parseSpecialTag( std::string const& tag ) {
24 if( startsWith( tag, '.' ) ||
25 tag == "!hide" )
27 else if( tag == "!throws" )
29 else if( tag == "!shouldfail" )
31 else if( tag == "!mayfail" )
33 else if( tag == "!nonportable" )
35 else if( tag == "!benchmark" )
37 else
38 return TestCaseInfo::None;
39 }
40 bool isReservedTag( std::string const& tag ) {
41 return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( static_cast<unsigned char>(tag[0]) );
42 }
43 void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) {
44 CATCH_ENFORCE( !isReservedTag(tag),
45 "Tag name: [" << tag << "] is not allowed.\n"
46 << "Tag names starting with non alphanumeric characters are reserved\n"
47 << _lineInfo );
48 }
49 }
50
52 std::string const& _className,
53 NameAndTags const& nameAndTags,
54 SourceLineInfo const& _lineInfo )
55 {
56 bool isHidden = false;
57
58 // Parse out tags
59 std::vector<std::string> tags;
60 std::string desc, tag;
61 bool inTag = false;
62 std::string _descOrTags = nameAndTags.tags;
63 for (char c : _descOrTags) {
64 if( !inTag ) {
65 if( c == '[' )
66 inTag = true;
67 else
68 desc += c;
69 }
70 else {
71 if( c == ']' ) {
72 TestCaseInfo::SpecialProperties prop = parseSpecialTag( tag );
73 if( ( prop & TestCaseInfo::IsHidden ) != 0 )
74 isHidden = true;
75 else if( prop == TestCaseInfo::None )
76 enforceNotReservedTag( tag, _lineInfo );
77
78 // Merged hide tags like `[.approvals]` should be added as
79 // `[.][approvals]`. The `[.]` is added at later point, so
80 // we only strip the prefix
81 if (startsWith(tag, '.') && tag.size() > 1) {
82 tag.erase(0, 1);
83 }
84 tags.push_back( tag );
85 tag.clear();
86 inTag = false;
87 }
88 else
89 tag += c;
90 }
91 }
92 if( isHidden ) {
93 tags.push_back( "." );
94 }
95
96 TestCaseInfo info( nameAndTags.name, _className, desc, tags, _lineInfo );
97 return TestCase( _testCase, std::move(info) );
98 }
99
100 void setTags( TestCaseInfo& testCaseInfo, std::vector<std::string> tags ) {
101 std::sort(begin(tags), end(tags));
102 tags.erase(std::unique(begin(tags), end(tags)), end(tags));
103 testCaseInfo.lcaseTags.clear();
104
105 for( auto const& tag : tags ) {
106 std::string lcaseTag = toLower( tag );
107 testCaseInfo.properties = static_cast<TestCaseInfo::SpecialProperties>( testCaseInfo.properties | parseSpecialTag( lcaseTag ) );
108 testCaseInfo.lcaseTags.push_back( lcaseTag );
109 }
110 testCaseInfo.tags = std::move(tags);
111 }
112
113 TestCaseInfo::TestCaseInfo( std::string const& _name,
114 std::string const& _className,
115 std::string const& _description,
116 std::vector<std::string> const& _tags,
117 SourceLineInfo const& _lineInfo )
118 : name( _name ),
119 className( _className ),
120 description( _description ),
121 lineInfo( _lineInfo ),
122 properties( None )
123 {
124 setTags( *this, _tags );
125 }
126
128 return ( properties & IsHidden ) != 0;
129 }
130 bool TestCaseInfo::throws() const {
131 return ( properties & Throws ) != 0;
132 }
134 return ( properties & (ShouldFail | MayFail ) ) != 0;
135 }
137 return ( properties & (ShouldFail ) ) != 0;
138 }
139
140 std::string TestCaseInfo::tagsAsString() const {
141 std::string ret;
142 // '[' and ']' per tag
143 std::size_t full_size = 2 * tags.size();
144 for (const auto& tag : tags) {
145 full_size += tag.size();
146 }
147 ret.reserve(full_size);
148 for (const auto& tag : tags) {
149 ret.push_back('[');
150 ret.append(tag);
151 ret.push_back(']');
152 }
153
154 return ret;
155 }
156
157
158 TestCase::TestCase( ITestInvoker* testCase, TestCaseInfo&& info ) : TestCaseInfo( std::move(info) ), test( testCase ) {}
159
160
161 TestCase TestCase::withName( std::string const& _newName ) const {
162 TestCase other( *this );
163 other.name = _newName;
164 return other;
165 }
166
167 void TestCase::invoke() const {
168 test->invoke();
169 }
170
171 bool TestCase::operator == ( TestCase const& other ) const {
172 return test.get() == other.test.get() &&
173 name == other.name &&
174 className == other.className;
175 }
176
177 bool TestCase::operator < ( TestCase const& other ) const {
178 return name < other.name;
179 }
180
182 {
183 return *this;
184 }
185
186} // end namespace Catch
#define CATCH_ENFORCE(condition, msg)
std::string name
bool operator<(TestCase const &other) const
TestCase withName(std::string const &_newName) const
bool operator==(TestCase const &other) const
TestCaseInfo const & getTestCaseInfo() const
TestCase(ITestInvoker *testCase, TestCaseInfo &&info)
TestCase makeTestCase(ITestInvoker *_testCase, std::string const &_className, NameAndTags const &nameAndTags, SourceLineInfo const &_lineInfo)
void setTags(TestCaseInfo &testCaseInfo, std::vector< std::string > tags)
bool startsWith(std::string const &s, std::string const &prefix)
std::string toLower(std::string const &s)
not_this_one end(...)
not_this_one begin(...)
Definition name.hpp:106
friend void setTags(TestCaseInfo &testCaseInfo, std::vector< std::string > tags)
std::vector< std::string > lcaseTags
std::string tagsAsString() const
TestCaseInfo(std::string const &_name, std::string const &_className, std::string const &_description, std::vector< std::string > const &_tags, SourceLineInfo const &_lineInfo)
std::vector< std::string > tags
SpecialProperties properties
CK_RV ret