Skip to content

Commit

Permalink
Merge branch 'simon-bourne-pointers'
Browse files Browse the repository at this point in the history
Merges in the changes in PR #11, but contains fixes for having both pre
C++11 and C++11 code in the same code base.

Right now pre C++11 code has no access to IsNull.

This should also fix Issue banditcpp/bandit#50
  • Loading branch information
joakimkarlsson committed Jul 18, 2015
2 parents 61ec284 + d06c692 commit 81438c1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
43 changes: 43 additions & 0 deletions example/basic_assertions.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdexcept>
#include <sstream>
#include <snowhouse/snowhouse.h>
using namespace snowhouse;
#include "tests.h"
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions snowhouse/assertmacro.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#include "assert.h"

#define SNOWHOUSE_ASSERT_THAT(p1,p2,FAILURE_HANDLER)\
ConfigurableAssert<FAILURE_HANDLER>::That((p1), (p2), __FILE__, __LINE__);\
::snowhouse::ConfigurableAssert<FAILURE_HANDLER>::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

Expand Down
9 changes: 9 additions & 0 deletions snowhouse/constraints/equalsconstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef IGLOO_EQUALSCONSTRAINT_H
#define IGLOO_EQUALSCONSTRAINT_H

#include <cstddef>

#include "./expressions/expression.h"

namespace snowhouse {
Expand Down Expand Up @@ -49,6 +51,13 @@ namespace snowhouse {
return EqualsConstraint<bool>(true);
}

#if __cplusplus > 199711L
inline EqualsConstraint<std::nullptr_t> IsNull()
{
return EqualsConstraint<std::nullptr_t>(nullptr);
}
#endif

template <>
struct Stringizer< EqualsConstraint< bool > >
{
Expand Down
10 changes: 5 additions & 5 deletions snowhouse/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<EXCEPTION_TYPE> IGLOO_CONCAT(IGLOO_storage_, __LINE__); IGLOO_CONCAT(IGLOO_storage_, __LINE__).compiler_thinks_i_am_unused(); \
::snowhouse::ExceptionStorage<EXCEPTION_TYPE> IGLOO_CONCAT(IGLOO_storage_, __LINE__); IGLOO_CONCAT(IGLOO_storage_, __LINE__).compiler_thinks_i_am_unused(); \
{ \
bool wrong_exception = false; \
bool no_exception = false; \
Expand All @@ -89,7 +89,7 @@ ExceptionStorage<EXCEPTION_TYPE> IGLOO_CONCAT(IGLOO_storage_, __LINE__); IGLOO_C
} \
catch (const EXCEPTION_TYPE& e) \
{ \
ExceptionStorage<EXCEPTION_TYPE>::store(e); \
::snowhouse::ExceptionStorage<EXCEPTION_TYPE>::store(e); \
} \
catch(...) \
{ \
Expand All @@ -99,19 +99,19 @@ ExceptionStorage<EXCEPTION_TYPE> IGLOO_CONCAT(IGLOO_storage_, __LINE__); IGLOO_C
{ \
std::ostringstream stm; \
stm << "Expected " << #EXCEPTION_TYPE << ". No exception was thrown."; \
ConfigurableAssert<FAILURE_HANDLER_TYPE>::Failure(stm.str()); \
::snowhouse::ConfigurableAssert<FAILURE_HANDLER_TYPE>::Failure(stm.str()); \
} \
if(wrong_exception) \
{ \
std::ostringstream stm; \
stm << "Expected " << #EXCEPTION_TYPE << ". Wrong exception was thrown."; \
ConfigurableAssert<FAILURE_HANDLER_TYPE>::Failure(stm.str()); \
::snowhouse::ConfigurableAssert<FAILURE_HANDLER_TYPE>::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

Expand Down
10 changes: 10 additions & 0 deletions snowhouse/fluent/expressionbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef IGLOO_EXPRESSIONBUILDER_H
#define IGLOO_EXPRESSIONBUILDER_H

#include <cstddef>

namespace snowhouse {

// ---- Evaluation of list of constraints
Expand Down Expand Up @@ -79,6 +81,14 @@ namespace snowhouse {
return EqualTo<bool>(true);
}

#if __cplusplus > 199711L
ExpressionBuilder<typename type_concat<ConstraintListType, ConstraintList<ConstraintAdapter<EqualsConstraint<std::nullptr_t> >, Nil> >::t>
Null()
{
return EqualTo<std::nullptr_t>(nullptr);
}
#endif

ExpressionBuilder<typename type_concat<ConstraintListType, ConstraintList<ConstraintAdapter<EqualsConstraint<std::string> >, Nil> >::t>
EqualTo(const char* expected)
{
Expand Down
14 changes: 14 additions & 0 deletions snowhouse/stringize.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#ifndef IGLOO_STRINGIZE_H
#define IGLOO_STRINGIZE_H

#include <cstddef>

namespace snowhouse {
namespace detail {

Expand Down Expand Up @@ -85,6 +87,18 @@ namespace snowhouse {
return detail::DefaultStringizer< T, detail::is_output_streamable<T>::value >::ToString(value);
}
};

#if __cplusplus > 199711L
// We need this because nullptr_t has ambiguous overloads of operator<< in the standard library.
template<>
struct Stringizer<std::nullptr_t>
{
static std::string ToString(std::nullptr_t)
{
return "nullptr";
}
};
#endif
}

#endif

0 comments on commit 81438c1

Please sign in to comment.