test_config.cpp 11 KB

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