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