-
Notifications
You must be signed in to change notification settings - Fork 2
KF-28 add tests for Guard #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kellesi
wants to merge
3
commits into
main
Choose a base branch
from
KF-28-test-Guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,176 @@ | ||
| #include "pch.h" | ||
| #include <kf/Guard.h> | ||
|
|
||
| struct TestResource | ||
| { | ||
| int m_value = 0; | ||
| }; | ||
|
|
||
| static int g_closeCallCount = 0; | ||
| static TestResource* g_lastClosedResource = nullptr; | ||
|
|
||
| void closeFn(TestResource* res) | ||
| { | ||
| ++g_closeCallCount; | ||
| g_lastClosedResource = res; | ||
| } | ||
|
|
||
| SCENARIO("Testing kf::Guard::Guard basic functionality") | ||
| { | ||
| using TestGuard = kf::Guard::Guard< TestResource*, nullptr, decltype(&closeFn), &closeFn>; | ||
|
|
||
| GIVEN("A default constructed Guard") | ||
| { | ||
| TestGuard guard; | ||
| g_closeCallCount = 0; | ||
| g_lastClosedResource = nullptr; | ||
|
|
||
| THEN("Its value is nullptr and close function is not called on destruction") | ||
| { | ||
| REQUIRE(static_cast<TestResource*>(guard) == nullptr); | ||
| REQUIRE(g_closeCallCount == 0); | ||
| } | ||
|
|
||
| WHEN("reset is called with nullptr explicitly") | ||
| { | ||
| guard.reset(nullptr); | ||
| THEN("Close function is not called") | ||
| { | ||
| REQUIRE(g_closeCallCount == 0); | ||
| REQUIRE(guard.get() == nullptr); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("A Guard constructed with a valid resource pointer") | ||
| { | ||
| int value = 123; | ||
| TestResource resource(value); | ||
| TestGuard guard(&resource); | ||
| g_closeCallCount = 0; | ||
| g_lastClosedResource = nullptr; | ||
|
|
||
| THEN("Value is stored correctly") | ||
| { | ||
| REQUIRE(guard.get() == &resource); | ||
| REQUIRE(guard.get()->m_value == value); | ||
| } | ||
|
|
||
| WHEN("Guard is reset with no arguments") | ||
| { | ||
| guard.reset(); | ||
|
|
||
| THEN("Close function is called once with the resource") | ||
| { | ||
| REQUIRE(g_closeCallCount == 1); | ||
| REQUIRE(g_lastClosedResource == &resource); | ||
| REQUIRE(guard.get() == nullptr); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("A Guard constructed with a valid resource pointer") | ||
| { | ||
| TestResource resource(123); | ||
| TestGuard guard(&resource); | ||
| g_closeCallCount = 0; | ||
| g_lastClosedResource = nullptr; | ||
|
|
||
| WHEN("Guard is released") | ||
| { | ||
| TestResource* released = guard.release(); | ||
|
|
||
| THEN("Released value is the resource pointer") | ||
| { | ||
| REQUIRE(released == &resource); | ||
| } | ||
|
|
||
| THEN("Guard now holds nullptr") | ||
| { | ||
| REQUIRE(guard.get() == nullptr); | ||
| } | ||
|
|
||
| THEN("Close function was not called on release") | ||
| { | ||
| REQUIRE(g_closeCallCount == 0); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("A Guard constructed with a valid resource pointer") | ||
| { | ||
| int value = 123; | ||
| TestResource resource(value); | ||
| TestGuard guard(&resource); | ||
| g_closeCallCount = 0; | ||
| g_lastClosedResource = nullptr; | ||
|
|
||
| WHEN("Guard is move-constructed from another Guard") | ||
| { | ||
| TestGuard otherGuard(std::move(guard)); | ||
|
|
||
| THEN("The second Guard owns the resource") | ||
| { | ||
| REQUIRE(otherGuard.get() == &resource); | ||
| REQUIRE(otherGuard.get()->m_value == value); | ||
| } | ||
|
|
||
| THEN("The original guard holds nullptr and no closeFn was called for the resource") | ||
| { | ||
| REQUIRE(g_closeCallCount == 0); | ||
| REQUIRE(guard.get() == nullptr); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("A Guard constructed with a valid resource pointer") | ||
| { | ||
| int value = 123; | ||
| TestResource resource(value); | ||
| TestGuard guard(&resource); | ||
| g_closeCallCount = 0; | ||
| g_lastClosedResource = nullptr; | ||
|
|
||
| WHEN("Guard is move-assigned from another Guard") | ||
| { | ||
| int otherValue = 456; | ||
| TestResource otherResource(otherValue); | ||
| TestGuard guard2(&otherResource); | ||
|
|
||
| guard2 = std::move(guard); | ||
|
|
||
| THEN("The second guard now owns the first resource") | ||
| { | ||
| REQUIRE(guard2.get() == &resource); | ||
|
belyshevdenis marked this conversation as resolved.
|
||
| REQUIRE(guard2.get()->m_value == value); | ||
| } | ||
|
|
||
| THEN("The original guard holds nullptr and closeFn was called for other resource") | ||
| { | ||
| REQUIRE(g_lastClosedResource == &otherResource); | ||
| REQUIRE(g_closeCallCount == 1); | ||
| REQUIRE(guard.get() == nullptr); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| GIVEN("A Guard constructed with a valid resource pointer") | ||
| { | ||
| TestResource resource(123); | ||
| g_closeCallCount = 0; | ||
| g_lastClosedResource = nullptr; | ||
|
|
||
| WHEN("Guard is destroyed") | ||
| { | ||
| { | ||
| TestGuard guard(&resource); | ||
| } | ||
|
|
||
| THEN("Close function is called on destruction") | ||
| { | ||
| REQUIRE(g_closeCallCount == 1); | ||
| REQUIRE(g_lastClosedResource == &resource); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.