Skip to content

Commit

Permalink
Add tests for RegexBuilders
Browse files Browse the repository at this point in the history
  • Loading branch information
DandyLyons committed Sep 2, 2024
1 parent eb4e444 commit 88feb98
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 25 deletions.
2 changes: 0 additions & 2 deletions Sources/NativeRegexExamples/DataTypes/email.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public extension RegexLiterals {

public extension RegexBuilders {
static let email = Regex {
"/"
OneOrMore {
CharacterClass(
.anyOf("._%+-"),
Expand All @@ -31,7 +30,6 @@ public extension RegexBuilders {
("a"..."z")
)
}
"/"
}
.anchorsMatchLineEndings()
}
44 changes: 40 additions & 4 deletions Tests/NativeRegexExamplesTests/EmailTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import Testing
@testable import NativeRegexExamples
import CustomDump

@Suite("Email")
struct EmailTests: RegexTestSuite {
@Suite(.tags(.literals, .email))
struct EmailTests_Literal: RegexTestSuite {
@Test(arguments: ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"])
func wholeMatch(_ input: String) throws {
let wholeMatchOptional = input.wholeMatch(of: RegexLiterals.email)
let wholeMatch = try #require(wholeMatchOptional) // unwrap
let output = String(wholeMatch.output) // convert Substring to String
expectNoDifference(output, input)
}

@Test("NOT wholeMatch(of:)",
arguments: ["@email.com", "myName@"]
arguments: ["@email.com", "myName@"]
)
func not_wholeMatch(_ input: String) throws {
let not_wholeMatch = input.wholeMatch(of: RegexLiterals.email)
Expand All @@ -38,4 +38,40 @@ some other text ⬛︎⬛︎⬛︎
}
}

@Suite(.tags(.builderDSL, .email))
struct EmailTests_DSL: RegexTestSuite {
@Test(arguments: ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"])
func wholeMatch(_ input: String) throws {
let wholeMatchOptional = input.wholeMatch(of: RegexBuilders.email)
let wholeMatch = try #require(wholeMatchOptional) // unwrap
let output = String(wholeMatch.output) // convert Substring to String
expectNoDifference(output, input)
}

@Test("NOT wholeMatch(of:)",
arguments: ["@email.com", "myName@"]
)
func not_wholeMatch(_ input: String) throws {
let not_wholeMatch = input.wholeMatch(of: RegexBuilders.email)
#expect(
not_wholeMatch == nil,
"False positive match found: \(input) should not match \(not_wholeMatch)"
)
}

@Test("replace(_ regex: with:)")
func replace() {
var text = """
[email protected] some other text
some other text [email protected]
"""
text.replace(RegexBuilders.email, with: "⬛︎⬛︎⬛︎")
let expected = """
⬛︎⬛︎⬛︎ some other text
some other text ⬛︎⬛︎⬛︎
"""
expectNoDifference(expected, text)
}
}


42 changes: 40 additions & 2 deletions Tests/NativeRegexExamplesTests/IPv4Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Testing
@testable import NativeRegexExamples
import CustomDump

@Suite("IPv4")
struct IPv4Tests: RegexTestSuite {
@Suite(.tags(.literals, .ipv4))
struct IPv4Tests_Literal: RegexTestSuite {
@Test(arguments: [
"127.0.0.1", "192.168.1.1", "0.0.0.0", "255.255.255.255", "1.2.3.4"
])
Expand Down Expand Up @@ -39,3 +39,41 @@ some other text ⬛︎⬛︎⬛︎
expectNoDifference(expected, text)
}
}

@Suite(.tags(.builderDSL, .ipv4))
struct IPv4Tests_DSL: RegexTestSuite {
@Test(arguments: [
"127.0.0.1", "192.168.1.1", "0.0.0.0", "255.255.255.255", "1.2.3.4"
])
func wholeMatch(_ input: String) throws {
let wholeMatchOptional = input.wholeMatch(of: RegexBuilders.ipv4)
let wholeMatch = try #require(wholeMatchOptional) // unwrap
let output = String(wholeMatch.output) // convert Substring to String
expectNoDifference(output, input)
}

@Test("NOT wholeMatch(of:)",
arguments: ["256.256.256.256", "999.999.999.999", "1.2.3"]
)
func not_wholeMatch(_ input: String) throws {
let not_wholeMatch = input.wholeMatch(of: RegexBuilders.ipv4)
#expect(
not_wholeMatch == nil,
"False positive match found: \(input) should not match \(not_wholeMatch)"
)
}

@Test("replace(_ regex: with:)")
func replace() {
var text = """
192.168.1.1 some other text
some other text 127.0.0.1
"""
text.replace(RegexBuilders.ipv4, with: "⬛︎⬛︎⬛︎")
let expected = """
⬛︎⬛︎⬛︎ some other text
some other text ⬛︎⬛︎⬛︎
"""
expectNoDifference(expected, text)
}
}
52 changes: 50 additions & 2 deletions Tests/NativeRegexExamplesTests/PhoneNumberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import Testing
@testable import NativeRegexExamples
import CustomDump

@Suite("Phone Numbers")
struct PhoneNumberTests: RegexTestSuite {
extension Tag {
@Tag static var literals: Self
@Tag static var builderDSL: Self
@Tag static var phoneNumber: Self
@Tag static var email: Self
@Tag static var date: Self
@Tag static var ipv4: Self
@Tag static var ssn: Self
}

@Suite(.tags(.literals, .phoneNumber))
struct Literals_PhoneNumberTests: RegexTestSuite {
@Test(arguments: ["555-1234", "5551234", "1-555-1234"])
func wholeMatch(_ input: String) throws {
let wholeMatchOptional = input.wholeMatch(of: RegexLiterals.phoneNumber)
Expand Down Expand Up @@ -40,3 +50,41 @@ some other text ⬛︎⬛︎⬛︎
}

}

@Suite(.tags(.builderDSL, .phoneNumber))
struct Builder_PhoneNumberTests: RegexTestSuite {
@Test(arguments: ["555-1234", "5551234", "1-555-1234"])
func wholeMatch(_ input: String) throws {
let wholeMatchOptional = input.wholeMatch(of: RegexBuilders.phoneNumber)
let wholeMatch = try #require(wholeMatchOptional) // unwrap
let output = String(wholeMatch.output) // convert Substring to String
expectNoDifference(output, input)
}

@Test("NOT wholeMatch(of:)",
arguments: ["5555-1234", "55-1234", "555-12345"]
)
func not_wholeMatch(_ input: String) throws {
let not_wholeMatch = input.wholeMatch(of: RegexBuilders.phoneNumber)
withKnownIssue {
#expect(
not_wholeMatch == nil,
"False positive match found: \(input) should not match \(String(not_wholeMatch?.output ?? ""))"
)
}
}

@Test("NOT passing in a Regex through @Test")
func replace() {
var text = """
555-1234 some other text
some other text 1-555-1234
"""
text.replace(RegexBuilders.phoneNumber, with: "⬛︎⬛︎⬛︎")
let expected = """
⬛︎⬛︎⬛︎ some other text
some other text ⬛︎⬛︎⬛︎
"""
expectNoDifference(expected, text)
}
}
53 changes: 38 additions & 15 deletions Tests/NativeRegexExamplesTests/SSNTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Testing
@testable import NativeRegexExamples
import CustomDump

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

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

var text = """
one SSN -> 111-11-1111
222-22-2222 <- another SSN
@Test(
"NOT wholeMatch(of:)",
arguments: [
"some other text", "", "-11-1111",
"666-11-1111", "000-11-1111", "900-11-1111"
]
)
func not_wholeMatch(_ input: String) throws {
let not_wholeMatch = input.wholeMatch(of: RegexBuilders.ssn)
#expect(
not_wholeMatch == nil,
"False positive match found: \(input) should not match \(not_wholeMatch)"
)
}

@Test("replace(_ regex: with:)")
func replace() {
var text = """
111-11-1111 some other text
some other text 222-22-2222
"""
text.replace(ssnRegex, with: "⬛︎⬛︎⬛︎")
// text is now:
// one SSN -> ⬛︎⬛︎⬛︎
// ⬛︎⬛︎⬛︎ <- another SSN
text.replace(RegexBuilders.ssn, with: "⬛︎⬛︎⬛︎")
let expected = """
⬛︎⬛︎⬛︎ some other text
some other text ⬛︎⬛︎⬛︎
"""
expectNoDifference(expected, text)
}
}

0 comments on commit 88feb98

Please sign in to comment.