OILS
/
ysh
/
builtin_oil_test.py
1 |
#!/usr/bin/env python2
|
2 |
"""builtin_oil_test.py: Tests for builtin_oil.py."""
|
3 |
from __future__ import print_function
|
4 |
|
5 |
import unittest
|
6 |
|
7 |
from mycpp.mylib import log
|
8 |
#from ysh import builtin_oil # module under test
|
9 |
|
10 |
import yajl # test this too
|
11 |
|
12 |
|
13 |
class YajlTest(unittest.TestCase):
|
14 |
def testMisc(self):
|
15 |
print(yajl.dumps({'foo': 42}))
|
16 |
|
17 |
# Gives us unicode back
|
18 |
print(yajl.loads('{"bar": 43}'))
|
19 |
|
20 |
def testIntOverflow(self):
|
21 |
log('OVERFLOW')
|
22 |
CASES = [
|
23 |
0,
|
24 |
2**31,
|
25 |
2**32,
|
26 |
2**64 - 1,
|
27 |
2**64,
|
28 |
2**128,
|
29 |
]
|
30 |
for i in CASES:
|
31 |
print('--')
|
32 |
|
33 |
# This raises Overflow? I guess the problem is that yajl takes an
|
34 |
# integer.
|
35 |
#print(yajl.dumps(i))
|
36 |
s = str(i)
|
37 |
print(s)
|
38 |
|
39 |
print(yajl.loads('{"k": %d}' % i))
|
40 |
|
41 |
# Why doesn't it parse raw integers?
|
42 |
#print(yajl.loads(s))
|
43 |
|
44 |
log('')
|
45 |
|
46 |
def testParseError(self):
|
47 |
if 0:
|
48 |
yajl.loads('[')
|
49 |
|
50 |
def testBool(self):
|
51 |
log('BOOL')
|
52 |
print(yajl.dumps(True))
|
53 |
print(yajl.loads('false'))
|
54 |
log('')
|
55 |
|
56 |
def testInt(self):
|
57 |
log('INT')
|
58 |
encoded = yajl.dumps(123)
|
59 |
print('encoded = %r' % encoded)
|
60 |
self.assertEqual('123', encoded)
|
61 |
|
62 |
# Bug fix over latest version of py-yajl: a lone int decodes
|
63 |
decoded = yajl.loads('123\n')
|
64 |
print('decoded = %r' % decoded)
|
65 |
self.assertEqual(123, decoded)
|
66 |
|
67 |
decoded = yajl.loads('{"a":123}\n')
|
68 |
print('decoded = %r' % decoded)
|
69 |
log('')
|
70 |
|
71 |
def testFloat(self):
|
72 |
log('FLOAT')
|
73 |
print(yajl.dumps(123.4))
|
74 |
|
75 |
# Bug fix over latest version of py-yajl: a lone float decodes
|
76 |
decoded = yajl.loads('123.4')
|
77 |
self.assertEqual(123.4, decoded)
|
78 |
log('')
|
79 |
|
80 |
def testList(self):
|
81 |
log('LIST')
|
82 |
print(yajl.dumps([4, "foo", False]))
|
83 |
print(yajl.loads('[4, "foo", false]'))
|
84 |
log('')
|
85 |
|
86 |
def testDict(self):
|
87 |
log('DICT')
|
88 |
d = {"bool": False, "int": 42, "float": 3.14, "string": "s"}
|
89 |
print(yajl.dumps(d))
|
90 |
|
91 |
s = '{"bool": false, "int": 42, "float": 3.14, "string": "s"}'
|
92 |
print(yajl.loads(s))
|
93 |
log('')
|
94 |
|
95 |
def testStringEncoding(self):
|
96 |
log('STRING ENCODE')
|
97 |
|
98 |
# It should just raise with Unicode instance
|
99 |
#print(yajl.dumps(u'abc\u0100def'))
|
100 |
|
101 |
# It inserts \xff literally, OK I guess that's fine. It's not valid utf-8
|
102 |
print(yajl.dumps('\x00\xff'))
|
103 |
|
104 |
# mu character
|
105 |
print(yajl.dumps('\xCE\xBC'))
|
106 |
|
107 |
def testStringDecoding(self):
|
108 |
log('STRING DECODE')
|
109 |
|
110 |
# This should decode to a utf-8 str()!
|
111 |
# Not a unicode instance!
|
112 |
|
113 |
s = yajl.loads('"abc"')
|
114 |
print(repr(s))
|
115 |
|
116 |
obj = yajl.loads('"\u03bc"')
|
117 |
assert isinstance(obj, str), repr(obj)
|
118 |
self.assertEqual(obj, '\xce\xbc')
|
119 |
|
120 |
obj = yajl.loads('"\xce\xbc"')
|
121 |
assert isinstance(obj, str), repr(obj)
|
122 |
self.assertEqual(obj, '\xce\xbc')
|
123 |
|
124 |
# Invalid utf-8. Doesn't give a good parse error!
|
125 |
if 0:
|
126 |
u = yajl.loads('"\xFF"')
|
127 |
print(repr(u))
|
128 |
|
129 |
def testOrdered(self):
|
130 |
from collections import OrderedDict
|
131 |
|
132 |
# Order is lost
|
133 |
d1 = OrderedDict({'a': 1, 'b': 2, 'c': 3})
|
134 |
|
135 |
# Preserved
|
136 |
d = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
|
137 |
d['a'] = 42
|
138 |
d['d'] = 50
|
139 |
|
140 |
#d = OrderedDict([('z', 1), ('y', 2), ('x', 3)])
|
141 |
#d['a'] = 42
|
142 |
#d['z'] = 50
|
143 |
|
144 |
actual = yajl.dumps(d)
|
145 |
self.assertEqual('{"a":42,"b":2,"c":3,"d":50}', actual)
|
146 |
|
147 |
#
|
148 |
# More tests in py-yajl/tests/unit.py
|
149 |
#
|
150 |
|
151 |
|
152 |
if __name__ == '__main__':
|
153 |
unittest.main()
|