forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_oil_test.py
executable file
·153 lines (114 loc) · 3.73 KB
/
builtin_oil_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
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
#!/usr/bin/env python2
"""builtin_oil_test.py: Tests for builtin_oil.py."""
from __future__ import print_function
import unittest
from mycpp.mylib import log
#from ysh import builtin_oil # module under test
import yajl # test this too
class YajlTest(unittest.TestCase):
def testMisc(self):
print(yajl.dumps({'foo': 42}))
# Gives us unicode back
print(yajl.loads('{"bar": 43}'))
def testIntOverflow(self):
log('OVERFLOW')
CASES = [
0,
2**31,
2**32,
2**64 - 1,
2**64,
2**128,
]
for i in CASES:
print('--')
# This raises Overflow? I guess the problem is that yajl takes an
# integer.
#print(yajl.dumps(i))
s = str(i)
print(s)
print(yajl.loads('{"k": %d}' % i))
# Why doesn't it parse raw integers?
#print(yajl.loads(s))
log('')
def testParseError(self):
if 0:
yajl.loads('[')
def testBool(self):
log('BOOL')
print(yajl.dumps(True))
print(yajl.loads('false'))
log('')
def testInt(self):
log('INT')
encoded = yajl.dumps(123)
print('encoded = %r' % encoded)
self.assertEqual('123', encoded)
# Bug fix over latest version of py-yajl: a lone int decodes
decoded = yajl.loads('123\n')
print('decoded = %r' % decoded)
self.assertEqual(123, decoded)
decoded = yajl.loads('{"a":123}\n')
print('decoded = %r' % decoded)
log('')
def testFloat(self):
log('FLOAT')
print(yajl.dumps(123.4))
# Bug fix over latest version of py-yajl: a lone float decodes
decoded = yajl.loads('123.4')
self.assertEqual(123.4, decoded)
log('')
def testList(self):
log('LIST')
print(yajl.dumps([4, "foo", False]))
print(yajl.loads('[4, "foo", false]'))
log('')
def testDict(self):
log('DICT')
d = {"bool": False, "int": 42, "float": 3.14, "string": "s"}
print(yajl.dumps(d))
s = '{"bool": false, "int": 42, "float": 3.14, "string": "s"}'
print(yajl.loads(s))
log('')
def testStringEncoding(self):
log('STRING ENCODE')
# It should just raise with Unicode instance
#print(yajl.dumps(u'abc\u0100def'))
# It inserts \xff literally, OK I guess that's fine. It's not valid utf-8
print(yajl.dumps('\x00\xff'))
# mu character
print(yajl.dumps('\xCE\xBC'))
def testStringDecoding(self):
log('STRING DECODE')
# This should decode to a utf-8 str()!
# Not a unicode instance!
s = yajl.loads('"abc"')
print(repr(s))
obj = yajl.loads('"\u03bc"')
assert isinstance(obj, str), repr(obj)
self.assertEqual(obj, '\xce\xbc')
obj = yajl.loads('"\xce\xbc"')
assert isinstance(obj, str), repr(obj)
self.assertEqual(obj, '\xce\xbc')
# Invalid utf-8. Doesn't give a good parse error!
if 0:
u = yajl.loads('"\xFF"')
print(repr(u))
def testOrdered(self):
from collections import OrderedDict
# Order is lost
d1 = OrderedDict({'a': 1, 'b': 2, 'c': 3})
# Preserved
d = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
d['a'] = 42
d['d'] = 50
#d = OrderedDict([('z', 1), ('y', 2), ('x', 3)])
#d['a'] = 42
#d['z'] = 50
actual = yajl.dumps(d)
self.assertEqual('{"a":42,"b":2,"c":3,"d":50}', actual)
#
# More tests in py-yajl/tests/unit.py
#
if __name__ == '__main__':
unittest.main()