-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_reflection.py
More file actions
37 lines (27 loc) · 870 Bytes
/
test_reflection.py
File metadata and controls
37 lines (27 loc) · 870 Bytes
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
import unittest
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
class PointClass:
def __init__(self, x, y):
self.x = x
self.y = y
class TestReflection(unittest.TestCase):
def setUp(self):
self.p = Point(9, 15)
self.pc = PointClass(23, 4)
self.all = [self.p, self.pc]
def test_attribute_checking(self):
for item in self.all:
self.assertTrue(hasattr(item, 'x'))
self.assertTrue(hasattr(item, 'y'))
self.assertFalse(hasattr(item, 'b'))
def test_attribute_reading(self):
for item in self.all:
self.assertEqual(getattr(item, 'x'), item.x)
self.assertEqual(getattr(item, 'y'), item.y)
def test_attribute_setting(self):
setattr(self.pc, 'b', 3)
self.assertTrue(hasattr(self.pc, 'b'))
self.assertEqual(getattr(self.pc, 'b'), 3)
setattr(self.pc, 'y', 14)
self.assertEqual(getattr(self.pc, 'y'), 14)