Skip to content

Commit

Permalink
Permit coBool options to be set with booleans; added an additional ty…
Browse files Browse the repository at this point in the history
…pe enforcement for setting it with a string (only accepts "1" and "0"). (slic3r#4668)
  • Loading branch information
lordofhyphens authored Jan 2, 2019
1 parent 90f108a commit 190de64
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/test/libslic3r/test_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,25 @@ SCENARIO("Config accessor functions perform as expected.") {
GIVEN("A config generated from default options") {
auto config {Slic3r::Config::new_from_defaults()};
WHEN("A boolean option is set to a boolean value") {
THEN("A BadOptionTypeException exception is thrown.") {
REQUIRE_THROWS_AS(config->set("gcode_comments", true), BadOptionTypeException);
REQUIRE_NOTHROW(config->set("gcode_comments", true));
THEN("The underlying value is set correctly.") {
REQUIRE(config->get<ConfigOptionBool>("gcode_comments").getBool() == true);
}
}
WHEN("A boolean option is set to a string value") {
config->set("gcode_comments", "1");
WHEN("A boolean option is set to a string value representing a 0 or 1") {
CHECK_NOTHROW(config->set("gcode_comments", "1"));
THEN("The underlying value is set correctly.") {
REQUIRE(config->get<ConfigOptionBool>("gcode_comments").getBool() == true);
}
}
WHEN("A boolean option is set to a string value representing something other than 0 or 1") {
THEN("A BadOptionTypeException exception is thrown.") {
REQUIRE_THROWS_AS(config->set("gcode_comments", "Z"), BadOptionTypeException);
}
AND_THEN("Value is unchanged.") {
REQUIRE(config->get<ConfigOptionBool>("gcode_comments").getBool() == false);
}
}
WHEN("A string option is set to an int value") {
THEN("A BadOptionTypeException exception is thrown.") {
REQUIRE_THROWS_AS(config->set("gcode_comments", 1), BadOptionTypeException);
Expand Down
4 changes: 4 additions & 0 deletions xs/src/libslic3r/ConfigBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,16 @@ class ConfigOptionBool : public ConfigOptionSingle<bool>
ConfigOptionBool* clone() const { return new ConfigOptionBool(this->value); };

bool getBool() const { return this->value; };
void setBool(bool val) { this->value = val; }

std::string serialize() const {
return std::string(this->value ? "1" : "0");
};

bool deserialize(std::string str, bool append = false) {
// Enforce the type on deserialize.
if (str.compare("1") != 0 && str.compare("0") != 0)
return false;
this->value = (str.compare("1") == 0);
return true;
};
Expand Down

0 comments on commit 190de64

Please sign in to comment.