From afc90ed91eb250541f1c0bc4eacac55379f1e62e Mon Sep 17 00:00:00 2001 From: Joseph Lenox Date: Sat, 24 Nov 2018 13:12:34 -0600 Subject: [PATCH] Test --dont-arrange and --center. --- src/test/GUI/test_cli.cpp | 93 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/test/GUI/test_cli.cpp b/src/test/GUI/test_cli.cpp index 12c95a980..e3d9df5eb 100644 --- a/src/test/GUI/test_cli.cpp +++ b/src/test/GUI/test_cli.cpp @@ -5,6 +5,7 @@ #include "test_options.hpp" #include "slic3r.hpp" +#include "GCodeReader.hpp" using namespace Slic3r; using namespace std::string_literals; @@ -30,6 +31,17 @@ void clean_file(const std::string& name, const std::string& ext, bool glob = fal std::remove(testfile(filename).c_str()); } + +std::string read_to_string(const std::string& name) { + std::stringstream buf; + std::ifstream f(testfile(name)); + if (!f.good()) return ""s; + std::copy(std::istreambuf_iterator(f), + std::istreambuf_iterator(), + std::ostreambuf_iterator(buf)); + f.close(); + return buf.str(); +} char** to_cstr_array(std::vector in, char** argv) { int i = 0; for (auto& str : in) { @@ -208,6 +220,87 @@ SCENARIO("CLI Transform arguments", "[!shouldfail]") { } } +// Test the --center and --dont-arrange parameters. +SCENARIO("CLI positioning arguments", "[!shouldfail]") { + char* args_cli[20]; + std::vector in_args; + in_args.reserve(20); + in_args.emplace_back("gui_test"s); + GIVEN( " 3D Model for a 20mm box, centered around 0,0 and gcode export" ) { + in_args.emplace_back(testfile("test_cli/20mmbox.stl"s)); + in_args.emplace(in_args.cend()-1, "-g"); + in_args.emplace(in_args.cend()-1, "--load"s); + in_args.emplace(in_args.cend()-1, testfile("test_cli/20mmbox_config.ini")); + CLI cut; + + WHEN("--center is supplied with 40,40") { + in_args.emplace(in_args.cend()-1, "--center"); + in_args.emplace(in_args.cend()-1, "40,40"); + cut.run(in_args.size(), to_cstr_array(in_args, args_cli)); + + THEN ("The first layer of the print should be centered around 0,0") { + std::string exported { read_to_string("test_cli/20mmbox.gcode"s)}; + auto reader {GCodeReader()}; + REQUIRE(exported != ""s); + double min_x = 0.0, max_x = 0.0, min_y = 0.0, max_y = 0.0; + reader.apply_config(cut.full_print_config_ref()); + reader.parse(exported, [&min_x, &min_y, &max_x, &max_y] (GCodeReader& self, const GCodeReader::GCodeLine& line) + { + if (self.Z < 0.6) { + min_x = std::min(min_x, static_cast(self.X)); + min_y = std::min(min_y, static_cast(self.Y)); + max_x = std::max(max_x, static_cast(self.X)); + max_y = std::max(max_y, static_cast(self.Y)); + } + }); + AND_THEN("Minimum X encountered is about -29.9") { + REQUIRE(min_x == Approx(-29.9)); + } + AND_THEN("Minimum Y encountered is about -29.9") { + REQUIRE(min_y == Approx(-29.9)); + } + AND_THEN("Maximum X encountered is about 39.9") { + REQUIRE(max_x == Approx(39.9)); + } + AND_THEN("Maximum Y encountered is about 39.9") { + REQUIRE(max_y == Approx(39.9)); + } + } } + WHEN("--dont-arrange is supplied") { + in_args.emplace(in_args.cend()-1, "--dont-arrange"); + cut.run(in_args.size(), to_cstr_array(in_args, args_cli)); + + THEN ("The first layer of the print should be centered around 0,0") { + auto reader {GCodeReader()}; + std::string exported { read_to_string("test_cli/20mmbox.gcode"s)}; + REQUIRE(exported != ""s); + double min_x, max_x, min_y, max_y; + reader.apply_config(cut.full_print_config_ref()); + reader.parse(exported, [&min_x, &min_y, &max_x, &max_y] (GCodeReader& self, const GCodeReader::GCodeLine& line) + { + if (self.Z < 0.6) { + min_x = std::min(min_x, static_cast(self.X)); + min_y = std::min(min_y, static_cast(self.Y)); + max_x = std::max(max_x, static_cast(self.X)); + max_y = std::max(max_y, static_cast(self.Y)); + } + }); + AND_THEN("Minimum X encountered is about -9.9") { + REQUIRE(min_x == Approx(-9.9)); + } + AND_THEN("Minimum Y encountered is about -9.9") { + REQUIRE(min_y == Approx(-9.9)); + } + AND_THEN("Maximum X encountered is about 9.9") { + REQUIRE(max_x == Approx(9.9)); + } + AND_THEN("Maximum Y encountered is about 9.9") { + REQUIRE(max_y == Approx(9.9)); + } + } + } + clean_array(in_args.size(), args_cli); + clean_file("test_cli/20mmbox", "gcode"); } }