-
Notifications
You must be signed in to change notification settings - Fork 1
Removing interface transitive dependency on <functional> #14
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,15 +10,10 @@ | |
|
||
#include <radsan/radsan_user_interface.h> | ||
|
||
#include <functional> | ||
|
||
namespace radsan { | ||
|
||
class Context { | ||
public: | ||
Context(); | ||
Context(std::function<OnErrorAction()> get_error_action); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seemed to me like a way to do dependency injection. If we want to keep the ability to do something like that, I could introduce a See the reworked unit test for more info |
||
|
||
void realtimePush(); | ||
void realtimePop(); | ||
|
||
|
@@ -34,7 +29,6 @@ class Context { | |
|
||
int realtime_depth_{0}; | ||
int bypass_depth_{0}; | ||
std::function<OnErrorAction()> get_error_action_; | ||
}; | ||
|
||
Context &getContextForThisThread(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,39 +17,33 @@ | |
|
||
namespace radsan { | ||
|
||
std::function<OnErrorAction()> createErrorActionGetter() { | ||
auto const continue_getter = []() { return OnErrorAction::Continue; }; | ||
auto const exit_getter = []() { return OnErrorAction::ExitWithFailure; }; | ||
auto const interactive_getter = []() { | ||
auto response = char{}; | ||
bool ShouldExit() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note this function no longer returns a function, but just returns directly whether or not we should exit. |
||
|
||
std::cout << "Continue? (Y/n): "; | ||
std::cin >> std::noskipws >> response; | ||
const bool defaultShouldExit = true; | ||
|
||
if (std::toupper(response) == 'N') | ||
return OnErrorAction::ExitWithFailure; | ||
else | ||
return OnErrorAction::Continue; | ||
}; | ||
|
||
auto user_mode = __sanitizer::GetEnv("RADSAN_ERROR_MODE"); | ||
static const char* user_mode = __sanitizer::GetEnv("RADSAN_ERROR_MODE"); | ||
if (user_mode == nullptr) { | ||
return exit_getter; | ||
return defaultShouldExit; | ||
} | ||
|
||
if (std::strcmp(user_mode, "interactive") == 0) { | ||
return interactive_getter; | ||
auto response = char{}; | ||
|
||
std::cout << "Continue? (Y/n): "; | ||
std::cin >> std::noskipws >> response; | ||
|
||
return std::toupper(response) == 'N'; | ||
} | ||
|
||
if (std::strcmp(user_mode, "continue") == 0) { | ||
return continue_getter; | ||
return false; | ||
} | ||
|
||
if (std::strcmp(user_mode, "exit") == 0) { | ||
return exit_getter; | ||
return true; | ||
} | ||
|
||
return exit_getter; | ||
return defaultShouldExit; | ||
} | ||
|
||
} // namespace radsan |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
|
||
namespace radsan { | ||
|
||
enum class OnErrorAction { | ||
Continue, | ||
ExitWithFailure, | ||
}; | ||
|
||
std::function<OnErrorAction()> createErrorActionGetter(); | ||
bool ShouldExit(); | ||
|
||
} | ||
} // namespace radsan |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,14 +68,14 @@ TEST(TestRadsanContext, | |
} | ||
|
||
TEST(TestRadsanContext, onlyDiesIfExitWithFailureReturnedFromUser) { | ||
auto fake_action = radsan::OnErrorAction::Continue; | ||
auto action_getter = [&fake_action]() { return fake_action; }; | ||
|
||
auto context = radsan::Context{action_getter}; | ||
setenv("RADSAN_ERROR_MODE", "continue", 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of injecting the action_getter, we skip the middle man and change the environment variables. This makes this test a little less unit-test, and a little more integration test, for better or for worse. |
||
|
||
auto context = radsan::Context{}; | ||
context.realtimePush(); | ||
|
||
context.expectNotRealtime("do_some_stuff_expecting_continue"); | ||
|
||
fake_action = radsan::OnErrorAction::ExitWithFailure; | ||
setenv("RADSAN_ERROR_MODE", "exit", 1); | ||
EXPECT_DEATH(context.expectNotRealtime("do_some_stuff_expecting_exit"), ""); | ||
} |
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.
transitive dependency pulled in by functional, now has to be explicit