Skip to content

Commit 0aec4da

Browse files
committed
Tests and conformance for Arrays.
1 parent 38b49e1 commit 0aec4da

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

jsonfsm.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ def array_fsm():
217217
if c != '[':
218218
raise JSONParseError("Arrays must begin with [")
219219

220-
while c != ']':
220+
c = (yield NOT_PARSED_YET)
221+
while c.isspace():
221222
c = (yield NOT_PARSED_YET)
222-
while c.isspace():
223-
c = (yield NOT_PARSED_YET)
224223

224+
while c != ']':
225225
vp = value_fsm()
226226
value = NOT_PARSED_YET
227227

@@ -233,6 +233,16 @@ def array_fsm():
233233
while c.isspace():
234234
c = (yield NOT_PARSED_YET)
235235

236+
# if we stopped at a , advance until the start of the next value
237+
# (and raise exception if the array ends)
238+
if c == ',':
239+
c = (yield NOT_PARSED_YET)
240+
while c.isspace():
241+
c = (yield NOT_PARSED_YET)
242+
243+
if c == ']':
244+
raise JSONParseError("Extra , before ] in array")
245+
236246
array.append(value)
237247

238248
yield array

test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,30 @@ def test_invalid_numbers(self):
8080
jsonfsm.loads,
8181
invalid)
8282

83+
class TestArray(unittest.TestCase):
84+
def setUp(self):
85+
self.valid = (
86+
('[]', []),
87+
("[1]", [1.0]),
88+
("[ 1, 2, 3]", [1.0, 2.0, 3.0]),
89+
('[ "a string with ]"]', [u"a string with ]"]),
90+
('[ ["nested array"], 1]', [[u"nested array"], 1.0]),
91+
)
92+
93+
self.invalid = (
94+
"[,]",
95+
"[1,]",
96+
)
97+
98+
def test_valid_arrays(self):
99+
for json, expected in self.valid:
100+
self.assertEquals(jsonfsm.loads(json), expected)
101+
102+
def test_invalid_arrays(self):
103+
for json in self.invalid:
104+
self.assertRaises(jsonfsm.JSONParseError,
105+
jsonfsm.loads,
106+
json)
107+
83108
if __name__ == "__main__":
84109
unittest.main()

0 commit comments

Comments
 (0)