match.t.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // match.t.h
  3. // case-matcher
  4. //
  5. // Created by Sam Jaffe on 2/3/17.
  6. //
  7. #pragma once
  8. #include "match.hpp"
  9. #include <cxxtest/TestSuite.h>
  10. struct example {
  11. example(long in) : i(in) {}
  12. long i;
  13. };
  14. bool operator==(example const & a, example const & b) {
  15. return a.i == b.i;
  16. }
  17. class case_matcher_TestSuite : public CxxTest::TestSuite {
  18. public:
  19. example const ex{1};
  20. bool const b{true};
  21. public:
  22. void test_match_explicit_case() {
  23. match(ex, b) {
  24. with(example{1}, true) {}
  25. nomatch() { TS_FAIL("Unable to match"); }
  26. TS_ASSERT( ! _matcher_local.unmatched() );
  27. }
  28. }
  29. void test_nomatch_incorrect_case() {
  30. match(ex, b) {
  31. with(example{1}, false) { TS_FAIL("Incorrect match!"); }
  32. TS_ASSERT( _matcher_local.unmatched() );
  33. }
  34. }
  35. void test_match_fallback_case() {
  36. match(ex, b) {
  37. with(example{1}, false) { TS_FAIL("Incorrect match!"); }
  38. with(example{1}, true) {}
  39. TS_ASSERT( !_matcher_local.unmatched() );
  40. }
  41. }
  42. void test_match_any_case() {
  43. match(ex, b) {
  44. with(example{1}, false) { TS_FAIL("Incorrect match!"); }
  45. with(matcher::any, true) {}
  46. TS_ASSERT( !_matcher_local.unmatched() );
  47. }
  48. }
  49. void test_match_multiple_cases() {
  50. int hits{0};
  51. match(ex, b) {
  52. with(matcher::any, true) { ++hits; }
  53. and_with(example{1}, matcher::any) { ++hits; }
  54. }
  55. TS_ASSERT_EQUALS(hits, 2);
  56. }
  57. };