Skip to content

Commit

Permalink
Merge pull request #31 from banditcpp/issue-14
Browse files Browse the repository at this point in the history
Make IsEmpty() and Is().Empty() independent of size() method
  • Loading branch information
sbeyer authored Apr 6, 2017
2 parents 723f73c + 75c9ae9 commit d5f541a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
10 changes: 2 additions & 8 deletions example/sequence_container_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static void TestEmpty(const T& container, const TEmpty& is_empty)

it("handles failing IsEmpty()");
{
AssertTestFails(AssertThat(container, IsEmpty()), "of length 0");
AssertTestFails(AssertThat(container, IsEmpty()), "empty");
}

it("handles Is().Empty()");
Expand All @@ -147,7 +147,7 @@ static void TestEmpty(const T& container, const TEmpty& is_empty)

it("handles failing Is().Empty()");
{
AssertTestFails(AssertThat(container, Is().Empty()), "of length 0");
AssertTestFails(AssertThat(container, Is().Empty()), "empty");
}
}

Expand All @@ -165,12 +165,6 @@ void TestEmpty(const std::array<int, 5>& container)
std::array<int, 0> is_empty;
TestEmpty(container, is_empty);
}

template<>
void TestEmpty(const std::forward_list<int>&)
{
// The constraint is size-based but there is no size() method available
}
#endif

template<typename T>
Expand Down
15 changes: 15 additions & 0 deletions example/string_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ void StringTests()
AssertThat("12345", HasLength(5));
}

it("handles failing HasLength()");
{
AssertTestFails(AssertThat("1234", HasLength(5)), "of length 5");
}

it("handles IsEmpty()");
{
AssertThat("", IsEmpty());
}

it("handles failing IsEmpty()");
{
AssertTestFails(AssertThat("not empty", IsEmpty()), "empty");
}

it("handles weird long expressions");
{
AssertThat("12345", HasLength(5) && StartsWith("123") && !EndsWith("zyxxy"));
Expand Down
1 change: 1 addition & 0 deletions snowhouse/constraints/constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "endswithconstraint.h"
#include "equalsconstraint.h"
#include "haslengthconstraint.h"
#include "isemptyconstraint.h"
#include "isgreaterthanconstraint.h"
#include "isgreaterthanorequaltoconstraint.h"
#include "islessthanconstraint.h"
Expand Down
5 changes: 0 additions & 5 deletions snowhouse/constraints/haslengthconstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ namespace snowhouse
return HasLengthConstraint<ExpectedType>(expected);
}

inline HasLengthConstraint<int> IsEmpty()
{
return HasLength<int>(0);
}

inline HasLengthConstraint<std::string> HasLength(const char* expected)
{
return HasLengthConstraint<std::string>(expected);
Expand Down
42 changes: 42 additions & 0 deletions snowhouse/constraints/isemptyconstraint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2017 Stephan Beyer
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef SNOWHOUSE_ISEMPTYCONSTRAINT_H
#define SNOWHOUSE_ISEMPTYCONSTRAINT_H

#include "expressions/expression.h"

namespace snowhouse
{
struct IsEmptyConstraint : Expression<IsEmptyConstraint>
{
// The ignored default argument is a workaround to make this class
// compatible to ConstraintAdapterType
IsEmptyConstraint(int = 0)
{
}

template<typename ActualType>
bool operator()(const ActualType& actual) const
{
return actual.empty();
}
};

inline IsEmptyConstraint IsEmpty()
{
return IsEmptyConstraint();
}

template<>
struct Stringizer<IsEmptyConstraint>
{
static std::string ToString(const IsEmptyConstraint&)
{
return "empty";
}
};
}

#endif
4 changes: 2 additions & 2 deletions snowhouse/fluent/expressionbuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ namespace snowhouse
return BuilderType(Concatenate(m_constraint_list, node));
}

ExpressionBuilder<typename type_concat<ConstraintListType, ConstraintList<ConstraintAdapter<HasLengthConstraint<int> >, Nil> >::t>
ExpressionBuilder<typename type_concat<ConstraintListType, ConstraintList<ConstraintAdapter<IsEmptyConstraint>, Nil> >::t>
Empty()
{
typedef ConstraintAdapter<HasLengthConstraint<int> > ConstraintAdapterType;
typedef ConstraintAdapter<IsEmptyConstraint> ConstraintAdapterType;

typedef ExpressionBuilder<typename type_concat<ConstraintListType, ConstraintList<ConstraintAdapterType, Nil> >::t> BuilderType;
ConstraintAdapterType constraint(0);
Expand Down

0 comments on commit d5f541a

Please sign in to comment.