| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- //
- // match.t.h
- // case-matcher
- //
- // Created by Sam Jaffe on 2/3/17.
- //
- #pragma once
- #include "match.hpp"
- #include <cxxtest/TestSuite.h>
- struct example {
- example(long in) : i(in) {}
- long i;
- };
- bool operator==(example const & a, example const & b) {
- return a.i == b.i;
- }
- class case_matcher_TestSuite : public CxxTest::TestSuite {
- public:
- example const ex{1};
- bool const b{true};
- public:
- void test_match_explicit_case() {
- match(ex, b) {
- with(example{1}, true) {}
- nomatch() { TS_FAIL("Unable to match"); }
- TS_ASSERT( ! _matcher_local.unmatched() );
- }
- }
-
- void test_nomatch_incorrect_case() {
- match(ex, b) {
- with(example{1}, false) { TS_FAIL("Incorrect match!"); }
- TS_ASSERT( _matcher_local.unmatched() );
- }
- }
-
- void test_match_fallback_case() {
- match(ex, b) {
- with(example{1}, false) { TS_FAIL("Incorrect match!"); }
- with(example{1}, true) {}
- TS_ASSERT( !_matcher_local.unmatched() );
- }
- }
-
- void test_match_any_case() {
- match(ex, b) {
- with(example{1}, false) { TS_FAIL("Incorrect match!"); }
- with(matcher::any, true) {}
- TS_ASSERT( !_matcher_local.unmatched() );
- }
- }
-
- void test_match_multiple_cases() {
- int hits{0};
- match(ex, b) {
- with(matcher::any, true) { ++hits; }
- and_with(example{1}, matcher::any) { ++hits; }
- }
- TS_ASSERT_EQUALS(hits, 2);
- }
-
- void test_match_any_of_list() {
- match(ex, b) {
- with(matcher::any_of(example{1}, example{0xDEADBEEF}), true) {}
- nomatch() { TS_FAIL("Unable to match with choice"); }
- }
- }
- void test_nomatch_any_of_list_without_self() {
- match(ex, b) {
- with(matcher::any_of(example{0}, example{0xDEADBEEF}), true) {
- TS_FAIL("Incorrect match!");
- }
- TS_ASSERT( _matcher_local.unmatched() );
- }
- }
- };
|