forked from faif/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_prototype.py
More file actions
51 lines (39 loc) · 1.98 KB
/
test_prototype.py
File metadata and controls
51 lines (39 loc) · 1.98 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
from creational.prototype import Prototype, PrototypeDispatcher
class TestPrototypeFeatures(unittest.TestCase):
def setUp(self):
self.prototype = Prototype()
def test_cloning_propperty_innate_values(self):
sample_object_1 = self.prototype.clone()
sample_object_2 = self.prototype.clone()
self.assertEqual(sample_object_1.value, sample_object_2.value)
def test_extended_property_values_cloning(self):
sample_object_1 = self.prototype.clone()
sample_object_1.some_value = 'test string'
sample_object_2 = self.prototype.clone()
self.assertRaises(AttributeError, lambda: sample_object_2.some_value)
def test_cloning_propperty_assigned_values(self):
sample_object_1 = self.prototype.clone()
sample_object_2 = self.prototype.clone(value='re-assigned')
self.assertNotEqual(sample_object_1.value, sample_object_2.value)
class TestDispatcherFeatures(unittest.TestCase):
def setUp(self):
self.dispatcher = PrototypeDispatcher()
self.prototype = Prototype()
c = self.prototype.clone()
a = self.prototype.clone(value='a-value', ext_value='E')
b = self.prototype.clone(value='b-value', diff=True)
self.dispatcher.register_object('A', a)
self.dispatcher.register_object('B', b)
self.dispatcher.register_object('C', c)
def test_batch_retrieving(self):
self.assertEqual(len(self.dispatcher.get_objects()), 3)
def test_particular_properties_retrieving(self):
self.assertEqual(self.dispatcher.get_objects()['A'].value, 'a-value')
self.assertEqual(self.dispatcher.get_objects()['B'].value, 'b-value')
self.assertEqual(self.dispatcher.get_objects()['C'].value, 'default')
def test_extended_properties_retrieving(self):
self.assertEqual(self.dispatcher.get_objects()['A'].ext_value, 'E')
self.assertTrue(self.dispatcher.get_objects()['B'].diff)