// // match.t.h // case-matcher // // Created by Sam Jaffe on 2/3/17. // #include #include "match/match.hpp" struct example { example(long in) : i(in) {} long i; }; bool operator==(example const & a, example const & b) { return a.i == b.i; } bool operator<(example const & a, example const & b) { return a.i < b.i; } struct CaseMatcherTest : public testing::Test { example const ex{1}; bool const b{true}; }; using ::testing::Eq; using ::testing::Not; MATCHER(IsUnmatched, negation ? "was matched" : "was not matched") { return arg.unmatched(); } TEST_F(CaseMatcherTest, MatchesCorrectValue) { match(ex, b) { with(example{1}, true) { } nomatch() { FAIL() << "Unable to match"; } } else { FAIL() << "Unable to construct matcher"; } } TEST_F(CaseMatcherTest, ImmediatlySetsMatchedProperty) { match(ex, b) { with(example{1}, true) { EXPECT_THAT(_matcher_local, Not(IsUnmatched())); } } else { FAIL() << "Unable to construct matcher"; } } TEST_F(CaseMatcherTest, NonMatchingClauseLeavesUnmatched) { match(ex, b) { with(example{1}, false) { FAIL() << "Incorrect match!"; } EXPECT_THAT(_matcher_local, IsUnmatched()); } else { FAIL() << "Unable to construct matcher"; } } TEST_F(CaseMatcherTest, SetsAsMatchedIfAnyClausePasses) { match(ex, b) { with(example{1}, false) { FAIL() << "Incorrect match!"; } with(example{1}, true) {} EXPECT_THAT(_matcher_local, Not(IsUnmatched())); } else { FAIL() << "Unable to construct matcher"; } } TEST_F(CaseMatcherTest, CanMatchWithWildcard) { match(ex, b) { with(example{1}, false) { FAIL() << "Incorrect match!"; } with(matcher::any, true) {} EXPECT_THAT(_matcher_local, Not(IsUnmatched())); } else { FAIL() << "Unable to construct matcher"; } } TEST_F(CaseMatcherTest, CanMatchMultipleUnlinkedClauses) { int hits{0}; match(ex, b) { with(matcher::any, true) { ++hits; } and_with(example{1}, matcher::any) { ++hits; } } else { FAIL() << "Unable to construct matcher"; } EXPECT_THAT(hits, Eq(2)); } TEST_F(CaseMatcherTest, CanMatchAnyOf) { match(ex, b) { with(matcher::any_of(example{1}, example{0xDEADBEEF}), true) { } nomatch() { FAIL() << "Unable to match with choice"; } EXPECT_THAT(_matcher_local, Not(IsUnmatched())); } else { FAIL() << "Unable to construct matcher"; } } TEST_F(CaseMatcherTest, WillNotMatchAnyIfNoSubMatches) { match(ex, b) { with(matcher::any_of(example{0}, example{0xDEADBEEF}), true) { FAIL() << "Incorrect match!"; } EXPECT_THAT(_matcher_local, IsUnmatched()); } else { FAIL() << "Unable to construct matcher"; } } TEST_F(CaseMatcherTest, CanMatchPredicate) { auto lt_5 = [](example e) { return e.i < 5; }; match(ex, b) { with(matcher::matches(lt_5), true) { } nomatch() { FAIL() << "Unable to match with choice"; } EXPECT_THAT(_matcher_local, Not(IsUnmatched())); } else { FAIL() << "Unable to construct matcher"; } } TEST_F(CaseMatcherTest, WillNotMatchIfPredicateFails) { auto lt_1 = [](example e) { return e.i < 1; }; match(ex, b) { with(matcher::matches(lt_1), true) { FAIL() << "Incorrect match!"; } EXPECT_THAT(_matcher_local, IsUnmatched()); } else { FAIL() << "Unable to construct matcher"; } }