From 04204ec0d3e740f80ecc1e526ad2f03e1943500a Mon Sep 17 00:00:00 2001 From: peachismomo Date: Sun, 27 Sep 2026 17:25:56 +0800 Subject: [PATCH] test: cover slicing lifecycle event context --- tests/fff_print/test_print.cpp | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/tests/fff_print/test_print.cpp b/tests/fff_print/test_print.cpp index 383a7165d5..83dbfa0945 100644 --- a/tests/fff_print/test_print.cpp +++ b/tests/fff_print/test_print.cpp @@ -15,6 +15,9 @@ #include "libslic3r/Layer.hpp" #include "libslic3r/Model.hpp" #include "libslic3r/GCodeReader.hpp" +#include "libslic3r/GCode/GCodeProcessor.hpp" +#include "libslic3r/Exception.hpp" +#include "libslic3r/LifecycleEvents.hpp" #include "test_helpers.hpp" #include "test_utils.hpp" @@ -22,7 +25,10 @@ #include #include #include +#include #include +#include +#include using namespace Slic3r; using namespace Slic3r::Test; @@ -224,8 +230,68 @@ std::string resolved_output_name(Model& model, const std::string& format, const return print.output_filename(filename_base); } +struct ScopedLifecycleHook +{ + explicit ScopedLifecycleHook(LifecycleHookFn hook) { set_lifecycle_hook_fn(std::move(hook)); } + ~ScopedLifecycleHook() { set_lifecycle_hook_fn(nullptr); } +}; + } // namespace +TEST_CASE("Slicing lifecycle events identify the model", "[Print][LifecycleEvents]") +{ + struct ObservedEvent { + LifecycleEvent event; + std::string id; + std::string name; + }; + std::vector events; + ScopedLifecycleHook hook([&](LifecycleEvent event, const LifecycleEventContext& ctx) { + events.push_back({ event, ctx.id, ctx.name }); + }); + + Print print; + Model model; + ModelInfo info; + info.model_name = "Lifecycle test model"; + model.model_info = std::make_shared(std::move(info)); + init_print({cube(20)}, print, model); + + print.process(); + ScopedTemporaryFile temp(".gcode"); + print.export_gcode(temp.string(), nullptr, nullptr); + GCodeProcessorResult result; + print.export_gcode_from_previous_file(temp.string(), &result); + + const std::string expected_id = std::to_string(print.model().id().id); + const std::vector expected_events = { + LifecycleEvent::SliceStarted, + LifecycleEvent::SliceGeometryFinished, + LifecycleEvent::GCodeExportStarted, + LifecycleEvent::GCodeExportFinished, + LifecycleEvent::GCodeExportStarted, + LifecycleEvent::GCodeExportFinished, + }; + REQUIRE(events.size() == expected_events.size()); + for (size_t i = 0; i < expected_events.size(); ++i) { + CHECK(events[i].event == expected_events[i]); + CHECK(events[i].id == expected_id); + CHECK(events[i].name == "Lifecycle test model"); + } +} + +TEST_CASE("Output filenames with numeric statistics fail before slicing finishes", "[Print][Regression]") +{ + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_key_value("filename_format", new ConfigOptionString("{int(total_weight*10) / 10.0}")); + + Print print; + Model model; + init_print({cube(20)}, print, model, config); + + CHECK_THROWS_AS(print.output_filename(), PlaceholderParserError); +} + TEST_CASE("Print: {first_object_name} names the first printable object on the plate", "[Print]") { Model model;