Skip to content

Commit

Permalink
Add to string for std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Moussu committed Jan 25, 2019
1 parent 4109870 commit c4f5f78
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 7 deletions.
12 changes: 12 additions & 0 deletions include/internal/catch_compiler_capabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@
#endif
#endif

////////////////////////////////////////////////////////////////////////////////
// Check if optional is available and usable
#if defined(__has_include)
# if __has_include(<optional>) && defined(CATCH_CPP17_OR_GREATER)
# define CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL
# endif // __has_include(<optional>) && defined(CATCH_CPP17_OR_GREATER)
#endif // __has_include

////////////////////////////////////////////////////////////////////////////////
// Check if variant is available and usable
#if defined(__has_include)
Expand Down Expand Up @@ -220,6 +228,10 @@
# define CATCH_CONFIG_CPP11_TO_STRING
#endif

#if defined(CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_NO_CPP17_VARIANT) && !defined(CATCH_CONFIG_CPP17_OPTIONAL)
# define CATCH_CONFIG_CPP17_OPTIONAL
#endif

#if defined(CATCH_INTERNAL_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS) && !defined(CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS)
# define CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS
#endif
Expand Down
19 changes: 19 additions & 0 deletions include/internal/catch_tostring.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ namespace Catch {
# define CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER
# define CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
# define CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER
#endif

// Separate std::pair specialization
Expand All @@ -369,6 +370,24 @@ namespace Catch {
}
#endif // CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER

#if defined(CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER) && defined(CATCH_CONFIG_CPP17_VARIANT)
#include <optional>
namespace Catch {
template<typename T>
struct StringMaker<std::optional<T> > {
static std::string convert(const std::optional<T>& optional) {
ReusableStringStream rss;
if (optional.has_value()) {
rss << ::Catch::Detail::stringify(*optional);
} else {
rss << "{ }";
}
return rss.str();
}
};
}
#endif // CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER

// Separate std::tuple specialization
#if defined(CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER)
#include <tuple>
Expand Down
5 changes: 5 additions & 0 deletions projects/SelfTest/Baselines/compact.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1292,13 +1292,18 @@ ToStringGeneral.tests.cpp:<line number>: passed: Catch::Detail::stringify( map )
ToStringGeneral.tests.cpp:<line number>: passed: Catch::Detail::stringify( map ) == "{ { \"abc\", 1 }, { \"def\", 2 }, { \"ghi\", 3 } }" for: "{ { "abc", 1 }, { "def", 2 }, { "ghi", 3 } }"
==
"{ { "abc", 1 }, { "def", 2 }, { "ghi", 3 } }"
ToStringOptional.tests.cpp:<line number>: passed: "{ }" == ::Catch::Detail::stringify( type{} ) for: "{ }" == "{ }"
ToStringOptional.tests.cpp:<line number>: passed: "0" == ::Catch::Detail::stringify( type{ 0 } ) for: "0" == "0"
ToStringOptional.tests.cpp:<line number>: passed: "{ }" == ::Catch::Detail::stringify( type{} ) for: "{ }" == "{ }"
ToStringOptional.tests.cpp:<line number>: passed: "\"abc\"" == ::Catch::Detail::stringify( type{ "abc" } ) for: ""abc"" == ""abc""
ToStringPair.tests.cpp:<line number>: passed: ::Catch::Detail::stringify(value) == "{ 34, \"xyzzy\" }" for: "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }"
ToStringPair.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( value ) == "{ 34, \"xyzzy\" }" for: "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }"
ToStringGeneral.tests.cpp:<line number>: passed: Catch::Detail::stringify( emptySet ) == "{ }" for: "{ }" == "{ }"
ToStringGeneral.tests.cpp:<line number>: passed: Catch::Detail::stringify( set ) == "{ \"one\" }" for: "{ "one" }" == "{ "one" }"
ToStringGeneral.tests.cpp:<line number>: passed: Catch::Detail::stringify( set ) == "{ \"abc\", \"def\", \"ghi\" }" for: "{ "abc", "def", "ghi" }"
==
"{ "abc", "def", "ghi" }"
ToStringOptional.tests.cpp:<line number>: passed: "{ 0, { }, 2 }" == ::Catch::Detail::stringify( type{ 0, {}, 2 } ) for: "{ 0, { }, 2 }" == "{ 0, { }, 2 }"
ToStringPair.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( pr ) == "{ { \"green\", 55 } }" for: "{ { "green", 55 } }"
==
"{ { "green", 55 } }"
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 @@ -1170,6 +1170,6 @@ due to unexpected exception with message:
Why would you throw a std::string?

===============================================================================
test cases: 243 | 183 passed | 56 failed | 4 failed as expected
assertions: 1325 | 1189 passed | 115 failed | 21 failed as expected
test cases: 246 | 186 passed | 56 failed | 4 failed as expected
assertions: 1330 | 1194 passed | 115 failed | 21 failed as expected

47 changes: 45 additions & 2 deletions projects/SelfTest/Baselines/console.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9892,6 +9892,38 @@ with expansion:
==
"{ { "abc", 1 }, { "def", 2 }, { "ghi", 3 } }"

-------------------------------------------------------------------------------
std::optional<int> -> toString
-------------------------------------------------------------------------------
ToStringOptional.tests.cpp:<line number>
...............................................................................

ToStringOptional.tests.cpp:<line number>: PASSED:
REQUIRE( "{ }" == ::Catch::Detail::stringify( type{} ) )
with expansion:
"{ }" == "{ }"

ToStringOptional.tests.cpp:<line number>: PASSED:
REQUIRE( "0" == ::Catch::Detail::stringify( type{ 0 } ) )
with expansion:
"0" == "0"

-------------------------------------------------------------------------------
std::optional<std::string> -> toString
-------------------------------------------------------------------------------
ToStringOptional.tests.cpp:<line number>
...............................................................................

ToStringOptional.tests.cpp:<line number>: PASSED:
REQUIRE( "{ }" == ::Catch::Detail::stringify( type{} ) )
with expansion:
"{ }" == "{ }"

ToStringOptional.tests.cpp:<line number>: PASSED:
REQUIRE( "\"abc\"" == ::Catch::Detail::stringify( type{ "abc" } ) )
with expansion:
""abc"" == ""abc""

-------------------------------------------------------------------------------
std::pair<int,const std::string> -> toString
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -9952,6 +9984,17 @@ with expansion:
==
"{ "abc", "def", "ghi" }"

-------------------------------------------------------------------------------
std::vector<std::optional<int> > -> toString
-------------------------------------------------------------------------------
ToStringOptional.tests.cpp:<line number>
...............................................................................

ToStringOptional.tests.cpp:<line number>: PASSED:
REQUIRE( "{ 0, { }, 2 }" == ::Catch::Detail::stringify( type{ 0, {}, 2 } ) )
with expansion:
"{ 0, { }, 2 }" == "{ 0, { }, 2 }"

-------------------------------------------------------------------------------
std::vector<std::pair<std::string,int> > -> toString
-------------------------------------------------------------------------------
Expand Down Expand Up @@ -10629,6 +10672,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: PASSED:

===============================================================================
test cases: 243 | 170 passed | 69 failed | 4 failed as expected
assertions: 1339 | 1189 passed | 129 failed | 21 failed as expected
test cases: 246 | 173 passed | 69 failed | 4 failed as expected
assertions: 1344 | 1194 passed | 129 failed | 21 failed as expected

5 changes: 4 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="113" tests="1340" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testsuite name="<exe-name>" errors="17" failures="113" tests="1345" 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 @@ -924,11 +924,14 @@ Message.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="std::map is convertible string/empty" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::map is convertible string/single item" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::map is convertible string/several items" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::optional&lt;int> -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::optional&lt;std::string> -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::pair&lt;int,const std::string> -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::pair&lt;int,std::string> -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::set is convertible string/empty" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::set is convertible string/single item" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::set is convertible string/several items" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::vector&lt;std::optional&lt;int> > -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="std::vector&lt;std::pair&lt;std::string,int> > -> toString" time="{duration}"/>
<testcase classname="<exe-name>.global" name="string literals of different sizes can be compared" time="{duration}">
<failure message="&quot;first&quot; == &quot;second&quot;" type="REQUIRE">
Expand Down
53 changes: 51 additions & 2 deletions projects/SelfTest/Baselines/xml.sw.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11486,6 +11486,44 @@ loose text artifact
</Section>
<OverallResult success="true"/>
</TestCase>
<TestCase name="std::optional&lt;int> -> toString" tags="[optional][toString]" filename="projects/<exe-name>/UsageTests/ToStringOptional.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringOptional.tests.cpp" >
<Original>
"{ }" == ::Catch::Detail::stringify( type{} )
</Original>
<Expanded>
"{ }" == "{ }"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringOptional.tests.cpp" >
<Original>
"0" == ::Catch::Detail::stringify( type{ 0 } )
</Original>
<Expanded>
"0" == "0"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="std::optional&lt;std::string> -> toString" tags="[optional][toString]" filename="projects/<exe-name>/UsageTests/ToStringOptional.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringOptional.tests.cpp" >
<Original>
"{ }" == ::Catch::Detail::stringify( type{} )
</Original>
<Expanded>
"{ }" == "{ }"
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringOptional.tests.cpp" >
<Original>
"\"abc\"" == ::Catch::Detail::stringify( type{ "abc" } )
</Original>
<Expanded>
""abc"" == ""abc""
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="std::pair&lt;int,const std::string> -> toString" tags="[pair][toString]" filename="projects/<exe-name>/UsageTests/ToStringPair.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringPair.tests.cpp" >
<Original>
Expand Down Expand Up @@ -11546,6 +11584,17 @@ loose text artifact
</Section>
<OverallResult success="true"/>
</TestCase>
<TestCase name="std::vector&lt;std::optional&lt;int> > -> toString" tags="[optional][toString]" filename="projects/<exe-name>/UsageTests/ToStringOptional.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringOptional.tests.cpp" >
<Original>
"{ 0, { }, 2 }" == ::Catch::Detail::stringify( type{ 0, {}, 2 } )
</Original>
<Expanded>
"{ 0, { }, 2 }" == "{ 0, { }, 2 }"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="std::vector&lt;std::pair&lt;std::string,int> > -> toString" tags="[pair][toString]" filename="projects/<exe-name>/UsageTests/ToStringPair.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/ToStringPair.tests.cpp" >
<Original>
Expand Down Expand Up @@ -12264,7 +12313,7 @@ loose text artifact
</Section>
<OverallResult success="true"/>
</TestCase>
<OverallResults successes="1189" failures="130" expectedFailures="21"/>
<OverallResults successes="1194" failures="130" expectedFailures="21"/>
</Group>
<OverallResults successes="1189" failures="129" expectedFailures="21"/>
<OverallResults successes="1194" failures="129" expectedFailures="21"/>
</Catch>
23 changes: 23 additions & 0 deletions projects/SelfTest/UsageTests/ToStringOptional.tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#define CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER
#include "catch.hpp"

#if defined(CATCH_CONFIG_CPP17_VARIANT)

TEST_CASE( "std::optional<int> -> toString", "[toString][optional]" ) {
using type = std::optional<int>;
REQUIRE( "{ }" == ::Catch::Detail::stringify( type{} ) );
REQUIRE( "0" == ::Catch::Detail::stringify( type{ 0 } ) );
}

TEST_CASE( "std::optional<std::string> -> toString", "[toString][optional]" ) {
using type = std::optional<std::string>;
REQUIRE( "{ }" == ::Catch::Detail::stringify( type{} ) );
REQUIRE( "\"abc\"" == ::Catch::Detail::stringify( type{ "abc" } ) );
}

TEST_CASE( "std::vector<std::optional<int> > -> toString", "[toString][optional]" ) {
using type = std::vector<std::optional<int> >;
REQUIRE( "{ 0, { }, 2 }" == ::Catch::Detail::stringify( type{ 0, {}, 2 } ) );
}

#endif // CATCH_INTERNAL_CONFIG_CPP17_VARIANT

0 comments on commit c4f5f78

Please sign in to comment.