-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87a9bb5
commit abaa1f5
Showing
3 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN | ||
#include <doctest.h> | ||
|
||
#include <functional> | ||
|
||
#include <nodec/observers.hpp> | ||
|
||
TEST_CASE("Check to notify on not reentrant") { | ||
class IObserver { | ||
public: | ||
virtual void on_notify() = 0; | ||
}; | ||
|
||
class TestObserver : public IObserver { | ||
public: | ||
void on_notify() override { | ||
notified = true; | ||
} | ||
|
||
bool notified = false; | ||
}; | ||
|
||
SUBCASE("add and remove") { | ||
nodec::Observers<IObserver> observers; | ||
|
||
TestObserver observer1; | ||
|
||
observers.add_observer(&observer1); | ||
CHECK(observer1.notified == false); | ||
|
||
observers.each([](IObserver *observer) { | ||
observer->on_notify(); | ||
}); | ||
CHECK(observer1.notified == true); | ||
|
||
observers.remove_observer(&observer1); | ||
|
||
observer1.notified = false; | ||
observers.each([](IObserver *observer) { | ||
observer->on_notify(); | ||
}); | ||
CHECK(observer1.notified == false); | ||
} | ||
} | ||
|
||
TEST_CASE("Check to notify on reentrant") { | ||
class IObserver { | ||
public: | ||
virtual void on_notify() = 0; | ||
}; | ||
|
||
class TestObserver : public IObserver { | ||
public: | ||
void on_notify() override { | ||
callback(); | ||
} | ||
|
||
std::function<void()> callback; | ||
}; | ||
|
||
SUBCASE("add while notify") { | ||
nodec::Observers<IObserver> observers; | ||
|
||
TestObserver observer1; | ||
TestObserver observer2; | ||
bool notified_1 = false; | ||
bool notified_2 = false; | ||
observer1.callback = [&]() { | ||
observers.add_observer(&observer2); | ||
notified_1 = true; | ||
}; | ||
observer2.callback = [&]() { | ||
notified_2 = true; | ||
}; | ||
|
||
observers.add_observer(&observer1); | ||
|
||
observers.each([](IObserver *observer) { | ||
observer->on_notify(); | ||
}); | ||
|
||
CHECK(notified_1 == true); | ||
CHECK(notified_2 == false); | ||
} | ||
} |