Skip to content

Commit 33e047a

Browse files
committed
Add Unscoped Capture Logger and Add Unit Tests
1 parent 3e82ef9 commit 33e047a

20 files changed

+686
-18
lines changed

src/catch2/catch_message.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,15 @@ namespace Catch {
112112
m_captured++;
113113
}
114114

115+
void Capturer::captureUnscopedValue( size_t index, std::string const& value ) {
116+
m_messages[index].message += value;
117+
getResultCapture().emplaceUnscopedMessage(Catch::MessageBuilder(
118+
m_messages[index].macroName,
119+
m_messages[index].lineInfo,
120+
m_messages[index].type) << m_messages[index].message);
121+
if(index == m_messages.size() - 1){
122+
m_messages.clear();
123+
}
124+
}
125+
115126
} // end namespace Catch

src/catch2/catch_message.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ namespace Catch {
7373

7474
~Capturer();
7575

76+
void captureUnscopedValue( size_t index, std::string const& value );
77+
78+
template<typename T>
79+
void captureUnscopedValues( size_t index, T const& value ) {
80+
captureUnscopedValue( index, Catch::Detail::stringify( value ) );
81+
}
82+
83+
template<typename T, typename... Ts>
84+
void captureUnscopedValues( size_t index, T const& value, Ts const&... values ) {
85+
captureUnscopedValue( index, Catch::Detail::stringify(value) );
86+
captureUnscopedValues( index+1, values... );
87+
}
88+
7689
void captureValue( size_t index, std::string const& value );
7790

7891
template<typename T>
@@ -85,6 +98,10 @@ namespace Catch {
8598
captureValue( index, Catch::Detail::stringify(value) );
8699
captureValues( index+1, values... );
87100
}
101+
102+
std::vector<MessageInfo> getMessageDetails() const {
103+
return m_messages;
104+
}
88105
};
89106

