-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_space.py
209 lines (163 loc) · 7.06 KB
/
test_space.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
from unittest import TestCase
from space import Space
__author__ = 'tesse'
class TestSpace(TestCase):
def test_space_exist(self):
self.assertTrue(isinstance(Space(), Space), 'Space should exist')
def test_index_of(self):
space = Space("Hello World")
self.assertEqual(space.length(), 1)
self.assertEqual(space.index_of('Hello'), 0, 'indexOf should be correct')
def test_get_set(self):
space = Space('Hello World')
self.assertEqual(space.get("Hello"), 'World', 'Property should be accessible')
self.assertEqual(type(space.get("Hello")), str, 'Leaf should be string')
space.set('foo', 'bar')
self.assertEqual(space.get('foo'), 'bar', 'Space should be modifiable' )
def test_nest_space(self):
space = Space('foobar\n one 1')
self.assertTrue(isinstance(space.get('foobar'), Space), 'Nested space should be Space type')
def test_key_not_exist(self):
space = Space('foobar\n leaf 1')
self.assertEqual(space.get('Hi'), None, 'Should return None if key does not exist')
def test_path_not_exist(self):
space = Space('Hello\n one 1\n two 2')
self.assertEqual(space.get('Hello world one'), None, 'Should return None if path does not exist')
def test_length(self):
space = Space('list\nsingle value')
self.assertEqual(space.length(), 2, 'space should have 2 names') # 'name' here means 'key'
self.assertTrue(isinstance(space.get('list'), Space), 'name without a trailing space should be name')
# skipped a couple of tests here, don't think can create from object literal in python
def test_multi_line(self):
string = 'user\n\
name Aristotle\n\
admin false\n\
stage\n\
name home\n\
domain test.test.com\n\
pro false\n\
domains\n\
test.test.com\n\
images\n\
blocks\n\
users\n\
stage home\n\
pages\n\
home\n\
settings\n\
data\n\
title Hello, World\n\
block1\n\
content Hello world\n'
space = Space(string)
self.assertEqual(space.get('domains test.test.com pages home settings data title'), 'Hello, World', 'Multiline creation shuold be OK')
def test_each(self):
space = Space('hello world\nhi mom')
string = ''
self.assertEqual(space.each(lambda (k, v): k), ['hello', 'hi'])
def test_prepend(self):
space = Space('hello world')
space.prepend('first', 'word')
self.assertEqual(space.__str__(), 'first word\nhello world\n')
def test_on_off(self):
space = Space('hello world')
test_fun = lambda: 'test'
space.on('test', test_fun)
self.assertIn('test', space._Space__events)
space.off('test', test_fun)
self.assertEqual(space._Space__events['test'], [])
def test_trigger(self):
space = Space('hello world')
class Count():
def __init__(self):
self.count = 0
def incr(self):
self.count += 1
c = Count()
space.on('change', c.incr)
space.trigger('change')
self.assertEqual(c.count, 1)
class TestAppend(TestCase): # haven't done the 'on append incre count' thing as in js verion
def test_append(self):
space = Space('hello world')
space.append('foo', 'bar')
space.set('foo2', 'bar')
space.append('foo', 'two')
self.assertEqual(space.get('foo'), 'bar')
self.assertEqual(space.length(), 4)
class TestClear(TestCase):
def test_clear(self):
space = Space('Hello world')
self.assertEqual(space.length(), 1)
self.assertTrue(isinstance(space.clear(), Space), 'clear returns Space so chainable')
self.assertEqual(space.length(), 0)
def test_clear_with_space_argument(self):
space = Space("Hellow world")
space.clear("hey there")
self.assertEqual(space.length(), 1)
self.assertEqual(space.get('hey'), 'there', 'Clear with a Space argument should deep copy the argument Space')
class TestClone(TestCase):
def testDeepCopy(self):
space1 = Space('hello world')
space2 = space1.clone()
self.assertEqual(space2.get('hello'), 'world', 'Clone should work')
space2.set('hello', 'mom')
self.assertEqual(space1.get('hello'), 'world', 'Clone makes deep copy')
space3 = space1
space3.set('hello', 'dad')
self.assertEqual(space1.get('hello'), 'dad', '= makes shallow copy')
space1.set('anotherSpace', Space('123 456'))
self.assertEqual(space3.get('anotherSpace 123'), '456')
class TestConcat(TestCase):
def testConcat(self):
space1 = Space('hi world')
b = Space('hello there')
space1.concat(b)
self.assertEqual(space1.get('hello'), 'there')
class TestMultiline(TestCase):
def test_multiline(self):
space = Space('my multiline\n string')
self.assertEqual(space.get('my'), 'multiline\nstring')
space2 = Space('my \n \n multiline\n string')
self.assertEqual(space2.get('my'), '\n\nmultiline\nstring')
space3 = Space('brave new\n world')
self.assertEqual(space3.get('brave'), 'new\nworld', 'ml value correct')
self.assertEqual(str(space3), 'brave new\n world\n', 'multiline does not begin with nl')
space4 = Space('brave \n new\n world')
self.assertEqual(space4.get('brave'), '\nnew\nworld', 'ml begin with nl value correct')
class TestToStr(TestCase):
def test__str__(self):
space = Space("hello world")
self.assertEqual(space.__str__(), 'hello world\n')
# ordered
space.set('foo', 'bar')
self.assertEqual(space.__str__(), 'hello world\nfoo bar\n')
# ordered
space = Space('hello world\nline two')
self.assertEqual(space.__str__(), 'hello world\nline two\n')
space2 = Space('john\n age 5')
self.assertEqual(space2.__str__(), 'john\n age 5\n')
space2.set('multiline', 'hello\nworld')
self.assertEqual(space2.__str__(), 'john\n age 5\nmultiline hello\n world\n')
space2.set('other', 'foobar')
self.assertEqual(space2.__str__(), 'john\n age 5\nmultiline hello\n world\nother foobar\n')
class TestSet(TestCase):
def test_set(self):
space = Space('hello world')
self.assertEqual(space.get('hello'), 'world')
self.assertTrue(isinstance(space.set('hello', 'mom'), Space), 'set returns self for chaining')
intSpace = Space()
intSpace.set(2, 'hi')
self.assertEqual(intSpace.get(2), 'hi')
emptyStringSpace = Space()
emptyStringSpace.set('boom', '')
self.assertEqual(emptyStringSpace.get('boom'), '')
# this is important, value should have been changed
self.assertEqual(space.get('hello'), 'mom')
space.set('head style color', 'blue')
self.assertEqual(space.get('head style color'), 'blue')
class TestHas(TestCase):
def test_has(self):
space = Space('hello world')
self.assertTrue(space.has('hello'))
self.assertFalse(space.has('world'))