Adds `make_mock` to `folly::LeakySingleton`.
authorJiahao Li <jiahaoli@fb.com>
Fri, 7 Jul 2017 19:22:51 +0000 (12:22 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 7 Jul 2017 19:35:22 +0000 (12:35 -0700)
Summary:
Provides a `make_mock` interface to `folly::LeakySingleton` for unit test
mocking.

Reviewed By: andriigrynenko

Differential Revision: D5382003

fbshipit-source-id: 9ab81dd64703f4f02772bfb8c85e020680abcc85

folly/Singleton.h

index 104054afaa42b30a6e8c7d9876e49a97e75b883f..64b6f37592580359c5124c46a0107706a545534b 100644 (file)
@@ -669,6 +669,21 @@ class LeakySingleton {
 
   static T& get() { return instance(); }
 
+  static void make_mock(std::nullptr_t /* c */ = nullptr) {
+    make_mock([]() { return new T; });
+  }
+
+  static void make_mock(CreateFunc createFunc) {
+    auto& entry = entryInstance();
+    if (createFunc == nullptr) {
+      throw std::logic_error(
+          "nullptr_t should be passed if you want T to be default constructed");
+    }
+
+    entry.createFunc = createFunc;
+    entry.state = State::Dead;
+  }
+
  private:
   enum class State { NotRegistered, Dead, Living };