Implemented boolean set and some tests to check those values.

This commit is contained in:
Joseph Lenox 2018-07-25 22:25:47 -05:00
parent 0015b3c7d3
commit 9411f5025d
3 changed files with 89 additions and 1 deletions

View File

@ -48,6 +48,24 @@ SCENARIO("Generic config validation performs as expected.") {
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 through the bool interface") {
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 through the string interface") {
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 through the int interface") {
config->set("gcode_comments", 1);
THEN("The underlying value is set correctly.") {
REQUIRE(config->get<ConfigOptionBool>("gcode_comments").getBool() == true);
}
}
WHEN("A numeric option is set through the string interface") {
config->set("bed_temperature", "100");
THEN("The underlying value is set correctly.") {

View File

@ -96,7 +96,7 @@ Config::set(const t_config_option_key& opt_key, const std::string& value)
case coInt:
{
auto* ptr {dynamic_cast<ConfigOptionInt*>(this->_config.optptr(opt_key, true))};
ptr->setInt(std::stoi(value));
ptr->value = std::stoi(value);
} break;
case coInts:
{
@ -137,6 +137,11 @@ Config::set(const t_config_option_key& opt_key, const std::string& value)
throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value."));
}
} break;
case coBool:
{
auto* ptr {dynamic_cast<ConfigOptionBool*>(this->_config.optptr(opt_key, true))};
ptr->deserialize(value);
} break;
default:
Slic3r::Log::warn("Config::set", "Unknown set type.");
}
@ -147,12 +152,70 @@ Config::set(const t_config_option_key& opt_key, const std::string& value)
}
}
void
Config::set(const t_config_option_key& opt_key, const bool value)
{
try {
const auto& def {print_config_def.options.at(opt_key)};
switch (def.type) {
case coBool:
{
auto* ptr {dynamic_cast<ConfigOptionBool*>(this->_config.optptr(opt_key, true))};
ptr->value = value;
} break;
case coInt:
{
auto* ptr {dynamic_cast<ConfigOptionInt*>(this->_config.optptr(opt_key, true))};
ptr->setInt(value);
} break;
case coInts:
{
auto* ptr {dynamic_cast<ConfigOptionInts*>(this->_config.optptr(opt_key, true))};
ptr->deserialize(std::to_string(value), true);
} break;
case coFloat:
{
auto* ptr {dynamic_cast<ConfigOptionFloat*>(this->_config.optptr(opt_key, true))};
ptr->setFloat(value);
} break;
case coFloatOrPercent:
{
auto* ptr {dynamic_cast<ConfigOptionFloatOrPercent*>(this->_config.optptr(opt_key, true))};
ptr->setFloat(value);
ptr->percent = false;
} break;
case coFloats:
{
auto* ptr {dynamic_cast<ConfigOptionFloats*>(this->_config.optptr(opt_key, true))};
ptr->deserialize(std::to_string(value), true);
} break;
case coString:
{
auto* ptr {dynamic_cast<ConfigOptionString*>(this->_config.optptr(opt_key, true))};
if (!ptr->deserialize(std::to_string(value)) ) {
throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value."));
}
} break;
default:
Slic3r::Log::warn("Config::set", "Unknown set type.");
}
} catch (std::out_of_range &e) {
throw InvalidOptionType(std::string(opt_key) + std::string(" is an invalid Slic3r option."));
}
}
void
Config::set(const t_config_option_key& opt_key, const int value)
{
try {
const auto& def {print_config_def.options.at(opt_key)};
switch (def.type) {
case coBool:
{
auto* ptr {dynamic_cast<ConfigOptionBool*>(this->_config.optptr(opt_key, true))};
ptr->value = (value != 0);
} break;
case coInt:
{
auto* ptr {dynamic_cast<ConfigOptionInt*>(this->_config.optptr(opt_key, true))};

View File

@ -95,11 +95,18 @@ public:
/// Function to parse value from a string to whatever opt_key is.
void set(const t_config_option_key& opt_key, const std::string& value);
void set(const t_config_option_key& opt_key, const char* value) { this->set(opt_key, std::string(value));}
/// Function to parse value from an integer to whatever opt_key is, if
/// opt_key is a numeric type. This will throw an exception and do
/// nothing if called with an incorrect type.
void set(const t_config_option_key& opt_key, const int value);
/// Function to parse value from an boolean to whatever opt_key is, if
/// opt_key is a numeric type. This will throw an exception and do
/// nothing if called with an incorrect type.
void set(const t_config_option_key& opt_key, const bool value);
/// Function to parse value from an integer to whatever opt_key is, if
/// opt_key is a numeric type. This will throw an exception and do