Просмотр исходного кода

Add support for matching against 'either' choices (or cases in an if statement).

Samuel Jaffe 8 лет назад
Родитель
Сommit
9d585b2c14
2 измененных файлов с 36 добавлено и 2 удалено
  1. 20 2
      match.hpp
  2. 16 0
      match.t.h

+ 20 - 2
match.hpp

@@ -7,6 +7,7 @@
 
 #pragma once
 
+#include <array>
 #include <tuple>
 
 namespace matcher {
@@ -15,9 +16,26 @@ namespace matcher {
   
   template <typename T>
   bool operator==(T const &, any_t) { return true; }
-  template <typename T>
-  bool operator==(any_t, T const &) { return true; }
+}
+
+namespace matcher {
+  template <typename T, size_t I>
+  struct any_of_t {
+    std::array<T, I> options;
+  };
+  
+  template <typename T, size_t I>
+  bool operator==(T const & t, any_of_t<T, I> const & of) {
+    return std::find(of.options.begin(), of.options.end(), t) != of.options.end();
+  }
   
+  template <typename... Args>
+  any_of_t<typename std::common_type<Args...>::type, sizeof...(Args)> any_of(Args &&... args) {
+    return {{args...}};
+  }
+}
+
+namespace matcher {
   template <typename... Args>
   struct matcher {
   public:

+ 16 - 0
match.t.h

@@ -64,4 +64,20 @@ public:
     }
     TS_ASSERT_EQUALS(hits, 2);
   }
+  
+  void test_match_any_of_list() {
+    match(ex, b) {
+      with(matcher::any_of(example{1}, example{0xDEADBEEF}), true) {}
+      nomatch() { TS_FAIL("Unable to match with choice"); }
+    }
+  }
+
+  void test_nomatch_any_of_list_without_self() {
+    match(ex, b) {
+      with(matcher::any_of(example{0}, example{0xDEADBEEF}), true) {
+        TS_FAIL("Incorrect match!");
+      }
+      TS_ASSERT( _matcher_local.unmatched() );
+    }
+  }
 };