Permit coBool options to be set with booleans; added an additional type enforcement for setting it with a string (only accepts "1" and "0"). (#4668)

This commit is contained in:
Joseph Lenox 2019-01-01 19:42:26 -06:00 committed by GitHub
parent 90f108ae8e
commit 190de64396
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 6 deletions

View File

@ -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);
}
}
WHEN("A boolean option is set to a string value") {
config->set("gcode_comments", "1");
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 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);

View File

@ -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;
};