Skip to content

Commit 88feb98

Browse files
committed
Add tests for RegexBuilders
1 parent eb4e444 commit 88feb98

File tree

5 files changed

+168
-25
lines changed

5 files changed

+168
-25
lines changed

Sources/NativeRegexExamples/DataTypes/email.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public extension RegexLiterals {
66

77
public extension RegexBuilders {
88
static let email = Regex {
9-
"/"
109
OneOrMore {
1110
CharacterClass(
1211
.anyOf("._%+-"),
@@ -31,7 +30,6 @@ public extension RegexBuilders {
3130
("a"..."z")
3231
)
3332
}
34-
"/"
3533
}
3634
.anchorsMatchLineEndings()
3735
}

Tests/NativeRegexExamplesTests/EmailTests.swift

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import Testing
22
@testable import NativeRegexExamples
33
import CustomDump
44

5-
@Suite("Email")
6-
struct EmailTests: RegexTestSuite {
5+
@Suite(.tags(.literals, .email))
6+
struct EmailTests_Literal: RegexTestSuite {
77
@Test(arguments: ["hello@email.com", "myemail@something.co.uk", "user@sub.example.com", "some.name@place.com", "user..name@example.com"])
88
func wholeMatch(_ input: String) throws {
99
let wholeMatchOptional = input.wholeMatch(of: RegexLiterals.email)
1010
let wholeMatch = try #require(wholeMatchOptional) // unwrap
1111
let output = String(wholeMatch.output) // convert Substring to String
1212
expectNoDifference(output, input)
1313
}
14-
14+
1515
@Test("NOT wholeMatch(of:)",
16-
arguments: ["@email.com", "myName@"]
16+
arguments: ["@email.com", "myName@"]
1717
)
1818
func not_wholeMatch(_ input: String) throws {
1919
let not_wholeMatch = input.wholeMatch(of: RegexLiterals.email)
@@ -38,4 +38,40 @@ some other text ⬛︎⬛︎⬛︎
3838
}
3939
}
4040

41+
@Suite(.tags(.builderDSL, .email))
42+
struct EmailTests_DSL: RegexTestSuite {
43+
@Test(arguments: ["hello@email.com", "myemail@something.co.uk", "user@sub.example.com", "some.name@place.com", "user..name@example.com"])
44+
func wholeMatch(_ input: String) throws {
45+
let wholeMatchOptional = input.wholeMatch(of: RegexBuilders.email)
46+
let wholeMatch = try #require(wholeMatchOptional) // unwrap
47+
let output = String(wholeMatch.output) // convert Substring to String
48+
expectNoDifference(output, input)
49+
}
50+
51+
@Test("NOT wholeMatch(of:)",
52+
arguments: ["@email.com", "myName@"]
53+
)
54+
func not_wholeMatch(_ input: String) throws {
55+
let not_wholeMatch = input.wholeMatch(of: RegexBuilders.email)
56+
#expect(
57+
not_wholeMatch == nil,
58+
"False positive match found: \(input) should not match \(not_wholeMatch)"
59+
)
60+
}
61+
62+
@Test("replace(_ regex: with:)")
63+
func replace() {
64+
var text = """
65+
hello@email.com some other text
66+
some other text myemail@example.org
67+
"""
68+
text.replace(RegexBuilders.email, with: "⬛︎⬛︎⬛︎")
69+
let expected = """
70+
⬛︎⬛︎⬛︎ some other text
71+
some other text ⬛︎⬛︎⬛︎
72+
"""
73+
expectNoDifference(expected, text)
74+
}
75+
}
76+
4177

Tests/NativeRegexExamplesTests/IPv4Tests.swift

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import Testing
22
@testable import NativeRegexExamples
33
import CustomDump
44

5-
@Suite("IPv4")
6-
struct IPv4Tests: RegexTestSuite {
5+
@Suite(.tags(.literals, .ipv4))
6+
struct IPv4Tests_Literal: RegexTestSuite {
77
@Test(arguments: [
88
"127.0.0.1", "192.168.1.1", "0.0.0.0", "255.255.255.255", "1.2.3.4"
99
])
@@ -39,3 +39,41 @@ some other text ⬛︎⬛︎⬛︎
3939
expectNoDifference(expected, text)
4040
}
4141
}
42+
43+
@Suite(.tags(.builderDSL, .ipv4))
44+
struct IPv4Tests_DSL: RegexTestSuite {
45+
@Test(arguments: [
46+
"127.0.0.1", "192.168.1.1", "0.0.0.0", "255.255.255.255", "1.2.3.4"
47+
])
48+
func wholeMatch(_ input: String) throws {
49+
let wholeMatchOptional = input.wholeMatch(of: RegexBuilders.ipv4)
50+
let wholeMatch = try #require(wholeMatchOptional) // unwrap
51+
let output = String(wholeMatch.output) // convert Substring to String
52+
expectNoDifference(output, input)
53+
}
54+
55+
@Test("NOT wholeMatch(of:)",
56+
arguments: ["256.256.256.256", "999.999.999.999", "1.2.3"]
57+
)
58+
func not_wholeMatch(_ input: String) throws {
59+
let not_wholeMatch = input.wholeMatch(of: RegexBuilders.ipv4)
60+
#expect(
61+
not_wholeMatch == nil,
62+
"False positive match found: \(input) should not match \(not_wholeMatch)"
63+
)
64+
}
65+
66+
@Test("replace(_ regex: with:)")
67+
func replace() {
68+
var text = """
69+
192.168.1.1 some other text
70+
some other text 127.0.0.1
71+
"""
72+
text.replace(RegexBuilders.ipv4, with: "⬛︎⬛︎⬛︎")
73+
let expected = """
74+
⬛︎⬛︎⬛︎ some other text
75+
some other text ⬛︎⬛︎⬛︎
76+
"""
77+
expectNoDifference(expected, text)
78+
}
79+
}

Tests/NativeRegexExamplesTests/PhoneNumberTests.swift

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@ import Testing
22
@testable import NativeRegexExamples
33
import CustomDump
44

5-
@Suite("Phone Numbers")
6-
struct PhoneNumberTests: RegexTestSuite {
5+
extension Tag {
6+
@Tag static var literals: Self
7+
@Tag static var builderDSL: Self
8+
@Tag static var phoneNumber: Self
9+
@Tag static var email: Self
10+
@Tag static var date: Self
11+
@Tag static var ipv4: Self
12+
@Tag static var ssn: Self
13+
}
14+
15+
@Suite(.tags(.literals, .phoneNumber))
16+
struct Literals_PhoneNumberTests: RegexTestSuite {
717
@Test(arguments: ["555-1234", "5551234", "1-555-1234"])
818
func wholeMatch(_ input: String) throws {
919
let wholeMatchOptional = input.wholeMatch(of: RegexLiterals.phoneNumber)
@@ -40,3 +50,41 @@ some other text ⬛︎⬛︎⬛︎
4050
}
4151

4252
}
53+
54+
@Suite(.tags(.builderDSL, .phoneNumber))
55+
struct Builder_PhoneNumberTests: RegexTestSuite {
56+
@Test(arguments: ["555-1234", "5551234", "1-555-1234"])
57+
func wholeMatch(_ input: String) throws {
58+
let wholeMatchOptional = input.wholeMatch(of: RegexBuilders.phoneNumber)
59+
let wholeMatch = try #require(wholeMatchOptional) // unwrap
60+
let output = String(wholeMatch.output) // convert Substring to String
61+
expectNoDifference(output, input)
62+
}
63+
64+
@Test("NOT wholeMatch(of:)",
65+
arguments: ["5555-1234", "55-1234", "555-12345"]
66+
)
67+
func not_wholeMatch(_ input: String) throws {
68+
let not_wholeMatch = input.wholeMatch(of: RegexBuilders.phoneNumber)
69+
withKnownIssue {
70+
#expect(
71+
not_wholeMatch == nil,
72+
"False positive match found: \(input) should not match \(String(not_wholeMatch?.output ?? ""))"
73+
)
74+
}
75+
}
76+
77+
@Test("NOT passing in a Regex through @Test")
78+
func replace() {
79+
var text = """
80+
555-1234 some other text
81+
some other text 1-555-1234
82+
"""
83+
text.replace(RegexBuilders.phoneNumber, with: "⬛︎⬛︎⬛︎")
84+
let expected = """
85+
⬛︎⬛︎⬛︎ some other text
86+
some other text ⬛︎⬛︎⬛︎
87+
"""
88+
expectNoDifference(expected, text)
89+
}
90+
}

Tests/NativeRegexExamplesTests/SSNTests.swift

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import Testing
22
@testable import NativeRegexExamples
33
import CustomDump
44

5-
@Suite("SSN")
6-
struct SSNTests: RegexTestSuite {
5+
@Suite(.tags(.literals, .ssn))
6+
struct SSNTests_Literal: RegexTestSuite {
77
@Test(arguments: ["123-45-6789"])
88
func wholeMatch(_ input: String) throws {
99
let wholeMatchOptional = input.wholeMatch(of: RegexLiterals.ssn)
@@ -42,19 +42,42 @@ some other text ⬛︎⬛︎⬛︎
4242
}
4343
}
4444

45-
@RegexActor
46-
func foo() {
47-
let ssnRegex = RegexLiterals.ssn
48-
let string = "111-11-1111"
49-
string.contains(ssnRegex) // true
50-
string.wholeMatch(of: ssnRegex)
45+
@Suite(.tags(.builderDSL, .ssn))
46+
struct SSNTests_DSL: RegexTestSuite {
47+
@Test(arguments: ["123-45-6789"])
48+
func wholeMatch(_ input: String) throws {
49+
let wholeMatchOptional = input.wholeMatch(of: RegexBuilders.ssn)
50+
let wholeMatch = try #require(wholeMatchOptional) // unwrap
51+
let output = String(wholeMatch.output) // convert Substring to String
52+
expectNoDifference(output, input)
53+
}
5154

52-
var text = """
53-
one SSN -> 111-11-1111
54-
222-22-2222 <- another SSN
55+
@Test(
56+
"NOT wholeMatch(of:)",
57+
arguments: [
58+
"some other text", "", "-11-1111",
59+
"666-11-1111", "000-11-1111", "900-11-1111"
60+
]
61+
)
62+
func not_wholeMatch(_ input: String) throws {
63+
let not_wholeMatch = input.wholeMatch(of: RegexBuilders.ssn)
64+
#expect(
65+
not_wholeMatch == nil,
66+
"False positive match found: \(input) should not match \(not_wholeMatch)"
67+
)
68+
}
69+
70+
@Test("replace(_ regex: with:)")
71+
func replace() {
72+
var text = """
73+
111-11-1111 some other text
74+
some other text 222-22-2222
5575
"""
56-
text.replace(ssnRegex, with: "⬛︎⬛︎⬛︎")
57-
// text is now:
58-
// one SSN -> ⬛︎⬛︎⬛︎
59-
// ⬛︎⬛︎⬛︎ <- another SSN
76+
text.replace(RegexBuilders.ssn, with: "⬛︎⬛︎⬛︎")
77+
let expected = """
78+
⬛︎⬛︎⬛︎ some other text
79+
some other text ⬛︎⬛︎⬛︎
80+
"""
81+
expectNoDifference(expected, text)
82+
}
6083
}

0 commit comments

Comments
 (0)