forked from edgeware/structprop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
109 lines (86 loc) · 2.78 KB
/
test.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
import unittest
from structprop import loads, dumps, ParserError
def handler(stmt, token, context):
if context == 'value':
return [token + '.value']
return {token: 'augmented'}
class ParserTestCase(unittest.TestCase):
def test_parser_error_on_missing_term(self):
self.assertRaises(ParserError, loads, '{ = 10')
def test_key_value(self):
result = loads('key = value')
self.assertEquals(result['key'], 'value')
def test_inline_comment(self):
result = loads('key = value #comment')
self.assertEquals(result['key'], 'value')
def test_augment_object(self):
result = loads('!include foo #comment', handler)
self.assertTrue('foo' in result)
self.assertEquals(result['foo'], 'augmented')
def test_augment_value(self):
result = loads('a = { !include foo }', handler)
self.assertTrue(u'foo.value' in result['a'])
def test_quoted_value(self):
result = loads('key = "a#comment{}=#"')
self.assertEquals(result['key'], 'a#comment{}=#')
def test_quoted_key(self):
result = loads('"key abc" = value')
self.assertEquals(result['key abc'], 'value')
def test_empty_object(self):
result = loads("a {}")
self.assertEquals(result['a'], {})
def test_object_key_value(self):
result = loads("a { key = value }")
self.assertEquals(result['a']['key'], 'value')
def test_comment_before_data(self):
result = loads(u"#xxx\na { key = value }")
self.assertEquals(result['a']['key'], 'value')
def test_nested_objects(self):
result = loads(u"a = { { b = c } { d = e } def abc }")
self.assertEquals(len(result), 1)
self.assertEquals(len(result['a']), 4)
self.assertEquals(result['a'][2], 'def')
self.assertEquals(result['a'][3], 'abc')
def test_true_bool(self):
result = loads('a = true')
self.assertTrue(result['a'])
def test_false_bool(self):
result = loads('a = false')
self.assertFalse(result['a'])
def test_dump_list(self):
data = dumps({'a': ['a', 'b', 'c']})
self.assertEquals(data, """\
a = {
a
b
c
}
""")
def test_dump_dict(self):
data = dumps({'a': {'d': 1}})
loads(data)
self.assertEquals(data, """\
a {
d = 1
}
""")
def test_dump_true_bool(self):
data = dumps({'a': True})
loads(data)
self.assertEquals(data, """\
a = true
""")
def test_dump_false_bool(self):
data = dumps({'a': False})
loads(data)
self.assertEquals(data, """\
a = false
""")
def test_escape_space(self):
data = dumps({'a b': 1})
loads(data)
self.assertEquals(data, """\
"a b" = 1
""")
if __name__ == '__main__':
unittest.main()