Anonymous avatar Anonymous committed 9891918

Improved tests and testing framework.

Comments (0)

Files changed (15)

         if mingw_root is not None and path_lower.startswith(mingw_root.lower()):
             return '/mingw' + path[len(mingw_root):].replace(os.sep, '/')
         drive, tail = os.path.splitdrive(path)
-        return '/%s%s' % (drive[0], tail.replace(os.sep, '/'))
+        tail = tail.replace(os.sep, '/')
+        tail = tail.replace(' ', '\\ ')
+        return '/%s%s' % (drive[0], tail)
+
 
     def msys_to_windows(self, path):
         """Return a Windows translation of an MSYS path
                     }
     package_data = {
         "pygame2.examples" : find_pkg_data ("examples"),
+        "pygame2.test" : find_pkg_data ("test"),
         }
 
     dllfiles = [ os.path.join ("pygame2", "dll"),

src/freetype/ft_wrap.c

     FT_Face face;
 
     error = FTC_Manager_LookupFace(ft->cache_manager,
-        (FTC_FaceID)(&font->id),
-        &face);
+        (FTC_FaceID)(&font->id), &face);
     if (error)
     {
         _PGFT_SetError(ft, "Failed to load face", error);
     font->id.open_args.flags = FT_OPEN_STREAM;
     font->id.open_args.stream = stream;
 
-    return _PGFT_Init_INTERNAL(ft, font);
+    if (_PGFT_Init_INTERNAL(ft, font) == -1)
+    {
+        PGFT_UnloadFont (ft, font);
+        return -1;
+    }
+    return 0;
 }
 
 int

test/base_bufferproxy_test.py

 import pygame2.sdl.video as video
 
 class BufferProxyTest (unittest.TestCase):
+
+    def setUp (self):
+        video.init ()
+
+    def tearDown (self):
+        video.quit ()
     
     def test_pygame2_base_BufferProxy_length(self):
 
         # __doc__ (as of 2010-01-13) for pygame2.base.BufferProxy.length:
 
         # Gets the size of the buffer data in bytes.
-        video.init ()
         for bpp in (32, 24, 16, 8):
             surface = video.Surface (10, 10, bpp)
             # 10 * 10 * bpp/8 byte
             buf = surface.pixels
             self.assertEqual (buf.length, surface.h * surface.pitch)
             del buf
-        video.quit ()
 
     def test_pygame2_base_BufferProxy_raw(self):
 
 
         # Gets the raw buffer data as string. The string may contain
         # NUL bytes.
-        video.init ()
         for bpp in (32, 24, 16, 8):
             surface = video.Surface (10, 10, bpp)
             buf = surface.pixels
                 for b in buf.raw:
                     self.assertEqual (b, 0)
             del buf
-        video.quit ()
 
     def test_pygame2_base_BufferProxy_write(self):
 
         # the length of the passed *buffer* exceeds the length of the
         # BufferProxy (reduced by *offset*), an IndexError will
         # be raised.
-        video.init ()
         for bpp in (32, 16, 8):
             surface = video.Surface (10, 10, bpp)
             buf = surface.pixels
             #     for y in range (surface.height):
             #         self.failUnlessEqual (getat (x, y), color,
             #             "%s != %s at (%d, %d)" % (getat (x, y), color, x, y))
-        video.quit ()
 
 if __name__ == '__main__':
     unittest.main ()

test/freetype_font_test.py

     def get_sans_fontfile (self):
         return open (os.path.join (FONTDIR, 'test_sans.ttf'), 'rb')
 
+    def setUp (self):
+        ft.init ()
+        pygame2.sdl.video.init()
+
+    def tearDown (self):
+        pygame2.sdl.video.quit()
+        ft.quit ()
+
     def test_pygame2_freetype_Font__from_stream (self):
-        ft.init ()
         fp = self.get_fixed_fontfile ()
         f = ft.Font (fp)
         def test_size(s):
         del f
         fp.close ()
 
-        ft.quit ()
-
     def test_pygame2_freetype_Font_fixed_width(self):
-        ft.init ()
         f = self.get_sans_font ()
         self.assertFalse(f.fixed_width)
 
         self.assertFalse(f.fixed_width)
 
         # TODO: Find a real fixed width font to test with
-        ft.quit ()
         
     def test_pygame2_freetype_Font_get_metrics(self):
-        ft.init ()
         font = self.get_sans_font ()
         
         # test for floating point values (BBOX_EXACT)
 
         # test for invalid string
         self.assertRaises(TypeError, font.get_metrics, 24, 24)
-        ft.quit ()
 
     def test_pygame2_freetype_Font_get_size(self):
-        ft.init()
         font = self.get_sans_font ()
 
         def test_size(s):
         size_null = font.get_size("", ptsize=24)
         test_size (size_null)
         self.assertTrue (size_null[0] == size_null[1] == 0)
-        ft.quit ()
 
     def test_pygame2_freetype_Font_height(self):
-        ft.init ()
         f = self.get_sans_font ()
         self.assertEqual(f.height, 2355)
 
         f = self.get_fixed_font ()
         self.assertEqual(f.height, 1100)
-        ft.quit ()
         
     def test_pygame2_freetype_Font_name(self):
-        ft.init ()
         f = self.get_sans_font ()
         self.assertEqual(f.name, 'Liberation Sans')
 
         f = self.get_fixed_font ()
         self.assertEqual(f.name, 'Inconsolata')
-        ft.quit ()
         
     def test_pygame2_freetype_Font_render(self):
-        ft.init ()
         font = self.get_sans_font ()
         
-        pygame2.sdl.video.init()
         surf = pygame2.sdl.video.Surface(800, 600)
         color = pygame2.base.Color(0, 0, 0)
 
         self.assertRaises(ValueError, font.render,'foobar', color, None,
                           style=97, ptsize=24)
 
-        pygame2.sdl.video.quit()
-        ft.quit ()
-
     def test_pygame2_freetype_Font_style(self):
-        ft.init ()
         font = self.get_sans_font ()
 
         # make sure STYLE_NORMAL is the default value
         # revert changes
         font.style = ft_const.STYLE_NORMAL
         self.assertEqual(ft_const.STYLE_NORMAL, font.style)
-        ft.quit ()
 
     def todo_test_pygame2_freetype_Font_copy (self):
 
 
         # Creates a copy of the Font.
         
-        ft.init ()
         font = self.get_sans_font ()
         fontcopy = font.copy ()
-        ft.quit ()
     
     def todo_test_pygame2_freetype_Font_antialiased(self):
 

test/sdl_cdrom_test.py

 
 if __name__ == "__main__":
     unittest.main ()
-

test/sdl_image_test.py

 import pygame2.sdl.constants as constants
 
 class SDLImageTest (unittest.TestCase):
+
+    def setUp (self):
+        video.init ()
+
+    def tearDown (self):
+        video.quit ()
+
     def test_pygame2_sdl_image_load_bmp(self):
 
         # __doc__ (as of 2009-05-14) for pygame2.sdl.image.load_bmp:
         # creates a pygame2.sdl.video.Surface from it.  Loads a BMP file and
         # creates a pygame2.sdl.video.Surface from it. The file argument can
         # be either a file object or the filename.
-        video.init ()
         imgdir = os.path.dirname (os.path.abspath (__file__))
         sf = image.load_bmp (os.path.join (imgdir, "test.bmp"))
         self.assertEqual (sf.size, (16, 16))
-        video.quit ()
 
     def test_pygame2_sdl_image_save_bmp(self):
 
         # save_bmp (surface, file) -> None  Saves a surface to a bitmap file.
         # Saves a pygame2.sdl.video.Surface to the specified file, where file
         # can be a filename or file object.
-        video.init ()
         imgdir = os.path.dirname (os.path.abspath (__file__))
         sf = image.load_bmp (os.path.join (imgdir, "test.bmp"))
         buf = None
         self.assert_ (image.save_bmp (sf, buf) == None)
         self.assertEqual (os.stat (os.path.join (imgdir, "test.bmp")).st_size,
                           len (buf.getvalue ()))
-        video.quit ()
 
 if __name__ == "__main__":
     unittest.main ()

test/sdl_keyboard_test.py

 import pygame2.sdl.constants as constants
 
 class SDLKeyboardTest (unittest.TestCase):
+
+    def setUp (self):
+        video.init ()
+
+    def tearDown (self):
+        video.quit ()
+
     def test_pygame2_sdl_keyboard_enable_repeat(self):
 
         # __doc__ (as of 2009-05-13) for pygame2.sdl.keyboard.enable_repeat:
         # repeating completely.
         # 
         # Setting delay to 0 will disable repeating completely.
-        video.init ()
         self.assert_ (keyboard.enable_repeat (0, 0) == None)
         self.assert_ (keyboard.enable_repeat (1, 1) == None)
         self.assert_ (keyboard.enable_repeat (900, 1000) == None)
         self.assertRaises (ValueError, keyboard.enable_repeat, -1, -1)
         self.assertRaises (ValueError, keyboard.enable_repeat,  1, -1)
         self.assertRaises (ValueError, keyboard.enable_repeat, -1,  1)
-        video.quit ()
 
     def test_pygame2_sdl_keyboard_enable_unicode(self):
 
         # state.  By default unicode handling is enabled and for
         # keyboard events, the *unicode* member of the event will be
         # filled with the corresponding unicode character.
-        video.init ()
         self.assertEqual (keyboard.enable_unicode (), True)
         self.assertEqual (keyboard.enable_unicode (True), True)
         self.assertEqual (keyboard.enable_unicode (False), True)
         self.assertEqual (keyboard.enable_unicode (False), True)
         self.assertEqual (keyboard.enable_unicode (False), False)
         self.assertEqual (keyboard.enable_unicode (), False)
-        video.quit ()
         
     def test_pygame2_sdl_keyboard_get_key_name(self):
 
         # get_key_name (key) -> str
         # 
         # Get the descriptive name for a key constant.
-        video.init ()
         self.assertEqual (keyboard.get_key_name (constants.K_a), 'a')
         self.assertEqual (keyboard.get_key_name (constants.K_b), 'b')
         self.assertEqual (keyboard.get_key_name (constants.K_q), 'q')
         self.assertEqual (keyboard.get_key_name (constants.K_PAGEUP), 'page up')
         self.assertEqual (keyboard.get_key_name (constants.K_KP4), '[4]')
         self.assertEqual (keyboard.get_key_name (constants.K_4), '4')
-        video.quit ()
 
     def test_pygame2_sdl_keyboard_get_mod_state(self):
 
         # if specific shift keys are pressed, the state of the capslock
         # button, and more.  The bitmask will consist of the various
         # KMOD_* flags as specified in the constants.
-        video.init ()
         self.assertEqual (keyboard.get_mod_state (), 0)
         kstate = constants.KMOD_LALT|constants.KMOD_NUM
         keyboard.set_mod_state (kstate)
         self.assertEqual (keyboard.get_mod_state (), constants.KMOD_CAPS)
         keyboard.set_mod_state (kstate)
         self.assertEqual (keyboard.get_mod_state (), kstate)
-        video.quit ()
 
     def test_pygame2_sdl_keyboard_get_repeat(self):
 
         # get_repeat () -> delay, interval
         # 
         # Gets the currently set keyboard repeat delay and interval.
-        video.init ()
         keyboard.enable_repeat (0, 0)
         self.assertEqual (keyboard.get_repeat (), (0, 0))
         keyboard.enable_repeat (10, 10)
         self.assertEqual (keyboard.get_repeat (), (0, 5))
         keyboard.enable_repeat (7, 0)
         self.assertEqual (keyboard.get_repeat (), (7, 0))
-        video.quit ()
 
     def test_pygame2_sdl_keyboard_get_state(self):
 
         # the dictionary are the key constants, the boolean values of
         # the dictionary indicate, whether a certain key is pressed or
         # not.
-        video.init ()
         self.assert_ (type (keyboard.get_state ()) == dict)
         self.assert_ (constants.K_a in keyboard.get_state ().keys ())
         self.assert_ (constants.K_b in keyboard.get_state ().keys ())
         self.assert_ (constants.K_q in keyboard.get_state ().keys ())
         self.assert_ (constants.K_KP4 in keyboard.get_state ().keys ())
-        video.quit ()
 
     def test_pygame2_sdl_keyboard_set_mod_state(self):
 
         # Sets the current modifier key state. mod has to be a bitwise OR'd
         # combination of the KMOD_* flags as they are specified in the
         # constants.
-        video.init ()
         self.assertEqual (keyboard.get_mod_state (), 0)
         kstate = constants.KMOD_LALT|constants.KMOD_NUM
         keyboard.set_mod_state (kstate)
         self.assertEqual (keyboard.get_mod_state (), constants.KMOD_CAPS)
         keyboard.set_mod_state (kstate)
         self.assertEqual (keyboard.get_mod_state (), kstate)
-        video.quit ()
 
 if __name__ == "__main__":
     unittest.main ()

test/sdl_time_test.py

         # 
         # Adds a timer callback to be called periodically.
         # 
-        # Adds a timer callback to be called periodically using the specified
-        # *interval*. *callable* can be any callable objet, method or function. On
-        # invocation, the optional *data* will be passed to the callable.
+        # Adds a timer callback to be called periodically using the
+        # specified *interval*. *callable* can be any callable objet,
+        # method or function. On invocation, the optional *data* will be
+        # passed to the callable.
         # 
-        # This will return an CObject that acts as unique id for the timer callback.
+        # This will return an CObject that acts as unique id for the
+        # timer callback.
         setargs = []
         
         def _timercb (l, arg1, arg2):
         # 
         # Removes a previously added timer callback.
         # 
-        # Removes a previously added timer callback and throws an exception, if the
-        # passed object is not a matching timer object.
+        # Removes a previously added timer callback and throws an
+        # exception, if the passed object is not a matching timer
+        # object.
         
         def _timercb (flag):
             flag.append (1)

test/sdl_video_surface_blit_test.py

                 self.failUnlessEqual \
                     (cc, c, failmsg % (cc, c, x, y, sfbpp, srcbpp))    
 
+    def setUp (self):
+        video.init ()
+
+    def tearDown (self):
+        video.quit ()
+
     def test_simple_32bpp_blit (self):
         # Simple 32bpp blit
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (127, 0, 0)
         color2 = Color (0, 127, 0)
             self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
             self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
             self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
     
     def test_simple_24bpp_blit (self):
         # Simple 24bpp blit
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (127, 0, 0)
         color2 = Color (0, 127, 0)
             self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
             self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
             self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
     
     def test_simple_16bpp_blit (self):
         # Simple 16bpp blit
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (127, 0, 0)
         color2 = Color (0, 127, 0)
             self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
             self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
             self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
     
     def test_simple_8bpp_blit (self):
         # Simple 8bpp blit
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (127, 0, 0)
         color2 = Color (0, 127, 0)
             self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
             self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
             self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_ADD (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (127, 0, 0)
         color2 = Color (0, 127, 0)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        
-        video.quit ()
 
     def test_BLEND_RGB_SUB (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (255, 255, 255)
         color2 = Color (0, 127, 0)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_MAX (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (30, 10, 100)
         color2 = Color (0, 127, 24)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_MIN (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (255, 100, 12)
         color2 = Color (133, 127, 16)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_MULT (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (8, 50, 10)
         color2 = Color (127, 44, 12)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_OR (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (8, 50, 10)
         color2 = Color (127, 44, 12)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_AND (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (8, 50, 10)
         color2 = Color (127, 44, 12)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_XOR (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (8, 50, 10)
         color2 = Color (127, 44, 12)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_DIFF (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (8, 50, 10)
         color2 = Color (127, 44, 12)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_AVG (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (8, 50, 10)
         color2 = Color (127, 44, 12)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
     def test_BLEND_RGB_SCREEN (self):
-        video.init ()
         modes = [32, 24, 16, 8]
         color1 = Color (8, 50, 10)
         color2 = Color (127, 44, 12)
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (0, 5, 5, 5))
                 self._cmpcolor (sf1, sf2, color1, Rect (5, 0, 5, 5))
-        video.quit ()
 
 if __name__ == "__main__":
     unittest.main ()

test/sdl_video_surface_test.py

             for y in range (sy, sy + h):
                 self.failUnlessEqual (getat (x, y), c,
                     failmsg % (getat (x, y), c, x, y))
+
+
+    def setUp (self):
+        video.init ()
+
+    def tearDown (self):
+        video.quit ()
     
     def test_pygame2_sdl_video_Surface_blit(self):
         # This is done in sdl_video_surface_blit_test.py
 
         # Gets or sets the current clipping rectangle for
         # operations on the Surface.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
             self._cmpcolor (sf, Color (0, 255, 0), Rect (4, 5, 2, 1))
             self._cmpcolor (sf, Color (255, 255, 255), Rect (6, 0,  4, 10))
             self._cmpcolor (sf, Color (255, 255, 255), Rect (0, 6, 10,  4))
-        
-        video.quit ()
 
     def todo_test_pygame2_sdl_video_Surface_convert(self):
 
         # 
         # This creates a new, converted surface and leaves the original one
         # untouched.
-        video.init ()
-        video.quit ()
         self.fail() 
 
     def test_pygame2_sdl_video_Surface_copy(self):
         # copy () -> Surface
         # 
         # Creates an exact copy of the Surface and its image data.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
             self.assertEqual (sf.format.masks, sfcopy3.format.masks)
             self._cmppixels (sf, sfcopy3)
 
-        video.quit ()
-
     def test_pygame2_sdl_video_Surface_fill(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.fill:
         # the specified area. The blendargs are the same as for the blit
         # operation, but compare the color with the specific Surface
         # pixel value.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (20, 20, bpp)
             self._cmpcolor (sf, Color ("yellow"), Rect (5, 5, 5, 5))
             self._cmpcolor (sf, Color ("cyan"), Rect (0, 0, 5, 20))
             self._cmpcolor (sf, Color ("cyan"), Rect (10, 10, 10, 10))
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_flags(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.flags:
 
         # The currently set flags for the Surface.
-        video.init ()
         # TODO: Add 24bpp and 8bpp alpha masks in the checks below.
         modes = [32, 24, 16, 8]
         c = constants
                 self.assertEqual ((sf.flags & c.SWSURFACE), c.SWSURFACE)
                 self.assertEqual ((sf.flags & c.SRCALPHA), c.SRCALPHA)
 
-        video.quit ()
-
     def test_pygame2_sdl_video_Surface_flip(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.flip:
         # 
         # Swaps screen buffers for the Surface, causing a full update
         # and redraw of its whole area.
-        video.init ()
         modes =  [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
             self.assert_ (sf.flip () == None)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_format(self):
 
 
         # Gets the (read-only) pygame2.sdl.video.PixelFormat for this
         # Surface.
-        video.init ()
         modes =  [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
             self.assertEqual (fmt.bits_per_pixel, bpp)
             self.assertEqual (fmt.bytes_per_pixel, bpp // 8)
             self.assertEqual (fmt.readonly, True)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_get_alpha(self):
 
         # Gets the current overall alpha value of the Surface. In case the
         # surface does not support alpha transparency (SRCALPHA flag not set),
         # None will be returned.
-        video.init ()
         modes = [32, 24, 16, 8]
         # TODO: Add 24bpp and 8bpp alpha masks in the checks below.
         for bpp in modes:
                         sf.set_alpha (sf.get_alpha ())
                     self.assertEqual (sf.get_alpha (), alpha)
 
-        video.quit ()
-
     def test_pygame2_sdl_video_Surface_get_at(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.get_at:
         # get_at (point) -> Color
         # 
         # Gets the Surface pixel value at the specified point.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
                     self.assertEqual (sf.get_at (x, y), color)
                     self.assertEqual (sf.get_at ((x, y)), color)
                     self.assertEqual (sf.get_at (rect), color)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_get_colorkey(self):
 
         # 
         # Gets the colorkey for the Surface or None in case it has no colorkey
         # (SRCCOLORKEY flag not set).
-        
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
             key = sf.format.get_rgba (color)
             self.assertTrue (sf.set_colorkey (color))
             self.assertEqual (sf.get_colorkey (), key)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_get_palette(self):
 
         #
         # Gets the palette colors used by the Surface or None, if the Surface
         # does not use any palette.
-        video.init ()
         palette = [Color (i, i, i) for i in range (256)]
         
         surface = video.Surface(2, 2, 8)
             self.failUnlessEqual (c2, c)
         for c in palette2:
             self.failUnless (isinstance (c, Color))
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_h(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.h:
 
         # Gets the height of the Surface.
-        video.init ()
         self.assertEqual (video.Surface (10, 10).h, 10)
         self.assertEqual (video.Surface (10, 1).h, 1)
         self.assertEqual (video.Surface (10, 100).h, 100)
         self.assertEqual (video.Surface (2, 65535).h, 65535)
         self.assertRaises (ValueError, video.Surface, -10, 10)
         self.assertRaises (pygame2.Error, video.Surface, 68000, 10)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_height(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.height:
 
         # Gets the height of the Surface.
-        video.init ()
         self.assertEqual (video.Surface (10, 10).height, 10)
         self.assertEqual (video.Surface (10, 1).height, 1)
         self.assertEqual (video.Surface (10, 100).height, 100)
         self.assertEqual (video.Surface (2, 65535).height, 65535)
         self.assertRaises (ValueError, video.Surface, -10, 10)
         self.assertRaises (pygame2.Error, video.Surface, 68000, 10)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_lock(self):
 
         # lock () -> None
         # 
         # Locks the Surface for a direct access to its internal pixel data.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
             self.assert_ (sf.lock () == None)
             self.assert_ (sf.unlock () == None)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_locked(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.locked:
 
         # Gets, whether the Surface is currently locked.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
             self.assertFalse (sf.locked)
             sf.unlock ()
             self.assertFalse (sf.locked)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_pitch(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.pitch:
 
         # Get the length of a surface scanline in bytes.
-        video.init ()
         sf = video.Surface (10, 10, 32)
         self.assert_ (sf.pitch >= 40) # 10 * 4 bpp
         sf = video.Surface (10, 10, 24)
         self.assert_ (sf.pitch >= 20) # 10 * 2 bpp
         sf = video.Surface (10, 10, 8)
         self.assert_ (sf.pitch >= 10) # 10 * 1 bpp
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_pixels(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.pixels:
 
         # Gets the pixel buffer of the Surface.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
             self.assert_ (buf.length >= 10 * sf.format.bytes_per_pixel)
             del buf
             self.assertFalse (sf.locked)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_save(self):
 
         # 
         # If no type information is supplied and the file type cannot be
         # determined either, it will use TGA.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf1 = video.Surface (16, 16, bpp)
             sf2 = sdlimage.load (buf, "tga").convert (sf1.format)
             self.assertEqual (sf1.size, sf2.size)
             self._cmppixels (sf1, sf2)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_set_alpha(self):
 
         # set_at (point, color) -> None
         # 
         # Sets the Surface pixel value at the specified point.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
                     sf.set_at ((x, y), color)
                     self.assertEqual (sf.get_at (x, y), cc)
             self._cmpcolor (sf, color, rect)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_set_colorkey(self):
 
         # 
         # If any other error occurs, False will be returned and the
         # Surface palette should be inspected for any changes.
-        video.init ()
-        video.quit ()
         self.fail() 
 
     def test_pygame2_sdl_video_Surface_set_palette(self):
         # 
         # If any other error occurs, False will be returned and the
         # Surface palette should be inspected for any changes.
-        video.init ()
-        
+
         palette = [Color (i, i, i) for i in range (256)]
         
         surface = video.Surface (2, 2, 8)
                                       "palette color %i" % (i,))
             self.failUnlessRaises (ValueError, surface.set_palette,
                                    (1, 2, 3, 254))
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_size(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.size:
 
         # Gets the size of the Surface.
-        video.init ()
         self.assertEqual (video.Surface (10, 10).size, (10, 10))
         self.assertEqual (video.Surface (1, 10).size, (1, 10))
         self.assertEqual (video.Surface (100, 10).size, (100, 10))
         self.assertEqual (video.Surface (2, 65535).size, (2, 65535))
         self.assertRaises (ValueError, video.Surface, -10, 10)
         self.assertRaises (pygame2.Error, video.Surface, 68000, 10)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_unlock(self):
 
         # unlock () -> None
         # 
         # Unlocks the Surface, releasing the direct access to the pixel data.
-        video.init ()
         modes = [32, 24, 16, 8]
         for bpp in modes:
             sf = video.Surface (10, 10, bpp)
             self.assert_ (sf.lock () == None)
             self.assert_ (sf.unlock () == None)
-        video.quit ()
 
     def todo_test_pygame2_sdl_video_Surface_update(self):
 
         # 
         # Upates the given area (or areas, if a list of rects is passed)
         # on the Surface.
-        video.init ()
-        video.quit ()
         self.fail() 
 
     def test_pygame2_sdl_video_Surface_w(self):
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.w:
 
         # Gets the width of the Surface.
-        video.init ()
         self.assertEqual (video.Surface (10, 10).w, 10)
         self.assertEqual (video.Surface (1, 10).w, 1)
         self.assertEqual (video.Surface (100, 10).w, 100)
         self.assertEqual (video.Surface (16383, 2).w, 16383)
         self.assertRaises (ValueError, video.Surface, -10, 10)
         self.assertRaises (pygame2.Error, video.Surface, 68000, 10)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_width(self):
 
         # __doc__ (as of 2009-05-15) for pygame2.sdl.video.Surface.width:
 
         # Gets the width of the Surface.
-        video.init ()
         self.assertEqual (video.Surface (10, 10).width, 10)
         self.assertEqual (video.Surface (1, 10).width, 1)
         self.assertEqual (video.Surface (100, 10).width, 100)
         self.assertEqual (video.Surface (16383, 2).width, 16383)
         self.assertRaises (ValueError, video.Surface, -10, 10)
         self.assertRaises (pygame2.Error, video.Surface, 68000, 10)
-        video.quit ()
 
     def test_pygame2_sdl_video_Surface_scroll (self):
 
         # retain their original pixel values. Scrolling is contained by
         # the Surface clip area. It is safe to have *dx* and *dy* values
         # that exceed the surface size.
-        video.init ()
 
         scrolls = [(8, 2, 3),
                    (16, 2, 3),
         sf.fill (Color (255, 0, 0), Rect (0, 0, 5, 5))
         sf.scroll (-10, -10)
         self._cmpcolor (sf, Color (0, 0, 0))
-        video.quit ()
         
     def test_pygame2_sdl_video_Surface___repr__(self):
-        video.init ()
         sf = video.Surface (10, 10, 8)
         text = "<Surface 10x10@8bpp>"
         self.assertEqual (repr (sf), text)
         sf = video.Surface (1, 1, 32)
         text = "<Surface 1x1@32bpp>"
         self.assertEqual (repr (sf), text)
-        video.quit ()
 
 if __name__ == "__main__":
     unittest.main ()

test/sdl_wm_test.py

 
 class SDLWMTest (unittest.TestCase):
 
+    def setUp (self):
+        video.init ()
+
+    def tearDown (self):
+        video.quit ()
+
     def test_pygame2_sdl_wm_get_caption(self):
 
         # __doc__ (as of 2010-01-13) for pygame2.sdl.wm.get_caption:
         # 
         # Gets the caption of the current SDL window.
         self.assertRaises (pygame2.Error, wm.get_caption)
-        video.init ()
-        self.assertRaises (pygame2.Error, wm.get_caption)
         sf = video.set_mode (10, 10)
         self.assertEqual (wm.get_caption (), (None, None))
         wm.set_caption ("test window")
         self.assertEqual (wm.get_caption (), ("", "icon"))
         wm.set_caption ("test window", "icon")
         self.assertEqual (wm.get_caption (), ("test window", "icon"))
-        video.quit ()
 
     def todo_test_pygame2_sdl_wm_get_info(self):
 
 
         # handled in wm_get_caption()
         self.assertRaises (pygame2.Error, wm.set_caption, "test", "test")
-        video.init ()
-        self.assertRaises (pygame2.Error, wm.set_caption, "test", "test")
         sf = video.set_mode (10, 10)
         self.assertEqual (wm.get_caption (), (None, None))
         self.assert_ (wm.set_caption ("test window") == None)
         self.assertEqual (wm.get_caption (), ("", "icon"))
         self.assert_ (wm.set_caption ("test window", "icon") == None)
         self.assertEqual (wm.get_caption (), ("test window", "icon"))
-        video.quit ()
 
     @interactive ("Was the window icon updated correctly?")
     def todo_test_pygame2_sdl_wm_set_icon(self):

test/util/runtests.py

 def printerror ():
     print (traceback.format_exc ())
 
-
 def create_options ():
     """Create the accepatble options for the test runner."""
     optparser = optparse.OptionParser ()
     optparser.add_option ("-n", "--nosubprocess", action="store_true",
+                          default=False,
                           help="run everything in a single process "
                           "(default: use seperate subprocesses)")
-    optparser.add_option ("-r", "--random", action="store_true",
+    optparser.add_option ("-v", "--verbose", action="store_true", default=False,
+                          help="be verbose adnd print anything instantly")
+    optparser.add_option ("-r", "--random", action="store_true", default=False,
                           help="randomize the order of tests")
     optparser.add_option ("-S", "--seed", type="int",
                           help="seed the randomizer (useful to "
     testfiles.sort ()
     return testdir, testfiles
 
-def loadtests (test, testdir, options):
+def loadtests (test, testdir, writer, options):
     """Loads a test."""
     suites = []
     testloader = testrunner.TagTestLoader (EXCLUDETAGS)
         testmod = os.path.splitext (test)[0]
         glob, loc = {}, {}
         package = __import__ (testmod, glob, loc)
-        print ("Loading tests from %s ..." % testmod)
+        if options.verbose:
+            writer.writeline ("Loading tests from [%s] ..." % testmod)
+        else:
+            writer.writesame ("Loading tests from [%s] ..." % testmod)
         for x in dir (package):
             val = package.__dict__[x]
             if hasattr (val, "setUp") and hasattr (val, "tearDown"):
 def run ():
     optparser = create_options ()
     options, args = optparser.parse_args ()
+    #err, out = support.redirect_output ()
+    writer = support.StreamOutput (sys.stdout)
 
-    print (HEAVYDELIM)
-    print ("-- Starting tests --")
-    print (HEAVYDELIM)
-    #err, out = support.redirect_output ()
+    if options.verbose:
+        writer.writeline (HEAVYDELIM)
+        writer.writeline ("-- Starting tests --")
+        writer.writeline (HEAVYDELIM)
+
     testdir, testfiles = gettestfiles ()
     testsuites = []
     for test in testfiles:
-        testsuites.extend (loadtests (test, testdir, options))
-    runner = testrunner.SimpleTestRunner (sys.stderr)
+        testsuites.extend (loadtests (test, testdir, writer, options))
+    if not options.verbose:
+        writer.writesame ("Tests loaded")
+    runner = testrunner.SimpleTestRunner (sys.stderr, options.verbose)
     
     results = []
     timetaken = 0
-    print (HEAVYDELIM)
-    print ("-- Executing tests --")
-    print (HEAVYDELIM)
+
+    if options.verbose:
+        writer.writeline (HEAVYDELIM)
+        writer.writeline ("-- Executing tests --")
+        writer.writeline (HEAVYDELIM)
+
+    maxcount = 0
+    curcount = 0
     for suite in testsuites:
-        result = runner.run (suite)
+        maxcount += suite.countTestCases ()
+
+    class writerunning:
+        def __init__ (self, maxcount, verbose):
+            self.curcount = 0
+            self.maxcount = maxcount
+            self.verbose = verbose
+
+        def __call__ (self):
+            self.curcount += 1
+            if not self.verbose:
+                writer.writesame ("Running tests [ %d / %d ] ..." %
+                                  (self.curcount, self.maxcount))
+
+    runwrite = writerunning (maxcount, options.verbose)
+
+    for suite in testsuites:
+        result = runner.run (suite, runwrite)
         timetaken += result.duration
+        curcount += result.testsRun
         results.append (result)
-    print (" Finished")
+    writer.writeline ()
     testcount, errors, failures, ok = prepare_results (results)
-    print (HEAVYDELIM)
-    print ("-- Statistics --")
-    print (HEAVYDELIM)
-    print ("Time taken:     %.3f seconds" % timetaken)
-    print ("Tests executed: %d " % testcount)
-    print ("Tests OK:       %d " % ok)
-    print ("Tests ERROR:    %d " % len (errors))
-    print ("Tests FAILURE:  %d " % len (failures))
+
+    writer.writeline (HEAVYDELIM)
+    writer.writeline ("-- Statistics --")
+    writer.writeline (HEAVYDELIM)
+    writer.writeline ("Time taken:     %.3f seconds" % timetaken)
+    writer.writeline ("Tests executed: %d " % testcount)
+    writer.writeline ("Tests OK:       %d " % ok)
+    writer.writeline ("Tests ERROR:    %d " % len (errors))
+    writer.writeline ("Tests FAILURE:  %d " % len (failures))
+    
     if len (errors) > 0:
-        print ("Errors:" + os.linesep)
+        writer.writeline ("Errors:" + os.linesep)
         for err in errors:
-            print (LINEDELIM)
-            print ("ERROR: " + err[0])
-            print (HEAVYDELIM)
-            print (err[1])
+            writer.writeline (LINEDELIM)
+            writer.writeline ("ERROR: " + err[0])
+            writer.writeline (HEAVYDELIM)
+            writer.writeline (err[1])
     if len (failures) > 0:
-        print ("Failures:" + os.linesep)
+        writer.writeline ("Failures:" + os.linesep)
         for fail in failures:
-            print (LINEDELIM)
-            print ("FAILURE: " + fail[0])
-            print (HEAVYDELIM)
-            print (fail[1])
+            writer.writeline (LINEDELIM)
+            writer.writeline ("FAILURE: " + fail[0])
+            writer.writeline (HEAVYDELIM)
+            writer.writeline (fail[1])
         
     #support.restore_output (err, out)
 

test/util/support.py

 def restore_output (err, out):
     """Restores stderr and stdout using the passed streams."""
     sys.stderr, sys.stdout = err, out
+
+class StreamOutput (object):
+    def __init__ (self, stream):
+        self.stream = stream
+        self.startoffset = self.stream.tell ()
+        self.curoffset = 0
+
+    def writeline (self, data=None):
+        if data:
+            self.stream.write (data)
+        self.stream.write (os.linesep)
+        if data:
+            self.curoffset = len (data)
+        else:
+            self.curoffset = 0
+        self.stream.flush ()
+
+    def write (self, data):
+        self.stream.write (data)
+        self.curoffset = len (data)
+        self.stream.flush ()
+
+    def writesame (self, data):
+        overhang = self.curoffset - len (data)
+        if overhang > 0:
+            self.stream.write (data + " " * overhang + "\r")
+        else:
+            self.stream.write (data + "\r")
+        self.curoffset = len (data)
+        self.stream.flush ()

test/util/testrunner.py

 import time
 
 class TagTestLoader (TestLoader):
-
+    """A TestLoader which handles additional __tags__ attributes for
+    test functions.
+    """
     def __init__ (self, excludetags):
         TestLoader.__init__ (self)
         self.excludetags = excludetags
 
     def getTestCaseNames(self, testCaseClass):
-        """Return a sorted sequence of method names found within testCaseClass
+        """
+        Gets only the tests, which are not within the tag exclusion.
+        The method overrides the original TestLoader.getTestCaseNames()
+        method, so we need to keep them in sync on updates.
         """
         def isTestMethod(attrname, testCaseClass=testCaseClass,
                          prefix=self.testMethodPrefix):
         return testFnNames
 
 class SimpleTestResult (TestResult):
