projects
/
folly.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
f0dfe0f
)
Adds `make_mock` to `folly::LeakySingleton`.
author
Jiahao Li
<jiahaoli@fb.com>
Fri, 7 Jul 2017 19:22:51 +0000
(12:22 -0700)
committer
Facebook 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
patch
|
blob
|
history
diff --git
a/folly/Singleton.h
b/folly/Singleton.h
index 104054afaa42b30a6e8c7d9876e49a97e75b883f..64b6f37592580359c5124c46a0107706a545534b 100644
(file)
--- a/
folly/Singleton.h
+++ b/
folly/Singleton.h
@@
-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 };