test_config.cpp 10.0 KB

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