Skip to content

Commit

Permalink
Fix CAPTURE macro for nontrivial uses
Browse files Browse the repository at this point in the history
The previous implemetation was just plain broken for most of
possible uses, the new one should work (even though it is ugly
as all hell, and should be improved ASAP).

Fixes #1436
  • Loading branch information
horenmar committed Nov 19, 2018
1 parent 77f29c2 commit 478bc2b
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 22 deletions.
34 changes: 23 additions & 11 deletions include/internal/catch_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,30 @@ namespace Catch {


Capturer::Capturer( StringRef macroName, SourceLineInfo const& lineInfo, ResultWas::OfType resultType, StringRef names ) {
auto start = std::string::npos;
for( size_t pos = 0; pos <= names.size(); ++pos ) {
char c = names[pos];
if( pos == names.size() || c == ' ' || c == '\t' || c == ',' || c == ']' ) {
if( start != std::string::npos ) {
m_messages.push_back( MessageInfo( macroName, lineInfo, resultType ) );
m_messages.back().message = names.substr( start, pos-start) + " := ";
start = std::string::npos;
}
auto massage = [&] (size_t start, size_t end) {
while (names[start] == ',' || isspace(names[start])) {
++start;
}
while (names[end] == ',' || isspace(names[end])) {
--end;
}
else if( c != '[' && c != ']' && start == std::string::npos )
return names.substr(start, end - start + 1);
};

size_t start = 0;
for (size_t pos = 0; pos < names.size(); ++pos) {
char c = names[pos];
if (start != pos && c == ',') {
m_messages.emplace_back(macroName, lineInfo, resultType);
m_messages.back().message = massage(start, pos);
m_messages.back().message += " := ";
start = pos;
}
}
if (start != names.size()) {
m_messages.emplace_back(macroName, lineInfo, resultType);
m_messages.back().message = massage(start, names.size() - 1);
m_messages.back().message += " := ";
}
}
Capturer::~Capturer() {
Expand All @@ -82,7 +94,7 @@ namespace Catch {
}
}

void Capturer::captureValue( size_t index, StringRef value ) {
void Capturer::captureValue( size_t index, std::string const& value ) {
assert( index < m_messages.size() );
m_messages[index].message += value;
m_resultCapture.pushScopedMessage( m_messages[index] );
Expand Down
8 changes: 4 additions & 4 deletions include/internal/catch_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ namespace Catch {
Capturer( StringRef macroName, SourceLineInfo const& lineInfo, ResultWas::OfType resultType, StringRef names );
~Capturer();

void captureValue( size_t index, StringRef value );
void captureValue( size_t index, std::string const& value );

template<typename T>
void captureValues( size_t index, T&& value ) {
void captureValues( size_t index, T const& value ) {
captureValue( index, Catch::Detail::stringify( value ) );
}

template<typename T, typename... Ts>
void captureValues( size_t index, T&& value, Ts&&... values ) {
captureValues( index, value );
void captureValues( size_t index, T const& value, Ts const&... values ) {
captureValue( index, Catch::Detail::stringify(value) );
captureValues( index+1, values... );
}
};
Expand Down
1 change: 1 addition & 0 deletions projects/SelfTest/Baselines/compact.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Approx.tests.cpp:<line number>: passed: NAN != Approx(NAN) for: nanf != Approx(
Approx.tests.cpp:<line number>: passed: !(NAN == Approx(NAN)) for: !(nanf == Approx( nan ))
Tricky.tests.cpp:<line number>: passed: y.v == 0 for: 0 == 0
Tricky.tests.cpp:<line number>: passed: 0 == y.v for: 0 == 0
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'
ToStringGeneral.tests.cpp:<line number>: passed: true with 1 message: 'i := 2'
ToStringGeneral.tests.cpp:<line number>: passed: true with 1 message: '3'
ToStringGeneral.tests.cpp:<line number>: passed: tab == '\t' for: '\t' == '\t'
Expand Down
4 changes: 2 additions & 2 deletions projects/SelfTest/Baselines/console.std.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,6 @@ due to unexpected exception with message:
Why would you throw a std::string?

===============================================================================
test cases: 226 | 170 passed | 52 failed | 4 failed as expected
assertions: 1308 | 1176 passed | 111 failed | 21 failed as expected
test cases: 227 | 171 passed | 52 failed | 4 failed as expected
assertions: 1309 | 1177 passed | 111 failed | 21 failed as expected

20 changes: 18 additions & 2 deletions projects/SelfTest/Baselines/console.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,22 @@ Tricky.tests.cpp:<line number>: PASSED:
with expansion:
0 == 0

-------------------------------------------------------------------------------
CAPTURE can deal with complex expressions
-------------------------------------------------------------------------------
Message.tests.cpp:<line number>
...............................................................................

Message.tests.cpp:<line number>: PASSED:
with messages:
a := 1
b := 2
c := 3
a + b := 3
a+b := 3
c > b := true
a == 1 := true

-------------------------------------------------------------------------------
Capture and info messages
Capture should stringify like assertions
Expand Down Expand Up @@ -10432,6 +10448,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: PASSED:

===============================================================================
test cases: 226 | 157 passed | 65 failed | 4 failed as expected
assertions: 1322 | 1176 passed | 125 failed | 21 failed as expected
test cases: 227 | 158 passed | 65 failed | 4 failed as expected
assertions: 1323 | 1177 passed | 125 failed | 21 failed as expected

3 changes: 2 additions & 1 deletion projects/SelfTest/Baselines/junit.sw.approved.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact
>
<testsuite name="<exe-name>" errors="17" failures="109" tests="1323" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="17" failures="109" tests="1324" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testcase classname="<exe-name>.global" name="# A test name that starts with a #" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#1005: Comparing pointer to int and long (NULL can be either on various systems)" time="{duration}"/>
<testcase classname="<exe-name>.global" name="#1027" time="{duration}"/>
Expand Down Expand Up @@ -141,6 +141,7 @@ Exception.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="Assertions then sections/A section/Another other section" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Assorted miscellaneous tests" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Bitfields can be captured (#1027)" time="{duration}"/>
<testcase classname="<exe-name>.global" name="CAPTURE can deal with complex expressions" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Capture and info messages/Capture should stringify like assertions" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Capture and info messages/Info should NOT stringify the way assertions do" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Character pretty printing/Specifically escaped" time="{duration}"/>
Expand Down
28 changes: 26 additions & 2 deletions projects/SelfTest/Baselines/xml.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,30 @@
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="CAPTURE can deal with complex expressions" tags="[messages]" filename="projects/<exe-name>/UsageTests/Message.tests.cpp" >
<Info>
a := 1
</Info>
<Info>
b := 2
</Info>
<Info>
c := 3
</Info>
<Info>
a + b := 3
</Info>
<Info>
a+b := 3
</Info>
<Info>
c > b := true
</Info>
<Info>
a == 1 := true
</Info>
<OverallResult success="true"/>
</TestCase>
<TestCase name="Capture and info messages" filename="projects/<exe-name>/UsageTests/ToStringGeneral.tests.cpp" >
<Section name="Capture should stringify like assertions" filename="projects/<exe-name>/UsageTests/ToStringGeneral.tests.cpp" >
<Info>
Expand Down Expand Up @@ -12051,7 +12075,7 @@ loose text artifact
</Section>
<OverallResult success="true"/>
</TestCase>
<OverallResults successes="1176" failures="126" expectedFailures="21"/>
<OverallResults successes="1177" failures="126" expectedFailures="21"/>
</Group>
<OverallResults successes="1176" failures="125" expectedFailures="21"/>
<OverallResults successes="1177" failures="125" expectedFailures="21"/>
</Catch>
8 changes: 8 additions & 0 deletions projects/SelfTest/UsageTests/Message.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,11 @@ TEST_CASE( "Pointers can be converted to strings", "[messages][.][approvals]" )
WARN( "actual address of p: " << &p );
WARN( "toString(p): " << ::Catch::Detail::stringify( &p ) );
}

TEST_CASE( "CAPTURE can deal with complex expressions", "[messages]" ) {
int a = 1;
int b = 2;
int c = 3;
CAPTURE( a, b, c, a + b, a+b, c > b, a == 1);
SUCCEED();
}

0 comments on commit 478bc2b

Please sign in to comment.