-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathextensions_test.py
More file actions
133 lines (109 loc) · 5.05 KB
/
extensions_test.py
File metadata and controls
133 lines (109 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
import unittest
import challenge.basics
import challenge.extensions
class ThingamajigTestMixin(object):
"""Base class for all Thingamajig TestCases.
Derived classes must define a setUp() method that defines a self.extra
attribute that matches the template type of the Thingamajig being tested.
"""
def test_standard_ctor_pos_all(self):
"""Test standard constructor with all positional arguments.
"""
t = challenge.extensions.Thingamajig(self.extra, "b", 0,
dtype=self.dtype)
self.assertIsInstance(t, challenge.extensions.Thingamajig)
self.assertEqual(t.get_extra(), self.extra)
self.assertEqual(t.name, "b")
self.assertEqual(t.value, 0)
def test_standard_ctor_pos_default(self):
"""Test standard constructor with positional arguments, with a default.
"""
t = challenge.extensions.Thingamajig(self.extra, "b")
self.assertIsInstance(t, challenge.extensions.Thingamajig)
self.assertEqual(t.get_extra(), self.extra)
self.assertEqual(t.name, "b")
self.assertEqual(t.value, 1)
def test_standard_ctor_kwarg_al(self):
"""Test standard constructor with all keyword arguments.
"""
t = challenge.extensions.Thingamajig(name="c", value=2,
extra=self.extra)
self.assertIsInstance(t, challenge.extensions.Thingamajig)
self.assertEqual(t.get_extra(), self.extra)
self.assertEqual(t.name, "c")
self.assertEqual(t.value, 2)
def test_standard_ctor_kwarg_default(self):
"""Test standard constructor with keyword arguments, with a default.
"""
t = challenge.extensions.Thingamajig(name="d", extra=self.extra)
self.assertIsInstance(t, challenge.extensions.Thingamajig)
self.assertEqual(t.get_extra(), self.extra)
self.assertEqual(t.name, "d")
self.assertEqual(t.value, 1)
def test_standard_ctor_mixed(self):
"""Test standard constructor with a mix of positional and keyword args.
"""
t = challenge.extensions.Thingamajig(self.extra, name="e", value=3)
self.assertIsInstance(t, challenge.extensions.Thingamajig)
self.assertEqual(t.get_extra(), self.extra)
self.assertEqual(t.name, "e")
self.assertEqual(t.value, 3)
def test_clone(self):
"""Test calling the clone() method, including downcasting.
"""
t1 = challenge.extensions.Thingamajig(self.extra, "g", 5)
t2 = t1.clone()
self.assertIsInstance(t2, challenge.extensions.Thingamajig)
self.assertEqual(t1.name, t2.name)
self.assertEqual(t1.value, t2.value)
self.assertEqual(t1.get_extra(), t2.get_extra())
self.assertTrue(
challenge.basics.adjacent(t1.get_secret(), t2.get_secret())
)
class FloatThingamajigTestCase(ThingamajigTestMixin, unittest.TestCase):
"""Test case for for Thingamajig<double> bindings.
All tests also require Doodad to be fully wrapped, and most require the
standard constructor to be wrapped to support at least positional arguments.
"""
def setUp(self):
self.extra = 0.5
self.dtype = float
def test_types(self):
"""Test the inheritance relationships and dtype status of Thingamajig.
"""
t = challenge.extensions.Thingamajig(-0.5, "a", 0, dtype=self.dtype)
self.assertIsInstance(t, challenge.basics.Doodad)
self.assertIsInstance(t, challenge.extensions.Thingamajig)
# Since "numpy.dtype(float) == float", we're not requiring
# the more restrictive "t.dtype is float".
self.assertEqual(t.dtype, float)
class DoodadThingamajigTestCase(ThingamajigTestMixin, unittest.TestCase):
"""Test case for for Thingamajig<Doodad> bindings.
All tests also require Doodad to be fully wrapped, and most require the
standard constructor to be wrapped to support at least positional arguments.
"""
def compare_Doodads(self, a, b, msg=None):
if a.name != b.name or a.value != b.value:
if msg is not None:
template = "({a.name}, {a.value}) != ({b.name}, {b.value})"
msg = template.format(a=a, b=b)
self.failureException(msg)
def __init__(self, *args, **kwds):
super(DoodadThingamajigTestCase, self).__init__(*args, **kwds)
self.addTypeEqualityFunc(challenge.basics.Doodad, self.compare_Doodads)
def setUp(self):
self.extra = challenge.basics.Doodad("asdf", 120)
self.dtype = challenge.basics.Doodad
def test_types(self):
"""Test the inheritance relationships and dtype status of Thingamajig.
"""
t = challenge.extensions.Thingamajig(
self.extra, "a", 0,
dtype=self.dtype
)
self.assertIsInstance(t, challenge.basics.Doodad)
self.assertIsInstance(t, challenge.extensions.Thingamajig)
self.assertEqual(t.dtype, challenge.basics.Doodad)
if __name__ == "__main__":
unittest.main()