Skip to content
Permalink
Browse files

Modernize test

  • Loading branch information
nyalldawson committed Mar 11, 2021
1 parent 5445ace commit ef214d51c7a5e6dd8732641fc426859b9dddfdcc
Showing with 38 additions and 141 deletions.
  1. +38 −141 tests/src/python/test_qgsrectangle.py
@@ -24,46 +24,20 @@ class TestQgsRectangle(unittest.TestCase):

def testCtor(self):
rect = QgsRectangle(5.0, 5.0, 10.0, 10.0)
myMessage = ('Expected: %s\nGot: %s\n' %
(5.0, rect.xMinimum()))
assert rect.xMinimum() == 5.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(5.0, rect.yMinimum()))
assert rect.yMinimum() == 5.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(10.0, rect.xMaximum()))
assert rect.xMaximum() == 10.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(10.0, rect.yMaximum()))
assert rect.yMaximum() == 10.0, myMessage
self.assertEqual(rect.xMinimum(), 5.0)
self.assertEqual(rect.yMinimum(), 5.0)
self.assertEqual(rect.xMaximum(), 10.0)
self.assertEqual(rect.yMaximum(), 10.0)

def testDimensions(self):
rect = QgsRectangle(0.0, 0.0, 10.0, 10.0)

myMessage = ('Expected: %s\nGot: %s\n' %
(10.0, rect.width()))
assert rect.width() == 10.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(10.0, rect.height()))
assert rect.height() == 10.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
("5.0, 5.0", rect.center().toString()))
assert rect.center() == QgsPointXY(5.0, 5.0), myMessage
rect = QgsRectangle(0.0, 0.0, 10.0, 20.0)
self.assertEqual(rect.width(), 10.0)
self.assertEqual(rect.height(), 20.0)
self.assertEqual(rect.center(), QgsPointXY(5.0, 10.0))

rect.scale(2.0)

myMessage = ('Expected: %s\nGot: %s\n' %
(20.0, rect.width()))
assert rect.width() == 20.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(20.0, rect.height()))
assert rect.height() == 20.0, myMessage
self.assertEqual(rect.width(), 20.0)
self.assertEqual(rect.height(), 40.0)

def testCalculations(self):
rect = QgsRectangle(0.0, 1.0, 20.0, 10.0)
@@ -74,20 +48,12 @@ def testIntersection(self):
rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0)
rect2 = QgsRectangle(2.0, 2.0, 7.0, 7.0)

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.intersects(rect2)))
assert rect1.intersects(rect2), myMessage
self.assertTrue(rect1.intersects(rect2))

rect3 = rect1.intersect(rect2)
self.assertFalse(rect3.isEmpty(), "Empty rectangle returned")

myMessage = ('Expected: %s\nGot: %s\n' %
(3.0, rect3.width()))
assert rect3.width() == 3.0, myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(3.0, rect3.height()))
assert rect3.height() == 3.0, myMessage
self.assertEqual(rect3.width(), 3.0)
self.assertEqual(rect3.height(), 3.0)

def testContains(self):
rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0)
@@ -96,77 +62,39 @@ def testContains(self):
pnt2 = QgsPointXY(6.0, 2.0)

rect3 = rect1.intersect(rect2)

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(rect3)))
assert rect1.contains(rect3), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect2.contains(rect3)))
assert rect2.contains(rect3), myMessage
self.assertTrue(rect1.contains(rect3))
self.assertTrue(rect2.contains(rect3))

# test for point
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(pnt1)))
assert rect1.contains(pnt1), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect2.contains(pnt1)))
assert rect2.contains(pnt1), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect3.contains(pnt1)))
assert rect3.contains(pnt1), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(False, rect1.contains(pnt2)))
self.assertFalse(rect1.contains(pnt2), myMessage)

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect2.contains(pnt2)))
assert rect2.contains(pnt2), myMessage

myMessage = ('Expected: %s\nGot: %s\n' %
(False, rect3.contains(pnt2)))
self.assertFalse(rect3.contains(pnt2), myMessage)

myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect3.contains(pnt1)))
self.assertTrue(rect3.contains(pnt1), myMessage)
self.assertTrue(rect1.contains(pnt1))
self.assertTrue(rect2.contains(pnt1))
self.assertTrue(rect3.contains(pnt1))
self.assertFalse(rect1.contains(pnt2))
self.assertTrue(rect2.contains(pnt2))
self.assertFalse(rect3.contains(pnt2))
self.assertTrue(rect3.contains(pnt1))

def testUnion(self):
rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0)
rect2 = QgsRectangle(2.0, 2.0, 7.0, 7.0)
pnt1 = QgsPointXY(6.0, 2.0)

rect1.combineExtentWith(rect2)
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(rect2)))
assert rect1.contains(rect2), myMessage
self.assertTrue(rect1.contains(rect2))

