-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsimple_block.py
executable file
·93 lines (74 loc) · 2.85 KB
/
simple_block.py
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
#!/usr/bin/env python
'''module that defines the Block class'''
import unittest
class Block():
'''Class representing a data block, a block has a name, a description,
a data type, and its data'''
def __init__(self, name):
'''constructor, takes the block's name as an argument'''
self._name = name
self._data = []
def __repr__(self):
'''create a string representation of the block'''
header = 'block {0}:'.format(self.get_name())
data = '\n\t'.join([str(item) for item in self._data])
return '{0}\n\t{1}'.format(header, data)
def __str__(self):
'''create a string of the block'''
header = 'begin {0}'.format(self.get_name())
data = '\n\t'.join([str(item) for item in self._data])
footer = 'end {0}'.format(self.get_name())
return '{0}\n\t{1}\n{2}'.format(header, data, footer)
def get_name(self):
'''returns the block's name'''
return self._name
def add_data(self, data):
'''given a data item, add it to the block's data'''
self._data.append(data)
def sort_data(self):
'''sort the block's data'''
self._data.sort()
def get_data(self):
'''returns the data, sorted'''
return self._data
# unit test implementation for Block class
class BlockTest(unittest.TestCase):
'''Tests for the block class'''
def test_constructor(self):
'''create an object, tests its name'''
name = 'test name'
block = Block(name)
self.assertIsNotNone(block)
self.assertEqual(block.get_name(), name)
def test_repr(self):
'''create a Block object, add some data, check its representation'''
name = 'test_name'
data = ['gamma', 'alpha', 'beta']
data_repr = '\n\t'.join([str(item) for item in data])
target = 'block {0}:\n\t{1}'.format(name, data_repr)
block = Block(name)
for item in data:
block.add_data(item)
self.assertEqual(repr(block), target)
def test_str(self):
'''create a Block object, add some data, check its representation'''
name = 'test_name'
data = ['gamma', 'alpha', 'beta']
data_repr = '\n\t'.join([str(item) for item in data])
target = 'begin {0}\n\t{1}\nend {0}'.format(name, data_repr)
block = Block(name)
for item in data:
block.add_data(item)
self.assertEqual(str(block), target)
def test_data_ordered(self):
'''create a Block object, add some data, check whether the data
is ordered upon return'''
name = 'test_name'
data = ['gamma', 'alpha', 'beta']
block = Block(name)
for item in data:
block.add_data(item)
block.sort_data()
self.assertEqual(block.get_data(), sorted(data))
if __name__ == '__main__':
unittest.main()