Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorVojtech Bubnik <bubnikv@gmail.com>2022-01-07 14:23:15 +0300
committerYuSanka <yusanka@gmail.com>2022-01-17 14:29:47 +0300
commitaa62868ccbc4cc2d296f263325adc5fe9ebfcec4 (patch)
treedda5f66e09f52e140d31d31e43f9d1a85c702d9c /tests
parent2da3d80a62e092495ccff543e93e399576e11204 (diff)
WIP G-code find & replace: Unit tests and some bug fixes.
Diffstat (limited to 'tests')
-rw-r--r--tests/fff_print/CMakeLists.txt1
-rw-r--r--tests/fff_print/test_gcodefindreplace.cpp218
2 files changed, 219 insertions, 0 deletions
diff --git a/tests/fff_print/CMakeLists.txt b/tests/fff_print/CMakeLists.txt
index c69e722af..50b45e384 100644
--- a/tests/fff_print/CMakeLists.txt
+++ b/tests/fff_print/CMakeLists.txt
@@ -7,6 +7,7 @@ add_executable(${_TEST_NAME}_tests
test_fill.cpp
test_flow.cpp
test_gcode.cpp
+ test_gcodefindreplace.cpp
test_gcodewriter.cpp
test_model.cpp
test_print.cpp
diff --git a/tests/fff_print/test_gcodefindreplace.cpp b/tests/fff_print/test_gcodefindreplace.cpp
new file mode 100644
index 000000000..be0dd8a01
--- /dev/null
+++ b/tests/fff_print/test_gcodefindreplace.cpp
@@ -0,0 +1,218 @@
+#include <catch2/catch.hpp>
+
+#include <memory>
+
+#include "libslic3r/GCode/FindReplace.hpp"
+
+using namespace Slic3r;
+
+SCENARIO("Find/Replace with plain text", "[GCodeFindReplace]") {
+ GIVEN("G-code") {
+ const std::string gcode =
+ "G1 Z0; home\n"
+ "G1 Z1; move up\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n";
+ WHEN("Replace \"move up\" with \"move down\", case sensitive") {
+ GCodeFindReplace find_replace({ "move up", "move down", "" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ WHEN("Replace \"move up\" with \"move down\", case insensitive") {
+ GCodeFindReplace find_replace({ "move up", "move down", "i" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ WHEN("Replace \"move UP\" with \"move down\", case insensitive") {
+ GCodeFindReplace find_replace({ "move UP", "move down", "i" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ WHEN("Replace \"move up\" with \"move down\", case sensitive") {
+ GCodeFindReplace find_replace({ "move UP", "move down", "" });
+ REQUIRE(find_replace.process_layer(gcode) == gcode);
+ }
+
+ // Whole word
+ WHEN("Replace \"move up\" with \"move down\", whole word") {
+ GCodeFindReplace find_replace({ "move up", "move down", "w" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ WHEN("Replace \"move u\" with \"move down\", whole word") {
+ GCodeFindReplace find_replace({ "move u", "move down", "w" });
+ REQUIRE(find_replace.process_layer(gcode) == gcode);
+ }
+ WHEN("Replace \"ove up\" with \"move down\", whole word") {
+ GCodeFindReplace find_replace({ "move u", "move down", "w" });
+ REQUIRE(find_replace.process_layer(gcode) == gcode);
+ }
+
+ // Multi-line replace
+ WHEN("Replace \"move up\\nG1 X0 \" with \"move down\\nG0 X1 \"") {
+ GCodeFindReplace find_replace({ "move up\\nG1 X0 ", "move down\\nG0 X1 ", "" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G0 X1 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ // Multi-line replace, whole word.
+ WHEN("Replace \"move up\\nG1 X0\" with \"move down\\nG0 X1\", whole word") {
+ GCodeFindReplace find_replace({ "move up\\nG1 X0", "move down\\nG0 X1", "w" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G0 X1 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ // Multi-line replace, whole word, fails.
+ WHEN("Replace \"move up\\nG1 X\" with \"move down\\nG0 X\", whole word") {
+ GCodeFindReplace find_replace({ "move up\\nG1 X", "move down\\nG0 X", "w" });
+ REQUIRE(find_replace.process_layer(gcode) == gcode);
+ }
+ }
+
+ GIVEN("G-code with decimals") {
+ const std::string gcode =
+ "G1 Z0.123; home\n"
+ "G1 Z1.21; move up\n"
+ "G1 X0 Y.33 Z.431 E1.2; perimeter\n";
+ WHEN("Regular expression NOT processed in non-regex mode") {
+ GCodeFindReplace find_replace({ "( [XYZEF]-?)\\.([0-9]+)", "\\10.\\2", "" });
+ REQUIRE(find_replace.process_layer(gcode) == gcode);
+ }
+ }
+}
+
+SCENARIO("Find/Replace with regexp", "[GCodeFindReplace]") {
+ GIVEN("G-code") {
+ const std::string gcode =
+ "G1 Z0; home\n"
+ "G1 Z1; move up\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n";
+ WHEN("Replace \"move up\" with \"move down\", case sensitive") {
+ GCodeFindReplace find_replace({ "move up", "move down", "r" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ WHEN("Replace \"move up\" with \"move down\", case insensitive") {
+ GCodeFindReplace find_replace({ "move up", "move down", "ri" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ WHEN("Replace \"move UP\" with \"move down\", case insensitive") {
+ GCodeFindReplace find_replace({ "move UP", "move down", "ri" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ WHEN("Replace \"move up\" with \"move down\", case sensitive") {
+ GCodeFindReplace find_replace({ "move UP", "move down", "r" });
+ REQUIRE(find_replace.process_layer(gcode) == gcode);
+ }
+
+ // Whole word
+ WHEN("Replace \"move up\" with \"move down\", whole word") {
+ GCodeFindReplace find_replace({ "move up", "move down", "rw" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G1 X0 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ WHEN("Replace \"move u\" with \"move down\", whole word") {
+ GCodeFindReplace find_replace({ "move u", "move down", "rw" });
+ REQUIRE(find_replace.process_layer(gcode) == gcode);
+ }
+ WHEN("Replace \"ove up\" with \"move down\", whole word") {
+ GCodeFindReplace find_replace({ "move u", "move down", "rw" });
+ REQUIRE(find_replace.process_layer(gcode) == gcode);
+ }
+
+ // Multi-line replace
+ WHEN("Replace \"move up\\nG1 X0 \" with \"move down\\nG0 X1 \"") {
+ GCodeFindReplace find_replace({ "move up\\nG1 X0 ", "move down\\nG0 X1 ", "r" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G0 X1 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ // Multi-line replace, whole word.
+ WHEN("Replace \"move up\\nG1 X0\" with \"move down\\nG0 X1\", whole word") {
+ GCodeFindReplace find_replace({ "move up\\nG1 X0", "move down\\nG0 X1", "rw" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0; home\n"
+ // substituted
+ "G1 Z1; move down\n"
+ "G0 X1 Y1 Z1; perimeter\n"
+ "G1 X13 Y32 Z1; infill\n"
+ "G1 X13 Y32 Z1; wipe\n");
+ }
+ // Multi-line replace, whole word, fails.
+ WHEN("Replace \"move up\\nG1 X\" with \"move down\\nG0 X\", whole word") {
+ GCodeFindReplace find_replace({ "move up\\nG1 X", "move down\\nG0 X", "rw" });
+ REQUIRE(find_replace.process_layer(gcode) == gcode);
+ }
+ }
+
+ GIVEN("G-code with decimals") {
+ const std::string gcode =
+ "G1 Z0.123; home\n"
+ "G1 Z1.21; move up\n"
+ "G1 X0 Y.33 Z.431 E1.2; perimeter\n";
+ WHEN("Missing zeros before dot filled in") {
+ GCodeFindReplace find_replace({ "( [XYZEF]-?)\\.([0-9]+)", "\\10.\\2", "r" });
+ REQUIRE(find_replace.process_layer(gcode) ==
+ "G1 Z0.123; home\n"
+ "G1 Z1.21; move up\n"
+ "G1 X0 Y0.33 Z0.431 E1.2; perimeter\n");
+ }
+ }
+}