match_test.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. } nomatch() {
  29. FAIL() << "Unable to match";
  30. }
  31. } else {
  32. FAIL() << "Unable to construct matcher";
  33. }
  34. }
  35. TEST_F(CaseMatcherTest, ImmediatlySetsMatchedProperty) {
  36. match(ex, b) {
  37. with(example{1}, true) {
  38. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  39. }
  40. } else {
  41. FAIL() << "Unable to construct matcher";
  42. }
  43. }
  44. TEST_F(CaseMatcherTest, NonMatchingClauseLeavesUnmatched) {
  45. match(ex, b) {
  46. with(example{1}, false) {
  47. FAIL() << "Incorrect match!";
  48. }
  49. EXPECT_THAT(_matcher_local, IsUnmatched());
  50. } else {
  51. FAIL() << "Unable to construct matcher";
  52. }
  53. }
  54. TEST_F(CaseMatcherTest, SetsAsMatchedIfAnyClausePasses) {
  55. match(ex, b) {
  56. with(example{1}, false) {
  57. FAIL() << "Incorrect match!";
  58. }
  59. with(example{1}, true) {}
  60. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  61. } else {
  62. FAIL() << "Unable to construct matcher";
  63. }
  64. }
  65. TEST_F(CaseMatcherTest, CanMatchWithWildcard) {
  66. match(ex, b) {
  67. with(example{1}, false) {
  68. FAIL() << "Incorrect match!";
  69. }
  70. with(matcher::any, true) {}
  71. EXPECT_THAT(_matcher_local, Not(IsUnmatched()));
  72. } else {
  73. FAIL() << "Unable to construct matcher";
  74. }
  75. }
  76. TEST_F(CaseMatcherTest, CanMatchMultipleUnlinkedClauses) {
  77. int hits{0};
  78. match(ex, b) {
  79. with(matcher::any, true) {
  80. ++hits;
  81. }
  82. and_with(example{1}, matcher::any) {
  83. ++hits;
  84. }
  85. } else {
  86. FAIL() << "Unable to construct matcher";
  87. }
  88. EXPECT_THAT(hits, Eq(2));
  89. }
  90. TEST_F(CaseMatcherTest, CanMatchCompoundList) {
  91. match(ex, b) {
  92. with(matcher::any_of(example{1}, example{0xDEADBEEF}), true) {
  93. } nomatch() {
  94. FAIL() << "Unable to match with choice";
  95. }
  96. } else {
  97. FAIL() << "Unable to construct matcher";
  98. }
  99. }
  100. TEST_F(CaseMatcherTest, WillNotMatchAnyIfNoSubMatches) {
  101. match(ex, b) {
  102. with(matcher::any_of(example{0}, example{0xDEADBEEF}), true) {
  103. FAIL() << "Incorrect match!";
  104. }
  105. EXPECT_THAT(_matcher_local, IsUnmatched());
  106. } else {
  107. FAIL() << "Unable to construct matcher";
  108. }
  109. }