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
|
import copy
import unittest
from saneterm.color import Color, ColorType
from saneterm.pty import PositionedIterator
from gi.repository import Gdk
TEST_STRING = 'foo;bar'
class TestPositionedIterator(unittest.TestCase):
def test_lossless(self):
it = PositionedIterator(TEST_STRING)
self.assertEqual([x for x in it], list(TEST_STRING))
self.assertEqual(it.wrapped, TEST_STRING)
def test_indices(self):
it = PositionedIterator(TEST_STRING)
self.assertEqual(it.pos, -1)
for x in it:
assert x == TEST_STRING[it.pos]
if x == ';':
break
self.assertEqual(it.pos, 3)
for x in it:
self.assertEqual(x, it.wrapped[it.pos])
self.assertTrue(it.empty())
def test_backtracking(self):
it = PositionedIterator(TEST_STRING)
semicolon_index = None
for x in it:
if x == ';':
it.waypoint()
semicolon_index = it.pos
self.assertEqual(semicolon_index, TEST_STRING.index(';'))
self.assertTrue(it.empty())
with self.assertRaises(StopIteration):
_ = it.next()
it.backtrack()
self.assertEqual(it.next(), ';')
self.assertEqual(it.pos, semicolon_index)
def test_takewhile(self):
it = PositionedIterator(TEST_STRING)
s = it.takewhile_greedy(lambda x: x != ';')
self.assertEqual(s, TEST_STRING.split(';')[0])
self.assertEqual(it.pos, len(s) - 1)
self.assertEqual(it.next(), ';')
def test_empty(self):
it = PositionedIterator(TEST_STRING)
for x in it:
if it.pos + 1 == len(TEST_STRING):
self.assertTrue(it.empty())
self.assertTrue(it.empty())
with self.assertRaises(StopIteration):
_ = it.next()
def test_take(self):
length = 3
it1 = PositionedIterator(TEST_STRING)
it2 = PositionedIterator(TEST_STRING)
s1 = it1.take(length)
s2 = ''
for x in it2:
if it2.pos >= length:
break
else:
s2 += x
self.assertEqual(s1, s2)
self.assertEqual(s1, TEST_STRING[0:length])
# using take does not consume the next element!
self.assertEqual(it1.pos, length - 1)
class TestColor(unittest.TestCase):
def test_256_colors(self):
"""
Check divmod based RGB value calculation against
256 color table generation as implemented in
XTerm's 256colres.pl.
"""
def channel_val(c):
return (c * 40 + 55 if c > 0 else 0) / 255
for r in range(6):
for g in range(6):
for b in range(6):
n = 16 + (r * 36) + (g * 6) + b
expected = Gdk.RGBA(*map(channel_val, (r, g, b)))
col = Color(ColorType.NUMBERED_256, n).to_gdk()
self.assertTrue(
expected.equal(col),
'Color {}: expected: {}; got: {}'.format(
n, expected.to_string(), col.to_string()
)
)
if __name__ == '__main__':
unittest.main()
|