forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathj8_test.py
executable file
·229 lines (190 loc) · 6.17 KB
/
j8_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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python2
from __future__ import print_function
import unittest
from _devbuild.gen.syntax_asdl import Id, Id_str
from core import error
from data_lang import j8
from mycpp.mylib import log
class FunctionsTest(unittest.TestCase):
def testUtf8Encode(self):
CASES = [
(u'\u0065'.encode('utf-8'), 0x0065),
(u'\u0100'.encode('utf-8'), 0x0100),
(u'\u1234'.encode('utf-8'), 0x1234),
(u'\U00020000'.encode('utf-8'), 0x00020000),
# Out of range is checked elsewhere
#('\xef\xbf\xbd', 0x10020000),
]
for expected, code_point in CASES:
print('')
print('Utf8Encode case %r %r' % (expected, code_point))
self.assertEqual(expected, j8.Utf8Encode(code_point))
def _PrintTokens(lex):
log('---')
log('J8 Case %r', lex.s)
pos = 0
while True:
id_, end_pos, decoded = lex.Next()
val = lex.s[pos:end_pos]
d = repr(decoded) if decoded is not None else '-'
log(' %20s %15r %15s', Id_str(id_), val, d)
if id_ == Id.Eol_Tok:
break
pos = end_pos
log('')
class Nil8Test(unittest.TestCase):
def testNil8Errors(self):
cases = [
#'()',
#'(42)',
#'(:)',
# extra input
'(command.Simple))',
'(',
'(obj.field',
'(obj.',
# Expected a value afterward
'(obj.)',
'(obj.+',
'(obj.+)',
'(obj.[)',
]
for s in cases:
p = j8.Nil8Parser(s, True)
try:
obj = p.ParseNil8()
except error.Decode as e:
print(e)
else:
self.fail('Expected error.Decode when parsing %r' % s)
def testNil8Operator(self):
# Should be equivalent!
cases = [
# These are equivalent, note that parens like (key: "value") add another layer
('(: key "value")', 'key: "value"'),
('((: k "v") (: k2 "v2"))', '(k: "v" k2: "v2")'),
('(@ "str" x123)', '"str" @ x123'),
('((! a b) c)', '( a ! b c)'),
('(c (! a b))', '( c a ! b )'),
('(. (. obj field1) field2)', 'obj.field1.field2'),
('((-> obj method) (. obj field))', '(obj->method obj.field1)'),
]
for prefix, infix in cases:
print()
print('PREFIX %s' % prefix)
p = j8.Nil8Parser(prefix, True)
obj1 = p.ParseNil8()
print(obj1)
log('len %d', len(obj1.items))
print()
print('INFIX %s' % infix)
p = j8.Nil8Parser(infix, True)
obj2 = p.ParseNil8()
print(obj2)
log('len %d', len(obj2.items))
self.assertEqual(obj1.tag(), obj2.tag(),
'%s != %s' % (obj1.tag(), obj2.tag()))
self.assertEqual(len(obj1.items), len(obj2.items))
def testNil8(self):
cases = [
'(unquoted)',
'(command.Simple)',
'(f x)',
'(f 42 "hi")',
'((-> obj method) (. obj field))',
# address
'(@ x123 (Token "foo"))',
'(: key "value")',
'(. x123)', # dereference, could be @
#'(Token "foo") @ x123',
# TODO: parse like infix
'(Token key:"value" k2:"v2")',
# Should be parsed like infix operator
'(key !x123)',
'(<-)', # symbol
"(<- 1 b'hi')", # any kinds of args
"(<- 1 'hi' (f [1 2 3]))", # symbol
'[]',
'[42]',
'[42 43]',
'[42 ["a" "b"]]',
'42',
'"hi"',
]
for s in cases:
p = j8.Nil8Parser(s, True)
obj = p.ParseNil8()
print(s)
print(' %s' % obj)
print()
class J8Test(unittest.TestCase):
def testJ8(self):
s = '{}'
p = j8.Parser(s, True)
obj = p.ParseValue()
print(obj)
def testLexerDecoder(self):
lex = j8.LexerDecoder(r'{"hi": "bye \n"}', True, 'J8')
_PrintTokens(lex)
lex = j8.LexerDecoder(r"{u'unicode': b'bytes \y1f \yff'}", True, 'J8')
_PrintTokens(lex)
lex = j8.LexerDecoder(
r'{"mu \u03BC \u0001":' + r"b'mu \u{03bc} \u{2620}'", True, 'J8')
_PrintTokens(lex)
lex = j8.LexerDecoder(r'{"x": [1, 2, 3.14, true]}', True, 'J8')
_PrintTokens(lex)
lex = j8.LexerDecoder(
r'''
[
1e9, 1e-9, -1e9, -1E-9, 42
]
''', True, 'J8')
_PrintTokens(lex)
try:
lex = j8.LexerDecoder('"\x01"', True, 'J8')
_PrintTokens(lex)
except error.Decode as e:
print(e)
else:
self.fail('Expected failure')
try:
lex = j8.LexerDecoder('"\x1f"', True, 'J8')
_PrintTokens(lex)
except error.Decode as e:
print(e)
else:
self.fail('Expected failure')
def testMoreTokens(self):
cases = [
'true # comment',
'truez8 # identifier',
'truez8> # symbol',
'(<= 1 3) # less than',
# Can allow hex identifiers here like 123
'(<- 123 (Token))',
'(Node left:(-> 123))',
]
for s in cases:
lex = j8.LexerDecoder(s, True, 'J8')
_PrintTokens(lex)
def testErrorMessagePosition(self):
lex = j8.LexerDecoder("[ u'hi']", False, 'J8')
try:
_PrintTokens(lex)
except error.Decode as e:
print(e)
self.assertEquals(2, e.start_pos)
self.assertEquals(4, e.end_pos)
else:
self.fail('Expected failure')
class YajlTest(unittest.TestCase):
"""
Note on old tests for YAJL. Differences
- It decoded to Python 2 str() type, not unicode()
- Bug in emitting \\xff literally, which is not valid JSON
- turns out there is a C level option for this
"""
pass
if __name__ == '__main__':
unittest.main()
# vim: sw=4