test_thumbnails_ini_string.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/Config.hpp"
  3. #include "libslic3r/PrintConfig.hpp"
  4. #include <libslic3r/GCode/Thumbnails.hpp>
  5. using namespace Slic3r;
  6. using namespace GCodeThumbnails;
  7. static std::string empty_thumbnails()
  8. {
  9. return "thumbnails = \n"
  10. "thumbnails_format = ";
  11. }
  12. static std::string valid_thumbnails()
  13. {
  14. return "thumbnails = 160x120/JPG, 23x78/QOI, 230x780/JPG\n"
  15. "thumbnails_format = JPG";
  16. }
  17. static std::string valid_thumbnails2()
  18. {
  19. return "thumbnails = 160x120/PNG, 23x78/QOi, 320x240/PNg, 230x780/JPG\n"
  20. "thumbnails_format = pnG";
  21. }
  22. static std::string valid_thumbnails3()
  23. {
  24. return "thumbnails = 160x120/JPG, 23x78/QOI, 230x780/JPG";
  25. }
  26. static std::string old_valid_thumbnails()
  27. {
  28. return "thumbnails = 160x120\n"
  29. "thumbnails_format = JPG";
  30. }
  31. static std::string old_valid_thumbnails2()
  32. {
  33. return "thumbnails = 160x120, 23x78, 320x240\n"
  34. "thumbnails_format = PNG";
  35. }
  36. static std::string old_invalid_thumbnails()
  37. {
  38. return "thumbnails = 160x\n"
  39. "thumbnails_format = JPG";
  40. }
  41. static std::string old_invalid_thumbnails2()
  42. {
  43. return "thumbnails = 160x120, 23*78, 320x240\n"
  44. "thumbnails_format = PNG";
  45. }
  46. static std::string out_of_range_thumbnails()
  47. {
  48. return "thumbnails = 1160x1200/PNG, 23x78/QOI, 320x240/PNG, 230x780/JPG\n"
  49. "thumbnails_format = PNG";
  50. }
  51. static std::string out_of_range_thumbnails2()
  52. {
  53. return "thumbnails = 1160x120/PNG, 23x78/QOI, -320x240/PNG, 230x780/JPG\n"
  54. "thumbnails_format = PNG";
  55. }
  56. static std::string invalid_ext_thumbnails()
  57. {
  58. return "thumbnails = 1160x120/PNk, 23x78/QOI, 320x240/PNG, 230x780/JPG\n"
  59. "thumbnails_format = QOI";
  60. }
  61. static std::string invalid_ext_thumbnails2()
  62. {
  63. return "thumbnails = 1160x120/PNG, 23x78/QO, 320x240/PNG, 230x780/JPG\n"
  64. "thumbnails_format = PNG";
  65. }
  66. static std::string invalid_val_thumbnails()
  67. {
  68. return "thumbnails = 1160x/PNg, 23x78/QOI, 320x240/PNG, 230x780/JPG\n"
  69. "thumbnails_format = JPG";
  70. }
  71. static std::string invalid_val_thumbnails2()
  72. {
  73. return "thumbnails = x120/PNg, 23x78/QOI, 320x240/PNG, 230x780/JPG\n"
  74. "thumbnails_format = PNG";
  75. }
  76. static std::string invalid_val_thumbnails3()
  77. {
  78. return "thumbnails = 1x/PNg, 23x78/QOI, 320x240/PNG, 230x780/JPG\n"
  79. "thumbnails_format = qoi";
  80. }
  81. static std::string invalid_val_thumbnails4()
  82. {
  83. return "thumbnails = 123*78/QOI, 320x240/PNG, 230x780/JPG\n"
  84. "thumbnails_format = jpG";
  85. }
  86. static DynamicPrintConfig thumbnails_config()
  87. {
  88. DynamicPrintConfig config;
  89. config.apply_only(FullPrintConfig::defaults() , { "thumbnails", "thumbnails_format" });
  90. return config;
  91. }
  92. TEST_CASE("Validate Empty Thumbnails", "[Thumbnails in Config]") {
  93. DynamicPrintConfig config = thumbnails_config();
  94. auto test_loaded_config = [](DynamicPrintConfig& config) {
  95. REQUIRE(config.opt<ConfigOptionString>("thumbnails")->empty());
  96. REQUIRE(config.option("thumbnails_format")->getInt() == (int)GCodeThumbnailsFormat::PNG);
  97. };
  98. SECTION("Load empty init_data") {
  99. REQUIRE_NOTHROW(config.load_from_ini_string("", Enable));
  100. test_loaded_config(config);
  101. }
  102. SECTION("Load empty format and empty thumbnails") {
  103. REQUIRE_THROWS_AS(config.load_from_ini_string(empty_thumbnails(), Enable), BadOptionValueException);
  104. test_loaded_config(config);
  105. }
  106. }
  107. TEST_CASE("Validate New Thumbnails", "[Thumbnails in Config]") {
  108. DynamicPrintConfig config = thumbnails_config();
  109. auto test_loaded_config = [](DynamicPrintConfig& config, GCodeThumbnailsFormat format) {
  110. REQUIRE(!config.opt<ConfigOptionString>("thumbnails")->empty());
  111. REQUIRE(config.option("thumbnails_format")->getInt() == (int)format);
  112. };
  113. SECTION("Test 1 (valid)") {
  114. REQUIRE_NOTHROW(config.load_from_ini_string(valid_thumbnails(), Enable));
  115. test_loaded_config(config, GCodeThumbnailsFormat::JPG);
  116. }
  117. SECTION("Test 2 (valid)") {
  118. REQUIRE_NOTHROW(config.load_from_ini_string(valid_thumbnails2(), Enable));
  119. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  120. }
  121. SECTION("Test 3 (valid)") {
  122. REQUIRE_NOTHROW(config.load_from_ini_string(valid_thumbnails3(), Enable));
  123. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  124. }
  125. SECTION("Test 1 (out_of_range)") {
  126. REQUIRE_THROWS_AS(config.load_from_ini_string(out_of_range_thumbnails(), Enable), BadOptionValueException);
  127. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  128. }
  129. SECTION("Test 2 (out_of_range)") {
  130. REQUIRE_THROWS_AS(config.load_from_ini_string(out_of_range_thumbnails2(), Enable), BadOptionValueException);
  131. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  132. }
  133. SECTION("Test 1 (invalid_ext)") {
  134. REQUIRE_THROWS_AS(config.load_from_ini_string(invalid_ext_thumbnails(), Enable), BadOptionValueException);
  135. test_loaded_config(config, GCodeThumbnailsFormat::QOI);
  136. }
  137. SECTION("Test 2 (invalid_ext)") {
  138. REQUIRE_THROWS_AS(config.load_from_ini_string(invalid_ext_thumbnails2(), Enable), BadOptionValueException);
  139. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  140. }
  141. SECTION("Test 1 (invalid_val)") {
  142. REQUIRE_THROWS_AS(config.load_from_ini_string(invalid_val_thumbnails(), Enable), BadOptionValueException);
  143. test_loaded_config(config, GCodeThumbnailsFormat::JPG);
  144. }
  145. SECTION("Test 2 (invalid_val)") {
  146. REQUIRE_THROWS_AS(config.load_from_ini_string(invalid_val_thumbnails2(), Enable), BadOptionValueException);
  147. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  148. }
  149. SECTION("Test 3 (invalid_val)") {
  150. REQUIRE_THROWS_AS(config.load_from_ini_string(invalid_val_thumbnails3(), Enable), BadOptionValueException);
  151. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  152. }
  153. SECTION("Test 4 (invalid_val)") {
  154. REQUIRE_THROWS_AS(config.load_from_ini_string(invalid_val_thumbnails4(), Enable), BadOptionValueException);
  155. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  156. }
  157. }
  158. TEST_CASE("Validate Old Thumbnails", "[Thumbnails in Config]") {
  159. DynamicPrintConfig config = thumbnails_config();
  160. auto test_loaded_config = [](DynamicPrintConfig& config, GCodeThumbnailsFormat format) {
  161. REQUIRE(!config.opt<ConfigOptionString>("thumbnails")->empty());
  162. REQUIRE(config.option("thumbnails_format")->getInt() == (int)format);
  163. };
  164. SECTION("Test 1 (valid)") {
  165. REQUIRE_NOTHROW(config.load_from_ini_string(old_valid_thumbnails(), Enable));
  166. test_loaded_config(config, GCodeThumbnailsFormat::JPG);
  167. }
  168. SECTION("Test 2 (valid)") {
  169. REQUIRE_NOTHROW(config.load_from_ini_string(old_valid_thumbnails2(), Enable));
  170. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  171. }
  172. SECTION("Test 1 (invalid)") {
  173. REQUIRE_THROWS_AS(config.load_from_ini_string(old_invalid_thumbnails(), Enable), BadOptionValueException);
  174. test_loaded_config(config, GCodeThumbnailsFormat::JPG);
  175. }
  176. SECTION("Test 2 (invalid)") {
  177. REQUIRE_THROWS_AS(config.load_from_ini_string(old_invalid_thumbnails2(), Enable), BadOptionValueException);
  178. test_loaded_config(config, GCodeThumbnailsFormat::PNG);
  179. }
  180. }