From 8e3a2d99cc749f4a6b0ac9063cd04fe246b4a09f Mon Sep 17 00:00:00 2001 From: "D.R.racer" Date: Wed, 20 Jul 2022 16:05:07 +0200 Subject: [PATCH] Unit test ErrDisengageIdler: make sure it never sets error while disengaging --- .../logic/load_filament/test_load_filament.cpp | 3 ++- tests/unit/logic/stubs/main_loop_stub.cpp | 15 +++++++++++++++ tests/unit/logic/stubs/main_loop_stub.h | 2 ++ tests/unit/logic/tool_change/test_tool_change.cpp | 3 ++- .../unload_filament/test_unload_filament.cpp | 2 +- 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/tests/unit/logic/load_filament/test_load_filament.cpp b/tests/unit/logic/load_filament/test_load_filament.cpp index e662e4a..5a65e31 100644 --- a/tests/unit/logic/load_filament/test_load_filament.cpp +++ b/tests/unit/logic/load_filament/test_load_filament.cpp @@ -113,7 +113,8 @@ void FailedLoadToFinda(uint8_t slot, logic::LoadFilament &lf) { REQUIRE(VerifyState(lf, mg::FilamentLoadState::InSelector, slot, slot, false, true, ml::off, ml::blink0, ErrorCode::RUNNING, ProgressCode::ERRDisengagingIdler)); // Stage 3 - disengaging idler in error mode - REQUIRE(WhileTopState(lf, ProgressCode::ERRDisengagingIdler, idlerEngageDisengageMaxSteps)); + SimulateErrDisengagingIdler(lf, ErrorCode::FINDA_DIDNT_SWITCH_ON); + REQUIRE(VerifyState(lf, mg::FilamentLoadState::InSelector, mi::Idler::IdleSlotIndex(), slot, false, false, ml::off, ml::blink0, ErrorCode::FINDA_DIDNT_SWITCH_ON, ProgressCode::ERRWaitingForUser)); } diff --git a/tests/unit/logic/stubs/main_loop_stub.cpp b/tests/unit/logic/stubs/main_loop_stub.cpp index da3f840..e003801 100644 --- a/tests/unit/logic/stubs/main_loop_stub.cpp +++ b/tests/unit/logic/stubs/main_loop_stub.cpp @@ -1,3 +1,4 @@ +#include "catch2/catch.hpp" #include "main_loop_stub.h" #include "homing.h" @@ -162,3 +163,17 @@ void SetFSensorStateAndDebounce(bool press) { main_loop(); } } + +void SimulateErrDisengagingIdler(logic::CommandBase &cb, ErrorCode deferredEC) { + REQUIRE(WhileCondition( + cb, [&](uint32_t) { + if (cb.TopLevelState() == ProgressCode::ERRDisengagingIdler) { + REQUIRE(cb.Error() == ErrorCode::RUNNING); // ensure the error gets never set while disengaging the idler + return true; + } else { + REQUIRE(cb.Error() == deferredEC); + return false; + } + }, + idlerEngageDisengageMaxSteps)); +} diff --git a/tests/unit/logic/stubs/main_loop_stub.h b/tests/unit/logic/stubs/main_loop_stub.h index 23a935c..bcd2847 100644 --- a/tests/unit/logic/stubs/main_loop_stub.h +++ b/tests/unit/logic/stubs/main_loop_stub.h @@ -41,3 +41,5 @@ static constexpr uint32_t idlerEngageDisengageMaxSteps = 40000UL; static constexpr uint32_t selectorMoveMaxSteps = 40000UL; void HomeIdlerAndSelector(); + +void SimulateErrDisengagingIdler(logic::CommandBase &cb, ErrorCode deferredEC); diff --git a/tests/unit/logic/tool_change/test_tool_change.cpp b/tests/unit/logic/tool_change/test_tool_change.cpp index 1d0aac6..fda5b60 100644 --- a/tests/unit/logic/tool_change/test_tool_change.cpp +++ b/tests/unit/logic/tool_change/test_tool_change.cpp @@ -193,7 +193,8 @@ void ToolChangeFailLoadToFinda(logic::ToolChange &tc, uint8_t fromSlot, uint8_t // should end up in error disengage idler REQUIRE(VerifyState(tc, mg::FilamentLoadState::InSelector, toSlot, toSlot, false, true, ml::off, ml::blink0, ErrorCode::RUNNING, ProgressCode::ERRDisengagingIdler)); - REQUIRE(WhileTopState(tc, ProgressCode::ERRDisengagingIdler, 5000)); + + SimulateErrDisengagingIdler(tc, ErrorCode::FINDA_DIDNT_SWITCH_ON); } void ToolChangeFailLoadToFindaLeftBtn(logic::ToolChange &tc, uint8_t toSlot) { diff --git a/tests/unit/logic/unload_filament/test_unload_filament.cpp b/tests/unit/logic/unload_filament/test_unload_filament.cpp index 5b21ad5..5276180 100644 --- a/tests/unit/logic/unload_filament/test_unload_filament.cpp +++ b/tests/unit/logic/unload_filament/test_unload_filament.cpp @@ -150,7 +150,7 @@ void FindaDidntTriggerCommonSetup(uint8_t slot, logic::UnloadFilament &uf) { REQUIRE(VerifyState(uf, mg::FilamentLoadState::InSelector, slot, slot, true, true, ml::off, ml::blink0, ErrorCode::RUNNING, ProgressCode::ERRDisengagingIdler)); // Stage 2 - idler should get disengaged - REQUIRE(WhileTopState(uf, ProgressCode::ERRDisengagingIdler, idlerEngageDisengageMaxSteps)); + SimulateErrDisengagingIdler(uf, ErrorCode::FINDA_DIDNT_SWITCH_OFF); // we still think we have filament loaded at this stage // idler should have been disengaged