mirror of
https://git.mirrors.martin98.com/https://github.com/slic3r/Slic3r.git
synced 2025-08-05 15:30:44 +08:00
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:
parent
90f108ae8e
commit
190de64396
@ -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);
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user