Skip to content

Commit 8493d3f

Browse files
committed
Update testReadmeExample, enable disabled tests.
1 parent 26edd0c commit 8493d3f

File tree

3 files changed

+61
-69
lines changed

3 files changed

+61
-69
lines changed

Tests/LongTests/LongTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ class LongTests: XCTestCase {
2222
"'•' operator isn't optimizing OneOf's properly.")
2323
}
2424

25+
func testOperatorPrecedence() throws {
26+
let p1 = "a" Skip() letter !alphanumeric "b"+
27+
XCTAssert(type(of: p1.first.first.first.second) == Skip<String>.self)
28+
XCTAssert(type(of: Literal<String.UTF8View>("a") "b" / "c" "d")
29+
== OrPattern<Concat<Literal<String.UTF8View>, Literal<String.UTF8View>>, Concat<Literal<String.UTF8View>, Literal<String.UTF8View>>>.self,
30+
#"`/` should have lower precedence than `•`"#)
31+
}
32+
2533
func testPlaygroundExample() throws {
2634
let text = #"""
2735
0 0.0 0.01

Tests/PatternsTests/ConcatenationTests.swift

Lines changed: 50 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,13 @@ class ConcatenationTests: XCTestCase {
7272
XCTAssert(type(of: Literal("q".utf8) Capture()).Input == String.UTF8View.self)
7373
}
7474

75-
/*
76-
func testRepeatOrThenEndOfLine() throws {
77-
assertParseAll(
78-
Capture((alphanumeric / OneOf(" "))+ • Line.end),
79-
input: "FMA026712 TECNOAUTOMOTRIZ ATLACOMULCO S",
80-
result: ["FMA026712 TECNOAUTOMOTRIZ ATLACOMULCO S"])
81-
}
82-
*/
75+
func testRepeatOrThenEndOfLine() throws {
76+
assertParseAll(
77+
Capture((alphanumeric / OneOf(" "))+ Line.end),
78+
input: "FMA026712 TECNOAUTOMOTRIZ ATLACOMULCO S",
79+
result: ["FMA026712 TECNOAUTOMOTRIZ ATLACOMULCO S"])
80+
}
81+
8382
func testMatchFullRange() throws {
8483
let text = """
8584
line 1
@@ -156,71 +155,58 @@ class ConcatenationTests: XCTestCase {
156155
0005..0010 ; Common # Cc [32] <control-0000>..<control-001F>
157156
002F ; Common # Zs SPACE
158157
"""
159-
/*
160-
lazy var rangeAndProperty: Parser<String> = {
161-
let hexNumber = Capture(name: "codePoint", hexDigit+)
162-
let hexRange = AnyPattern("\(hexNumber)..\(hexNumber)") / hexNumber
163-
return try! Parser(search: AnyPattern("\n\(hexRange • Skip()); \(Capture(name: "property", Skip())) "))
164-
}()
165-
166-
func testStringInterpolation() throws {
167-
assertCaptures(rangeAndProperty, input: text, result: [["0005", "0010", "Common"], ["002F", "Common"]])
168-
}
169-
170-
func testMatchDecoding() throws {
171-
struct Property: Decodable, Equatable {
172-
let codePoint: [Int]
173-
let property: String
174-
let notCaptured: String?
175-
}
176-
177-
let matches = Array(rangeAndProperty.matches(in: text))
178-
let property = try matches.first!.decode(Property.self, from: text)
179-
XCTAssertEqual(property, Property(codePoint: [5, 10], property: "Common", notCaptured: nil))
180-
181-
XCTAssertThrowsError(try matches.last!.decode(Property.self, from: text))
182-
}
183-
184-
func testParserDecoding() {
185-
struct Property: Decodable, Equatable {
186-
let codePoint: [String]
187-
let property: String
188-
}
189-
190-
XCTAssertEqual(try rangeAndProperty.decode([Property].self, from: text),
191-
[Property(codePoint: ["0005", "0010"], property: "Common"),
192-
Property(codePoint: ["002F"], property: "Common")])
193-
XCTAssertEqual(try rangeAndProperty.decodeFirst(Property.self, from: text),
194-
Property(codePoint: ["0005", "0010"], property: "Common"))
195-
}
196-
*/
197-
func testReadmeExample() throws {
198-
let text = "This is a point: (43,7), so is (0,5). But my final point is (3,-1)."
199158

200-
let number = OneOf("+-")¿ digit+
201-
let point = try Parser(search: AnyPattern("(\(Capture(name: "x", number)),\(Capture(name: "y", number)))"))
159+
lazy var rangeAndProperty: Parser<String> = {
160+
let hexNumber = Capture(name: "codePoint", hexDigit+)
161+
let hexRange = AnyPattern("\(hexNumber)..\(hexNumber)") / hexNumber
162+
return try! Parser(search: AnyPattern("\n\(hexRange Skip()); \(Capture(name: "property", Skip())) "))
163+
}()
202164

203-
let pointsAsSubstrings = point.matches(in: text).map { match in
204-
(text[match[one: "x"]!], text[match[one: "y"]!])
165+
func testStringInterpolation() throws {
166+
assertCaptures(rangeAndProperty, input: text, result: [["0005", "0010", "Common"], ["002F", "Common"]])
167+
}
168+
169+
func testMatchDecoding() throws {
170+
struct Property: Decodable, Equatable {
171+
let codePoint: [Int]
172+
let property: String
173+
let notCaptured: String?
205174
}
206175

176+
let matches = Array(rangeAndProperty.matches(in: text))
177+
let property = try matches.first!.decode(Property.self, from: text)
178+
XCTAssertEqual(property, Property(codePoint: [5, 10], property: "Common", notCaptured: nil))
179+
180+
XCTAssertThrowsError(try matches.last!.decode(Property.self, from: text))
181+
}
182+
183+
func testParserDecoding() {
184+
struct Property: Decodable, Equatable {
185+
let codePoint: [String]
186+
let property: String
187+
}
188+
189+
XCTAssertEqual(try rangeAndProperty.decode([Property].self, from: text),
190+
[Property(codePoint: ["0005", "0010"], property: "Common"),
191+
Property(codePoint: ["002F"], property: "Common")])
192+
XCTAssertEqual(try rangeAndProperty.decodeFirst(Property.self, from: text),
193+
Property(codePoint: ["0005", "0010"], property: "Common"))
194+
}
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+
207203
struct Point: Codable, Equatable {
208204
let x, y: Int
209205
}
210206

211-
let points = try point.decode([Point].self, from: text)
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)])
212209

213210
assertCaptures(point, input: text, result: [["43", "7"], ["0", "5"], ["3", "-1"]])
214-
_ = (pointsAsSubstrings, points)
215211
}
216-
217-
/*
218-
func testOperatorPrecedence() throws {
219-
let p1 = "a" • Skip() • letter • !alphanumeric • "b"+
220-
XCTAssert(type(of: p1.first.first.first.second) == Skip.self)
221-
XCTAssert(type(of: "a" • "b" / "c" • "d")
222-
== OrPattern<Concat<Literal, Literal>, Concat<Literal, Literal>>.self,
223-
#"`/` should have lower precedence than `•`"#)
224-
}
225-
*/
226212
}

Tests/PatternsTests/PatternTests.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,8 @@ class PatternTests: XCTestCase {
285285
assertParseAll(Capture(&&letter ascii), input: "1abøcæ", result: ["a", "b", "c"])
286286
assertParseAll(Capture(&&Line.Start() "a"), input: "abø\ncæa\na".utf8, result: "a".utf8, count: 2)
287287

288-
/* TODO: uncomment
289-
// find last occurence of "xuxu", even if it overlaps with itself.
290-
assertParseMarkers(try Parser(Grammar { g in g.last <- &&"xuxu" • any / any • g.last }+ • any.repeat(3)),
291-
input: "xuxuxuxu|i")
292-
*/
288+
// find last occurence of "xuxu", even if it overlaps with itself.
289+
assertParseMarkers(try Parser(Grammar { g in g.last <- &&"xuxu" any / any g.last }+ any.repeat(3)),
290+
input: "xuxuxuxu|i")
293291
}
294292
}

0 commit comments

Comments
 (0)