From 0f76496b2e566c78fb31b868af6212f49be412ea Mon Sep 17 00:00:00 2001 From: Joseph Lenox Date: Sun, 11 Nov 2018 00:42:52 -0600 Subject: [PATCH] added test for preset; added constructor for default preset. --- src/GUI/Preset.hpp | 4 +- src/test/GUI/test_preset.cpp | 116 ++++++++++++++++++ .../test_preset/preset_load_numeric.ini | 2 + 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/test/GUI/test_preset.cpp create mode 100644 src/test/inputs/test_preset/preset_load_numeric.ini diff --git a/src/GUI/Preset.hpp b/src/GUI/Preset.hpp index 6c6c0d637..0f4952779 100644 --- a/src/GUI/Preset.hpp +++ b/src/GUI/Preset.hpp @@ -89,7 +89,9 @@ public: bool operator==(const wxString& _name) const { return this->operator==(_name.ToStdString()); } bool operator==(const std::string& _name) const { return _name.compare(this->name) == 0; } + /// Constructor for adding a file-backed Preset(std::string load_dir, std::string filename, preset_t p); + Preset(bool is_default, wxString name, preset_t p) : group(p), name(name), external(false), default_preset(is_default) {}; private: /// store to keep config options for this preset @@ -103,7 +105,7 @@ private: config_ptr _dirty_config { nullptr }; /// Underlying filename for this preset config - wxFileName _file {}; + wxFileName _file {wxFileName()}; /// All the options owned by the corresponding editor t_config_option_keys _group_keys() const; diff --git a/src/test/GUI/test_preset.cpp b/src/test/GUI/test_preset.cpp new file mode 100644 index 000000000..60ee86758 --- /dev/null +++ b/src/test/GUI/test_preset.cpp @@ -0,0 +1,116 @@ +#include +#include "Preset.hpp" +#include "Config.hpp" +#include + +using namespace Slic3r::GUI; +using namespace std::literals::string_literals; + +SCENARIO( "Preset construction" ) { + GIVEN("A preset file with at least one configuration") { + WHEN( "Preset is constructed." ) { + Preset item {std::string(testfile_dir) + "test_preset", "preset_load_numeric.ini"s, preset_t::Print}; + THEN("Name is preset_load_numeric") { + REQUIRE(item.name == "preset_load_numeric"s); + } + THEN("group is Print") { + REQUIRE(item.group == preset_t::Print); + } + THEN("preset default is false.") { + REQUIRE(item.default_preset == false); + } + } + } + GIVEN("A default preset construction") { + WHEN( "Preset is constructed." ) { + Preset item {true, "- default -", preset_t::Print}; + THEN("Name is - default -") { + REQUIRE(item.name == "- default -"s); + } + THEN("group is Print") { + REQUIRE(item.group == preset_t::Print); + } + THEN("preset default is true.") { + REQUIRE(item.default_preset == true); + } + THEN("file_exists() is false") { + REQUIRE(item.file_exists() == false); + } + } + } +} +SCENARIO( "Preset loading" ) { + GIVEN("A preset file with a config item that has adaptive_slicing = 1") { + Preset item {std::string(testfile_dir) + "test_preset", "preset_load_numeric.ini"s, preset_t::Print}; + THEN("file_exists() returns true") { + REQUIRE(item.file_exists() == true); + } + config_ptr ref; + WHEN("The preset file with one item is loaded") { + ref = item.load_config(); + THEN("Config is not dirty.") { + REQUIRE(item.dirty() == false); + } + THEN("adaptive_slicing = 1 in the preset config") { + REQUIRE(item.dirty_config().get("adaptive_slicing").getBool() == true); + } + } + auto config { Slic3r::Config::new_from_defaults() }; + ref = item.load_config(); + WHEN("Option is changed in the config via loading a config") { + // precondition: adaptive_slicing is still true + REQUIRE(item.dirty_config().get("adaptive_slicing").getBool() == true); + // precondition: default option for adaptice_slicing is false + REQUIRE(config->get("adaptive_slicing").getBool() == false); + ref->apply(config); + THEN("Config is dirty.") { + REQUIRE(item.dirty() == true); + } + THEN("adaptive_slicing = 0 in the preset config") { + REQUIRE(item.dirty_config().get("adaptive_slicing").getBool() == false); + } + THEN("subsequent calls yield the same reference") { + REQUIRE(item.load_config() == ref); + } + } + } + GIVEN("A preset file with a config item that has adaptive_slicing = 1 and default_preset = true") { + Preset item {std::string(testfile_dir) + "test_preset", "preset_load_numeric.ini"s, preset_t::Print}; + item.default_preset = true; + config_ptr ref; + WHEN("The preset file with one item is loaded") { + ref = item.load_config(); + THEN("Config is not dirty.") { + REQUIRE(item.dirty() == false); + } + THEN("adaptive_slicing = 0 in the preset config") { + REQUIRE(item.dirty_config().get("adaptive_slicing").getBool() == false); + } + THEN("loaded is true") { + REQUIRE(item.loaded() == true); + } + THEN("subsequent calls yield the same reference") { + REQUIRE(item.load_config() == ref); + } + } + ref = item.load_config(); + WHEN("Option is changed in the config") { + ref->set("adaptive_slicing", true); + THEN("Config is dirty.") { + REQUIRE(item.dirty() == true); + } + THEN("adaptive_slicing = 1 in the preset config") { + REQUIRE(item.dirty_config().get("adaptive_slicing").getBool() == true); + } + } + } + GIVEN("An invalid preset file") { + Preset item {std::string(testfile_dir) + "test_preset", "___invalid__preset_load_numeric.ini"s, preset_t::Print}; + THEN("file_exists() returns false") { + REQUIRE(item.file_exists() == false); + } + THEN("loaded is false") { + REQUIRE(item.loaded() == false); + } + } +} diff --git a/src/test/inputs/test_preset/preset_load_numeric.ini b/src/test/inputs/test_preset/preset_load_numeric.ini new file mode 100644 index 000000000..d1e79e8e5 --- /dev/null +++ b/src/test/inputs/test_preset/preset_load_numeric.ini @@ -0,0 +1,2 @@ +# generated by joseph lenox +adaptive_slicing = 1