Skip to content

Commit 4e18ef2

Browse files
committed
Add Unscoped Capture Logger and Add Unit Tests
1 parent ccabd4d commit 4e18ef2

20 files changed

+687
-0
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ Message from section two
295295
:test-result: PASS Tracker
296296
:test-result: PASS Trim strings
297297
:test-result: PASS Type conversions of RangeEquals and similar
298+
:test-result: PASS UNSCOPED CAPTURE can deal with complex expressions
299+
:test-result: PASS UNSCOPED_CAPTURE parses string and character constants
298300
:test-result: FAIL Unexpected exceptions can be translated
299301
:test-result: PASS Upcasting special member functions
300302
:test-result: PASS Usage of AllMatch range matcher
@@ -394,6 +396,7 @@ b1!
394396
:test-result: PASS shortened hide tags are split apart
395397
:test-result: SKIP skipped tests can optionally provide a reason
396398
:test-result: PASS splitString
399+
:test-result: FAIL stacks unscoped capture in loops
397400
:test-result: FAIL stacks unscoped info in loops
398401
:test-result: PASS startsWith
399402
:test-result: PASS std::map is convertible string

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@
288288
:test-result: PASS Tracker
289289
:test-result: PASS Trim strings
290290
:test-result: PASS Type conversions of RangeEquals and similar
291+
:test-result: PASS UNSCOPED CAPTURE can deal with complex expressions
292+
:test-result: PASS UNSCOPED_CAPTURE parses string and character constants
291293
:test-result: FAIL Unexpected exceptions can be translated
292294
:test-result: PASS Upcasting special member functions
293295
:test-result: PASS Usage of AllMatch range matcher
@@ -383,6 +385,7 @@
383385
:test-result: PASS shortened hide tags are split apart
384386
:test-result: SKIP skipped tests can optionally provide a reason
385387
:test-result: PASS splitString
388+
:test-result: FAIL stacks unscoped capture in loops
386389
:test-result: FAIL stacks unscoped info in loops
387390
:test-result: PASS startsWith
388391
:test-result: PASS std::map is convertible string

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,6 +2184,8 @@ MatchersRanges.tests.cpp:<line number>: passed: a, !RangeEquals( b ) for: { 1, 2
21842184
MatchersRanges.tests.cpp:<line number>: passed: a, UnorderedRangeEquals( b ) for: { 1, 2, 3 } unordered elements are { 3, 2, 1 }
21852185
MatchersRanges.tests.cpp:<line number>: passed: vector_a, RangeEquals( array_a_plus_1, close_enough ) for: { 1, 2, 3 } elements are { 2, 3, 4 }
21862186
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 }
2187+
Message.tests.cpp:<line number>: passed: with 14 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' and '' and '' and '' and '' and '' and '' and ''
2188+
Message.tests.cpp:<line number>: passed: with 22 messages: '("comma, in string", "escaped, \", ") := "escaped, ", "' and '"single quote in string,'," := "single quote in string,',"' and '"some escapes, \\,\\\\" := "some escapes, \,\\"' and '' and '' and '' and '"some, ), unmatched, } prenheses {[<" := "some, ), unmatched, } prenheses {[<"' and '' and ''"' := '"'' and ''\'' := '''' and '',' := ','' and ''}' := '}'' and '')' := ')'' and ''(' := '('' and ''{' := '{'' and '' and '' and '' and '' and '' and '' and ''
21872189
Exception.tests.cpp:<line number>: failed: unexpected exception with message: '3.14000000000000012'
21882190
UniquePtr.tests.cpp:<line number>: passed: bptr->i == 3 for: 3 == 3
21892191
UniquePtr.tests.cpp:<line number>: passed: bptr->i == 3 for: 3 == 3
@@ -2710,6 +2712,8 @@ Skip.tests.cpp:<line number>: skipped: 'skipping because answer = 43'
27102712
StringManip.tests.cpp:<line number>: passed: splitStringRef("", ','), Equals(std::vector<StringRef>()) for: { } Equals: { }
27112713
StringManip.tests.cpp:<line number>: passed: splitStringRef("abc", ','), Equals(std::vector<StringRef>{"abc"}) for: { abc } Equals: { abc }
27122714
StringManip.tests.cpp:<line number>: passed: splitStringRef("abc,def", ','), Equals(std::vector<StringRef>{"abc", "def"}) for: { abc, def } Equals: { abc, def }
2715+
Message.tests.cpp:<line number>: failed: false with 5 messages: '"Count 1 to 3..." := "Count 1 to 3..."' and '' and '' and '' and ''
2716+
Message.tests.cpp:<line number>: failed: false with 6 messages: '' and '"Count 4 to 6..." := "Count 4 to 6..."' and '' and '' and '' and ''
27132717
Message.tests.cpp:<line number>: failed: false with 4 messages: 'Count 1 to 3...' and '1' and '2' and '3'
27142718
Message.tests.cpp:<line number>: failed: false with 4 messages: 'Count 4 to 6...' and '4' and '5' and '6'
27152719
StringManip.tests.cpp:<line number>: passed: !(startsWith("", 'c')) for: !false
@@ -2871,7 +2875,12 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
28712875
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
28722876
Misc.tests.cpp:<line number>: passed:
28732877
Misc.tests.cpp:<line number>: passed:
2878+
<<<<<<< HEAD
28742879
test cases: 432 | 314 passed | 95 failed | 6 skipped | 17 failed as expected
28752880
assertions: 2290 | 2092 passed | 157 failed | 41 failed as expected
2881+
=======
2882+
test cases: 434 | 315 passed | 96 failed | 6 skipped | 17 failed as expected
2883+
assertions: 2292 | 2092 passed | 159 failed | 41 failed as expected
2884+
>>>>>>> b280084d (Changing Baseline Files for New Tests)
28762885

28772886

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,8 @@ MatchersRanges.tests.cpp:<line number>: passed: a, !RangeEquals( b ) for: { 1, 2
21772177
MatchersRanges.tests.cpp:<line number>: passed: a, UnorderedRangeEquals( b ) for: { 1, 2, 3 } unordered elements are { 3, 2, 1 }
21782178
MatchersRanges.tests.cpp:<line number>: passed: vector_a, RangeEquals( array_a_plus_1, close_enough ) for: { 1, 2, 3 } elements are { 2, 3, 4 }
21792179
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 }
2180+
Message.tests.cpp:<line number>: passed: with 14 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' and '' and '' and '' and '' and '' and '' and ''
2181+
Message.tests.cpp:<line number>: passed: with 22 messages: '("comma, in string", "escaped, \", ") := "escaped, ", "' and '"single quote in string,'," := "single quote in string,',"' and '"some escapes, \\,\\\\" := "some escapes, \,\\"' and '' and '' and '' and '"some, ), unmatched, } prenheses {[<" := "some, ), unmatched, } prenheses {[<"' and '' and ''"' := '"'' and ''\'' := '''' and '',' := ','' and ''}' := '}'' and '')' := ')'' and ''(' := '('' and ''{' := '{'' and '' and '' and '' and '' and '' and '' and ''
21802182
Exception.tests.cpp:<line number>: failed: unexpected exception with message: '3.14000000000000012'
21812183
UniquePtr.tests.cpp:<line number>: passed: bptr->i == 3 for: 3 == 3
21822184
UniquePtr.tests.cpp:<line number>: passed: bptr->i == 3 for: 3 == 3
@@ -2699,6 +2701,8 @@ Skip.tests.cpp:<line number>: skipped: 'skipping because answer = 43'
26992701
StringManip.tests.cpp:<line number>: passed: splitStringRef("", ','), Equals(std::vector<StringRef>()) for: { } Equals: { }
27002702
StringManip.tests.cpp:<line number>: passed: splitStringRef("abc", ','), Equals(std::vector<StringRef>{"abc"}) for: { abc } Equals: { abc }
27012703
StringManip.tests.cpp:<line number>: passed: splitStringRef("abc,def", ','), Equals(std::vector<StringRef>{"abc", "def"}) for: { abc, def } Equals: { abc, def }
2704+
Message.tests.cpp:<line number>: failed: false with 5 messages: '"Count 1 to 3..." := "Count 1 to 3..."' and '' and '' and '' and ''
2705+
Message.tests.cpp:<line number>: failed: false with 6 messages: '' and '"Count 4 to 6..." := "Count 4 to 6..."' and '' and '' and '' and ''
27022706
Message.tests.cpp:<line number>: failed: false with 4 messages: 'Count 1 to 3...' and '1' and '2' and '3'
27032707
Message.tests.cpp:<line number>: failed: false with 4 messages: 'Count 4 to 6...' and '4' and '5' and '6'
27042708
StringManip.tests.cpp:<line number>: passed: !(startsWith("", 'c')) for: !false
@@ -2860,7 +2864,12 @@ InternalBenchmark.tests.cpp:<line number>: passed: med == 18. for: 18.0 == 18.0
28602864
InternalBenchmark.tests.cpp:<line number>: passed: q3 == 23. for: 23.0 == 23.0
28612865
Misc.tests.cpp:<line number>: passed:
28622866
Misc.tests.cpp:<line number>: passed:
2867+
<<<<<<< HEAD
28632868
test cases: 432 | 314 passed | 95 failed | 6 skipped | 17 failed as expected
28642869
assertions: 2290 | 2092 passed | 157 failed | 41 failed as expected
2870+
=======
2871+
test cases: 434 | 315 passed | 96 failed | 6 skipped | 17 failed as expected
2872+
assertions: 2292 | 2092 passed | 159 failed | 41 failed as expected
2873+
>>>>>>> b280084d (Changing Baseline Files for New Tests)
28652874