90107
} // end namespace Catch
@@ -105,6 +122,14 @@ namespace Catch {
105122
#__VA_ARGS__##_catch_sr ); \
106123
varName.captureValues( 0, __VA_ARGS__ )
107124

125+
///////////////////////////////////////////////////////////////////////////////
126+
#define INTERNAL_CATCH_UNSCOPED_CAPTURE( varName, macroName, ... ) \
127+
Catch::Capturer varName( macroName##_catch_sr, \
128+
CATCH_INTERNAL_LINEINFO, \
129+
Catch::ResultWas::Info, \
130+
#__VA_ARGS__##_catch_sr ); \
131+
varName.captureUnscopedValues( 0, __VA_ARGS__ );
132+
108133
///////////////////////////////////////////////////////////////////////////////
109134
#define INTERNAL_CATCH_INFO( macroName, log ) \
110135
const Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage )( Catch::MessageBuilder( macroName##_catch_sr, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log )
@@ -120,27 +145,33 @@ namespace Catch {
120145
#define CATCH_UNSCOPED_INFO( msg ) INTERNAL_CATCH_UNSCOPED_INFO( "CATCH_UNSCOPED_INFO", msg )
121146
#define CATCH_WARN( msg ) INTERNAL_CATCH_MSG( "CATCH_WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg )
122147
#define CATCH_CAPTURE( ... ) INTERNAL_CATCH_CAPTURE( INTERNAL_CATCH_UNIQUE_NAME(capturer), "CATCH_CAPTURE", __VA_ARGS__ )
148+
#define CATCH_UNSCOPED_CAPTURE( ... ) INTERNAL_CATCH_UNSCOPED_CAPTURE( INTERNAL_CATCH_UNIQUE_NAME(capturer), "CATCH_UNSCOPED_CAPTURE", __VA_ARGS__ )
123149

124150
#elif defined(CATCH_CONFIG_PREFIX_MESSAGES) && defined(CATCH_CONFIG_DISABLE)
125151

126152
#define CATCH_INFO( msg ) (void)(0)
127153
#define CATCH_UNSCOPED_INFO( msg ) (void)(0)
128154
#define CATCH_WARN( msg ) (void)(0)
129155
#define CATCH_CAPTURE( ... ) (void)(0)
156+
#define CATCH_UNSCOPED_CAPTURE( ... ) (void)(0)
157+
130158

131159
#elif !defined(CATCH_CONFIG_PREFIX_MESSAGES) && !defined(CATCH_CONFIG_DISABLE)
132160

133161
#define INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg )
134162
#define UNSCOPED_INFO( msg ) INTERNAL_CATCH_UNSCOPED_INFO( "UNSCOPED_INFO", msg )
135163
#define WARN( msg ) INTERNAL_CATCH_MSG( "WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg )
136164
#define CAPTURE( ... ) INTERNAL_CATCH_CAPTURE( INTERNAL_CATCH_UNIQUE_NAME(capturer), "CAPTURE", __VA_ARGS__ )
165+
#define UNSCOPED_CAPTURE( ... ) INTERNAL_CATCH_UNSCOPED_CAPTURE( INTERNAL_CATCH_UNIQUE_NAME(capturer), "UNSCOPED_CAPTURE", __VA_ARGS__ )
166+
137167

138168
#elif !defined(CATCH_CONFIG_PREFIX_MESSAGES) && defined(CATCH_CONFIG_DISABLE)
139169

140170
#define INFO( msg ) (void)(0)
141171
#define UNSCOPED_INFO( msg ) (void)(0)
142172
#define WARN( msg ) (void)(0)
143173
#define CAPTURE( ... ) (void)(0)
174+
#define UNSCOPED_CAPTURE( ... ) (void)(0)
144175

145176
#endif // end of user facing macro declarations
146177

tests/SelfTest/Baselines/automake.sw.approved.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ Message from section two
297297
:test-result: PASS Tracker
298298
:test-result: PASS Trim strings
299299
:test-result: PASS Type conversions of RangeEquals and similar
300+
:test-result: PASS UNSCOPED CAPTURE can deal with complex expressions
301+
:test-result: PASS UNSCOPED_CAPTURE parses string and character constants
300302
:test-result: FAIL Unexpected exceptions can be translated
301303
:test-result: PASS Upcasting special member functions
302304
:test-result: PASS Usage of AllMatch range matcher
@@ -396,6 +398,8 @@ b1!
396398
:test-result: PASS shortened hide tags are split apart
397399
:test-result: SKIP skipped tests can optionally provide a reason
398400
:test-result: PASS splitString
401+
:test-result: FAIL stacks unscoped capture for vector
402+
:test-result: FAIL stacks unscoped capture in loops
399403
:test-result: FAIL stacks unscoped info in loops
400404
:test-result: PASS startsWith
401405
:test-result: PASS std::map is convertible string

tests/SelfTest/Baselines/automake.sw.multi.approved.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@
290290
:test-result: PASS Tracker
291291
:test-result: PASS Trim strings
292292
:test-result: PASS Type conversions of RangeEquals and similar
293+
:test-result: PASS UNSCOPED CAPTURE can deal with complex expressions
294+
:test-result: PASS UNSCOPED_CAPTURE parses string and character constants
293295
:test-result: FAIL Unexpected exceptions can be translated
294296
:test-result: PASS Upcasting special member functions
295297
:test-result: PASS Usage of AllMatch range matcher
@@ -385,6 +387,8 @@
385387
:test-result: PASS shortened hide tags are split apart
386388
:test-result: SKIP skipped tests can optionally provide a reason
387389
:test-result: PASS splitString
390+
:test-result: FAIL stacks unscoped capture for vector
391+
:test-result: FAIL stacks unscoped capture in loops
388392
:test-result: FAIL stacks unscoped info in loops
389393
:test-result: PASS startsWith
390394
:test-result: PASS std::map is convertible string

tests/SelfTest/Baselines/compact.sw.approved.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,8 @@ MatchersRanges.tests.cpp:<line number>: passed: a, !RangeEquals( b ) for: { 1, 2
21942194
MatchersRanges.tests.cpp:<line number>: passed: a, UnorderedRangeEquals( b ) for: { 1, 2, 3 } unordered elements are { 3, 2, 1 }
21952195
MatchersRanges.tests.cpp:<line number>: passed: vector_a, RangeEquals( array_a_plus_1, close_enough ) for: { 1, 2, 3 } elements are { 2, 3, 4 }
21962196
MatchersRanges.tests.cpp:<line number>: passed: vector_a, UnorderedRangeEquals( array_a_plus_1, close_enough ) for: { 1, 2, 3 } unordered elements are { 2, 3, 4 }
2197+
Message.tests.cpp:<line number>: passed: with 7 messages: 'a := 1' and 'b := 2' and 'c := 3' and 'a + b := 3' and 'a+b := 3' and 'c > b := true' and 'a == 1 := true'
2198+
Message.tests.cpp:<line number>: passed: with 11 messages: '("comma, in string", "escaped, \", ") := "escaped, ", "' and '"single quote in string,'," := "single quote in string,',"' and '"some escapes, \\,\\\\" := "some escapes, \,\\"' and '"some, ), unmatched, } prenheses {[<" := "some, ), unmatched, } prenheses {[<"' and ''"' := '"'' and ''\'' := '''' and '',' := ','' and ''}' := '}'' and '')' := ')'' and ''(' := '('' and ''{' := '{''
21972199
Exception.tests.cpp:<line number>: failed: unexpected exception with message: '3.14000000000000012'
21982200
UniquePtr.tests.cpp:<line number>: passed: bptr->i == 3 for: 3 == 3
21992201
UniquePtr.tests.cpp:<line number>: passed: bptr->i == 3 for: 3 == 3
@@ -2720,6 +2722,9 @@ Skip.tests.cpp:<line number>: skipped: 'skipping because answer = 43'
27202722
StringManip.tests.cpp:<line number>: passed: splitStringRef("", ','), Equals(std::vector<StringRef>()) for: { } Equals: { }
27212723
StringManip.tests.cpp:<line number>: passed: splitStringRef("abc", ','), Equals(std::vector<StringRef>{"abc"}) for: { abc } Equals: { abc }
27222724
StringManip.tests.cpp:<line number>: passed: splitStringRef("abc,def", ','), Equals(std::vector<StringRef>{"abc", "def"}) for: { abc, def } Equals: { abc, def }
2725+
Message.tests.cpp:<line number>: failed: false with 1 message: 'input := { 7, 8, 9 }'
2726+
Message.tests.cpp:<line number>: failed: false with 4 messages: '"Count 1 to 3..." := "Count 1 to 3..."' and 'input := 1' and 'input := 2' and 'input := 3'
2727+
Message.tests.cpp:<line number>: failed: false with 4 messages: '"Count 4 to 6..." := "Count 4 to 6..."' and 'input := 4' and 'input := 5' and 'input := 6'
27232728
Message.tests.cpp:<line number>: failed: false with 4 messages: 'Count 1 to 3...' and '1' and '2' and '3'
27242729
Message.tests.cpp:<line number>: failed: false with 4 messages: 'Count 4 to 6...' and '4' and '5' and '6'
27252730
StringManip.tests.cpp:<line number>: passed: !(startsWith("", 'c')) for: !false
@@ -2884,7 +2889,7 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
28842889
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
28852890
Misc.tests.cpp:<line number>: passed:
28862891
Misc.tests.cpp:<line number>: passed:
2887-
test cases: 435 | 317 passed | 95 failed | 6 skipped | 17 failed as expected
2888-
assertions: 2299 | 2101 passed | 157 failed | 41 failed as expected
2892+
test cases: 439 | 319 passed | 97 failed | 6 skipped | 17 failed as expected
2893+
assertions: 2304 | 2103 passed | 160 failed | 41 failed as expected
28892894

28902895

tests/SelfTest/Baselines/compact.sw.multi.approved.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,8 @@ MatchersRanges.tests.cpp:<line number>: passed: a, !RangeEquals( b ) for: { 1, 2
21872187
MatchersRanges.tests.cpp:<line number>: passed: a, UnorderedRangeEquals( b ) for: { 1, 2, 3 } unordered elements are { 3, 2, 1 }
21882188
MatchersRanges.tests.cpp:<line number>: passed: vector_a, RangeEquals( array_a_plus_1, close_enough ) for: { 1, 2, 3 } elements are { 2, 3, 4 }
21892189
MatchersRanges.tests.cpp:<line number>: passed: vector_a, UnorderedRangeEquals( array_a_plus_1, close_enough ) for: { 1, 2, 3 } unordered elements are { 2, 3, 4 }
2190+
Message.tests.cpp:<line number>: passed: with 7 messages: 'a := 1' and 'b := 2' and 'c := 3' and 'a + b := 3' and 'a+b := 3' and 'c > b := true' and 'a == 1 := true'
2191+
Message.tests.cpp:<line number>: passed: with 11 messages: '("comma, in string", "escaped, \", ") := "escaped, ", "' and '"single quote in string,'," := "single quote in string,',"' and '"some escapes, \\,\\\\" := "some escapes, \,\\"' and '"some, ), unmatched, } prenheses {[<" := "some, ), unmatched, } prenheses {[<"' and ''"' := '"'' and ''\'' := '''' and '',' := ','' and ''}' := '}'' and '')' := ')'' and ''(' := '('' and ''{' := '{''
21902192
Exception.tests.cpp:<line number>: failed: unexpected exception with message: '3.14000000000000012'
21912193
UniquePtr.tests.cpp:<line number>: passed: bptr->i == 3 for: 3 == 3
21922194
UniquePtr.tests.cpp:<line number>: passed: bptr->i == 3 for: 3 == 3
@@ -2709,6 +2711,9 @@ Skip.tests.cpp:<line number>: skipped: 'skipping because answer = 43'
27092711
StringManip.tests.cpp:<line number>: passed: splitStringRef("", ','), Equals(std::vector<StringRef>()) for: { } Equals: { }
27102712
StringManip.tests.cpp:<line number>: passed: splitStringRef("abc", ','), Equals(std::vector<StringRef>{"abc"}) for: { abc } Equals: { abc }
27112713
StringManip.tests.cpp:<line number>: passed: splitStringRef("abc,def", ','), Equals(std::vector<StringRef>{"abc", "def"}) for: { abc, def } Equals: { abc, def }
2714+
Message.tests.cpp:<line number>: failed: false with 1 message: 'input := { 7, 8, 9 }'
2715+
Message.tests.cpp:<line number>: failed: false with 4 messages: '"Count 1 to 3..." := "Count 1 to 3..."' and 'input := 1' and 'input := 2' and 'input := 3'
2716+
Message.tests.cpp:<line number>: failed: false with 4 messages: '"Count 4 to 6..." := "Count 4 to 6..."' and 'input := 4' and 'input := 5' and 'input := 6'
27122717
Message.tests.cpp:<line number>: failed: false with 4 messages: 'Count 1 to 3...' and '1' and '2' and '3'
27132718
Message.tests.cpp:<line number>: failed: false with 4 messages: 'Count 4 to 6...' and '4' and '5' and '6'
27142719
StringManip.tests.cpp:<line number>: passed: !(startsWith("", 'c')) for: !false
@@ -2873,7 +2878,7 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
28732878
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
28742879
Misc.tests.cpp:<line number>: passed:
28752880
Misc.tests.cpp:<line number>: passed:
2876-
test cases: 435 | 317 passed | 95 failed | 6 skipped | 17 failed as expected
2877-
assertions: 2299 | 2101 passed | 157 failed | 41 failed as expected
2881+
test cases: 439 | 319 passed | 97 failed | 6 skipped | 17 failed as expected
2882+
assertions: 2304 | 2103 passed | 160 failed | 41 failed as expected
28782883

28792884

tests/SelfTest/Baselines/console.std.approved.txt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,39 @@ Skip.tests.cpp:<line number>: SKIPPED:
16781678
explicitly with message:
16791679
skipping because answer = 43
16801680

1681+
-------------------------------------------------------------------------------
1682+
stacks unscoped capture for vector
1683+
-------------------------------------------------------------------------------
1684+
Message.tests.cpp:<line number>
1685+
...............................................................................
1686+
1687+
Message.tests.cpp:<line number>: FAILED:
1688+
CHECK( false )
1689+
with message:
1690+
input := { 7, 8, 9 }
1691+
1692+
-------------------------------------------------------------------------------
1693+
stacks unscoped capture in loops
1694+
-------------------------------------------------------------------------------
1695+
Message.tests.cpp:<line number>
1696+
...............................................................................
1697+
1698+
Message.tests.cpp:<line number>: FAILED:
1699+
CHECK( false )
1700+
with messages:
1701+
"Count 1 to 3..." := "Count 1 to 3..."
1702+
input := 1
1703+
input := 2
1704+
input := 3
1705+
1706+
Message.tests.cpp:<line number>: FAILED:
1707+
CHECK( false )
1708+
with messages:
1709+
"Count 4 to 6..." := "Count 4 to 6..."
1710+
input := 4
1711+
input := 5
1712+
input := 6
1713+
16811714
-------------------------------------------------------------------------------
16821715
stacks unscoped info in loops
16831716
-------------------------------------------------------------------------------
@@ -1719,6 +1752,6 @@ due to unexpected exception with message:
17191752
Why would you throw a std::string?
17201753

17211754
===============================================================================
1722-
test cases: 435 | 335 passed | 76 failed | 7 skipped | 17 failed as expected
1723-
assertions: 2278 | 2101 passed | 136 failed | 41 failed as expected
1755+
test cases: 439 | 337 passed | 78 failed | 7 skipped | 17 failed as expected
1756+
assertions: 2283 | 2103 passed | 139 failed | 41 failed as expected
17241757

tests/SelfTest/Baselines/console.sw.approved.txt

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14232,6 +14232,43 @@ MatchersRanges.tests.cpp:<line number>: PASSED:
1423214232
with expansion:
1423314233
{ 1, 2, 3 } unordered elements are { 2, 3, 4 }
1423414234

14235+
-------------------------------------------------------------------------------
14236+
UNSCOPED CAPTURE can deal with complex expressions
14237+
-------------------------------------------------------------------------------
14238+
Message.tests.cpp:<line number>
14239+
...............................................................................
14240+
14241+
Message.tests.cpp:<line number>: PASSED:
14242+
with messages:
14243+
a := 1
14244+
b := 2
14245+
c := 3
14246+
a + b := 3
14247+
a+b := 3
14248+
c > b := true
14249+
a == 1 := true
14250+
14251+
-------------------------------------------------------------------------------
14252+
UNSCOPED_CAPTURE parses string and character constants
14253+
-------------------------------------------------------------------------------
14254+
Message.tests.cpp:<line number>
14255+
...............................................................................
14256+
14257+
Message.tests.cpp:<line number>: PASSED:
14258+
with messages:
14259+
("comma, in string", "escaped, \", ") := "escaped, ", "
14260+
"single quote in string,'," := "single quote in string,',"
14261+
"some escapes, \\,\\\\" := "some escapes, \,\\"
14262+
"some, ), unmatched, } prenheses {[<" := "some, ), unmatched, } prenheses {[
14263+
<"
14264+
'"' := '"'
14265+
'\'' := '''
14266+
',' := ','
14267+
'}' := '}'
14268+
')' := ')'
14269+
'(' := '('
14270+
'{' := '{'
14271+
1423514272
-------------------------------------------------------------------------------
1423614273
Unexpected exceptions can be translated
1423714274
-------------------------------------------------------------------------------
@@ -18117,6 +18154,39 @@ StringManip.tests.cpp:<line number>: PASSED:
1811718154
with expansion:
1811818155
{ abc, def } Equals: { abc, def }
1811918156

18157+
-------------------------------------------------------------------------------
18158+
stacks unscoped capture for vector
18159+
-------------------------------------------------------------------------------
18160+
Message.tests.cpp:<line number>
18161+
...............................................................................
18162+
18163+
Message.tests.cpp:<line number>: FAILED:
18164+
CHECK( false )
18165+
with message:
18166+
input := { 7, 8, 9 }
18167+
18168+
-------------------------------------------------------------------------------
18169+
stacks unscoped capture in loops
18170+
-------------------------------------------------------------------------------
18171+
Message.tests.cpp:<line number>
18172+
...............................................................................
18173+
18174+
Message.tests.cpp:<line number>: FAILED:
18175+
CHECK( false )
18176+
with messages:
18177+
"Count 1 to 3..." := "Count 1 to 3..."
18178+
input := 1
18179+
input := 2
18180+
input := 3
18181+
18182+
Message.tests.cpp:<line number>: FAILED:
18183+
CHECK( false )
18184+
with messages:
18185+
"Count 4 to 6..." := "Count 4 to 6..."
18186+
input := 4
18187+
input := 5
18188+
input := 6
18189+
1812018190
-------------------------------------------------------------------------------
1812118191
stacks unscoped info in loops
1812218192
-------------------------------------------------------------------------------
@@ -19267,6 +19337,6 @@ Misc.tests.cpp:<line number>
1926719337
Misc.tests.cpp:<line number>: PASSED:
1926819338

1926919339
===============================================================================
19270-
test cases: 435 | 317 passed | 95 failed | 6 skipped | 17 failed as expected
19271-
assertions: 2299 | 2101 passed | 157 failed | 41 failed as expected
19340+
test cases: 439 | 319 passed | 97 failed | 6 skipped | 17 failed as expected
19341+
assertions: 2304 | 2103 passed | 160 failed | 41 failed as expected
1927219342

0 commit comments

Comments
 (0)