Add test to set string options.

This commit is contained in:
Joseph Lenox 2018-07-12 22:02:09 -05:00
parent 7861037556
commit 932a238328
2 changed files with 42 additions and 2 deletions

View File

@ -54,6 +54,7 @@ SCENARIO("Config accessor functions perform as expected.") {
REQUIRE(config->get<ConfigOptionInt>("bed_temperature").getInt() == 100);
}
}
WHEN("An integer-based option is set through the integer interface") {
config->set("bed_temperature", 100);
THEN("The underlying value is set correctly.") {
@ -92,6 +93,24 @@ SCENARIO("Config accessor functions perform as expected.") {
REQUIRE(config->get<ConfigOptionFloat>("perimeter_speed").getFloat() == 60.0);
}
}
WHEN("A string option is set through the string interface") {
config->set("octoprint_apikey", "100");
THEN("The underlying value is set correctly.") {
REQUIRE(config->get<ConfigOptionString>("octoprint_apikey").getString() == "100");
}
}
WHEN("A string option is set through the integer interface") {
config->set("octoprint_apikey", 100);
THEN("The underlying value is set correctly.") {
REQUIRE(config->get<ConfigOptionString>("octoprint_apikey").getString() == "100");
}
}
WHEN("A string option is set through the double interface") {
config->set("octoprint_apikey", 100.5);
THEN("The underlying value is set correctly.") {
REQUIRE(config->get<ConfigOptionString>("octoprint_apikey").getString() == std::to_string(100.5));
}
}
WHEN("An invalid option is requested during set (string).") {
auto except_thrown {false};
try {

View File

@ -127,12 +127,19 @@ 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 coString:
{
auto* ptr {dynamic_cast<ConfigOptionString*>(this->optptr(opt_key, true))};
if (!ptr->deserialize(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::invalid_argument &e) {
} catch (std::invalid_argument e) {
throw InvalidOptionValue(std::string(opt_key) + std::string(" set with invalid value."));
} catch (std::out_of_range &e) {
} catch (std::out_of_range e) {
throw InvalidOptionType(std::string(opt_key) + std::string(" is an invalid Slic3r option."));
}
}
@ -168,6 +175,13 @@ Config::set(const t_config_option_key& opt_key, const int value)
auto* ptr {dynamic_cast<ConfigOptionFloats*>(this->optptr(opt_key, true))};
ptr->deserialize(std::to_string(value), true);
} break;
case coString:
{
auto* ptr {dynamic_cast<ConfigOptionString*>(this->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.");
}
@ -208,6 +222,13 @@ Config::set(const t_config_option_key& opt_key, const double value)
auto* ptr {dynamic_cast<ConfigOptionFloats*>(this->optptr(opt_key, true))};
ptr->deserialize(std::to_string(value), true);
} break;
case coString:
{
auto* ptr {dynamic_cast<ConfigOptionString*>(this->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.");
}