Skip to content

Commit 59d69a7

Browse files
authored
Added more parsing tests (#6)
1 parent a714f50 commit 59d69a7

File tree

4 files changed

+114
-11
lines changed

4 files changed

+114
-11
lines changed

Tests/JSONParsingTests/ArrayParserTests.swift

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,31 @@ class ArrayParserTests: XCTestCase {
1919
XCTAssertEqual(result, [.bool(true)])
2020
}
2121

22+
func testNestedArrays() throws {
23+
var parser = JSONParserImpl(bytes: [UInt8]("[[]]".utf8))
24+
let _ = try XCTUnwrap(parser.reader.read())
25+
26+
let result = try parser.parseArray()
27+
XCTAssertEqual(result, [.array([])])
28+
}
29+
30+
func testHighlyNestedArray() throws {
31+
// test 512 should succeed
32+
let passingString = String(repeating: "[", count: 512) + String(repeating: "]", count: 512)
33+
_ = try JSONParser().parse(bytes: [UInt8](passingString.utf8))
34+
35+
let failingString = String(repeating: "[", count: 513)
36+
do {
37+
_ = try JSONParser().parse(bytes: [UInt8](failingString.utf8))
38+
}
39+
catch JSONError.tooManyNestedArraysOrDictionaries(characterIndex: 512) {
40+
//expected case
41+
}
42+
catch {
43+
XCTFail("Unexpected error: \(error)")
44+
}
45+
}
46+
2247
func testSimpleTrueAndStringArray() throws {
2348
var parser = JSONParserImpl(bytes: [UInt8](#"[ true , "hello" ]"#.utf8))
2449
let _ = try XCTUnwrap(parser.reader.read())
@@ -27,7 +52,7 @@ class ArrayParserTests: XCTestCase {
2752
XCTAssertEqual(result, [.bool(true), .string("hello")])
2853
}
2954

30-
func testCommaBeforeEnd() throws {
55+
func testCommaBeforeEnd() {
3156
do {
3257
var parser = JSONParserImpl(bytes: [UInt8]("[ true , ]".utf8))
3358
_ = try XCTUnwrap(parser.reader.read())
@@ -42,7 +67,7 @@ class ArrayParserTests: XCTestCase {
4267
}
4368
}
4469

45-
func testInvalidCharacterAfterFirstValue() throws {
70+
func testInvalidCharacterAfterFirstValue() {
4671
do {
4772
var parser = JSONParserImpl(bytes: [UInt8]("[ true asd ]".utf8))
4873
_ = try XCTUnwrap(parser.reader.read())
@@ -57,17 +82,28 @@ class ArrayParserTests: XCTestCase {
5782
}
5883
}
5984

60-
func testHighlyNestedArray() throws {
61-
// test 512 should succeed
62-
let passingString = String(repeating: "[", count: 512) + String(repeating: "]", count: 512)
63-
_ = try JSONParser().parse(bytes: [UInt8](passingString.utf8))
64-
65-
let failingString = String(repeating: "[", count: 513)
85+
func testNumberCommaBeforeEnd() {
6686
do {
67-
_ = try JSONParser().parse(bytes: [UInt8](failingString.utf8))
87+
var parser = JSONParserImpl(bytes: [UInt8]("[ 1 , ]".utf8))
88+
_ = try XCTUnwrap(parser.reader.read())
89+
_ = try parser.parseArray()
90+
XCTFail("this point should not be reached")
6891
}
69-
catch JSONError.tooManyNestedArraysOrDictionaries(characterIndex: 512) {
70-
//expected case
92+
catch JSONError.unexpectedCharacter(ascii: UInt8(ascii: "]")) {
93+
// expected
94+
}
95+
catch {
96+
XCTFail("Unexpected error: \(error)")
97+
}
98+
}
99+
100+
func testIncompleteLiteralInArray() throws {
101+
do {
102+
_ = try JSONParser().parse(bytes: [UInt8]("[ nu ]".utf8))
103+
XCTFail("this point should not be reached")
104+
}
105+
catch JSONError.unexpectedCharacter(ascii: UInt8(ascii: " ")) {
106+
// expected
71107
}
72108
catch {
73109
XCTFail("Unexpected error: \(error)")

Tests/JSONParsingTests/BoolParserTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ class BoolParserTests: XCTestCase {
3434
XCTAssertEqual(remaining, [UInt8(ascii: ",")])
3535
}
3636

37+
func testMiddleCharacterInvalidCase() throws {
38+
var parser = JSONParserImpl(bytes: [UInt8]("faLse,".utf8))
39+
_ = try XCTUnwrap(parser.reader.read())
40+
41+
// rfc8259 §3
42+
// The literal names MUST be lowercase. No other literal names are allowed.
43+
do {
44+
_ = try parser.parseBool()
45+
XCTFail("this point should not be reached")
46+
}
47+
catch JSONError.unexpectedCharacter(ascii: UInt8(ascii: "L")) {
48+
// expected
49+
}
50+
catch {
51+
XCTFail("Unexpected error: \(error)")
52+
}
53+
}
54+
3755
func testInvalidCharacter() throws {
3856
var parser = JSONParserImpl(bytes: [UInt8]("fal67,".utf8))
3957
_ = try XCTUnwrap(parser.reader.read())
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import XCTest
2+
@testable import PureSwiftJSONParsing
3+
4+
class NullParserTests: XCTestCase {
5+
6+
func testSimpleNull() throws {
7+
var parser = JSONParserImpl(bytes: [UInt8]("null".utf8))
8+
_ = try XCTUnwrap(parser.reader.read())
9+
10+
try parser.parseNull()
11+
}
12+
13+
func testMiddleCharacterInvalidCase() throws {
14+
var parser = JSONParserImpl(bytes: [UInt8]("nuLl".utf8))
15+
_ = try XCTUnwrap(parser.reader.read())
16+
17+
do {
18+
try parser.parseNull()
19+
XCTFail("this point should not be reached")
20+
}
21+
catch JSONError.unexpectedCharacter(ascii: UInt8(ascii: "L")) {
22+
// expected
23+
}
24+
catch {
25+
XCTFail("Unexpected error: \(error)")
26+
}
27+
}
28+
}

Tests/JSONParsingTests/ObjectParserTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,25 @@ class ObjectParserTests: XCTestCase {
4444
XCTFail("Unexpected error: \(error)")
4545
}
4646
}
47+
48+
func testEmptyKey() throws {
49+
var parser = JSONParserImpl(bytes: [UInt8]("{\"\": \"foobar\"}".utf8))
50+
let _ = try XCTUnwrap(parser.reader.read())
51+
52+
let result = try parser.parseObject()
53+
XCTAssertEqual(result, ["": .string("foobar")])
54+
}
55+
56+
func testDuplicateKey() throws {
57+
var parser = JSONParserImpl(bytes: [UInt8]("{\"a\": \"foo\", \"a\": \"bar\"}".utf8))
58+
let _ = try XCTUnwrap(parser.reader.read())
59+
60+
// current behavior is last key is given, should be documented?
61+
// rfc does not define how to handle duplicate keys
62+
// parser should be able to parse anyways
63+
64+
let result = try parser.parseObject()
65+
XCTAssertEqual(result, ["a": .string("bar")])
66+
}
67+
4768
}

0 commit comments

Comments
 (0)