From 190de643966c419facba805c611294495de88ad7 Mon Sep 17 00:00:00 2001 From: Joseph Lenox Date: Tue, 1 Jan 2019 19:42:26 -0600 Subject: [PATCH] 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) --- src/test/libslic3r/test_config.cpp | 21 +++++++++++++++------ xs/src/libslic3r/ConfigBase.hpp | 4 ++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/test/libslic3r/test_config.cpp b/src/test/libslic3r/test_config.cpp index c2c150bf6..bc0071499 100644 --- a/src/test/libslic3r/test_config.cpp +++ b/src/test/libslic3r/test_config.cpp @@ -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("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("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("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); diff --git a/xs/src/libslic3r/ConfigBase.hpp b/xs/src/libslic3r/ConfigBase.hpp index bd8b272ce..986fdb127 100644 --- a/xs/src/libslic3r/ConfigBase.hpp +++ b/xs/src/libslic3r/ConfigBase.hpp @@ -480,12 +480,16 @@ class ConfigOptionBool : public ConfigOptionSingle 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; };