Skip to content

Commit a349369

Browse files
authored
Merge pull request #86 from tayloraswift/add-fragment-parser
Add fragment parser
2 parents 264cb5b + 6c9d5db commit a349369

File tree

4 files changed

+165
-100
lines changed

4 files changed

+165
-100
lines changed

Package.resolved

Lines changed: 3 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/JSONParsing/JSON.Node (ext).swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,27 @@ extension JSON.Node
2424
{
2525
self = try JSON.RootRule<String.Index>.parse(string.utf8)
2626
}
27+
28+
/// Attempts to parse a a JSON fragment from a string.
29+
public
30+
init(parsingFragment string:String) throws
31+
{
32+
self = try JSON.NodeRule<String.Index>.parse(string.utf8)
33+
}
34+
/// Attempts to parse a a JSON fragment from a substring.
35+
public
36+
init(parsingFragment string:Substring) throws
37+
{
38+
self = try JSON.NodeRule<String.Index>.parse(string.utf8)
39+
}
2740
}
2841
extension JSON.Node:LosslessStringConvertible
2942
{
30-
/// See ``init(parsing:) (String)``.
43+
/// See ``init(parsingFragment:) (String)``.
3144
public
3245
init?(_ description:String)
3346
{
34-
do { try self.init(parsing: description) }
47+
do { try self.init(parsingFragment: description) }
3548
catch { return nil }
3649
}
3750
}
Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,136 @@
11
import JSON
22
import Testing
33

