| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- //
- // match.t.h
- // case-matcher
- //
- // Created by Sam Jaffe on 2/3/17.
- //
- #include <gmock/gmock.h>
- #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";
- }
- }
|