-
Notifications
You must be signed in to change notification settings - Fork 58
/
test_block.py
214 lines (145 loc) · 4.22 KB
/
test_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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
import pytest
from .. import ipythonblocks
@pytest.fixture
def basic_block():
return ipythonblocks.Block(5, 6, 7, size=20)
def test_basic_api(basic_block):
"""
Check that inputs are going to the right attributes and that assignment
works when it should and not when it shouldn't.
"""
bb = basic_block
assert bb.rgb == (5, 6, 7)
assert bb.red == 5
bb.red = 1
assert bb.red == 1
assert bb.green == 6
bb.green = 2
assert bb.green == 2
assert bb.blue == 7
bb.blue = 3
assert bb.blue == 3
assert bb.rgb == (1, 2, 3)
assert bb.size == 20
bb.size = 10
assert bb.size == 10
assert bb.row is None
with pytest.raises(AttributeError):
bb.row = 5
assert bb.col is None
with pytest.raises(AttributeError):
bb.col = 5
def test_attribute_limits(basic_block):
"""
Color and size attributes have some builtin limits, test that they
are respected.
"""
bb = basic_block
bb.red = -50
assert bb.red == 0
bb.green = 1000
assert bb.green == 255
bb.size = -200
assert bb.size == ipythonblocks._SMALLEST_BLOCK
def test_check_value(basic_block):
"""
Test the Block._check_value method that enforces color limits,
converts to int, and checks values are numbers.
"""
bb = basic_block
bb.red = 4.56
assert isinstance(bb.red, int)
assert bb.red == 5
bb.blue = 200.1
assert isinstance(bb.blue, int)
assert bb.blue == 200
with pytest.raises(ipythonblocks.InvalidColorSpec):
bb.green = 'green'
def test_set_colors(basic_block):
"""
Test the Block.set_colors method.
"""
bb = basic_block
bb.set_colors(200, 201, 202)
assert bb.red == 200
assert bb.green == 201
assert bb.blue == 202
def test_rgb_attr(basic_block):
"""
Test out the .rgb attribute.
"""
bb = basic_block
assert bb.rgb == (5, 6, 7)
bb.rgb = (1, 2, 3)
assert bb.rgb == (1, 2, 3)
assert bb._red == 1
assert bb._green == 2
assert bb._blue == 3
with pytest.raises(ValueError):
bb.rgb = (1, 2)
with pytest.raises(ValueError):
bb.rgb = (4, 5, 6, 7, 8)
def test_td(basic_block):
"""
Test the Block._td proerty that returns an HTML table cell.
"""
bb = basic_block
bb._row = 1
bb._col = 2
title = ipythonblocks._TITLE.format(bb._row, bb._col,
bb.red, bb.green, bb.blue)
rgb = ipythonblocks._RGB.format(bb.red, bb.green, bb.blue)
td = ipythonblocks._TD.format(title, bb.size, rgb)
assert bb._td == td
def test_repr_html(basic_block, monkeypatch):
"""
Test the Block._repr_html_ method that returns a single cell HTML table.
"""
from .test_blockgrid import uuid, fake_uuid
bb = basic_block
monkeypatch.setattr(uuid, 'uuid4', fake_uuid)
table = ipythonblocks._TABLE.format(fake_uuid(), 0,
ipythonblocks._TR.format(bb._td))
assert bb._repr_html_() == table
def test_str1(basic_block):
"""
Test the Block.__str__ method used with print.
"""
bb = basic_block
s = os.linesep.join(['Block', 'Color: (5, 6, 7)'])
assert bb.__str__() == s
def test_str2(basic_block):
"""
Test the Block.__str__ method used with print.
"""
bb = basic_block
bb._row = 8
bb._col = 9
s = os.linesep.join(['Block [8, 9]', 'Color: (5, 6, 7)'])
assert bb.__str__() == s
def test_repr(basic_block):
assert repr(basic_block) == "Block(5, 6, 7, size=20)"
def test_eq():
b1 = ipythonblocks.Block(0, 0, 0)
b2 = ipythonblocks.Block(0, 0, 0)
b3 = ipythonblocks.Block(1, 1, 1)
b4 = ipythonblocks.Block(0, 0, 0, size=30)
assert b1 == b1
assert b1 == b2
assert b1 != b3
assert b1 != b4
assert b1 != 42
def test_hash(basic_block):
with pytest.raises(TypeError):
set([basic_block])
def test_update():
b1 = ipythonblocks.Block(0, 0, 0)
b2 = ipythonblocks.Block(1, 1, 1, size=30)
b1._update((42, 42, 42))
assert b1.rgb == (42, 42, 42)
b1._update(b2)
assert b1.rgb == b2.rgb
assert b1.size == b2.size
with pytest.raises(ValueError):
b1._update((1, 2, 3, 4))