From 48844df5cce1fd62d3a7b1706fc566cbda13c577 Mon Sep 17 00:00:00 2001 From: Daniel Pope Date: Fri, 15 Nov 2019 08:49:30 +0000 Subject: [PATCH] Fix flake8 errors in test/ --- .flake8 | 3 + test/game_tests/helper.py | 1 + test/game_tests/importing.py | 1 - test/rect_test.py | 736 +++++++++++++++++----------------- test/test_animation.py | 1 - test/test_event_dispatch.py | 1 + test/test_osx.py | 17 - test/test_sound_formats.py | 7 +- test/test_storage.py | 4 +- test/test_tone.py | 23 +- test/test_transform_anchor.py | 1 - 11 files changed, 390 insertions(+), 405 deletions(-) delete mode 100644 test/test_osx.py diff --git a/.flake8 b/.flake8 index 2bcd70e3..155d7701 100644 --- a/.flake8 +++ b/.flake8 @@ -1,2 +1,5 @@ [flake8] max-line-length = 88 +builtins = + Actor, Rect, ZRect, animate, clock, exit, images, keyboard, keymods, + keys, mouse, music, screen, sounds, storage, tone diff --git a/test/game_tests/helper.py b/test/game_tests/helper.py index f2c76a98..8e9c2279 100644 --- a/test/game_tests/helper.py +++ b/test/game_tests/helper.py @@ -1,5 +1,6 @@ RECT = ZRect(30, 30, 40, 40) + def circle(): screen.draw.circle((50, 50), radius=20, color='cyan') diff --git a/test/game_tests/importing.py b/test/game_tests/importing.py index dedd337d..fe8d85fa 100644 --- a/test/game_tests/importing.py +++ b/test/game_tests/importing.py @@ -5,4 +5,3 @@ def draw(): helper.circle() - diff --git a/test/rect_test.py b/test/rect_test.py index 11e86f77..d4024394 100644 --- a/test/rect_test.py +++ b/test/rect_test.py @@ -3,380 +3,380 @@ from pgzero.rect import ZRect as Rect -class RectTypeTest( unittest.TestCase ): - def testConstructionXYWidthHeight( self ): - r = Rect(1,2,3,4) - self.assertEqual( 1, r.left ) - self.assertEqual( 2, r.top ) - self.assertEqual( 3, r.width ) - self.assertEqual( 4, r.height ) - - def testConstructionTopLeftSize( self ): - r = Rect( (1,2), (3,4) ) - self.assertEqual( 1, r.left ) - self.assertEqual( 2, r.top ) - self.assertEqual( 3, r.width ) - self.assertEqual( 4, r.height ) - - def testCalculatedAttributes( self ): - r = Rect( 1, 2, 3, 4 ) - - self.assertEqual( r.left+r.width, r.right ) - self.assertEqual( r.top+r.height, r.bottom ) - self.assertEqual( (r.width,r.height), r.size ) - self.assertEqual( (r.left,r.top), r.topleft ) - self.assertEqual( (r.right,r.top), r.topright ) - self.assertEqual( (r.left,r.bottom), r.bottomleft ) - self.assertEqual( (r.right,r.bottom), r.bottomright ) +class RectTypeTest(unittest.TestCase): + def testConstructionXYWidthHeight(self): + r = Rect(1, 2, 3, 4) + self.assertEqual(1, r.left) + self.assertEqual(2, r.top) + self.assertEqual(3, r.width) + self.assertEqual(4, r.height) + + def testConstructionTopLeftSize(self): + r = Rect((1, 2), (3, 4)) + self.assertEqual(1, r.left) + self.assertEqual(2, r.top) + self.assertEqual(3, r.width) + self.assertEqual(4, r.height) + + def testCalculatedAttributes(self): + r = Rect(1, 2, 3, 4) + + self.assertEqual(r.left+r.width, r.right) + self.assertEqual(r.top+r.height, r.bottom) + self.assertEqual((r.width, r.height), r.size) + self.assertEqual((r.left, r.top), r.topleft) + self.assertEqual((r.right, r.top), r.topright) + self.assertEqual((r.left, r.bottom), r.bottomleft) + self.assertEqual((r.right, r.bottom), r.bottomright) midx = r.left + r.width / 2 midy = r.top + r.height / 2 - self.assertEqual( midx, r.centerx ) - self.assertEqual( midy, r.centery ) - self.assertEqual( (r.centerx,r.centery), r.center ) - self.assertEqual( (r.centerx,r.top), r.midtop ) - self.assertEqual( (r.centerx,r.bottom), r.midbottom ) - self.assertEqual( (r.left,r.centery), r.midleft ) - self.assertEqual( (r.right,r.centery), r.midright ) + self.assertEqual(midx, r.centerx) + self.assertEqual(midy, r.centery) + self.assertEqual((r.centerx, r.centery), r.center) + self.assertEqual((r.centerx, r.top), r.midtop) + self.assertEqual((r.centerx, r.bottom), r.midbottom) + self.assertEqual((r.left, r.centery), r.midleft) + self.assertEqual((r.right, r.centery), r.midright) - def test_normalize( self ): - r = Rect( 1, 2, -3, -6 ) + def test_normalize(self): + r = Rect(1, 2, -3, -6) r2 = Rect(r) r2.normalize() - self.assertTrue( r2.width >= 0 ) - self.assertTrue( r2.height >= 0 ) - self.assertEqual( (abs(r.width),abs(r.height)), r2.size ) - self.assertEqual( (-2,-4), r2.topleft ) + self.assertTrue(r2.width >= 0) + self.assertTrue(r2.height >= 0) + self.assertEqual((abs(r.width), abs(r.height)), r2.size) + self.assertEqual((-2, -4), r2.topleft) - def test_left( self ): + def test_left(self): """Changing the left attribute moves the rect and does not change the rect's width """ - r = Rect( 1, 2, 3, 4 ) + r = Rect(1, 2, 3, 4) new_left = 10 r.left = new_left - self.assertEqual( new_left, r.left ) - self.assertEqual( Rect(new_left,2,3,4), r ) + self.assertEqual(new_left, r.left) + self.assertEqual(Rect(new_left, 2, 3, 4), r) - def test_right( self ): + def test_right(self): """Changing the right attribute moves the rect and does not change the rect's width """ - r = Rect( 1, 2, 3, 4 ) + r = Rect(1, 2, 3, 4) new_right = r.right + 20 expected_left = r.left + 20 old_width = r.width r.right = new_right - self.assertEqual( new_right, r.right ) - self.assertEqual( expected_left, r.left ) - self.assertEqual( old_width, r.width ) + self.assertEqual(new_right, r.right) + self.assertEqual(expected_left, r.left) + self.assertEqual(old_width, r.width) - def test_top( self ): + def test_top(self): """Changing the top attribute moves the rect and does not change the rect's width """ - r = Rect( 1, 2, 3, 4 ) + r = Rect(1, 2, 3, 4) new_top = 10 r.top = new_top - self.assertEqual( Rect(1,new_top,3,4), r ) - self.assertEqual( new_top, r.top ) + self.assertEqual(Rect(1, new_top, 3, 4), r) + self.assertEqual(new_top, r.top) - def test_bottom( self ): + def test_bottom(self): """Changing the bottom attribute moves the rect and does not change the rect's height """ - r = Rect( 1, 2, 3, 4 ) + r = Rect(1, 2, 3, 4) new_bottom = r.bottom + 20 expected_top = r.top + 20 old_height = r.height r.bottom = new_bottom - self.assertEqual( new_bottom, r.bottom ) - self.assertEqual( expected_top, r.top ) - self.assertEqual( old_height, r.height ) + self.assertEqual(new_bottom, r.bottom) + self.assertEqual(expected_top, r.top) + self.assertEqual(old_height, r.height) - def test_centerx( self ): + def test_centerx(self): """Changing the centerx attribute moves the rect and does not change the rect's width """ - r = Rect( 1, 2, 3, 4 ) + r = Rect(1, 2, 3, 4) new_centerx = r.centerx + 20 expected_left = r.left + 20 old_width = r.width r.centerx = new_centerx - self.assertEqual( new_centerx, r.centerx ) - self.assertEqual( expected_left, r.left ) - self.assertEqual( old_width, r.width ) + self.assertEqual(new_centerx, r.centerx) + self.assertEqual(expected_left, r.left) + self.assertEqual(old_width, r.width) - def test_centery( self ): + def test_centery(self): """Changing the centerx attribute moves the rect and does not change the rect's width """ - r = Rect( 1, 2, 3, 4 ) + r = Rect(1, 2, 3, 4) new_centery = r.centery + 20 expected_top = r.top + 20 old_height = r.height r.centery = new_centery - self.assertEqual( new_centery, r.centery ) - self.assertEqual( expected_top, r.top ) - self.assertEqual( old_height, r.height ) + self.assertEqual(new_centery, r.centery) + self.assertEqual(expected_top, r.top) + self.assertEqual(old_height, r.height) - def test_topleft( self ): + def test_topleft(self): """Changing the topleft attribute moves the rect and does not change the rect's size """ - r = Rect( 1, 2, 3, 4 ) - new_topleft = (r.left+20,r.top+30) + r = Rect(1, 2, 3, 4) + new_topleft = (r.left+20, r.top+30) old_size = r.size r.topleft = new_topleft - self.assertEqual( new_topleft, r.topleft ) - self.assertEqual( old_size, r.size ) + self.assertEqual(new_topleft, r.topleft) + self.assertEqual(old_size, r.size) - def test_bottomleft( self ): + def test_bottomleft(self): """Changing the bottomleft attribute moves the rect and does not change the rect's size """ - r = Rect( 1, 2, 3, 4 ) - new_bottomleft = (r.left+20,r.bottom+30) - expected_topleft = (r.left+20,r.top+30) + r = Rect(1, 2, 3, 4) + new_bottomleft = (r.left+20, r.bottom+30) + expected_topleft = (r.left+20, r.top+30) old_size = r.size r.bottomleft = new_bottomleft - self.assertEqual( new_bottomleft, r.bottomleft ) - self.assertEqual( expected_topleft, r.topleft ) - self.assertEqual( old_size, r.size ) + self.assertEqual(new_bottomleft, r.bottomleft) + self.assertEqual(expected_topleft, r.topleft) + self.assertEqual(old_size, r.size) - def test_topright( self ): + def test_topright(self): """Changing the bottomleft attribute moves the rect and does not change the rect's size """ - r = Rect( 1, 2, 3, 4 ) - new_topright = (r.right+20,r.top+30) - expected_topleft = (r.left+20,r.top+30) + r = Rect(1, 2, 3, 4) + new_topright = (r.right+20, r.top+30) + expected_topleft = (r.left+20, r.top+30) old_size = r.size r.topright = new_topright - self.assertEqual( new_topright, r.topright ) - self.assertEqual( expected_topleft, r.topleft ) - self.assertEqual( old_size, r.size ) + self.assertEqual(new_topright, r.topright) + self.assertEqual(expected_topleft, r.topleft) + self.assertEqual(old_size, r.size) - def test_bottomright( self ): + def test_bottomright(self): """Changing the bottomright attribute moves the rect and does not change the rect's size """ - r = Rect( 1, 2, 3, 4 ) - new_bottomright = (r.right+20,r.bottom+30) - expected_topleft = (r.left+20,r.top+30) + r = Rect(1, 2, 3, 4) + new_bottomright = (r.right+20, r.bottom+30) + expected_topleft = (r.left+20, r.top+30) old_size = r.size r.bottomright = new_bottomright - self.assertEqual( new_bottomright, r.bottomright ) - self.assertEqual( expected_topleft, r.topleft ) - self.assertEqual( old_size, r.size ) + self.assertEqual(new_bottomright, r.bottomright) + self.assertEqual(expected_topleft, r.topleft) + self.assertEqual(old_size, r.size) - def test_center( self ): + def test_center(self): """Changing the center attribute moves the rect and does not change the rect's size """ - r = Rect( 1, 2, 3, 4 ) - new_center = (r.centerx+20,r.centery+30) - expected_topleft = (r.left+20,r.top+30) + r = Rect(1, 2, 3, 4) + new_center = (r.centerx+20, r.centery+30) + expected_topleft = (r.left+20, r.top+30) old_size = r.size r.center = new_center - self.assertEqual( new_center, r.center ) - self.assertEqual( expected_topleft, r.topleft ) - self.assertEqual( old_size, r.size ) + self.assertEqual(new_center, r.center) + self.assertEqual(expected_topleft, r.topleft) + self.assertEqual(old_size, r.size) - def test_midleft( self ): + def test_midleft(self): """Changing the midleft attribute moves the rect and does not change the rect's size """ - r = Rect( 1, 2, 3, 4 ) - new_midleft = (r.left+20,r.centery+30) - expected_topleft = (r.left+20,r.top+30) + r = Rect(1, 2, 3, 4) + new_midleft = (r.left+20, r.centery+30) + expected_topleft = (r.left+20, r.top+30) old_size = r.size r.midleft = new_midleft - self.assertEqual( new_midleft, r.midleft ) - self.assertEqual( expected_topleft, r.topleft ) - self.assertEqual( old_size, r.size ) + self.assertEqual(new_midleft, r.midleft) + self.assertEqual(expected_topleft, r.topleft) + self.assertEqual(old_size, r.size) - def test_midright( self ): + def test_midright(self): """Changing the midright attribute moves the rect and does not change the rect's size """ - r = Rect( 1, 2, 3, 4 ) - new_midright= (r.right+20,r.centery+30) - expected_topleft = (r.left+20,r.top+30) + r = Rect(1, 2, 3, 4) + new_midright = (r.right+20, r.centery+30) + expected_topleft = (r.left+20, r.top+30) old_size = r.size r.midright = new_midright - self.assertEqual( new_midright, r.midright ) - self.assertEqual( expected_topleft, r.topleft ) - self.assertEqual( old_size, r.size ) + self.assertEqual(new_midright, r.midright) + self.assertEqual(expected_topleft, r.topleft) + self.assertEqual(old_size, r.size) - def test_midtop( self ): + def test_midtop(self): """Changing the midtop attribute moves the rect and does not change the rect's size """ - r = Rect( 1, 2, 3, 4 ) - new_midtop= (r.centerx+20,r.top+30) - expected_topleft = (r.left+20,r.top+30) + r = Rect(1, 2, 3, 4) + new_midtop = (r.centerx+20, r.top+30) + expected_topleft = (r.left+20, r.top+30) old_size = r.size r.midtop = new_midtop - self.assertEqual( new_midtop, r.midtop ) - self.assertEqual( expected_topleft, r.topleft ) - self.assertEqual( old_size, r.size ) + self.assertEqual(new_midtop, r.midtop) + self.assertEqual(expected_topleft, r.topleft) + self.assertEqual(old_size, r.size) - def test_midbottom( self ): + def test_midbottom(self): """Changing the midbottom attribute moves the rect and does not change the rect's size """ - r = Rect( 1, 2, 3, 4 ) - new_midbottom = (r.centerx+20,r.bottom+30) - expected_topleft = (r.left+20,r.top+30) + r = Rect(1, 2, 3, 4) + new_midbottom = (r.centerx+20, r.bottom+30) + expected_topleft = (r.left+20, r.top+30) old_size = r.size r.midbottom = new_midbottom - self.assertEqual( new_midbottom, r.midbottom ) - self.assertEqual( expected_topleft, r.topleft ) - self.assertEqual( old_size, r.size ) + self.assertEqual(new_midbottom, r.midbottom) + self.assertEqual(expected_topleft, r.topleft) + self.assertEqual(old_size, r.size) - def test_width( self ): + def test_width(self): "Changing the width resizes the rect from the top-left corner" - r = Rect( 1, 2, 3, 4 ) + r = Rect(1, 2, 3, 4) new_width = 10 old_topleft = r.topleft old_height = r.height r.width = new_width - self.assertEqual( new_width, r.width ) - self.assertEqual( old_height, r.height ) - self.assertEqual( old_topleft, r.topleft ) + self.assertEqual(new_width, r.width) + self.assertEqual(old_height, r.height) + self.assertEqual(old_topleft, r.topleft) - def test_height( self ): + def test_height(self): "Changing the height resizes the rect from the top-left corner" - r = Rect( 1, 2, 3, 4 ) + r = Rect(1, 2, 3, 4) new_height = 10 old_topleft = r.topleft old_width = r.width r.height = new_height - self.assertEqual( new_height, r.height ) - self.assertEqual( old_width, r.width ) - self.assertEqual( old_topleft, r.topleft ) + self.assertEqual(new_height, r.height) + self.assertEqual(old_width, r.width) + self.assertEqual(old_topleft, r.topleft) - def test_size( self ): + def test_size(self): "Changing the size resizes the rect from the top-left corner" - r = Rect( 1, 2, 3, 4 ) - new_size = (10,20) + r = Rect(1, 2, 3, 4) + new_size = (10, 20) old_topleft = r.topleft r.size = new_size - self.assertEqual( new_size, r.size ) - self.assertEqual( old_topleft, r.topleft ) - - def test_contains( self ): - r = Rect( 1, 2, 3, 4 ) - - self.assertTrue( r.contains( Rect( 2, 3, 1, 1 ) ), - "r does not contain Rect(2,3,1,1)" ) - self.assertTrue( r.contains( Rect(r) ), - "r does not contain the same rect as itself" ) - self.assertTrue( r.contains( Rect(2,3,0,0) ), - "r does not contain an empty rect within its bounds" ) - self.assertFalse( r.contains( Rect(0,0,1,2) ), - "r contains Rect(0,0,1,2)" ) - self.assertFalse( r.contains( Rect(4,6,1,1) ), - "r contains Rect(4,6,1,1)" ) - self.assertFalse( r.contains( Rect(4,6,0,0) ), - "r contains Rect(4,6,0,0)" ) - - def test_collidepoint( self ): - r = Rect( 1, 2, 3, 4 ) - - self.assertTrue( r.collidepoint( r.left, r.top ), - "r does not collide with point (left,top)" ) - self.assertFalse( r.collidepoint( r.left-1, r.top ), - "r collides with point (left-1,top)" ) - self.assertFalse( r.collidepoint( r.left, r.top-1 ), - "r collides with point (left,top-1)" ) - self.assertFalse( r.collidepoint( r.left-1,r.top-1 ), - "r collides with point (left-1,top-1)" ) - - self.assertTrue( r.collidepoint( r.right-1, r.bottom-1 ), - "r does not collide with point (right-1,bottom-1)") - self.assertFalse( r.collidepoint( r.right, r.bottom ), - "r collides with point (right,bottom)" ) - self.assertFalse( r.collidepoint( r.right-1, r.bottom ), - "r collides with point (right-1,bottom)" ) - self.assertFalse( r.collidepoint( r.right, r.bottom-1 ), - "r collides with point (right,bottom-1)" ) - - def test_inflate__larger( self ): + self.assertEqual(new_size, r.size) + self.assertEqual(old_topleft, r.topleft) + + def test_contains(self): + r = Rect(1, 2, 3, 4) + + self.assertTrue(r.contains(Rect(2, 3, 1, 1)), + "r does not contain Rect(2,3,1,1)") + self.assertTrue(r.contains(Rect(r)), + "r does not contain the same rect as itself") + self.assertTrue(r.contains(Rect(2, 3, 0, 0)), + "r does not contain an empty rect within its bounds") + self.assertFalse(r.contains(Rect(0, 0, 1, 2)), + "r contains Rect(0,0,1,2)") + self.assertFalse(r.contains(Rect(4, 6, 1, 1)), + "r contains Rect(4,6,1,1)") + self.assertFalse(r.contains(Rect(4, 6, 0, 0)), + "r contains Rect(4,6,0,0)") + + def test_collidepoint(self): + r = Rect(1, 2, 3, 4) + + self.assertTrue(r.collidepoint(r.left, r.top), + "r does not collide with point (left,top)") + self.assertFalse(r.collidepoint(r.left-1, r.top), + "r collides with point (left-1,top)") + self.assertFalse(r.collidepoint(r.left, r.top-1), + "r collides with point (left,top-1)") + self.assertFalse(r.collidepoint(r.left-1, r.top-1), + "r collides with point (left-1,top-1)") + + self.assertTrue(r.collidepoint(r.right-1, r.bottom-1), + "r does not collide with point (right-1,bottom-1)") + self.assertFalse(r.collidepoint(r.right, r.bottom), + "r collides with point (right,bottom)") + self.assertFalse(r.collidepoint(r.right-1, r.bottom), + "r collides with point (right-1,bottom)") + self.assertFalse(r.collidepoint(r.right, r.bottom-1), + "r collides with point (right,bottom-1)") + + def test_inflate__larger(self): "The inflate method inflates around the center of the rectangle" - r = Rect( 2, 4, 6, 8 ) - r2 = r.inflate( 4, 6 ) - - self.assertEqual( r.center, r2.center ) - self.assertEqual( r.left-2, r2.left ) - self.assertEqual( r.top-3, r2.top ) - self.assertEqual( r.right+2, r2.right ) - self.assertEqual( r.bottom+3, r2.bottom ) - self.assertEqual( r.width+4, r2.width ) - self.assertEqual( r.height+6, r2.height ) - - def test_inflate__smaller( self ): + r = Rect(2, 4, 6, 8) + r2 = r.inflate(4, 6) + + self.assertEqual(r.center, r2.center) + self.assertEqual(r.left-2, r2.left) + self.assertEqual(r.top-3, r2.top) + self.assertEqual(r.right+2, r2.right) + self.assertEqual(r.bottom+3, r2.bottom) + self.assertEqual(r.width+4, r2.width) + self.assertEqual(r.height+6, r2.height) + + def test_inflate__smaller(self): "The inflate method inflates around the center of the rectangle" - r = Rect( 2, 4, 6, 8 ) - r2 = r.inflate( -4, -6 ) - - self.assertEqual( r.center, r2.center ) - self.assertEqual( r.left+2, r2.left ) - self.assertEqual( r.top+3, r2.top ) - self.assertEqual( r.right-2, r2.right ) - self.assertEqual( r.bottom-3, r2.bottom ) - self.assertEqual( r.width-4, r2.width ) - self.assertEqual( r.height-6, r2.height ) - - def test_inflate_ip__larger( self ): + r = Rect(2, 4, 6, 8) + r2 = r.inflate(-4, -6) + + self.assertEqual(r.center, r2.center) + self.assertEqual(r.left+2, r2.left) + self.assertEqual(r.top+3, r2.top) + self.assertEqual(r.right-2, r2.right) + self.assertEqual(r.bottom-3, r2.bottom) + self.assertEqual(r.width-4, r2.width) + self.assertEqual(r.height-6, r2.height) + + def test_inflate_ip__larger(self): "The inflate_ip method inflates around the center of the rectangle" - r = Rect( 2, 4, 6, 8 ) - r2 = Rect( r ) - r2.inflate_ip( -4, -6 ) - - self.assertEqual( r.center, r2.center ) - self.assertEqual( r.left+2, r2.left ) - self.assertEqual( r.top+3, r2.top ) - self.assertEqual( r.right-2, r2.right ) - self.assertEqual( r.bottom-3, r2.bottom ) - self.assertEqual( r.width-4, r2.width ) - self.assertEqual( r.height-6, r2.height ) - - def test_inflate_ip__smaller( self ): + r = Rect(2, 4, 6, 8) + r2 = Rect(r) + r2.inflate_ip(-4, -6) + + self.assertEqual(r.center, r2.center) + self.assertEqual(r.left+2, r2.left) + self.assertEqual(r.top+3, r2.top) + self.assertEqual(r.right-2, r2.right) + self.assertEqual(r.bottom-3, r2.bottom) + self.assertEqual(r.width-4, r2.width) + self.assertEqual(r.height-6, r2.height) + + def test_inflate_ip__smaller(self): "The inflate method inflates around the center of the rectangle" - r = Rect( 2, 4, 6, 8 ) - r2 = Rect( r ) - r2.inflate_ip( -4, -6 ) - - self.assertEqual( r.center, r2.center ) - self.assertEqual( r.left+2, r2.left ) - self.assertEqual( r.top+3, r2.top ) - self.assertEqual( r.right-2, r2.right ) - self.assertEqual( r.bottom-3, r2.bottom ) - self.assertEqual( r.width-4, r2.width ) - self.assertEqual( r.height-6, r2.height ) - - def test_clamp( self ): + r = Rect(2, 4, 6, 8) + r2 = Rect(r) + r2.inflate_ip(-4, -6) + + self.assertEqual(r.center, r2.center) + self.assertEqual(r.left+2, r2.left) + self.assertEqual(r.top+3, r2.top) + self.assertEqual(r.right-2, r2.right) + self.assertEqual(r.bottom-3, r2.bottom) + self.assertEqual(r.width-4, r2.width) + self.assertEqual(r.height-6, r2.height) + + def test_clamp(self): r = Rect(10, 10, 10, 10) c = Rect(19, 12, 5, 5).clamp(r) self.assertEqual(c.right, r.right) @@ -386,7 +386,7 @@ def test_clamp( self ): c = Rect(5, 500, 22, 33).clamp(r) self.assertEqual(c.center, r.center) - def test_clamp_ip( self ): + def test_clamp_ip(self): r = Rect(10, 10, 10, 10) c = Rect(19, 12, 5, 5) c.clamp_ip(r) @@ -399,115 +399,113 @@ def test_clamp_ip( self ): c.clamp_ip(r) self.assertEqual(c.center, r.center) - def test_clip( self ): - r1 = Rect( 1, 2, 3, 4 ) - self.assertEqual( Rect( 1, 2, 2, 2 ), r1.clip( Rect(0,0,3,4) ) ) - self.assertEqual( Rect( 2, 2, 2, 4 ), r1.clip( Rect(2,2,10,20) ) ) - self.assertEqual( Rect(2,3,1,2), r1.clip( Rect(2,3,1,2) ) ) - self.assertEqual( (0,0), r1.clip(20,30,5,6).size ) - self.assertEqual( r1, r1.clip( Rect(r1) ), - "r1 does not clip an identical rect to itself" ) - - def test_move( self ): - r = Rect( 1, 2, 3, 4 ) + def test_clip(self): + r1 = Rect(1, 2, 3, 4) + self.assertEqual(Rect(1, 2, 2, 2), r1.clip(Rect(0, 0, 3, 4))) + self.assertEqual(Rect(2, 2, 2, 4), r1.clip(Rect(2, 2, 10, 20))) + self.assertEqual(Rect(2, 3, 1, 2), r1.clip(Rect(2, 3, 1, 2))) + self.assertEqual((0, 0), r1.clip(20, 30, 5, 6).size) + self.assertEqual(r1, r1.clip(Rect(r1)), + "r1 does not clip an identical rect to itself") + + def test_move(self): + r = Rect(1, 2, 3, 4) move_x = 10 move_y = 20 - r2 = r.move( move_x, move_y ) - expected_r2 = Rect(r.left+move_x,r.top+move_y,r.width,r.height) - self.assertEqual( expected_r2, r2 ) + r2 = r.move(move_x, move_y) + expected_r2 = Rect(r.left+move_x, r.top+move_y, r.width, r.height) + self.assertEqual(expected_r2, r2) - def test_move_ip( self ): - r = Rect( 1, 2, 3, 4 ) - r2 = Rect( r ) + def test_move_ip(self): + r = Rect(1, 2, 3, 4) + r2 = Rect(r) move_x = 10 move_y = 20 - r2.move_ip( move_x, move_y ) - expected_r2 = Rect(r.left+move_x,r.top+move_y,r.width,r.height) - self.assertEqual( expected_r2, r2 ) - - def test_union( self ): - r1 = Rect( 1, 1, 1, 2 ) - r2 = Rect( -2, -2, 1, 2 ) - self.assertEqual( Rect( -2, -2, 4, 5 ), r1.union(r2) ) - - def test_union__with_identical_Rect( self ): - r1 = Rect( 1, 2, 3, 4 ) - self.assertEqual( r1, r1.union( Rect(r1) ) ) - - def test_union_ip( self ): - r1 = Rect( 1, 1, 1, 2 ) - r2 = Rect( -2, -2, 1, 2 ) + r2.move_ip(move_x, move_y) + expected_r2 = Rect(r.left+move_x, r.top+move_y, r.width, r.height) + self.assertEqual(expected_r2, r2) + + def test_union(self): + r1 = Rect(1, 1, 1, 2) + r2 = Rect(-2, -2, 1, 2) + self.assertEqual(Rect(-2, -2, 4, 5), r1.union(r2)) + + def test_union__with_identical_Rect(self): + r1 = Rect(1, 2, 3, 4) + self.assertEqual(r1, r1.union(Rect(r1))) + + def test_union_ip(self): + r1 = Rect(1, 1, 1, 2) + r2 = Rect(-2, -2, 1, 2) r1.union_ip(r2) - self.assertEqual( Rect( -2, -2, 4, 5 ), r1 ) - - def test_unionall( self ): - r1 = Rect( 0, 0, 1, 1 ) - r2 = Rect( -2, -2, 1, 1 ) - r3 = Rect( 2, 2, 1, 1 ) - - r4 = r1.unionall( [r2,r3] ) - self.assertEqual( Rect(-2, -2, 5, 5), r4 ) - - def test_unionall_ip( self ): - r1 = Rect( 0, 0, 1, 1 ) - r2 = Rect( -2, -2, 1, 1 ) - r3 = Rect( 2, 2, 1, 1 ) - - r1.unionall_ip( [r2,r3] ) - self.assertEqual( Rect(-2, -2, 5, 5), r1 ) - - - - def test_colliderect( self ): - r1 = Rect(1,2,3,4) - self.assertTrue( r1.colliderect( Rect(0,0,2,3) ), - "r1 does not collide with Rect(0,0,2,3)" ) - self.assertFalse( r1.colliderect( Rect(0,0,1,2) ), - "r1 collides with Rect(0,0,1,2)" ) - self.assertFalse( r1.colliderect( Rect(r1.right,r1.bottom,2,2) ), - "r1 collides with Rect(r1.right,r1.bottom,2,2)" ) - self.assertTrue( r1.colliderect( Rect(r1.left+1,r1.top+1, - r1.width-2,r1.height-2) ), - "r1 does not collide with Rect(r1.left+1,r1.top+1,"+ - "r1.width-2,r1.height-2)" ) - self.assertTrue( r1.colliderect( Rect(r1.left-1,r1.top-1, - r1.width+2,r1.height+2) ), - "r1 does not collide with Rect(r1.left-1,r1.top-1,"+ - "r1.width+2,r1.height+2)" ) - self.assertTrue( r1.colliderect( Rect(r1) ), - "r1 does not collide with an identical rect" ) - self.assertFalse( r1.colliderect( Rect(r1.right,r1.bottom,0,0) ), - "r1 collides with Rect(r1.right,r1.bottom,0,0)" ) - self.assertFalse( r1.colliderect( Rect(r1.right,r1.bottom,1,1) ), - "r1 collides with Rect(r1.right,r1.bottom,1,1)" ) - - def testEquals( self ): + self.assertEqual(Rect(-2, -2, 4, 5), r1) + + def test_unionall(self): + r1 = Rect(0, 0, 1, 1) + r2 = Rect(-2, -2, 1, 1) + r3 = Rect(2, 2, 1, 1) + + r4 = r1.unionall([r2, r3]) + self.assertEqual(Rect(-2, -2, 5, 5), r4) + + def test_unionall_ip(self): + r1 = Rect(0, 0, 1, 1) + r2 = Rect(-2, -2, 1, 1) + r3 = Rect(2, 2, 1, 1) + + r1.unionall_ip([r2, r3]) + self.assertEqual(Rect(-2, -2, 5, 5), r1) + + def test_colliderect(self): + r1 = Rect(1, 2, 3, 4) + self.assertTrue(r1.colliderect(Rect(0, 0, 2, 3)), + "r1 does not collide with Rect(0,0,2,3)") + self.assertFalse(r1.colliderect(Rect(0, 0, 1, 2)), + "r1 collides with Rect(0,0,1,2)") + self.assertFalse(r1.colliderect(Rect(r1.right, r1.bottom, 2, 2)), + "r1 collides with Rect(r1.right,r1.bottom,2,2)") + self.assertTrue(r1.colliderect(Rect(r1.left+1, r1.top+1, + r1.width-2, r1.height-2)), + "r1 does not collide with Rect(r1.left+1,r1.top+1," + + "r1.width-2,r1.height-2)") + self.assertTrue(r1.colliderect(Rect(r1.left-1, r1.top-1, + r1.width+2, r1.height+2)), + "r1 does not collide with Rect(r1.left-1,r1.top-1," + + "r1.width+2,r1.height+2)") + self.assertTrue(r1.colliderect(Rect(r1)), + "r1 does not collide with an identical rect") + self.assertFalse(r1.colliderect(Rect(r1.right, r1.bottom, 0, 0)), + "r1 collides with Rect(r1.right,r1.bottom,0,0)") + self.assertFalse(r1.colliderect(Rect(r1.right, r1.bottom, 1, 1)), + "r1 collides with Rect(r1.right,r1.bottom,1,1)") + + def testEquals(self): """ check to see how the rect uses __eq__ """ - r1 = Rect(1,2,3,4) - r2 = Rect(10,20,30,40) - r3 = (10,20,30,40) - r4 = Rect(10,20,30,40) + r1 = Rect(1, 2, 3, 4) + r2 = Rect(10, 20, 30, 40) + r3 = (10, 20, 30, 40) + r4 = Rect(10, 20, 30, 40) class foo (Rect): - def __eq__(self,other): + def __eq__(self, other): return id(self) == id(other) - def __ne__(self,other): + + def __ne__(self, other): return id(self) != id(other) class foo2 (Rect): pass - r5 = foo(10,20,30,40) - r6 = foo2(10,20,30,40) + r5 = foo(10, 20, 30, 40) + r6 = foo2(10, 20, 30, 40) self.assertNotEqual(r5, r2) # because we define equality differently for this subclass. self.assertEqual(r6, r2) - - rect_list = [r1,r2,r3,r4,r6] + rect_list = [r1, r2, r3, r4, r6] # see if we can remove 4 of these. rect_list.remove(r2) @@ -520,15 +518,15 @@ def test_collidedict(self): # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidedict: - # Rect.collidedict(dict): return (key, value) - # test if one rectangle in a dictionary intersects - # - # Returns the key and value of the first dictionary value that - # collides with the Rect. If no collisions are found, None is - # returned. - # - # Rect objects are not hashable and cannot be used as keys in a - # dictionary, only as values. + # Rect.collidedict(dict): return (key, value) + # test if one rectangle in a dictionary intersects + # + # Returns the key and value of the first dictionary value that + # collides with the Rect. If no collisions are found, None is + # returned. + # + # Rect objects are not hashable and cannot be used as keys in a + # dictionary, only as values. r = Rect(1, 1, 10, 10) r1 = Rect(1, 1, 10, 10) @@ -554,20 +552,19 @@ def test_collidedict(self): self.assertEqual(k3, 3) self.assertEqual(v3, r3) - def test_collidedictall(self): # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidedictall: - # Rect.collidedictall(dict): return [(key, value), ...] - # test if all rectangles in a dictionary intersect - # - # Returns a list of all the key and value pairs that intersect with - # the Rect. If no collisions are found an empty dictionary is - # returned. - # - # Rect objects are not hashable and cannot be used as keys in a - # dictionary, only as values. + # Rect.collidedictall(dict): return [(key, value), ...] + # test if all rectangles in a dictionary intersect + # + # Returns a list of all the key and value pairs that intersect with + # the Rect. If no collisions are found an empty dictionary is + # returned. + # + # Rect objects are not hashable and cannot be used as keys in a + # dictionary, only as values. r = Rect(1, 1, 10, 10) @@ -578,8 +575,8 @@ def test_collidedictall(self): rects_values = 1 d = {2: r2} - l = r.collidedictall(d, rects_values) - self.assertEqual(l, [(2, r2)]) + collisions = r.collidedictall(d, rects_values) + self.assertEqual(collisions, [(2, r2)]) d2 = {2: r2, 3: r3, 4: r4, 5: r5} l2 = r.collidedictall(d2, rects_values) @@ -589,57 +586,55 @@ def test_collidelist(self): # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidelist: - # Rect.collidelist(list): return index - # test if one rectangle in a list intersects - # - # Test whether the rectangle collides with any in a sequence of - # rectangles. The index of the first collision found is returned. If - # no collisions are found an index of -1 is returned. + # Rect.collidelist(list): return index + # test if one rectangle in a list intersects + # + # Test whether the rectangle collides with any in a sequence of + # rectangles. The index of the first collision found is returned. If + # no collisions are found an index of -1 is returned. r = Rect(1, 1, 10, 10) - l = [Rect(50, 50, 1, 1), Rect(5, 5, 10, 10), Rect(15, 15, 1, 1)] + other = [Rect(50, 50, 1, 1), Rect(5, 5, 10, 10), Rect(15, 15, 1, 1)] - self.assertEqual(r.collidelist(l), 1) + self.assertEqual(r.collidelist(other), 1) f = [Rect(50, 50, 1, 1), (100, 100, 4, 4)] self.assertEqual(r.collidelist(f), -1) - def test_collidelistall(self): # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidelistall: - # Rect.collidelistall(list): return indices - # test if all rectangles in a list intersect - # - # Returns a list of all the indices that contain rectangles that - # collide with the Rect. If no intersecting rectangles are found, an - # empty list is returned. + # Rect.collidelistall(list): return indices + # test if all rectangles in a list intersect + # + # Returns a list of all the indices that contain rectangles that + # collide with the Rect. If no intersecting rectangles are found, an + # empty list is returned. r = Rect(1, 1, 10, 10) - l = [ + other = [ Rect(1, 1, 10, 10), Rect(5, 5, 10, 10), Rect(15, 15, 1, 1), Rect(2, 2, 1, 1), ] - self.assertEqual(r.collidelistall(l), [0, 1, 3]) + self.assertEqual(r.collidelistall(other), [0, 1, 3]) f = [Rect(50, 50, 1, 1), Rect(20, 20, 5, 5)] self.assertFalse(r.collidelistall(f)) - def test_fit(self): # __doc__ (as of 2008-08-02) for pygame.rect.Rect.fit: - # Rect.fit(Rect): return Rect - # resize and move a rectangle with aspect ratio - # - # Returns a new rectangle that is moved and resized to fit another. - # The aspect ratio of the original Rect is preserved, so the new - # rectangle may be smaller than the target in either width or height. + # Rect.fit(Rect): return Rect + # resize and move a rectangle with aspect ratio + # + # Returns a new rectangle that is moved and resized to fit another. + # The aspect ratio of the original Rect is preserved, so the new + # rectangle may be smaller than the target in either width or height. r = Rect(10, 10, 30, 30) @@ -651,13 +646,12 @@ def test_fit(self): f2 = r2.fit(r) self.assertTrue(r.contains(f2)) - - def test_copy(self): r = Rect(1, 2, 10, 20) c = r.copy() self.assertEqual(c, r) + class PGZeroRectText(unittest.TestCase): def test_constructor_from_tuple(self): @@ -684,7 +678,8 @@ def test_constructor_from_PygameRect(self): def test_constructor_from_rect_tuple(self): "Build a rect from an object with a rect attribute which is a tuple" - class Obj: pass + class Obj: + pass obj = Obj() obj.rect = 1, 2, 3, 4 r = Rect(obj) @@ -692,7 +687,8 @@ class Obj: pass def test_constructor_from_rect_object(self): "Build a rect from an object with a rect attribute which is an object" - class Obj: pass + class Obj: + pass obj = Obj() obj.rect = Rect(1, 2, 3, 4) r = Rect(obj) @@ -702,7 +698,8 @@ def test_constructor_from_rect_indirect_tuple(self): """Build a rect from an object with a rect attribute which is an object which has a rect attribute which is a tuple """ - class Obj: pass + class Obj: + pass obj = Obj() obj1 = Obj() obj1.rect = Rect(1, 2, 3, 4) @@ -770,5 +767,6 @@ def test_collidepoint_tuple(self): r = Rect(0, 0, 1, 1) self.assertTrue(r.collidepoint((0.5, 0.5))) + if __name__ == '__main__': unittest.main() diff --git a/test/test_animation.py b/test/test_animation.py index 9ba0af4c..c060191c 100644 --- a/test/test_animation.py +++ b/test/test_animation.py @@ -292,4 +292,3 @@ def test_stop_with_complete_true_after_running(self): # Ensure animation stopped and attr as expected. self.assertFalse(anim.running) self.assertEqual(test_obj.attr, expected_attr_val) - diff --git a/test/test_event_dispatch.py b/test/test_event_dispatch.py index 7c965627..5697a242 100644 --- a/test/test_event_dispatch.py +++ b/test/test_event_dispatch.py @@ -6,6 +6,7 @@ class Event: """Mock event.""" + def __init__(self, **kwargs): self.__dict__.update(kwargs) diff --git a/test/test_osx.py b/test/test_osx.py deleted file mode 100644 index 93151485..00000000 --- a/test/test_osx.py +++ /dev/null @@ -1,17 +0,0 @@ -# -*- coding: utf-8 -*- -# test_osx.py - -# If on a Mac, launch with pgzrun, and check for correct process name: -# Without fix: -# 502 49953 21828 0 10:12pm ttys002 0:23.14 /Users/rcollin2/.virtualenvs/pgzero/bin/python /Users/rcollin2/.virtualenvs/pgzero/bin/pgzrun key_test.py -# With fix: -# -# $ ps -ef | grep -i pgzero | grep -i python -# 502 49953 21828 0 10:12pm ttys002 0:23.14 /Users/rcollin2/.virtualenvs/pgzero/bin/python /Users/rcollin2/.virtualenvs/pgzero/bin/pgzrun key_test.py -# -# $ ps -ef | grep -i pgzero | grep -i python -# 502 59772 21828 0 10:32pm ttys002 0:01.94 /Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python -m pgzero key_test.py - -# ps -ef | grep -i python | grep -iE 'pgzero | pgzrun' -# 502 59772 21828 0 10:32pm ttys002 4:23.73 /Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python -m pgzero key_test.py - diff --git a/test/test_sound_formats.py b/test/test_sound_formats.py index 91a6efcb..94c51e26 100644 --- a/test/test_sound_formats.py +++ b/test/test_sound_formats.py @@ -1,4 +1,3 @@ -import sys from unittest import TestCase, expectedFailure, skip import pygame from pgzero.loaders import sounds, set_root, UnsupportedFormat @@ -14,10 +13,10 @@ def setUpClass(self): def assert_loadable(self, name): s = sounds.load(name) - l = s.get_length() + duration = s.get_length() - assert 0.85 < l < 1.0, \ - "Failed to correctly load sound (got length %0.1fs)" % l + assert 0.85 < duration < 1.0, \ + "Failed to correctly load sound (got length %0.1fs)" % duration def assert_errmsg(self, name, pattern): with self.assertRaisesRegex(UnsupportedFormat, pattern): diff --git a/test/test_storage.py b/test/test_storage.py index da7b0537..64ab60ab 100644 --- a/test/test_storage.py +++ b/test/test_storage.py @@ -18,8 +18,8 @@ def test_dict_with_errors(self): result = list(Storage._get_json_error_keys(obj)) self.assertEqual( - result, - [("storage['obj']", "object")] + result, + [("storage['obj']", "object")] ) def test_dict_with_nested_dicts_errors(self): diff --git a/test/test_tone.py b/test/test_tone.py index 9eed70f1..c3db8e05 100644 --- a/test/test_tone.py +++ b/test/test_tone.py @@ -9,15 +9,16 @@ ) TEST_NOTES = { - 'A4': dict(val = 0, hertz = 440, parts = ('A', '', 4)), - 'C4': dict(val = -9, hertz = 261.63, parts = ('C', '', 4)), - 'C0': dict(val = -57, hertz = 16.35, parts = ('C', '', 0)), - 'B8': dict(val = 50, hertz = 7902.13, parts = ('B', '', 8)), - 'A#4': dict(val = 1, hertz = 466.16, parts = ('A', '#', 4)), - 'Ab4': dict(val = -1, hertz = 415.30, parts = ('A', 'b', 4)), - 'Bb4': dict(val = 1, hertz = 466.16, parts = ('B', 'b', 4)), + 'A4': dict(val=0, hertz=440, parts=('A', '', 4)), + 'C4': dict(val=-9, hertz=261.63, parts=('C', '', 4)), + 'C0': dict(val=-57, hertz=16.35, parts=('C', '', 0)), + 'B8': dict(val=50, hertz=7902.13, parts=('B', '', 8)), + 'A#4': dict(val=1, hertz=466.16, parts=('A', '#', 4)), + 'Ab4': dict(val=-1, hertz=415.30, parts=('A', 'b', 4)), + 'Bb4': dict(val=1, hertz=466.16, parts=('B', 'b', 4)), } + class ToneTest(TestCase): def test_sine_array(self): numpy.allclose( @@ -33,15 +34,17 @@ def test_validate_note(self): self.assertEqual(validate_note(note), val['parts']) for note in ['A9', 'H4', '4A', 'a4', 'Az4']: - with self.assertRaisesRegex(InvalidNote, re.escape('%s is not a valid note. notes are A-F, are either normal, flat (b) or sharp (#) and of octave 0-8' % note)): + errmsg = re.escape( + '%s is not a valid note. notes are A-F, are either normal, ' + 'flat (b) or sharp (#) and of octave 0-8' % note + ) + with self.assertRaisesRegex(InvalidNote, errmsg): validate_note(note) def test_note_value(self): for note, val in TEST_NOTES.items(): self.assertEqual(note_value(*val['parts']), val['val']) - def test_note_to_hertz(self): for note, val in TEST_NOTES.items(): self.assertAlmostEqual(note_to_hertz(note), val['hertz'], 2) - diff --git a/test/test_transform_anchor.py b/test/test_transform_anchor.py index 9e859385..9c9e507f 100644 --- a/test/test_transform_anchor.py +++ b/test/test_transform_anchor.py @@ -1,4 +1,3 @@ -from math import sin, cos, radians from unittest import TestCase from pgzero.actor import transform_anchor