match_test.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // match.t.h
  3. // case-matcher
  4. //
  5. // Created by Sam Jaffe on 2/3/17.
  6. //
  7. #include <gmock/gmock.h>
  8. #include "match/match.hpp"
  9. struct example {
  10. example(long in) : i(in) {}
  11. long i;
  12. };
  13. bool operator==(example const & a, example const & b) {
  14. return a.i == b.i;
  15. }
  16. bool operator<(example const & a, example const & b) {
  17. return a.i < b.i;
  18. }
  19. struct CaseMatcherTest : public testing::Test {
  20. example const ex{1};
  21. bool const b{true};
  22. };
  23. using ::testing::Eq;
  24. using ::testing::Not;
  25. MATCHER(IsUnmatched, negation ? "was matched" : "was not matched") {
  26. return arg.unmatched();
  27. }
  28. TEST_F(CaseMatcherTest, MatchesCorrectValue) {
  29. match(ex, b) {
  30. with(example{1}, true) {
  31. } nomatch() {
  32. FAIL() << "Unable to match";
  33. }
  34. } else {
  35. FAIL() << "Unable to construct matcher";
  36. }
  37. }
  38. TEST_F(CaseMatcherTest, ImmediatlySetsMatchedProperty) {
  39. match(ex, b) {
  40. with(example{1}, true) {
  41. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  42. }
  43. } else {
  44. FAIL() << "Unable to construct matcher";
  45. }
  46. }
  47. TEST_F(CaseMatcherTest, NonMatchingClauseLeavesUnmatched) {
  48. match(ex, b) {
  49. with(example{1}, false) {
  50. FAIL() << "Incorrect match!";
  51. }
  52. EXPECT_THAT(_matcher_local, IsUnmatched());
  53. } else {
  54. FAIL() << "Unable to construct matcher";
  55. }
  56. }
  57. TEST_F(CaseMatcherTest, SetsAsMatchedIfAnyClausePasses) {
  58. match(ex, b) {
  59. with(example{1}, false) {
  60. FAIL() << "Incorrect match!";
  61. }
  62. with(example{1}, true) {}
  63. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  64. } else {
  65. FAIL() << "Unable to construct matcher";
  66. }
  67. }
  68. TEST_F(CaseMatcherTest, CanMatchWithWildcard) {
  69. match(ex, b) {
  70. with(example{1}, false) {
  71. FAIL() << "Incorrect match!";
  72. }
  73. with(matcher::any, true) {}
  74. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  75. } else {
  76. FAIL() << "Unable to construct matcher";
  77. }
  78. }
  79. TEST_F(CaseMatcherTest, CanMatchMultipleUnlinkedClauses) {
  80. int hits{0};
  81. match(ex, b) {
  82. with(matcher::any, true) {
  83. ++hits;
  84. }
  85. and_with(example{1}, matcher::any) {
  86. ++hits;
  87. }
  88. } else {
  89. FAIL() << "Unable to construct matcher";
  90. }
  91. EXPECT_THAT(hits, Eq(2));
  92. }
  93. TEST_F(CaseMatcherTest, CanMatchAnyOf) {
  94. match(ex, b) {
  95. with(matcher::any_of(example{1}, example{0xDEADBEEF}), true) {
  96. } nomatch() {
  97. FAIL() << "Unable to match with choice";
  98. }
  99. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  100. } else {
  101. FAIL() << "Unable to construct matcher";
  102. }
  103. }
  104. TEST_F(CaseMatcherTest, WillNotMatchAnyIfNoSubMatches) {
  105. match(ex, b) {
  106. with(matcher::any_of(example{0}, example{0xDEADBEEF}), true) {
  107. FAIL() << "Incorrect match!";
  108. }
  109. EXPECT_THAT(_matcher_local, IsUnmatched());
  110. } else {
  111. FAIL() << "Unable to construct matcher";
  112. }
  113. }
  114. TEST_F(CaseMatcherTest, CanMatchPredicate) {
  115. auto lt_5 = [](example e) { return e.i < 5; };
  116. match(ex, b) {
  117. with(matcher::matches(lt_5), true) {
  118. } nomatch() {
  119. FAIL() << "Unable to match with choice";
  120. }
  121. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  122. } else {
  123. FAIL() << "Unable to construct matcher";
  124. }
  125. }
  126. TEST_F(CaseMatcherTest, WillNotMatchIfPredicateFails) {
  127. auto lt_1 = [](example e) { return e.i < 1; };
  128. match(ex, b) {
  129. with(matcher::matches(lt_1), true) {
  130. FAIL() << "Incorrect match!";
  131. }
  132. EXPECT_THAT(_matcher_local, IsUnmatched());
  133. } else {
  134. FAIL() << "Unable to construct matcher";
  135. }
  136. }