Commits

Anonymous committed 03126e4

Added a transform.rotate 90 degrees lossless test, moved some helpers to test_utils.py, added a groupcollide test to sprite_test.py.

Comments (0)

Files changed (4)

test/draw_test.py

 
 ################################################################################
 
-def rect_area_pts(rect):
-    for l in xrange(rect.left, rect.right):
-        for t in xrange(rect.top, rect.bottom):
-            yield l, t
-
-def rect_perimeter_pts(rect):
-    """
-    
-    Returns a list of pts ((L, T) tuples) encompassing the perimeter of a rect.
-    
-    The order is clockwise:
-        
-          topleft to topright
-         topright to bottomright
-      bottomright to bottomleft
-       bottomleft to topleft
-    
-    Duplicate pts are not returned
-
-    """
-    clock_wise_from_top_left = (
-      ((l,       rect.top) for l in xrange(rect.left,      rect.right)      ),
-      ((rect.right -1,  t) for t in xrange(rect.top   + 1, rect.bottom)     ), 
-      ((l, rect.bottom -1) for l in xrange(rect.right  -2, rect.left -1, -1)), 
-      ((rect.left,      t) for t in xrange(rect.bottom -2, rect.top,     -1))
-    )
-    
-    for line in clock_wise_from_top_left:
-        for pt in line: yield pt
-    
-def rect_outer_bounds(rect):
-    """
-
-    Returns topleft outerbound if possible and then the other pts, that are 
-    "exclusive" bounds of the rect
-    
-    """
-    return (
-         (rect.left is not 0 and [(rect.left-1, rect.top)] or []) +
-        [ rect.topright,                                          
-          rect.bottomleft,                                             
-          rect.bottomright]  
-    ) 
-
-def test_helpers():
-    r = pygame.Rect(0, 0, 10, 10)
-    assert (
-        rect_outer_bounds ( r ) == [(10,  0), # tr
-                                    ( 0, 10), # bl
-                                    (10, 10)] # br
-    )
-    assert len(list(rect_area_pts(r))) == 100 
-    
-    r = pygame.Rect(0, 0, 3, 3)
-    assert list(rect_perimeter_pts(r)) == [
-        (0, 0), (1, 0), (2, 0),                 # tl -> tr
-        (2, 1), (2, 2),                         # tr -> br  
-        (1, 2), (0, 2),                         # br -> bl
-        (0, 1)                                  # bl -> tl
-    ]
-
-if 1:
-    test_helpers()
-
-################################################################################
-
 class DrawModuleTest(unittest.TestCase):
     def setUp(self):
         (self.surf_w, self.surf_h) = self.surf_size = (320, 200)
         self.assert_(drawn == rect)
 
         #Should be colored where it's supposed to be
-        for pt in rect_area_pts(rect):
+        for pt in test_utils.rect_area_pts(rect):
             color_at_pt = self.surf.get_at(pt)
             self.assert_(color_at_pt == self.color)
 
         #And not where it shouldn't
-        for pt in rect_outer_bounds(rect):
+        for pt in test_utils.rect_outer_bounds(rect):
             color_at_pt = self.surf.get_at(pt)
             self.assert_(color_at_pt != self.color)
     
         self.assert_(drawn == rect)
 
         #Should be colored where it's supposed to be
-        for pt in rect_perimeter_pts(drawn):
+        for pt in test_utils.rect_perimeter_pts(drawn):
             color_at_pt = self.surf.get_at(pt)
             self.assert_(color_at_pt == self.color)
 
         #And not where it shouldn't
-        for pt in rect_outer_bounds(drawn):
+        for pt in test_utils.rect_outer_bounds(drawn):
             color_at_pt = self.surf.get_at(pt)
             self.assert_(color_at_pt != self.color)
+
+    def test_line(self):
+
+        # __doc__ (as of 2008-06-25) for pygame.draw.line:
+
+          # pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect
+          # draw a straight line segment
+
+        drawn = draw.line(self.surf, self.color, (1, 0), (200, 0)) #(l, t), (l, t)
+        self.assert_(drawn.right == 201,
+            "end point arg should be (or at least was) inclusive"
+        )
+
+        #Should be colored where it's supposed to be
+        for pt in test_utils.rect_area_pts(drawn):
+            self.assert_(self.surf.get_at(pt) == self.color)
+
+        #And not where it shouldn't
+        for pt in test_utils.rect_outer_bounds(drawn):
+            self.assert_(self.surf.get_at(pt) != self.color)
+
         
     def test_aaline(self):
         # __doc__ (as of 2008-06-25) for pygame.draw.aaline:
           # draw a round shape inside a rectangle
 
         self.assert_(test_not_implemented()) 
-
-    def test_line(self):
-
-        # __doc__ (as of 2008-06-25) for pygame.draw.line:
-
-          # pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect
-          # draw a straight line segment
-
-        drawn = draw.line(self.surf, self.color, (1, 0), (200, 0)) #(l, t), (l, t)
-        self.assert_(drawn.right == 201,
-            "end point arg should be (or at least was) inclusive"
-        )
-
-        #Should be colored where it's supposed to be
-        for pt in rect_area_pts(drawn):
-            self.assert_(self.surf.get_at(pt) == self.color)
-
-        #And not where it shouldn't
-        for pt in rect_outer_bounds(drawn):
-            self.assert_(self.surf.get_at(pt) != self.color)
         
     def test_lines(self):
 

test/sprite_test.py

     def test_groupcollide(self):
     
         # __doc__ (as of 2008-06-24) for pygame.sprite.groupcollide:
