forked from jsymons/pyp-u1-c1-collections-hierarchy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
90 lines (63 loc) · 2 KB
/
tests.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
import pytest
from collections_hierarchy.node import Node
from collections_hierarchy.concrete_classes import List, Stack
from collections_hierarchy.base_classes import (
Sequenceable, Appendable, Pushable, Popable)
def test_sequenceable():
n1 = Node('X')
n2 = Node('Y')
n3 = Node('Z')
n1.next = n2
n2.next = n3
sequence = Sequenceable()
assert sequence.get_elements() == []
# Warning. Violating encapsulation
sequence.start = n1
assert sequence.get_elements() == ['X', 'Y', 'Z']
def test_appendable():
class TestAppendableSequence(Sequenceable, Appendable):
pass
sequence = TestAppendableSequence()
assert sequence.get_elements() == []
sequence.append('X')
assert sequence.get_elements() == ['X']
sequence.append('Y')
assert sequence.get_elements() == ['X', 'Y']
sequence.append('Z')
assert sequence.get_elements() == ['X', 'Y', 'Z']
def test_popable():
class TestPopableSequence(Sequenceable, Popable):
pass
sequence = TestPopableSequence()
with pytest.raises(IndexError):
sequence.pop()
n1 = Node('X')
n2 = Node('Y')
n3 = Node('Z')
n1.next = n2
n2.next = n3
sequence.start = n1
# Preconditions
assert sequence.get_elements() == ['X', 'Y', 'Z']
elem = sequence.pop()
assert elem == 'X'
assert sequence.get_elements() == ['Y', 'Z']
elem = sequence.pop()
assert elem == 'Y'
assert sequence.get_elements() == ['Z']
elem = sequence.pop()
assert elem == 'Z'
assert sequence.get_elements() == []
with pytest.raises(IndexError):
sequence.pop()
def test_pushable():
class TestPushableSequence(Sequenceable, Pushable):
pass
sequence = TestPushableSequence()
assert sequence.get_elements() == []
sequence.push('C')
assert sequence.get_elements() == ['C']
sequence.push('B')
assert sequence.get_elements() == ['B', 'C']
sequence.push('A')
assert sequence.get_elements() == ['A', 'B', 'C']