Skip to content

Commit 75d4931

Browse files
committed
Initial custom-component test infrastructure
1 parent 3f24f5b commit 75d4931

File tree

1 file changed

+54
-28
lines changed

1 file changed

+54
-28
lines changed

Tests/RegexTests/CustomTests.swift

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,65 @@ private struct Asciibbler: Nibbler {
3737
}
3838
}
3939

40-
extension RegexTests {
41-
42-
// TODO: Refactor below into more exhaustive, declarative
43-
// tests.
44-
func testMatchingConsumers() {
45-
46-
let regex = Regex {
47-
Numbler()
48-
Asciibbler()
49-
}
40+
enum MatchCall {
41+
case match
42+
case firstMatch
43+
}
5044

51-
guard let result = "4t".match(regex) else {
52-
XCTFail()
53-
return
45+
func customTest<Match: Equatable>(
46+
_ regex: Regex<Match>,
47+
_ tests: (input: String, call: MatchCall, match: Match?)...
48+
) {
49+
for (input, call, match) in tests {
50+
let result: Match?
51+
switch call {
52+
case .match:
53+
result = input.match(regex)?.match
54+
case .firstMatch:
55+
result = input.firstMatch(of: regex)?.result
5456
}
55-
XCTAssert(result.match == "4t")
57+
XCTAssertEqual(result, match)
58+
}
59+
}
5660

57-
XCTAssertNil("4".match(regex))
58-
XCTAssertNil("t".match(regex))
59-
XCTAssertNil("t4".match(regex))
61+
extension RegexTests {
6062

61-
let regex2 = Regex {
62-
oneOrMore {
63+
// TODO: Refactor below into more exhaustive, declarative
64+
// tests.
65+
func testCustomRegexComponents() {
66+
customTest(
67+
Regex {
6368
Numbler()
64-
}
65-
}
66-
67-
guard let res2 = "ab123c".firstMatch(of: regex2) else {
68-
XCTFail()
69-
return
70-
}
71-
72-
XCTAssertEqual(res2.match, "123")
69+
Asciibbler()
70+
},
71+
("4t", .match, "4t"),
72+
("4", .match, nil),
73+
("t", .match, nil),
74+
("t x1y z", .firstMatch, "1y"),
75+
("t4", .match, nil))
76+
77+
customTest(
78+
Regex {
79+
oneOrMore { Numbler() }
80+
},
81+
("ab123c", .firstMatch, "123"),
82+
("abc", .firstMatch, nil),
83+
("55z", .match, nil),
84+
("55z", .firstMatch, "55"))
85+
86+
// FIXME: Requires we return a value instead of a range
87+
// customTest(
88+
// Regex {
89+
// Numbler()
90+
// },
91+
// ("ab123c", .firstMatch, 1),
92+
// ("abc", .firstMatch, nil),
93+
// ("55z", .match, nil),
94+
// ("55z", .firstMatch, 5))
95+
96+
// TODO: Convert below tests to better infra. Right now
97+
// it's hard because `Match` is constrained to be
98+
// `Equatable` which tuples cannot be.
7399

74100
let regex3 = Regex {
75101
capture {

0 commit comments

Comments
 (0)