-
Notifications
You must be signed in to change notification settings - Fork 90
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
Make conversion of Option to bool stricter #2282
Conversation
Previously, only tested (case-insensitively) if first character was 'n', 'f', '0', 'y', 't', or '1'. Now only allow (still case-insensitively but checking full strings) 'n', 'no', 'f', 'false', '0', 'y', 'yes', 't', 'true', or '1'.
clang-tidy review says "All clean, LGTM! 👍" |
Tests are failing because we check the old behaviour: BOUT-dev/tests/unit/sys/test_options.cxx Lines 293 to 296 in b5e2c32
Deleting those lines should be sufficient |
|
||
auto c = static_cast<char>(toupper((strvalue)[0])); | ||
if ((c == 'Y') || (c == 'T') || (c == '1')) { | ||
if ((strvalue == "y") or (strvalue == "yes") or (strvalue == "t") |
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.
Just for my own curiosity, I had a stab at rewriting this to avoid the long-ish chain of comparisons, but I think this is probably worse:
auto one_of = [&strvalue](const std::vector<std::string>& values) -> bool {
return std::any_of(
values.begin(), values.end(),
[&strvalue](const auto& value) { return value == strvalue; });
};
if (one_of({"y", "yes", "t", "true", "1"})) {
result = true;
} else if (one_of({"n", "no", "f", "false", "0"})) {
result = false;
} else {
Could be worth if it one_of
were a function we could reuse somewhere else.
I think it just betrays my love of lambdas and C++'s love of verbose grammar. Here it is in Python:
if strvalue.lower() in ["y", "yes", "t", "true", "1"]:
result = True
elif if strvalue.lower() in ["n", "no", "f", "false", "0"]:
result = False
else:
raise ValueError()
Looks good, thanks @johnomotani ! |
Co-authored-by: Peter Hill <zed.three@gmail.com>
c690a9e
to
854c3a7
Compare
Previously,
Options::as<bool>()
only tested (case-insensitively) if first character was 'n', 'f', '0', 'y', 't', or '1'. Now only allow (still case-insensitively but checking full strings) 'n', 'no', 'f', 'false', '0', 'y', 'yes', 't', 'true', or '1'. See discussion on boutproject/boutdata#32.I found a nice intro to parameterised tests here https://www.sandordargo.com/blog/2019/04/24/parameterized-testing-with-gtest, which I used for the unit tests.