annotate test/_view_test.py @ 3172:d1b367fc08bc tests-returncode

Use a return code of 1 when the tests fail.
author Thomas Kluyver <takowl@gmail.com>
date Wed, 09 Jan 2013 10:13:57 +0000
parents 841b902f3d15
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2131
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
1 import sys
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
2 import re
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
3 import weakref
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
4 if __name__ == '__main__':
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
5 import os
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
6 pkg_dir = os.path.split(os.path.abspath(__file__))[0]
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
7 parent_dir, pkg_name = os.path.split(pkg_dir)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
8 is_pygame_pkg = (pkg_name == 'tests' and
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
9 os.path.split(parent_dir)[1] == 'pygame')
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
10 if not is_pygame_pkg:
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
11 sys.path.insert(0, parent_dir)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
12 else:
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
13 is_pygame_pkg = __name__.startswith('pygame.tests.')
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
14
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
15 if is_pygame_pkg:
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
16 from pygame.tests.test_utils import test_not_implemented, unittest
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
17 else:
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
18 from test.test_utils import test_not_implemented, unittest
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
19 import pygame
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
20 from pygame._view import View
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
21
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
22 class ViewTest(unittest.TestCase):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
23 def test___array_struct___property(self):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
24 c = []
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
25 v = View(c)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
26 self.assert_(v.__array_struct__ is c)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
27
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
28 def test_parent_property(self):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
29 c = []
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
30 p = []
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
31 v = View(c, p)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
32 self.assert_(v.parent is p)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
33
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
34 def test_destructor(self):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
35 def d(capsule, parent):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
36 success.append(capsule is c)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
37 success.append(parent is None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
38
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
39 success = []
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
40 c = []
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
41 view_id = id(View(c, destructor=d))
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
42 self.assert_(success)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
43 self.assert_(success[0])
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
44 self.assert_(success[1])
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
45
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
46 def test_weakref(self):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
47 v = View([])
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
48 weak_v = weakref.ref(v)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
49 self.assert_(weak_v() is v)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
50 del v
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
51 self.assert_(weak_v() is None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
52
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
53 def test_gc(self):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
54 """refcount agnostic check that contained objects are freed"""
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
55 def d(capsule, parent):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
56 return r[0]
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
57 class Obj(object):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
58 pass
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
59 c = Obj()
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
60 p = Obj()
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
61 r = [Obj()]
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
62 weak_c = weakref.ref(c)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
63 weak_p = weakref.ref(p)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
64 weak_r0 = weakref.ref(r[0])
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
65 weak_d = weakref.ref(d)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
66 v = View(c, p, d)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
67 weak_v = weakref.ref(v)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
68 del c, p, d
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
69 self.assert_(weak_c() is not None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
70 self.assert_(weak_p() is not None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
71 self.assert_(weak_d() is not None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
72 del v
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
73 self.assert_(weak_v() is None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
74 self.assert_(weak_c() is None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
75 self.assert_(weak_p() is None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
76 self.assert_(weak_d() is None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
77 self.assert_(weak_r0() is not None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
78 del r[0]
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
79 self.assert_(weak_r0() is None)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
80
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
81 def test_c_api(self):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
82 api = pygame._view._PYGAME_C_API
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
83 self.assert_(isinstance(api, type(pygame.base._PYGAME_C_API)))
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
84
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
85 def test_repr(self):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
86 z = 0
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
87 cname = re.findall(r"'([^']+)'", repr(View))[0]
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
88 oname, ovalue = re.findall(r"<([^)]+)\(([^)]+)\)>", repr(View(0)))[0]
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
89 self.assertEqual(oname, cname)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
90 self.assertEqual(id(z), int(ovalue, 16))
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
91
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
92 def test_subclassing(self):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
93 class MyView(View):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
94 def __repr__(self):
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
95 return "*%s*" % (View.__repr__(self),)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
96 v = MyView(0)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
97 self.assertEqual(v.__array_struct__, 0)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
98 r = repr(v)
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
99 self.assertEqual(r[:2], '*<')
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
100 self.assertEqual(r[-2:], '>*')
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
101
3014
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
102
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
103 class GetArrayInterfaceTest(unittest.TestCase):
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
104
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
105 def test_get_array_interface(self):
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
106 surf = pygame.Surface((7, 11), 0, 32)
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
107 d = pygame._view.get_array_interface(surf.get_view("2"))
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
108 self.assertEqual(len(d), 5)
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
109 self.assertEqual(d['version'], 3)
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
110 if pygame.get_sdl_byteorder() == pygame.LIL_ENDIAN:
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
111 byteorder = '<'
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
112 else:
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
113 byteorder = '>'
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
114 self.assertEqual(d['typestr'], byteorder + 'u4')
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
115 self.assertEqual(d['shape'], (7, 11))
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
116 self.assertEqual(d['strides'], (4, 28))
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
117 self.assertEqual(d['data'], (surf._pixels_address, False))
841b902f3d15 Centralize common array struct interface operations in the _view module
Lenard Lindstrom <len-l@telus.net>
parents: 2134
diff changeset
118
2131
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
119 if __name__ == '__main__':
018779fb04f5 Merge the buffer branch back into trunk.
Lenard Lindstrom <len-l@telus.net>
parents:
diff changeset
120 unittest.main()