-
-
Notifications
You must be signed in to change notification settings - Fork 623
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
Add support for mocking to tests & test X11 struts #2115
Draft
Caellian
wants to merge
6
commits into
main
Choose a base branch
from
dev/test-x11-struts
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.
Draft
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44cd89c
Add support for mocking to tests
Caellian a0c1502
Properly handle predefined X11 atoms in mocking
Caellian 089f9ce
Fix mock linking
Caellian 384822e
Add X11 mocking utilities
Caellian 1784a59
Fix x11_change_property not properly copying data
Caellian 992e67c
Add display mocking
Caellian 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 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
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,22 @@ | ||
#include "mock.hh" | ||
#include <optional> | ||
#include <utility> | ||
|
||
namespace mock { | ||
std::deque<std::unique_ptr<state_change>> _state_changes; | ||
|
||
std::deque<std::unique_ptr<state_change>> take_state_changes() { | ||
std::deque<std::unique_ptr<mock::state_change>> result; | ||
std::swap(_state_changes, result); | ||
return result; | ||
} | ||
std::optional<std::unique_ptr<state_change>> next_state_change() { | ||
if (_state_changes.empty()) { return std::nullopt; } | ||
auto front = std::move(_state_changes.front()); | ||
_state_changes.pop_front(); | ||
return front; | ||
} | ||
void push_state_change(std::unique_ptr<state_change> change) { | ||
_state_changes.push_back(std::move(change)); | ||
} | ||
} // namespace mock |
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,97 @@ | ||
#ifndef MOCK_HH | ||
#define MOCK_HH | ||
|
||
#include <cstdio> | ||
#include <deque> | ||
#include <memory> | ||
#include <optional> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <type_traits> | ||
|
||
namespace mock { | ||
|
||
/// Ponyfill for `std::format`. | ||
template <typename... Args> | ||
std::string debug_format(const std::string& format, Args... args) { | ||
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + | ||
1; // Extra space for '\0' | ||
if (size_s <= 0) { throw std::runtime_error("error during formatting"); } | ||
auto size = static_cast<size_t>(size_s); | ||
std::unique_ptr<char[]> buf(new char[size]); | ||
std::snprintf(buf.get(), size, format.c_str(), args...); | ||
return std::string(buf.get(), | ||
buf.get() + size - 1); // We don't want the '\0' inside | ||
} | ||
|
||
/// Base class of state changes. | ||
/// | ||
/// A state change represents some side effect that mutates system state via | ||
/// mocked functions. | ||
/// | ||
/// For directly accessible globals and fields, those should be used instead. | ||
/// This is intended for cases where some library function is internally invoked | ||
/// but would fail if conditions only present at runtime aren't met. | ||
struct state_change { | ||
virtual ~state_change() = default; | ||
|
||
static std::string change_name() { return "state_change"; } | ||
|
||
/// Returns a string representation of this state change with information | ||
/// necessary to differentiate it from other variants of the same type. | ||
virtual std::string debug() = 0; | ||
}; | ||
|
||
/// Implementation detail; shouldn't be used directly. | ||
extern std::deque<std::unique_ptr<state_change>> _state_changes; | ||
|
||
/// Removes all `state_change`s from the queue (clearing it) and returns them. | ||
std::deque<std::unique_ptr<state_change>> take_state_changes(); | ||
|
||
/// Pops the next `state_change` from the queue, or returns `std::nullopt` if | ||
/// there are none. | ||
std::optional<std::unique_ptr<state_change>> next_state_change(); | ||
|
||
/// Pushes some `state_change` to the back of the queue. | ||
void push_state_change(std::unique_ptr<state_change> change); | ||
|
||
/// Pops some `state_change` of type `T` if it's the next change in the queue. | ||
/// Otherwise it returns `std::nullopt`. | ||
template <typename T> | ||
std::optional<T> next_state_change_t() { | ||
static_assert(std::is_base_of_v<state_change, T>, "T must be a state_change"); | ||
auto result = next_state_change(); | ||
if (!result.has_value()) { return std::nullopt; } | ||
auto cast_result = dynamic_cast<T*>(result.value().get()); | ||
if (!cast_result) { | ||
_state_changes.push_front(std::move(result.value())); | ||
return std::nullopt; | ||
} | ||
return *dynamic_cast<T*>(result.value().release()); | ||
} | ||
} // namespace mock | ||
|
||
/// A variant of `mock::next_state_change_t` that integrates into Catch2. | ||
/// It's a macro because using `FAIL` outside of a test doesn't compile. | ||
#define EXPECT_NEXT_CHANGE(T) \ | ||
[]() { \ | ||
static_assert(std::is_base_of_v<mock::state_change, T>, \ | ||
#T " isn't a state_change"); \ | ||
auto result = mock::next_state_change(); \ | ||
if (!result.has_value()) { \ | ||
FAIL("no more state changes; expected '" #T "'"); \ | ||
return *reinterpret_cast<T*>(malloc(sizeof(T))); \ | ||
} \ | ||
auto cast_result = dynamic_cast<T*>(result.value().get()); \ | ||
if (!cast_result) { \ | ||
FAIL("expected '" #T "' as next state change, got: " \ | ||
<< result.value().get()->debug()); \ | ||
return *reinterpret_cast<T*>(malloc(sizeof(T))); \ | ||
} else { \ | ||
return *dynamic_cast<T*>(result.value().release()); \ | ||
} \ | ||
}(); | ||
// garbage reinterpretation after FAIL doesn't get returned because FAIL stops | ||
// the test. Should be UNREACHABLE, but I have trouble including it. | ||
|
||
#endif /* MOCK_HH */ |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alignment
wasn't available to some file (wayland?) that uses it. Note to self to check before merge.