28662875

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

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

1681+
-------------------------------------------------------------------------------
1682+
stacks unscoped capture in loops
1683+
-------------------------------------------------------------------------------
1684+
Message.tests.cpp:<line number>
1685+
...............................................................................
1686+
1687+
Message.tests.cpp:<line number>: FAILED:
1688+
CHECK( false )
1689+
with messages:
1690+
"Count 1 to 3..." := "Count 1 to 3..."
1691+
1692+
1693+
1694+
1695+
1696+
Message.tests.cpp:<line number>: FAILED:
1697+
CHECK( false )
1698+
with messages:
1699+
1700+
"Count 4 to 6..." := "Count 4 to 6..."
1701+
1702+
1703+
1704+
1705+
16811706
-------------------------------------------------------------------------------
16821707
stacks unscoped info in loops
16831708
-------------------------------------------------------------------------------
@@ -1719,6 +1744,11 @@ due to unexpected exception with message:
17191744
Why would you throw a std::string?
17201745

17211746
===============================================================================
1747+
<<<<<<< HEAD
17221748
test cases: 432 | 332 passed | 76 failed | 7 skipped | 17 failed as expected
17231749
assertions: 2269 | 2092 passed | 136 failed | 41 failed as expected
1750+
=======
1751+
test cases: 434 | 333 passed | 77 failed | 7 skipped | 17 failed as expected
1752+
assertions: 2271 | 2092 passed | 138 failed | 41 failed as expected
1753+
>>>>>>> b280084d (Changing Baseline Files for New Tests)
17241754

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14178,6 +14178,61 @@ MatchersRanges.tests.cpp:<line number>: PASSED:
1417814178
with expansion:
1417914179
{ 1, 2, 3 } unordered elements are { 2, 3, 4 }
1418014180