-    
+        
           # pygame.sprite.groupcollide(groupa, groupb, dokilla, dokillb) -> dict
           # collision detection between group and group
           # 
           # sprites must have a "rect" value, which is a
           # rectangle of the sprite area, which will be used
           # to calculate the collision.
-    
-        self.assert_(test_not_implemented())
-    
+        
+        collision = pygame.sprite.groupcollide(self.ag, self.ag2, False, False)
+        
+        self.assert_(collision == {self.s1 : [self.s2]})
+
     def test_spritecollideany(self):
     
         # __doc__ (as of 2008-06-24) for pygame.sprite.spritecollideany:
     
         self.assert_(test_not_implemented())
 
-    def test_collide_rect(self):
+    test_collide_rect = test_spritecollide__collided_defaults_to_collide_rect
     
         # __doc__ (as of 2008-06-24) for pygame.sprite.collide_rect:
     
           # 
           # New in pygame 1.8.0
     
-        self.assert_(test_not_implemented())
 
 ################################################################################
 

test/test_utils.py

 #################################### IMPORTS ###################################
 
-import tempfile, sys
+import tempfile, sys, pygame
 
 ############################### INCOMPLETE TESTS ###############################
 
         
 #################################### HELPERS ###################################
 
+def rgba_between(value, minimum=0, maximum=255):
+    if value < minimum: return minimum
+    elif value > maximum: return maximum
+    else: return value
+
+def gradient(width, height):
+    """
+
+    Yields a pt and corresponding RGBA tuple, for every (width, height) combo.
+    Useful for generating gradients.
+    
+    Actual gradient may be changed, no tests rely on specific values.
+    
+    Used in transform.rotate lossless tests to generate a fixture.
+
+    """
+
+    for l in xrange(width):
+        for t in xrange(height):
+            yield (l,t), tuple(map(rgba_between, (l, t, l, l+t)))
+
 def unordered_equality(seq1, seq2):
-    """ 
+    """
+    
     Tests to see if the contents of one sequence is contained in the other
     and that they are of the same length.
+    
     """
     
     if len(seq1) != len(seq2):
         return False
-    
+
     for val in seq1:
         if val not in seq2:
             return False
         
     return True
 
+def rect_area_pts(rect):
+    for l in xrange(rect.left, rect.right):
+        for t in xrange(rect.top, rect.bottom):
+            yield l, t
+
+def rect_perimeter_pts(rect):
+    """
+    
+    Returns pts ((L, T) tuples) encompassing the perimeter of a rect.
+    
+    The order is clockwise:
+        
+          topleft to topright
+         topright to bottomright
+      bottomright to bottomleft
+       bottomleft to topleft
+    
+    Duplicate pts are not returned
+
+    """
+    clock_wise_from_top_left = (
+      ((l,       rect.top) for l in xrange(rect.left,      rect.right)      ),
+      ((rect.right -1,  t) for t in xrange(rect.top   + 1, rect.bottom)     ), 
+      ((l, rect.bottom -1) for l in xrange(rect.right  -2, rect.left -1, -1)), 
+      ((rect.left,      t) for t in xrange(rect.bottom -2, rect.top,     -1))
+    )
+    
+    for line in clock_wise_from_top_left:
+        for pt in line: yield pt
+    
+def rect_outer_bounds(rect):
+    """
+
+    Returns topleft outerbound if possible and then the other pts, that are 
+    "exclusive" bounds of the rect
+        
+   ?------O     
+    |RECT|      ?|0)uterbound
+    |----|     
+   O      O
+
+    """
+    return (
+         (rect.left is not 0 and [(rect.left-1, rect.top)] or []) +
+        [ rect.topright,                                          
+          rect.bottomleft,                                             
+          rect.bottomright]  
+    ) 
+
+def helpers_test():
+    """
+    
+    Lightweight test for helpers
+    
+    """
+
+    r = pygame.Rect(0, 0, 10, 10)
+    assert (
+        rect_outer_bounds ( r ) == [(10,  0), # tr
+                                    ( 0, 10), # bl
+                                    (10, 10)] # br
+    )
+    
+    assert len(list(rect_area_pts(r))) == 100 
+    
+    
+    r = pygame.Rect(0, 0, 3, 3)
+    assert list(rect_perimeter_pts(r)) == [
+        (0, 0), (1, 0), (2, 0),                 # tl -> tr
+        (2, 1), (2, 2),                         # tr -> br  
+        (1, 2), (0, 2),                         # br -> bl
+        (0, 1)                                  # bl -> tl
+    ]
+        
+    return 'Tests: OK'
+if __name__ == '__main__':
+    print helpers_test()
+
 ################################################################################

test/transform_test.py

         self.assert_(test_not_implemented()) 
 
     def test_rotate(self):
-
         # __doc__ (as of 2008-06-25) for pygame.transform.rotate:
 
           # pygame.transform.rotate(Surface, angle): return Surface
           # rotate an image
 
-        self.assert_(test_not_implemented()) 
+        self.assert_(test_not_implemented())
+
+    def test_rotate__lossless_at_90_degrees(self):
+        w, h = 32, 32
+        s = pygame.Surface((w, h), pygame.SRCALPHA)
+
+        gradient = list(test_utils.gradient(w, h))
+
+        for pt, color in gradient: s.set_at(pt, color)
+
+        for rotation in (90, -90): 
+            s = pygame.transform.rotate(s,rotation)
+
+        for pt, color in gradient:
+            self.assert_(s.get_at(pt) == color)
 
     def test_rotozoom(self):