match_test.cxx 2.5 KB

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