test_config.cpp 10 KB

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