| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- //
- // 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;
- }
- struct CaseMatcherTest : public testing::Test {
- example const ex{1};
- bool const b{true};
- };
- using ::testing::Eq;
- TEST_F(CaseMatcherTest, MatchesCorrectValue) {
- match(ex, b) {
- with(example{1}, true) {
- EXPECT_FALSE(_matcher_local.unmatched());
- } nomatch() {
- FAIL() << "Unable to match";
- }
- EXPECT_FALSE(_matcher_local.unmatched());
- } else {
- FAIL() << "Unable to construct matcher";
- }
- }
- TEST_F(CaseMatcherTest, NonMatchingClauseLeavesUnmatched) {
- match(ex, b) {
- with(example{1}, false) {
- FAIL() << "Incorrect match!";
- }
- EXPECT_TRUE(_matcher_local.unmatched());
- } else {
- FAIL() << "Unable to construct matcher";
- }
- }
- TEST_F(CaseMatcherTest, CanPassMatchingWithSecondClauseListing) {
- match(ex, b) {
- with(example{1}, false) {
- FAIL() << "Incorrect match!";
- }
- EXPECT_TRUE(_matcher_local.unmatched());
- with(example{1}, true) {
- EXPECT_FALSE(_matcher_local.unmatched());
- }
- EXPECT_FALSE(_matcher_local.unmatched());
- } 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_FALSE(_matcher_local.unmatched());
- }
- EXPECT_FALSE(_matcher_local.unmatched());
- } 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, CanMatchCompoundList) {
- match(ex, b) {
- with(matcher::any_of(example{1}, example{0xDEADBEEF}), true) {
- } nomatch() {
- FAIL() << "Unable to match with choice";
- }
- } else {
- FAIL() << "Unable to construct matcher";
- }
- }
- TEST_F(CaseMatcherTest, nomatch_any_of_list_without_self) {
- match(ex, b) {
- with(matcher::any_of(example{0}, example{0xDEADBEEF}), true) {
- FAIL() << "Incorrect match!";
- }
- EXPECT_TRUE(_matcher_local.unmatched());
- } else {
- FAIL() << "Unable to construct matcher";
- }
- }
|