diff --git a/README.md b/README.md index 8729164..ecd6e03 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,15 @@ AssertThat(x, IsLessThanOrEqualTo(6)); AssertThat(x, Is().LessThanOrEqualTo(6)); ``` +### Pointer Constraints + +Used to check for `nullptr` equality. + +```cpp +AssertThat(x, IsNull()); +AssertThat(x, Is().Null()); +``` + ### String Constraints String assertions in Snowhouse are used to verify the values of STL strings (std::string). diff --git a/example/basic_assertions.cpp b/example/basic_assertions.cpp index 0af2036..2766ec0 100644 --- a/example/basic_assertions.cpp +++ b/example/basic_assertions.cpp @@ -1,4 +1,5 @@ #include +#include #include using namespace snowhouse; #include "tests.h" @@ -182,4 +183,46 @@ void BasicAssertions() AssertTestFails(Assert::That(6, IsLessThanOrEqualTo(5)), "Expected: less than or equal to 5\nActual: 6\n"); } + +#if __cplusplus > 199711L + std::cout << "ShouldHandleNull" << std::endl; + { + Assert::That(nullptr, IsNull()); + } + + std::cout << "ShouldHandleNull" << std::endl; + { + Assert::That(nullptr, Is().Null()); + } + + std::cout << "ShouldHandleNotNull" << std::endl; + { + int anInt = 0; + Assert::That(&anInt, ! IsNull()); + } + + std::cout << "ShouldDetectWhenIsNullFails" << std::endl; + { + int anInt = 0; + std::ostringstream message; + message << "Expected: equal to nullptr\nActual: " << &anInt << "\n"; + AssertTestFails(Assert::That(&anInt, IsNull()), message.str()); + } + + std::cout << "ShouldDetectWhenIsNullFails" << std::endl; + { + int anInt = 0; + std::ostringstream message; + message << "Expected: equal to nullptr\nActual: " << &anInt << "\n"; + AssertTestFails(Assert::That(&anInt, Is().Null()), message.str()); + } + + std::cout << "ShouldDetectWhenIsNotNullFails" << std::endl; + { + std::ostringstream message; + message << "Expected: not equal to nullptr\nActual: nullptr\n"; + + AssertTestFails(Assert::That(nullptr, ! IsNull()), message.str()); + } +#endif } diff --git a/snowhouse/assertmacro.h b/snowhouse/assertmacro.h index cc12bb7..df5b4b3 100644 --- a/snowhouse/assertmacro.h +++ b/snowhouse/assertmacro.h @@ -10,12 +10,12 @@ #include "assert.h" #define SNOWHOUSE_ASSERT_THAT(p1,p2,FAILURE_HANDLER)\ - ConfigurableAssert::That((p1), (p2), __FILE__, __LINE__);\ + ::snowhouse::ConfigurableAssert::That((p1), (p2), __FILE__, __LINE__);\ #ifndef SNOWHOUSE_NO_MACROS #define AssertThat(p1,p2)\ - SNOWHOUSE_ASSERT_THAT((p1), (p2), DefaultFailureHandler);\ + SNOWHOUSE_ASSERT_THAT((p1), (p2), ::snowhouse::DefaultFailureHandler);\ #endif // SNOWHOUSE_NO_MACROS diff --git a/snowhouse/constraints/equalsconstraint.h b/snowhouse/constraints/equalsconstraint.h index cbdc405..a47f6bf 100644 --- a/snowhouse/constraints/equalsconstraint.h +++ b/snowhouse/constraints/equalsconstraint.h @@ -7,6 +7,8 @@ #ifndef IGLOO_EQUALSCONSTRAINT_H #define IGLOO_EQUALSCONSTRAINT_H +#include + #include "./expressions/expression.h" namespace snowhouse { @@ -49,6 +51,13 @@ namespace snowhouse { return EqualsConstraint(true); } +#if __cplusplus > 199711L + inline EqualsConstraint IsNull() + { + return EqualsConstraint(nullptr); + } +#endif + template <> struct Stringizer< EqualsConstraint< bool > > { diff --git a/snowhouse/exceptions.h b/snowhouse/exceptions.h index b89aa42..22ad11e 100644 --- a/snowhouse/exceptions.h +++ b/snowhouse/exceptions.h @@ -78,7 +78,7 @@ namespace snowhouse { #define IGLOO_CONCAT(a, b) IGLOO_CONCAT2(a, b) #define SNOWHOUSE_ASSERT_THROWS(EXCEPTION_TYPE, METHOD, FAILURE_HANDLER_TYPE) \ -ExceptionStorage IGLOO_CONCAT(IGLOO_storage_, __LINE__); IGLOO_CONCAT(IGLOO_storage_, __LINE__).compiler_thinks_i_am_unused(); \ +::snowhouse::ExceptionStorage IGLOO_CONCAT(IGLOO_storage_, __LINE__); IGLOO_CONCAT(IGLOO_storage_, __LINE__).compiler_thinks_i_am_unused(); \ { \ bool wrong_exception = false; \ bool no_exception = false; \ @@ -89,7 +89,7 @@ ExceptionStorage IGLOO_CONCAT(IGLOO_storage_, __LINE__); IGLOO_C } \ catch (const EXCEPTION_TYPE& e) \ { \ - ExceptionStorage::store(e); \ + ::snowhouse::ExceptionStorage::store(e); \ } \ catch(...) \ { \ @@ -99,19 +99,19 @@ ExceptionStorage IGLOO_CONCAT(IGLOO_storage_, __LINE__); IGLOO_C { \ std::ostringstream stm; \ stm << "Expected " << #EXCEPTION_TYPE << ". No exception was thrown."; \ - ConfigurableAssert::Failure(stm.str()); \ + ::snowhouse::ConfigurableAssert::Failure(stm.str()); \ } \ if(wrong_exception) \ { \ std::ostringstream stm; \ stm << "Expected " << #EXCEPTION_TYPE << ". Wrong exception was thrown."; \ - ConfigurableAssert::Failure(stm.str()); \ + ::snowhouse::ConfigurableAssert::Failure(stm.str()); \ } \ } #ifndef SNOWHOUSE_NO_MACROS -#define AssertThrows(EXCEPTION_TYPE, METHOD) SNOWHOUSE_ASSERT_THROWS(EXCEPTION_TYPE, (METHOD), DefaultFailureHandler) +#define AssertThrows(EXCEPTION_TYPE, METHOD) SNOWHOUSE_ASSERT_THROWS(EXCEPTION_TYPE, (METHOD), ::snowhouse::DefaultFailureHandler) #endif // SNOWHOUSE_NO_MACROS diff --git a/snowhouse/fluent/expressionbuilder.h b/snowhouse/fluent/expressionbuilder.h index fce6cb9..f0889f1 100644 --- a/snowhouse/fluent/expressionbuilder.h +++ b/snowhouse/fluent/expressionbuilder.h @@ -7,6 +7,8 @@ #ifndef IGLOO_EXPRESSIONBUILDER_H #define IGLOO_EXPRESSIONBUILDER_H +#include + namespace snowhouse { // ---- Evaluation of list of constraints @@ -79,6 +81,14 @@ namespace snowhouse { return EqualTo(true); } +#if __cplusplus > 199711L + ExpressionBuilder >, Nil> >::t> + Null() + { + return EqualTo(nullptr); + } +#endif + ExpressionBuilder >, Nil> >::t> EqualTo(const char* expected) { diff --git a/snowhouse/stringize.h b/snowhouse/stringize.h index ba2a017..42249f5 100644 --- a/snowhouse/stringize.h +++ b/snowhouse/stringize.h @@ -7,6 +7,8 @@ #ifndef IGLOO_STRINGIZE_H #define IGLOO_STRINGIZE_H +#include + namespace snowhouse { namespace detail { @@ -85,6 +87,18 @@ namespace snowhouse { return detail::DefaultStringizer< T, detail::is_output_streamable::value >::ToString(value); } }; + +#if __cplusplus > 199711L + // We need this because nullptr_t has ambiguous overloads of operator<< in the standard library. + template<> + struct Stringizer + { + static std::string ToString(std::nullptr_t) + { + return "nullptr"; + } + }; +#endif } #endif