match_test.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. struct CaseMatcherTest : public testing::Test {
  17. example const ex{1};
  18. bool const b{true};
  19. };
  20. using ::testing::Eq;
  21. using ::testing::Not;
  22. MATCHER(IsUnmatched, negation ? "was matched" : "was not matched") {
  23. return arg.unmatched();
  24. }
  25. TEST_F(CaseMatcherTest, MatchesCorrectValue) {
  26. match(ex, b) {
  27. with(example{1}, true) {
  28. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  29. } nomatch() {
  30. FAIL() << "Unable to match";
  31. }
  32. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  33. } else {
  34. FAIL() << "Unable to construct matcher";
  35. }
  36. }
  37. TEST_F(CaseMatcherTest, NonMatchingClauseLeavesUnmatched) {
  38. match(ex, b) {
  39. with(example{1}, false) {
  40. FAIL() << "Incorrect match!";
  41. }
  42. EXPECT_THAT(_matcher_local, IsUnmatched());
  43. } else {
  44. FAIL() << "Unable to construct matcher";
  45. }
  46. }
  47. TEST_F(CaseMatcherTest, CanPassMatchingWithSecondClauseListing) {
  48. match(ex, b) {
  49. with(example{1}, false) {
  50. FAIL() << "Incorrect match!";
  51. }
  52. EXPECT_THAT(_matcher_local, IsUnmatched());
  53. with(example{1}, true) {
  54. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  55. }
  56. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  57. } else {
  58. FAIL() << "Unable to construct matcher";
  59. }
  60. }
  61. TEST_F(CaseMatcherTest, CanMatchWithWildcard) {
  62. match(ex, b) {
  63. with(example{1}, false) {
  64. FAIL() << "Incorrect match!";
  65. }
  66. with(matcher::any, true) {
  67. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  68. }
  69. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  70. } else {
  71. FAIL() << "Unable to construct matcher";
  72. }
  73. }
  74. TEST_F(CaseMatcherTest, CanMatchMultipleUnlinkedClauses) {
  75. int hits{0};
  76. match(ex, b) {
  77. with(matcher::any, true) {
  78. ++hits;
  79. }
  80. and_with(example{1}, matcher::any) {
  81. ++hits;
  82. }
  83. } else {
  84. FAIL() << "Unable to construct matcher";
  85. }
  86. EXPECT_THAT(hits, Eq(2));
  87. }
  88. TEST_F(CaseMatcherTest, CanMatchCompoundList) {
  89. match(ex, b) {
  90. with(matcher::any_of(example{1}, example{0xDEADBEEF}), true) {
  91. } nomatch() {
  92. FAIL() << "Unable to match with choice";
  93. }
  94. } else {
  95. FAIL() << "Unable to construct matcher";
  96. }
  97. }
  98. TEST_F(CaseMatcherTest, nomatch_any_of_list_without_self) {
  99. match(ex, b) {
  100. with(matcher::any_of(example{0}, example{0xDEADBEEF}), true) {
  101. FAIL() << "Incorrect match!";
  102. }
  103. EXPECT_THAT(_matcher_local, IsUnmatched());
  104. } else {
  105. FAIL() << "Unable to construct matcher";
  106. }
  107. }