14181+
-------------------------------------------------------------------------------
14182+
UNSCOPED CAPTURE can deal with complex expressions
14183+
-------------------------------------------------------------------------------
14184+
Message.tests.cpp:<line number>
14185+
...............................................................................
14186+
14187+
Message.tests.cpp:<line number>: PASSED:
14188+
with messages:
14189+
a := 1
14190+
b := 2
14191+
c := 3
14192+
a + b := 3
14193+
a+b := 3
14194+
c > b := true
14195+
a == 1 := true
14196+
14197+
14198+
14199+
14200+
14201+
14202+
14203+
14204+
-------------------------------------------------------------------------------
14205+
UNSCOPED_CAPTURE parses string and character constants
14206+
-------------------------------------------------------------------------------
14207+
Message.tests.cpp:<line number>
14208+
...............................................................................
14209+
14210+
Message.tests.cpp:<line number>: PASSED:
14211+
with messages:
14212+
("comma, in string", "escaped, \", ") := "escaped, ", "
14213+
"single quote in string,'," := "single quote in string,',"
14214+
"some escapes, \\,\\\\" := "some escapes, \,\\"
14215+
14216+
14217+
14218+
"some, ), unmatched, } prenheses {[<" := "some, ), unmatched, } prenheses {[
14219+
<"
14220+
14221+
'"' := '"'
14222+
'\'' := '''
14223+
',' := ','
14224+
'}' := '}'
14225+
')' := ')'
14226+
'(' := '('
14227+
'{' := '{'
14228+
14229+
14230+
14231+
14232+
14233+
14234+
14235+
1418114236
-------------------------------------------------------------------------------
1418214237
Unexpected exceptions can be translated
1418314238
-------------------------------------------------------------------------------
@@ -18063,6 +18118,31 @@ StringManip.tests.cpp:<line number>: PASSED:
1806318118
with expansion:
1806418119
{ abc, def } Equals: { abc, def }
1806518120

18121+
-------------------------------------------------------------------------------
18122+
stacks unscoped capture in loops
18123+
-------------------------------------------------------------------------------
18124+
Message.tests.cpp:<line number>
18125+
...............................................................................
18126+
18127+
Message.tests.cpp:<line number>: FAILED:
18128+
CHECK( false )
18129+
with messages:
18130+
"Count 1 to 3..." := "Count 1 to 3..."
18131+
18132+
18133+
18134+
18135+
18136+
Message.tests.cpp:<line number>: FAILED:
18137+
CHECK( false )
18138+
with messages:
18139+
18140+
"Count 4 to 6..." := "Count 4 to 6..."
18141+
18142+
18143+
18144+
18145+
1806618146
-------------------------------------------------------------------------------
1806718147
stacks unscoped info in loops
1806818148
-------------------------------------------------------------------------------
@@ -19200,6 +19280,11 @@ Misc.tests.cpp:<line number>
1920019280
Misc.tests.cpp:<line number>: PASSED:
1920119281

1920219282
===============================================================================
19283+
<<<<<<< HEAD
1920319284
test cases: 432 | 314 passed | 95 failed | 6 skipped | 17 failed as expected
1920419285
assertions: 2290 | 2092 passed | 157 failed | 41 failed as expected
19286+
=======
19287+
test cases: 434 | 315 passed | 96 failed | 6 skipped | 17 failed as expected
19288+
assertions: 2292 | 2092 passed | 159 failed | 41 failed as expected
19289+
>>>>>>> b280084d (Changing Baseline Files for New Tests)
1920519290

0 commit comments

Comments
 (0)