Skip to content

Commit c693330

Browse files
committed
Unit test all readme code.
1 parent 8493d3f commit c693330

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,14 @@ struct Point: Codable {
169169
let x, y: Int
170170
}
171171

172-
let points = try Parser(search: point).decode([Point].self, from: text)
172+
let parser = try Parser(search: point)
173+
let points = try parser.decode([Point].self, from: text)
173174
```
174175

175176
Or you can use subscripting:
176177

177178
```swift
178-
let pointsAsSubstrings = point.matches(in: text).map { match in
179+
let pointsAsSubstrings = parser.matches(in: text).map { match in
179180
(text[match[one: "x"]!], text[match[one: "y"]!])
180181
}
181182
```

Tests/PatternsTests/ConcatenationTests.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,4 @@ class ConcatenationTests: XCTestCase {
192192
XCTAssertEqual(try rangeAndProperty.decodeFirst(Property.self, from: text),
193193
Property(codePoint: ["0005", "0010"], property: "Common"))
194194
}
195-
196-
func testReadmeExample() throws {
197-
let text = "This is a point: (43,7), so is (0, 5). But my final point is (3,-1)."
198-
199-
let number = ("+" / "-" / "") digit+
200-
let point = "(" Capture(name: "x", number)
201-
"," " "¿ Capture(name: "y", number) ")"
202-
203-
struct Point: Codable, Equatable {
204-
let x, y: Int
205-
}
206-
207-
let points = try Parser(search: point).decode([Point].self, from: text)
208-
XCTAssertEqual(points, [Point(x: 43, y: 7), Point(x: 0, y: 5), Point(x: 3, y: -1)])
209-
210-
assertCaptures(point, input: text, result: [["43", "7"], ["0", "5"], ["3", "-1"]])
211-
}
212195
}

Tests/PatternsTests/PatternTests.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,66 @@ class PatternTests: XCTestCase {
289289
assertParseMarkers(try Parser(Grammar { g in g.last <- &&"xuxu" any / any g.last }+ any.repeat(3)),
290290
input: "xuxuxuxu|i")
291291
}
292+
293+
func testReadmeExample() throws {
294+
let text = "This is a point: (43,7), so is (0, 5). But my final point is (3,-1)."
295+
296+
let number = ("+" / "-" / "") digit+
297+
let point = "(" Capture(name: "x", number)
298+
"," " "¿ Capture(name: "y", number) ")"
299+
300+
struct Point: Codable, Equatable {
301+
let x, y: Int
302+
}
303+
304+
let points = try Parser(search: point).decode([Point].self, from: text)
305+
XCTAssertEqual(points, [Point(x: 43, y: 7), Point(x: 0, y: 5), Point(x: 3, y: -1)])
306+
307+
assertCaptures(point, input: text, result: [["43", "7"], ["0", "5"], ["3", "-1"]])
308+
}
309+
310+
func testReadme() throws {
311+
do {
312+
let l = OneOf(description: "ten") { character in
313+
character.wholeNumberValue == 10
314+
}
315+
316+
let arithmetic = Grammar { g in
317+
g.all <- g.expr !any
318+
g.expr <- g.sum
319+
g.sum <- g.product (("+" / "-") g.product)*
320+
g.product <- g.power (("*" / "/") g.power)*
321+
g.power <- g.value ("^" g.power)¿
322+
g.value <- digit+ / "(" g.expr ")"
323+
}
324+
325+
let parser = try Parser(search: l)
326+
for match in parser.matches(in: "text") {
327+
_ = match
328+
// ...
329+
}
330+
_ = arithmetic
331+
}
332+
333+
do {
334+
let text = "This is a point: (43,7), so is (0, 5). But my final point is (3,-1)."
335+
336+
let number = ("+" / "-" / "") digit+
337+
let point = "(" Capture(name: "x", number)
338+
"," " "¿ Capture(name: "y", number) ")"
339+
340+
struct Point: Codable {
341+
let x, y: Int
342+
}
343+
344+
let parser = try Parser(search: point)
345+
let points = try parser.decode([Point].self, from: text)
346+
347+
let pointsAsSubstrings = parser.matches(in: text).map { match in
348+
(text[match[one: "x"]!], text[match[one: "y"]!])
349+
}
350+
351+
_ = (points, pointsAsSubstrings)
352+
}
353+
}
292354
}

0 commit comments

Comments
 (0)