print((rect1.toString()))
assert rect1 == QgsRectangle(
0.0, 0.0, 7.0, 7.0), 'Wrong combine with rectangle result'
self.assertEqual(rect1, QgsRectangle(0.0, 0.0, 7.0, 7.0))

rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0)
rect1.combineExtentWith(6.0, 2.0)
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(pnt1)))
assert rect1.contains(pnt1), myMessage
self.assertTrue(rect1.contains(pnt1))

myExpectedResult = QgsRectangle(0.0, 0.0, 6.0, 5.0).toString()
myResult = rect1.toString()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedResult, myResult))
self.assertEqual(myResult, myExpectedResult, myMessage)
self.assertEqual(rect1.toString(), QgsRectangle(0.0, 0.0, 6.0, 5.0).toString())

rect1 = QgsRectangle(0.0, 0.0, 5.0, 5.0)
rect1.combineExtentWith(rect2)
myMessage = ('Expected: %s\nGot: %s\n' %
(True, rect1.contains(rect2)))
assert rect1.contains(rect2), myMessage
self.assertTrue(rect1.contains(rect2))

assert rect1 == QgsRectangle(0.0, 0.0, 7.0, 7.0), "Wrong union result"
self.assertEqual(rect1, QgsRectangle(0.0, 0.0, 7.0, 7.0))

def testAsWktCoordinates(self):
"""Test that we can get a proper wkt representation fo the rect"""
@@ -176,7 +104,7 @@ def testAsWktCoordinates(self):
myWkt = rect1.asWktCoordinates()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedWkt, myWkt))
assert compareWkt(myWkt, myExpectedWkt), myMessage
self.assertTrue(compareWkt(myWkt, myExpectedWkt), myMessage)

def testAsWktPolygon(self):
"""Test that we can get a proper rect wkt polygon representation for rect"""
@@ -189,56 +117,25 @@ def testAsWktPolygon(self):
myWkt = rect1.asWktPolygon()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedWkt, myWkt))
assert compareWkt(myWkt, myExpectedWkt), myMessage
self.assertTrue(compareWkt(myWkt, myExpectedWkt), myMessage)

def testToString(self):
"""Test the different string representations"""
self.assertEqual(QgsRectangle().toString(), 'Empty')
rect = QgsRectangle(0, 0.1, 0.2, 0.3)
myExpectedString = '0.0000000000000000,0.1000000000000000 : 0.2000000000000000,0.3000000000000000'
myString = rect.toString()
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage
self.assertEqual(rect.toString(), '0.0000000000000000,0.1000000000000000 : 0.2000000000000000,0.3000000000000000')

# can't test the actual result here, because floating point inaccuracies mean the result is unpredictable
# at this precision
self.assertEqual(len(rect.toString(20)), 93)

myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

myExpectedString = '0,0 : 0,0'
myString = rect.toString(0)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

myExpectedString = '0.00,0.10 : 0.20,0.30'
myString = rect.toString(2)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

myExpectedString = '0.0,0.1 : 0.2,0.3'
myString = rect.toString(1)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage

myExpectedString = '0.00,0.10 : 0.20,0.30'
myString = rect.toString(-1)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage
self.assertEqual(rect.toString(0), '0,0 : 0,0')
self.assertEqual(rect.toString(2), '0.00,0.10 : 0.20,0.30')
self.assertEqual(rect.toString(1), '0.0,0.1 : 0.2,0.3')
self.assertEqual(rect.toString(-1), '0.00,0.10 : 0.20,0.30')

rect = QgsRectangle(5000000.01111, -0.3, 5000000.44111, 99.8)
myExpectedString = '5000000.01,-0.30 : 5000000.44,99.80'
myString = rect.toString(-1)
myMessage = ('Expected: %s\nGot: %s\n' %
(myExpectedString, myString))
assert myString == myExpectedString, myMessage
self.assertEqual(rect.toString(-1), '5000000.01,-0.30 : 5000000.44,99.80')

def testAsPolygon(self):
"""Test string representation as polygon"""
@@ -258,11 +155,11 @@ def testToBox3d(self):
def testOperators(self):
rect1 = QgsRectangle(10, 20, 40, 40)
rect2 = rect1 + QgsVector(3, 5.5)
assert rect2 == QgsRectangle(13, 25.5, 43, 45.5), "QgsRectangle + operator does no work"
self.assertEqual(rect2, QgsRectangle(13, 25.5, 43, 45.5))

# Subtracting the center point, so it becomes zero.
rect1 -= rect1.center() - QgsPointXY(0, 0)
assert rect1.center() == QgsPointXY(0, 0)
self.assertEqual(rect1.center(), QgsPointXY(0, 0))

def testInvert(self):
rect = QgsRectangle(0, 0.1, 0.2, 0.3)

0 comments on commit ef214d5

Please sign in to comment.