-    def __init__ (self, stream=sys.stderr):
+    """A simple TestResult class with output capabilities.
+    """
+    def __init__ (self, stream=sys.stderr, verbose=False, countcall=None):
         TestResult.__init__ (self)
         self.stream = stream
         self.duration = 0
+        self.verbose = verbose
+        self.countcall = countcall
     
     def addSuccess (self, test):
         TestResult.addSuccess (self, test)
-        self.stream.write (".")
-        self.stream.flush ()
+        if self.verbose:
+            self.stream.write (".")
+            self.stream.flush ()
+        self.countcall ()
 
     def addError (self, test, err):
         TestResult.addError (self, test, err)
-        self.stream.write ("E")
-        self.stream.flush ()
+        if self.verbose:
+            self.stream.write ("E")
+            self.stream.flush ()
+        self.countcall ()
 
     def addFailure (self, test, err):
         TestResult.addFailure (self, test, err)
-        self.stream.write ("F")
-        self.stream.flush ()
+        if self.verbose:
+            self.stream.write ("F")
+            self.stream.flush ()
+        self.countcall ()
 
 class SimpleTestRunner (object):
-    def __init__ (self, stream=sys.stderr):
+    def __init__ (self, stream=sys.stderr, verbose=False):
         self.stream = stream
+        self.verbose = verbose
 
-    def run (self, test):
-        result = SimpleTestResult (self.stream)
+    def run (self, test, countcall):
+        result = SimpleTestResult (self.stream, self.verbose, countcall)
         starttime = time.time ()
         test (result)
         endtime = time.time ()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.