def test_exitExcludedRegion_zIncreased(self):
        """Test exitExcludedRegion when the final Z is greater than the initial Z."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        unit.excluding = True
        unit.excludeStartTime = time.time()
        unit.feedRate = 1000
        unit.feedRateUnitMultiplier = 1
        unit.lastPosition = create_position(x=1, y=2, z=3, extruderPosition=4)
        unit.position = create_position(x=10, y=20, z=30, extruderPosition=40)

        with mock.patch.object(
                unit, '_processPendingCommands') as mockProcessPendingCommands:
            mockProcessPendingCommands.return_value = ["pendingCommand"]

            result = unit.exitExcludedRegion("G1 X1 Y2")

            mockProcessPendingCommands.assert_called_with()
            self.assertEqual(
                result, [
                    "pendingCommand", "G92 E40", "G0 F1000 Z30",
                    "G0 F1000 X10 Y20"
                ], "The result should be a list of the expected commands.")
            self.assertFalse(unit.excluding,
                             "The excluding flag should be cleared.")
    def _test_enterExcludedRegion_common(self, enteringExcludedRegionGcode):
        """Test common functionality of enterExcludedRegion when exclusion is enabled."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        unit.excluding = False
        unit.excludeStartTime = "oldStartTime"
        unit.numExcludedCommands = 42
        unit.numCommands = 84
        unit.lastPosition = "oldPosition"

        unit.enteringExcludedRegionGcode = enteringExcludedRegionGcode

        result = unit.enterExcludedRegion("G1 X1 Y2")

        self.assertTrue(unit.excluding, "The excluding flag should be True")
        self.assertNotEqual(unit.excludeStartTime, "oldStartTime",
                            "The excludeStartTime should be updated")
        self.assertEqual(unit.numExcludedCommands, 0,
                         "The numExcludedCommands should be reset to 0")
        self.assertEqual(unit.numCommands, 0,
                         "The numCommands should be reset to 0")
        self.assertNotEqual(unit.lastPosition, "oldPosition",
                            "The position should be updated")

        return result
    def _resetStateSetup():
        """Create and initialize the instance to test the resetState method on."""
        # pylint: disable=protected-access
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        unit.excludedRegions = ["abc"]
        unit.position.X_AXIS.current = 100
        unit.feedRate = 100
        unit.feedRateUnitMultiplier = 1234
        unit._exclusionEnabled = False
        unit.excluding = True
        unit.excludeStartTime = None
        unit.numExcludedCommands = 4321
        unit.numCommands = 6789
        unit.lastRetraction = "abc"
        unit.lastPosition = "123"
        unit.pendingCommands = {"a": 1}

        return unit
    def test_exitExcludedRegion_unitMultiplier(self):
        """Test exitExcludedRegion when a non-native unit multiplier is in effect."""
        mockLogger = mock.Mock()
        unit = ExcludeRegionState(mockLogger)

        unit.excluding = True
        unit.excludeStartTime = time.time()
        unit.feedRate = 1000
        unit.feedRateUnitMultiplier = INCH_TO_MM_FACTOR
        unit.lastPosition = create_position(x=1,
                                            y=2,
                                            z=3,
                                            extruderPosition=4,
                                            unitMultiplier=INCH_TO_MM_FACTOR)
        unit.position = create_position(x=10,
                                        y=20,
                                        z=30,
                                        extruderPosition=40,
                                        unitMultiplier=INCH_TO_MM_FACTOR)

        with mock.patch.object(
                unit, '_processPendingCommands') as mockProcessPendingCommands:
            mockProcessPendingCommands.return_value = []

            result = unit.exitExcludedRegion("G1 X1 Y2")

            mockProcessPendingCommands.assert_called_with()
            self.assertEqual(
                result, [
                    "G92 E%s" % (40 / INCH_TO_MM_FACTOR),
                    "G0 F%s Z%s" %
                    (1000 / INCH_TO_MM_FACTOR, 30 / INCH_TO_MM_FACTOR),
                    "G0 F%s X%s Y%s" %
                    (1000 / INCH_TO_MM_FACTOR, 10 / INCH_TO_MM_FACTOR,
                     20 / INCH_TO_MM_FACTOR)
                ], "The result should be a list of the expected commands.")
            self.assertFalse(unit.excluding,
                             "The excluding flag should be cleared.")