def test_deleteRegion_found_middle(self): """Test the deleteRegion method when the region is neither first nor last in the list.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) firstRegion = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="firstId") findRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="findId") lastRegion = RectangularRegion(x1=20, y1=20, x2=30, y2=30, id="lastId") unit.addRegion(firstRegion) unit.addRegion(findRegion) unit.addRegion(lastRegion) self.assertTrue( unit.deleteRegion("findId"), "deleteRegion should return True when the region is found and removed (middle)" ) self.assertEqual( unit.excludedRegions, [firstRegion, lastRegion], "The excluded regions should be updated by deleteRegion if the ID is found (middle)" )
def test_replaceRegion_found_last(self): """Test the replaceRegion method when the region matches the last in the list.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) regionToMatch = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="matchId") otherRegion = RectangularRegion(x1=20, y1=20, x2=30, y2=30, id="otherId") newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="matchId") unit.addRegion(otherRegion) unit.addRegion(regionToMatch) unit.replaceRegion(newRegion) self.assertEqual( unit.excludedRegions, [otherRegion, newRegion], "The excluded regions should be updated by replaceRegion if the ID is found (last)" )
def test_replaceRegion_found_middle(self): """Test the replaceRegion method when the matched region not first nor last in the list.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) firstRegion = RectangularRegion(x1=20, y1=20, x2=30, y2=30, id="firstId") regionToMatch = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="matchId") lastRegion = RectangularRegion(x1=30, y1=30, x2=40, y2=40, id="lastId") newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="matchId") unit.addRegion(firstRegion) unit.addRegion(regionToMatch) unit.addRegion(lastRegion) unit.replaceRegion(newRegion) self.assertEqual( unit.excludedRegions, [firstRegion, newRegion, lastRegion], "The excluded regions should be updated by replaceRegion if the ID is found (last)" )
def test_replaceRegion_missingId(self): """Test the replaceRegion method when the region procided doesn't have an assigned ID.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100) newRegion.id = None with self.assertRaises(ValueError): unit.replaceRegion(newRegion)
def test_copy_constructor(self): """Test the constructor when passed a RectangularRegion instance.""" toCopy = RectangularRegion(x1=1, y1=2, x2=3, y2=4, id="myTestId") unit = RectangularRegion(toCopy) self.assertEqual(unit.x1, 1, "x1 should be 1") self.assertEqual(unit.y1, 2, "y1 should be 2") self.assertEqual(unit.x2, 3, "x2 should be 3") self.assertEqual(unit.y2, 4, "y2 should be 2") self.assertEqual(unit.id, "myTestId", "id should be 'myTestId'") self.assertProperties(unit, RectangularRegionTests.expectedProperties)
def test_replaceRegion_notFound(self): """Test the replaceRegion method when the region is not found.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) existingRegion = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="otherId") newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="someId") unit.addRegion(existingRegion) with self.assertRaises(ValueError): unit.replaceRegion(newRegion)
def test_addRegion_idExists(self): """Test the addRegion method when the ID already exists.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="anId") conflictingRegion = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="anId") unit.addRegion(aRegion) with self.assertRaises(ValueError): unit.addRegion(conflictingRegion)
def test_containsRegion_Rectangular(self): """Test the containsRegion method when passed a RectangularRegion.""" unit = CircularRegion(cx=10, cy=10, r=3) self.assertTrue( unit.containsRegion(RectangularRegion(x1=9, y1=9, x2=11, y2=11)), "it should contain Rect(9,9-11,11)" ) self.assertFalse( unit.containsRegion(RectangularRegion(x1=7.5, y1=7.5, x2=10, y2=10)), "it should not contain Rect(7.5,7.5-10,10)" ) self.assertFalse( unit.containsRegion(RectangularRegion(x1=7.5, y1=12.5, x2=10, y2=10)), "it should not contain Rect(7.5,12.5-10,10)" ) self.assertFalse( unit.containsRegion(RectangularRegion(x1=12.5, y1=7.5, x2=10, y2=10)), "it should not contain Rect(12.5,7.5-10,10)" ) self.assertFalse( unit.containsRegion(RectangularRegion(x1=7.5, y1=12.5, x2=10, y2=10)), "it should not contain Rect(7.5,12.5-10,10)" ) self.assertFalse( unit.containsRegion(RectangularRegion(x1=0, y1=0, x2=1, y2=1)), "it should not contain a RectangularRegion completely outside" ) self.assertFalse( unit.containsRegion(RectangularRegion(x1=0, y1=0, x2=20, y2=20)), "it should not contain a RectangularRegion containing this region" )
def test_replaceRegion_noRegions(self): """Test the replaceRegion method when no regions are defined.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="someId") with self.assertRaises(ValueError): unit.replaceRegion(newRegion)
def test_isAnyPointExcluded_lastExcluded(self): """Test the isAnyPointExcluded method when only the last point is excluded.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=5, y2=5) unit.addRegion(aRegion) self.assertTrue(unit.isAnyPointExcluded(10, 10, 0, 0), "(0,0) should be excluded (last)")
def test_isAnyPointExcluded_unmatchedPairs(self): """Test the isAnyPointExcluded method when an odd number of arguments are provided.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=5, y2=5) unit.addRegion(aRegion) with self.assertRaises(IndexError): unit.isAnyPointExcluded(0)
def test_isAnyPointExcluded_middleExcluded(self): """Test the isAnyPointExcluded method a point other than the first or last is excluded.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=5, y2=5) unit.addRegion(aRegion) self.assertTrue(unit.isAnyPointExcluded(10, 10, 0, 0, 20, 20), "(0,0) should be excluded (middle)")
def test_isAnyPointExcluded_noArguments(self): """Test the isAnyPointExcluded method when no arguments are provided.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=5, y2=5) unit.addRegion(aRegion) self.assertFalse( unit.isAnyPointExcluded(), "isAnyPointExcluded should return false when passed no arguments")
def test_replaceRegion_found_mustContain_contained(self): """Test the replaceRegion method when mustContainOldRegion is True and a match is found.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) regionToMatch = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="matchId") newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="matchId") unit.addRegion(regionToMatch) unit.replaceRegion(newRegion, True) self.assertEqual( unit.excludedRegions, [newRegion], "The excluded regions should be updated by replaceRegion if the ID is found (contained)" )
def test_isAnyPointExcluded_oneRegion(self): """Test the isAnyPointExcluded method when one region is defined.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=5, y2=5) unit.addRegion(aRegion) self.assertTrue(unit.isAnyPointExcluded(0, 0), "(0,0) should be excluded (one region)") self.assertFalse(unit.isAnyPointExcluded(10, 10), "(10,10) should NOT be excluded (one region)")
def test_replaceRegion_found_single(self): """Test the replaceRegion method when the region matches the only one defined.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) regionToMatch = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="matchId") newRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="matchId") unit.addRegion(regionToMatch) unit.replaceRegion(newRegion) self.assertEqual( unit.excludedRegions, [newRegion], "The excluded regions should be updated by replaceRegion if the ID is found (single)" )
def test_containsPoint(self): """Test the containsPoint method.""" unit = RectangularRegion(x1=0, y1=0, x2=10, y2=10) self.assertTrue(unit.containsPoint(0, 0), "it should contain [0, 0]") self.assertTrue(unit.containsPoint(10, 10), "it should contain [10, 10]") self.assertTrue(unit.containsPoint(0, 10), "it should contain [0, 10]") self.assertTrue(unit.containsPoint(10, 0), "it should contain [10, 0]") self.assertTrue(unit.containsPoint(5, 5), "it should contain [5, 5]") self.assertFalse(unit.containsPoint(-1, 5), "it should not contain [-1, 5]") self.assertFalse(unit.containsPoint(5, -1), "it should not contain [5, -1]") self.assertFalse(unit.containsPoint(5, 11), "it should not contain [5, 11]") self.assertFalse(unit.containsPoint(11, 5), "it should not contain [11, 5]")
def test_constructor_kwargs(self): """Test the constructor when passed keyword arguments.""" unit = RectangularRegion(x1=3, y1=4, x2=1, y2=2, id="myTestId") self.assertIsInstance(unit, RectangularRegion) self.assertEqual(unit.x1, 1, "x1 should be 1") self.assertEqual(unit.y1, 2, "y1 should be 2") self.assertEqual(unit.x2, 3, "x2 should be 3") self.assertEqual(unit.y2, 4, "y2 should be 2") self.assertEqual(unit.id, "myTestId", "id should be 'myTestId'") self.assertProperties(unit, RectangularRegionTests.expectedProperties)
def test_replaceRegion_found_mustContain_notContained(self): """Test the replaceRegion method when mustContainOldRegion is True and no match is found.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) regionToMatch = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="matchId") newRegion = RectangularRegion(x1=0, y1=0, x2=5, y2=5, id="matchId") unit.addRegion(regionToMatch) with self.assertRaises(ValueError): unit.replaceRegion(newRegion, True) self.assertEqual( unit.excludedRegions, [regionToMatch], "The excluded regions should not be modified by replaceRegion (match, not contained)" )
def test_default_constructor(self): """Test the constructor when passed no arguments.""" unit = RectangularRegion() self.assertIsInstance(unit, RectangularRegion) self.assertEqual(unit.x1, 0, "x1 should be 0") self.assertEqual(unit.y1, 0, "y1 should be 0") self.assertEqual(unit.x2, 0, "x2 should be 0") self.assertEqual(unit.y2, 0, "y2 should be 0") self.assertRegex(unit.id, "^[-0-9a-fA-F]{36}$", "id should be a UUID string") self.assertProperties(unit, RectangularRegionTests.expectedProperties)
def test_isAnyPointExcluded_exclusionDisabled(self): """Test the isAnyPointExcluded method when exclusion is disabled.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=5, y2=5) unit.addRegion(aRegion) unit.disableExclusion("Disable for test") self.assertFalse(unit.isAnyPointExcluded(0, 0), "(0,0) should NOT be excluded (exclusion disabled)") self.assertFalse( unit.isAnyPointExcluded(10, 10), "(10,10) should NOT be excluded (exclusion disabled)")
def test_deleteRegion_found_last(self): """Test the deleteRegion method when the specified region is last in the list.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) findRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="findId") otherRegion = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="otherId") unit.addRegion(otherRegion) unit.addRegion(findRegion) self.assertTrue( unit.deleteRegion("findId"), "deleteRegion should return True when the region is found and removed (last)" ) self.assertEqual( unit.excludedRegions, [otherRegion], "The excluded regions should be updated by deleteRegion if the ID is found (last)" )
def test_addRegion_newId(self): """Test the addRegion method when the ID has not yet been added.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="anId") otherRegion = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="otherId") unit.addRegion(aRegion) self.assertEqual( unit.excludedRegions, [aRegion], "The list of excluded regions should contain the new region") unit.addRegion(otherRegion) self.assertEqual( unit.excludedRegions, [aRegion, otherRegion], "The list of excluded regions should contain both regions")
def test_getRegion_exists(self): """Test the getRegion method when a matching region has been defined.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="anId") otherRegion = RectangularRegion(x1=10, y1=10, x2=20, y2=20, id="otherId") unit.addRegion(aRegion) unit.addRegion(otherRegion) self.assertIs( unit.getRegion("anId"), aRegion, "getRegion return the region matching the id when such a region is defined" ) self.assertIs( unit.getRegion("otherId"), otherRegion, "getRegion return the region matching the id when such a region is defined" )
def test_deleteRegion_notFound(self): """Test the deleteRegion method when the specified region is not found.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="anId") unit.addRegion(aRegion) self.assertFalse( unit.deleteRegion("notFound"), "deleteRegion should return False when the region is not found") self.assertEqual( unit.excludedRegions, [aRegion], "The excluded regions should not be modified by deleteRegion if the ID was not found" )
def test_deleteRegion_found_single(self): """Test the deleteRegion method when the specified region is the only one defined.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) findRegion = RectangularRegion(x1=0, y1=0, x2=100, y2=100, id="findId") unit.addRegion(findRegion) self.assertTrue( unit.deleteRegion("findId"), "deleteRegion should return True when the region is found and removed (single)" ) self.assertEqual( unit.excludedRegions, [], "The excluded regions should be updated by deleteRegion if the ID is found (single)" )
def test_isAnyPointExcluded_unitMultiplier(self): """Test the isAnyPointExcluded method honors the unit multipler in effect.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) unit.position.setUnitMultiplier(INCH_TO_MM_FACTOR) aRegion = RectangularRegion(x1=INCH_TO_MM_FACTOR, y1=INCH_TO_MM_FACTOR, x2=2 * INCH_TO_MM_FACTOR, y2=2 * INCH_TO_MM_FACTOR) unit.addRegion(aRegion) self.assertTrue(unit.isAnyPointExcluded(1, 1), "(1 inch, 1 inch) should be excluded") self.assertTrue(unit.isAnyPointExcluded(2, 2), "(2 inch, 2 inch) should be excluded") self.assertFalse(unit.isAnyPointExcluded(2.1, 2), "(2.1 inch, 2 inch) should NOT be excluded")
def test_isPointExcluded_multipleRegions(self): """Test the isPointExcluded method when multiple regions are defined.""" mockLogger = mock.Mock() unit = ExcludeRegionState(mockLogger) aRegion = RectangularRegion(x1=0, y1=0, x2=5, y2=5) anotherRegion = CircularRegion(cx=20, cy=20, r=10) unit.addRegion(aRegion) unit.addRegion(anotherRegion) self.assertTrue(unit.isPointExcluded(0, 0), "(0,0) should be excluded (mult regions)") self.assertFalse(unit.isPointExcluded(10, 10), "(10,10) should NOT be excluded (mult regions)") self.assertTrue(unit.isPointExcluded(20, 20), "(20,20) should be excluded (mult regions)") self.assertFalse(unit.isPointExcluded(30, 10), "(30,10) should NOT be excluded (mult regions)")
def test_containsRegion_Rectangular(self): """Test the containsRegion method when passed a RectangularRegion.""" unit = RectangularRegion(x1=0, y1=0, x2=10, y2=10) self.assertTrue(unit.containsRegion(unit), "it should contain itself") self.assertTrue( unit.containsRegion(RectangularRegion(x1=0, y1=0, x2=10, y2=10)), "it should contain a RectangularRegion representing the same geometric region" ) self.assertTrue( unit.containsRegion(RectangularRegion(x1=2, y1=2, x2=8, y2=8)), "it should contain a RectangularRegion inside") self.assertTrue( unit.containsRegion(RectangularRegion(x1=0, y1=4, x2=5, y2=6)), "it should contain a RectangularRegion inside, but tangent to the left edge" ) self.assertTrue( unit.containsRegion(RectangularRegion(x1=5, y1=4, x2=10, y2=6)), "it should contain a RectangularRegion inside, but tangent to the right edge" ) self.assertTrue( unit.containsRegion(RectangularRegion(x1=4, y1=0, x2=6, y2=5)), "it should contain a RectangularRegion inside, but tangent to the bottom edge" ) self.assertTrue( unit.containsRegion(RectangularRegion(x1=4, y1=5, x2=6, y2=10)), "it should contain a RectangularRegion inside, but tangent to the top edge" ) self.assertFalse( unit.containsRegion(RectangularRegion(x1=-1, y1=0, x2=5, y2=5)), "it should not contain a RectangularRegion that extends outside") self.assertFalse( unit.containsRegion(RectangularRegion(x1=-1, y1=0, x2=5, y2=5)), "it should not contain a RectangularRegion that extends outside")
def test_constructor_exception(self): """Test the constructor when passed a single non-RectangularRegion parameter.""" with self.assertRaises(AssertionError): RectangularRegion("NotARectangularRegionInstance")