match_test.cxx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. void test_match_any_of_list() {
  58. match(ex, b) {
  59. with(matcher::any_of(example{1}, example{0xDEADBEEF}), true) {}
  60. nomatch() { TS_FAIL("Unable to match with choice"); }
  61. }
  62. }
  63. void test_nomatch_any_of_list_without_self() {
  64. match(ex, b) {
  65. with(matcher::any_of(example{0}, example{0xDEADBEEF}), true) {
  66. TS_FAIL("Incorrect match!");
  67. }
  68. TS_ASSERT( _matcher_local.unmatched() );
  69. }
  70. }
  71. };