4-
struct IntegerOverflow
4+
@Suite
5+
enum IntegerOverflow
56
{
6-
private
7-
func expect<Signed>(_ value:Int64, overflows:Signed.Type)
8-
where Signed:SignedInteger & JSONDecodable
7+
@Test
8+
static func AsInt8()
99
{
10-
let field:JSON.FieldDecoder<Never?> = .init(key: nil,
11-
value: .number(.init(value)))
12-
13-
#expect(throws: JSON.DecodingError<Never?>.self)
14-
{
15-
let _:Signed = try field.decode()
16-
}
10+
Self.expect(256, overflows: Int8.self)
1711
}
18-
private
19-
func decode<Signed>(_ value:Int64, to:Signed.Type)
20-
where Signed:SignedInteger & JSONDecodable
21-
{
22-
let field:JSON.FieldDecoder<Never?> = .init(key: nil,
23-
value: .number(.init(value)))
2412

25-
#expect(throws: Never.self)
26-
{
27-
let _:Signed = try field.decode()
28-
}
13+
@Test
14+
static func AsInt16()
15+
{
16+
Self.decode(256, to: Int16.self)
2917
}
3018

31-
private
32-
func expect<Unsigned>(_ value:UInt64, overflows:Unsigned.Type)
33-
where Unsigned:UnsignedInteger & JSONDecodable
19+
@Test
20+
static func AsInt32()
3421
{
35-
let field:JSON.FieldDecoder<Never?> = .init(key: nil,
36-
value: .number(.init(value)))
37-
38-
#expect(throws: JSON.DecodingError<Never?>.self)
39-
{
40-
let _:Unsigned = try field.decode()
41-
}
22+
Self.decode(256, to: Int32.self)
4223
}
43-
private
44-
func decode<Unsigned>(_ value:UInt64, to:Unsigned.Type)
45-
where Unsigned:UnsignedInteger & JSONDecodable
46-
{
47-
let field:JSON.FieldDecoder<Never?> = .init(key: nil,
48-
value: .number(.init(value)))
4924

50-
#expect(throws: Never.self)
51-
{
52-
let _:Unsigned = try field.decode()
53-
}
54-
}
55-
}
56-
extension IntegerOverflow
57-
{
58-
@Test("Int8")
59-
func int8()
25+
@Test
26+
static func AsInt64()
6027
{
61-
self.expect(256, overflows: Int8.self)
28+
Self.decode(256, to: Int64.self)
6229
}
6330

64-
@Test("Int16")
65-
func int16()
31+
@Test
32+
static func AsInt64Max()
6633
{
67-
self.decode(256, to: Int16.self)
34+
Self.decode(Int64.max, to: Int64.self)
6835
}
6936

70-
@Test("Int32")
71-
func int32()
37+
@Test
38+
static func AsInt64Min()
7239
{
73-
self.decode(256, to: Int32.self)
40+
Self.decode(Int64.min, to: Int64.self)
7441
}
7542

76-
@Test("Int64")
77-
func int64()
43+
@Test
44+
static func AsInt()
7845
{
79-
self.decode(256, to: Int64.self)
46+
Self.decode(256, to: Int.self)
8047
}
8148

82-
@Test("Int64.max")
83-
func int64max()
49+
@Test
50+
static func AsUInt8()
8451
{
85-
self.decode(Int64.max, to: Int64.self)
52+
Self.expect(256, overflows: UInt8.self)
8653
}
8754

88-
@Test("Int64.min")
89-
func int64min()
55+
@Test
56+
static func AsUInt16()
9057
{
91-
self.decode(Int64.min, to: Int64.self)
58+
Self.decode(256, to: UInt16.self)
9259
}
9360

94-
@Test("Int")
95-
func int()
61+
@Test
62+
static func AsUInt32()
9663
{
97-
self.decode(256, to: Int.self)
64+
Self.decode(256, to: UInt32.self)
9865
}
9966

100-
@Test("UInt8")
101-
func uint8()
67+
@Test
68+
static func AsUInt64()
10269
{
103-
self.expect(256, overflows: UInt8.self)
70+
Self.decode(256, to: UInt64.self)
10471
}
10572

106-
@Test("UInt16")
107-
func uint16()
73+
@Test
74+
static func AsUInt64Max()
10875
{
109-
self.decode(256, to: UInt16.self)
76+
Self.decode(UInt64.max, to: UInt64.self)
11077
}
11178

112-
@Test("UInt32")
113-
func uint32()
79+
@Test
80+
static func AsUInt()
11481
{
115-
self.decode(256, to: UInt32.self)
82+
Self.decode(256, to: UInt.self)
11683
}
84+
}
85+
extension IntegerOverflow
86+
{
87+
private
88+
static func expect<Signed>(_ value:Int64, overflows:Signed.Type)
89+
where Signed:SignedInteger & JSONDecodable
90+
{
91+
let field:JSON.FieldDecoder<Never?> = .init(key: nil,
92+
value: .number(.init(value)))
11793

118-
@Test("UInt64")
119-
func uint64()
94+
#expect(throws: JSON.DecodingError<Never?>.self)
95+
{
96+
let _:Signed = try field.decode()
97+
}
98+
}
99+
private
100+
static func decode<Signed>(_ value:Int64, to:Signed.Type)
101+
where Signed:SignedInteger & JSONDecodable
120102
{
121-
self.decode(256, to: UInt64.self)
103+
let field:JSON.FieldDecoder<Never?> = .init(key: nil,
104+
value: .number(.init(value)))
105+
106+
#expect(throws: Never.self)
107+
{
108+
let _:Signed = try field.decode()
109+
}
122110
}
123111

124-
@Test("UInt64.max")
125-
func uint64max()
112+
private
113+
static func expect<Unsigned>(_ value:UInt64, overflows:Unsigned.Type)
114+
where Unsigned:UnsignedInteger & JSONDecodable
126115
{
127-
self.decode(UInt64.max, to: UInt64.self)
128-
}
116+
let field:JSON.FieldDecoder<Never?> = .init(key: nil,
117+
value: .number(.init(value)))
129118

130-
@Test("UInt")
131-
func uint()
119+
#expect(throws: JSON.DecodingError<Never?>.self)
120+
{
121+
let _:Unsigned = try field.decode()
122+
}
123+
}
124+
private
125+
static func decode<Unsigned>(_ value:UInt64, to:Unsigned.Type)
126+
where Unsigned:UnsignedInteger & JSONDecodable
132127
{
133-
self.decode(256, to: UInt.self)
128+
let field:JSON.FieldDecoder<Never?> = .init(key: nil,
129+
value: .number(.init(value)))
130+
131+
#expect(throws: Never.self)
132+
{
133+
let _:Unsigned = try field.decode()
134+
}
134135
}
135136
}
136-

Sources/JSONTests/Parsing.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import JSON
2+
import Testing
3+
4+
@Suite
5+
enum Parsing
6+
{
7+
@Test
8+
static func Null() throws
9+
{
10+
guard case JSON.Node.null? = try .init(parsingFragment: "null")
11+
else
12+
{
13+
Issue.record()
14+
return
15+
}
16+
}
17+
18+
@Test
19+
static func BoolTrue() throws
20+
{
21+
guard case JSON.Node.bool(true)? = try .init(parsingFragment: "true")
22+
else
23+
{
24+
Issue.record()
25+
return
26+
}
27+
}
28+
@Test
29+
static func BoolFalse() throws
30+
{
31+
guard case JSON.Node.bool(false)? = try .init(parsingFragment: "false")
32+
else
33+
{
34+
Issue.record()
35+
return
36+
}
37+
}
38+
39+
@Test
40+
static func Number0() throws
41+
{
42+
guard case JSON.Node.number(.init(sign: .plus, units: 0, places: 0))? = try .init(
43+
parsingFragment: "0")
44+
else
45+
{
46+
Issue.record()
47+
return
48+
}
49+
}
50+
51+
@Test
52+
static func String() throws
53+
{
54+
guard case JSON.Node.string(.init("a"))? = try .init(parsingFragment: "\"a\"")
55+
else
56+
{
57+
Issue.record()
58+
return
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)