test_config.cpp 12 KB

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