test_config.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/Config.hpp"
  3. #include "libslic3r/PrintConfig.hpp"
  4. #include "libslic3r/LocalesUtils.hpp"
  5. #include <cereal/types/polymorphic.hpp>
  6. #include <cereal/types/string.hpp>
  7. #include <cereal/types/vector.hpp>
  8. #include <cereal/archives/binary.hpp>
  9. using namespace Slic3r;
  10. SCENARIO("Generic config validation performs as expected.", "[Config]") {
  11. GIVEN("A config generated from default options") {
  12. Slic3r::DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  13. WHEN("perimeter_extrusion_width is set to 250%, a valid value") {
  14. config.set_deserialize_strict("perimeter_extrusion_width", "250%");
  15. THEN( "The config is read as valid.") {
  16. REQUIRE(config.validate().empty());
  17. }
  18. }
  19. WHEN("perimeter_extrusion_width is set to -10, an invalid value") {
  20. config.set("perimeter_extrusion_width", -10);
  21. THEN( "Validate returns error") {
  22. REQUIRE(! config.validate().empty());
  23. }
  24. }
  25. WHEN("perimeters is set to -10, an invalid value") {
  26. config.set("perimeters", -10);
  27. THEN( "Validate returns error") {
  28. REQUIRE(! config.validate().empty());
  29. }
  30. }
  31. }
  32. }
  33. SCENARIO("Config accessor functions perform as expected.", "[Config]") {
  34. auto test = [](ConfigBase &config) {
  35. WHEN("A boolean option is set to a boolean value") {
  36. REQUIRE_NOTHROW(config.set("gcode_comments", true));
  37. THEN("The underlying value is set correctly.") {
  38. REQUIRE(config.opt<ConfigOptionBool>("gcode_comments")->getBool() == true);
  39. }
  40. }
  41. WHEN("A boolean option is set to a string value representing a 0 or 1") {
  42. CHECK_NOTHROW(config.set_deserialize_strict("gcode_comments", "1"));
  43. THEN("The underlying value is set correctly.") {
  44. REQUIRE(config.opt<ConfigOptionBool>("gcode_comments")->getBool() == true);
  45. }
  46. }
  47. WHEN("A boolean option is set to a string value representing something other than 0 or 1") {
  48. THEN("A BadOptionTypeException exception is thrown.") {
  49. REQUIRE_THROWS_AS(config.set("gcode_comments", "Z"), BadOptionTypeException);
  50. }
  51. AND_THEN("Value is unchanged.") {
  52. REQUIRE(config.opt<ConfigOptionBool>("gcode_comments")->getBool() == false);
  53. }
  54. }
  55. WHEN("A boolean option is set to an int value") {
  56. THEN("A BadOptionTypeException exception is thrown.") {
  57. REQUIRE_THROWS_AS(config.set("gcode_comments", 1), BadOptionTypeException);
  58. }
  59. }
  60. WHEN("A numeric option is set from serialized string") {
  61. config.set_deserialize_strict("bed_temperature", "100");
  62. THEN("The underlying value is set correctly.") {
  63. REQUIRE(config.opt<ConfigOptionInts>("bed_temperature")->get_at(0) == 100);
  64. }
  65. }
  66. #if 0
  67. //FIXME better design accessors for vector elements.
  68. WHEN("An integer-based option is set through the integer interface") {
  69. config.set("bed_temperature", 100);
  70. THEN("The underlying value is set correctly.") {
  71. REQUIRE(config.opt<ConfigOptionInts>("bed_temperature")->get_at(0) == 100);
  72. }
  73. }
  74. #endif
  75. WHEN("An floating-point option is set through the integer interface") {
  76. config.set("perimeter_speed", 10);
  77. THEN("The underlying value is set correctly.") {
  78. REQUIRE(config.opt<ConfigOptionFloat>("perimeter_speed")->getFloat() == 10.0);
  79. }
  80. }
  81. WHEN("A floating-point option is set through the double interface") {
  82. config.set("perimeter_speed", 5.5);
  83. THEN("The underlying value is set correctly.") {
  84. REQUIRE(config.opt<ConfigOptionFloat>("perimeter_speed")->getFloat() == 5.5);
  85. }
  86. }
  87. WHEN("An integer-based option is set through the double interface") {
  88. THEN("A BadOptionTypeException exception is thrown.") {
  89. REQUIRE_THROWS_AS(config.set("bed_temperature", 5.5), BadOptionTypeException);
  90. }
  91. }
  92. WHEN("A numeric option is set to a non-numeric value.") {
  93. THEN("A BadOptionTypeException exception is thown.") {
  94. REQUIRE_THROWS_AS(config.set_deserialize_strict("perimeter_speed", "zzzz"), BadOptionValueException);
  95. }
  96. THEN("The value does not change.") {
  97. REQUIRE(config.opt<ConfigOptionFloat>("perimeter_speed")->getFloat() == 60.0);
  98. }
  99. }
  100. WHEN("A string option is set through the string interface") {
  101. config.set("end_gcode", "100");
  102. THEN("The underlying value is set correctly.") {
  103. REQUIRE(config.opt<ConfigOptionString>("end_gcode")->value == "100");
  104. }
  105. }
  106. WHEN("A string option is set through the integer interface") {
  107. config.set("end_gcode", 100);
  108. THEN("The underlying value is set correctly.") {
  109. REQUIRE(config.opt<ConfigOptionString>("end_gcode")->value == "100");
  110. }
  111. }
  112. WHEN("A string option is set through the double interface") {
  113. config.set("end_gcode", 100.5);
  114. THEN("The underlying value is set correctly.") {
  115. REQUIRE(config.opt<ConfigOptionString>("end_gcode")->value == float_to_string_decimal_point(100.5));
  116. }
  117. }
  118. WHEN("A float or percent is set as a percent through the string interface.") {
  119. config.set_deserialize_strict("first_layer_extrusion_width", "100%");
  120. THEN("Value and percent flag are 100/true") {
  121. auto tmp = config.opt<ConfigOptionFloatOrPercent>("first_layer_extrusion_width");
  122. REQUIRE(tmp->percent == true);
  123. REQUIRE(tmp->value == 100);
  124. }
  125. }
  126. WHEN("A float or percent is set as a float through the string interface.") {
  127. config.set_deserialize_strict("first_layer_extrusion_width", "100");
  128. THEN("Value and percent flag are 100/false") {
  129. auto tmp = config.opt<ConfigOptionFloatOrPercent>("first_layer_extrusion_width");
  130. REQUIRE(tmp->percent == false);
  131. REQUIRE(tmp->value == 100);
  132. }
  133. }
  134. WHEN("A float or percent is set as a float through the int interface.") {
  135. config.set("first_layer_extrusion_width", 100);
  136. THEN("Value and percent flag are 100/false") {
  137. auto tmp = config.opt<ConfigOptionFloatOrPercent>("first_layer_extrusion_width");
  138. REQUIRE(tmp->percent == false);
  139. REQUIRE(tmp->value == 100);
  140. }
  141. }
  142. WHEN("A float or percent is set as a float through the double interface.") {
  143. config.set("first_layer_extrusion_width", 100.5);
  144. THEN("Value and percent flag are 100.5/false") {
  145. auto tmp = config.opt<ConfigOptionFloatOrPercent>("first_layer_extrusion_width");
  146. REQUIRE(tmp->percent == false);
  147. REQUIRE(tmp->value == 100.5);
  148. }
  149. }
  150. WHEN("An invalid option is requested during set.") {
  151. THEN("A BadOptionTypeException exception is thrown.") {
  152. REQUIRE_THROWS_AS(config.set("deadbeef_invalid_option", 1), UnknownOptionException);
  153. REQUIRE_THROWS_AS(config.set("deadbeef_invalid_option", 1.0), UnknownOptionException);
  154. REQUIRE_THROWS_AS(config.set("deadbeef_invalid_option", "1"), UnknownOptionException);
  155. REQUIRE_THROWS_AS(config.set("deadbeef_invalid_option", true), UnknownOptionException);
  156. }
  157. }
  158. WHEN("An invalid option is requested during get.") {
  159. THEN("A UnknownOptionException exception is thrown.") {
  160. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionString>("deadbeef_invalid_option", false), UnknownOptionException);
  161. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionFloat>("deadbeef_invalid_option", false), UnknownOptionException);
  162. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionInt>("deadbeef_invalid_option", false), UnknownOptionException);
  163. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionBool>("deadbeef_invalid_option", false), UnknownOptionException);
  164. }
  165. }
  166. WHEN("An invalid option is requested during opt.") {
  167. THEN("A UnknownOptionException exception is thrown.") {
  168. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionString>("deadbeef_invalid_option", false), UnknownOptionException);
  169. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionFloat>("deadbeef_invalid_option", false), UnknownOptionException);
  170. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionInt>("deadbeef_invalid_option", false), UnknownOptionException);
  171. REQUIRE_THROWS_AS(config.option_throw<ConfigOptionBool>("deadbeef_invalid_option", false), UnknownOptionException);
  172. }
  173. }
  174. WHEN("getX called on an unset option.") {
  175. THEN("The default is returned.") {
  176. REQUIRE(config.opt_float("layer_height") == 0.3);
  177. REQUIRE(config.opt_int("raft_layers") == 0);
  178. REQUIRE(config.opt_bool("support_material") == false);
  179. }
  180. }
  181. WHEN("getFloat called on an option that has been set.") {
  182. config.set("layer_height", 0.5);
  183. THEN("The set value is returned.") {
  184. REQUIRE(config.opt_float("layer_height") == 0.5);
  185. }
  186. }
  187. };
  188. GIVEN("DynamicPrintConfig generated from default options") {
  189. auto config = Slic3r::DynamicPrintConfig::full_print_config();
  190. test(config);
  191. }
  192. GIVEN("FullPrintConfig generated from default options") {
  193. Slic3r::FullPrintConfig config;
  194. test(config);
  195. }
  196. }
  197. SCENARIO("Config ini load/save interface", "[Config]") {
  198. WHEN("new_from_ini is called") {
  199. Slic3r::DynamicPrintConfig config;
  200. std::string path = std::string(TEST_DATA_DIR) + "/test_config/new_from_ini.ini";
  201. config.load_from_ini(path, ForwardCompatibilitySubstitutionRule::Disable);
  202. THEN("Config object contains ini file options.") {
  203. REQUIRE(config.option_throw<ConfigOptionStrings>("filament_colour", false)->values.size() == 1);
  204. REQUIRE(config.option_throw<ConfigOptionStrings>("filament_colour", false)->values.front() == "#ABCD");
  205. }
  206. }
  207. }
  208. SCENARIO("DynamicPrintConfig serialization", "[Config]") {
  209. WHEN("DynamicPrintConfig is serialized and deserialized") {
  210. FullPrintConfig full_print_config;
  211. DynamicPrintConfig cfg;
  212. cfg.apply(full_print_config, false);
  213. std::string serialized;
  214. try {
  215. std::ostringstream ss;
  216. cereal::BinaryOutputArchive oarchive(ss);
  217. oarchive(cfg);
  218. serialized = ss.str();
  219. } catch (const std::runtime_error & /* e */) {
  220. // e.what();
  221. }
  222. THEN("Config object contains ini file options.") {
  223. DynamicPrintConfig cfg2;
  224. try {
  225. std::stringstream ss(serialized);
  226. cereal::BinaryInputArchive iarchive(ss);
  227. iarchive(cfg2);
  228. } catch (const std::runtime_error & /* e */) {
  229. // e.what();
  230. }
  231. REQUIRE(cfg == cfg2);
  232. }
  233. }
  234. }