Skip to content

Commit 54fe648

Browse files
committed
Migrated from XCTest to SwiftTesting.
1 parent 68236c5 commit 54fe648

File tree

7 files changed

+73
-52
lines changed

7 files changed

+73
-52
lines changed

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
disabled_rules:
44
inclusive_language
5+
non_optional_string_data_conversion
56

67
nesting:
78
type_level:

Tests/FaviconFinderTests/FaviconFinderTests.swift

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
//
88

99
@testable import FaviconFinder
10-
import XCTest
10+
import Foundation
11+
import Testing
1112

12-
class FaviconFinderTests: XCTestCase {
13+
struct FaviconFinderTests {
1314

1415
// MARK: - Tests
1516

17+
@Test("Test URLs")
1618
func testURLs() async throws {
1719
// Remove the URL that requires meta-refresh redirect
1820
var testURLs = TestURL.allCases
@@ -27,6 +29,7 @@ class FaviconFinderTests: XCTestCase {
2729
}
2830
}
2931

32+
@Test("Test ICO Favicon")
3033
func testIco() async throws {
3134
let favicon = try await FaviconFinder(
3235
url: TestURL.google.url,
@@ -37,13 +40,14 @@ class FaviconFinderTests: XCTestCase {
3740
.first()
3841

3942
// Ensure that our favicon is actually valid
40-
let image = try XCTUnwrap(favicon.image)
41-
XCTAssertTrue(image.isValidImage)
43+
let image = try #require(favicon.image)
44+
#expect(image.isValidImage == true)
4245

4346
// Ensure that our favicon was retrieved from the desired source
44-
XCTAssertTrue(favicon.url.sourceType == .ico)
47+
#expect(favicon.url.sourceType == .ico)
4548
}
4649

50+
@Test("Test HTML Favicon")
4751
func testHtml() async throws {
4852
let favicon = try await FaviconFinder(
4953
url: TestURL.w3Schools.url,
@@ -54,13 +58,14 @@ class FaviconFinderTests: XCTestCase {
5458
.first()
5559

5660
// Ensure that our favicon is actually valid
57-
let image = try XCTUnwrap(favicon.image)
58-
XCTAssertTrue(image.isValidImage)
61+
let image = try #require(favicon.image)
62+
#expect(image.isValidImage == true)
5963

6064
// Ensure that our favicon was retrieved from the desired source
61-
XCTAssertTrue(favicon.url.sourceType == .html)
65+
#expect(favicon.url.sourceType == .html)
6266
}
6367

68+
@Test("Test WebApplicationManifestFile Favicon")
6469
func testWebApplicationManifestFile() async throws {
6570
let favicon = try await FaviconFinder(
6671
url: TestURL.webApplicationManifest.url,
@@ -71,13 +76,14 @@ class FaviconFinderTests: XCTestCase {
7176
.first()
7277

7378
// Ensure that our favicon is actually valid
74-
let image = try XCTUnwrap(favicon.image)
75-
XCTAssertTrue(image.isValidImage)
79+
let image = try #require(favicon.image)
80+
#expect(image.isValidImage == true)
7681

7782
// Ensure that our favicon was retrieved from the desired source
78-
XCTAssertTrue(favicon.url.sourceType == .webApplicationManifestFile)
83+
#expect(favicon.url.sourceType == .webApplicationManifestFile)
7984
}
8085

86+
@Test("Test Meta Refresh Redirect Favicon")
8187
func testCheckForMetaRefreshRedirect() async throws {
8288
let favicon = try await FaviconFinder(
8389
url: TestURL.metaRefreshRedirect.url,
@@ -91,41 +97,43 @@ class FaviconFinderTests: XCTestCase {
9197
.first()
9298

9399
// Ensure that our favicon is actually valid
94-
let image = try XCTUnwrap(favicon.image)
95-
XCTAssertTrue(image.isValidImage)
100+
let image = try #require(favicon.image)
101+
#expect(image.isValidImage == true)
96102

97103
// Ensure that our favicon was retrieved from the desired source
98-
XCTAssertTrue(favicon.url.sourceType == .html)
104+
#expect(favicon.url.sourceType == .html)
99105
}
100106

107+
@Test("Test ForeignEncoding Favicon")
101108
func testForeignEncoding() async throws {
102109
let favicon = try await FaviconFinder(url: TestURL.nonUtf8Encoded.url)
103110
.fetchFaviconURLs()
104111
.download()
105112
.first()
106113

107114
// Ensure that our favicon is actually valid
108-
let image = try XCTUnwrap(favicon.image)
109-
XCTAssertTrue(image.isValidImage)
115+
let image = try #require(favicon.image)
116+
#expect(image.isValidImage == true)
110117
}
111118

119+
@Test("Test Cancel")
112120
func testCancel() async throws {
113121
let faviconFinder = FaviconFinder(
114122
url: TestURL.google.url,
115123
configuration: .init(preferredSource: .mock)
116124
)
117125

118-
let expectation = expectation(description: "Task should be cancelled")
126+
// We're expecting to catch an error, and we'll store it here
127+
var caughtError: Error?
119128

120-
// Find the Favicon's in a separate Task
129+
// Find the Favicon's in a separate Task, so we can cancel it
121130
Task {
122131
do {
123132
_ = try await faviconFinder.fetchFaviconURLs()
124-
XCTFail("Expected fetchFaviconURLs to be cancelled, but it completed")
125-
} catch is CancellationError {
126-
expectation.fulfill()
133+
Issue.record("Expected fetchFaviconURLs to be cancelled, but it completed")
127134
} catch {
128-
XCTFail("Unexpected error: \(error)")
135+
// Store the error
136+
caughtError = error
129137
}
130138
}
131139

@@ -135,8 +143,11 @@ class FaviconFinderTests: XCTestCase {
135143
// Cancel the finding
136144
faviconFinder.cancel()
137145

138-
// Wait for the expectation to be fulfilled
139-
await fulfillment(of: [expectation], timeout: 5)
146+
// Wait a couple seconds
147+
try await Task.sleep(for: .seconds(2))
148+
149+
// We got a CancellationError, meaning that we got a cancellation, yay
150+
#expect(caughtError is CancellationError)
140151
}
141152

142153
}
@@ -151,7 +162,8 @@ private extension FaviconFinderTests {
151162
.fetchFaviconURLs()
152163
.download()
153164
.first()
154-
XCTAssertNotNil(favicon.image)
165+
166+
#expect(favicon.image != nil)
155167
}
156168

157169
}

Tests/FaviconFinderTests/Toolbox/Extensions/ArrayReorderTests.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,28 @@
77
//
88

99
@testable import FaviconFinder
10-
import XCTest
10+
import Testing
1111

12-
class ArrayReorderTests: XCTestCase {
12+
struct ArrayReorderTests {
1313

14+
@Test("Reorder Elements")
1415
func testReorder() {
1516
var array = [Int]()
1617

1718
array = [1, 2, 3]
1819
array = array.movingElementToFront(2)
1920

20-
XCTAssertEqual(array, [2, 1, 3])
21+
#expect(array == [2, 1, 3])
2122

2223
array = [1, 2, 3]
2324
array = array.movingElementToFront(1)
2425

25-
XCTAssertEqual(array, [1, 2, 3])
26+
#expect(array == [1, 2, 3])
2627

2728
array = [1, 2, 3]
2829
array = array.movingElementToFront(4)
2930

30-
XCTAssertEqual(array, [1, 2, 3])
31+
#expect(array == [1, 2, 3])
3132
}
3233

3334
}

Tests/FaviconFinderTests/Toolbox/Extensions/RegexTests.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
//
88

99
@testable import FaviconFinder
10-
import XCTest
10+
import Testing
1111

12-
class RegexTests: XCTestCase {
12+
struct RegexTests {
1313

1414
// MARK: - Properties
1515

@@ -19,14 +19,16 @@ class RegexTests: XCTestCase {
1919

2020
// MARK: - Tests
2121

22-
func testRegexTest() {
22+
@Test("Test Regex")
23+
func regexTest() {
2324
let regex = Regex("go+gle")
24-
XCTAssert(regex.test(input: "goooooogle"))
25+
#expect(regex.test(input: "goooooogle") == true)
2526
}
2627

27-
func testRegexTestForHttpsOrHttp() {
28-
XCTAssert(Regex.testForHttpsOrHttp(input: httpWebsite))
29-
XCTAssert(Regex.testForHttpsOrHttp(input: httpsWebsite))
30-
XCTAssert(Regex.testForHttpsOrHttp(input: plainWebsite) == false)
28+
@Test("Test Regex for HTTP or HTTPS")
29+
func regexTestForHttpsOrHttp() {
30+
#expect(Regex.testForHttpsOrHttp(input: httpWebsite) == true)
31+
#expect(Regex.testForHttpsOrHttp(input: httpsWebsite) == true)
32+
#expect(Regex.testForHttpsOrHttp(input: plainWebsite) == false)
3133
}
3234
}

Tests/FaviconFinderTests/Toolbox/Extensions/StringRemovalTests.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
//
88

99
import FaviconFinder
10-
import XCTest
10+
import Testing
1111

12-
class StringRemovalTests: XCTestCase {
12+
struct StringRemovalTests {
1313

14-
func testRemoveEverythingAfter() {
14+
@Test("Test Remove Everything After")
15+
func removeEverythingAfter() {
1516
var str = "abcdef1234"
1617
str.removeEverythingAfter(str: "12")
1718

18-
XCTAssert(str == "abcdef", "RemoveEverythingAfter failed, str should be abcdef, but instead is \(str)")
19+
#expect(str == "abcdef")
1920
}
2021
}

Tests/FaviconFinderTests/Toolbox/Extensions/URLParsingTests.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,34 @@
77
//
88

99
import FaviconFinder
10-
import XCTest
10+
import Foundation
11+
import Testing
12+
13+
struct URLParsingTests {
1114

12-
class URLParsingTests: XCTestCase {
1315
let gmailUrl = URL(string: "https://mail.google.com")!
1416
let googleUrl = URL(string: "https://google.com")!
1517

1618
let appleAuUrl = URL(string: "https://apple.com/au")!
1719
let appleUrl = URL(string: "https://apple.com")!
1820

19-
func testUrlWithoutSubdomains() {
21+
@Test("URL Without Subdomains")
22+
func urlWithoutSubdomains() {
2023
guard let strippedGmailUrl = self.gmailUrl.urlWithoutSubdomains else {
21-
XCTAssert(false, "\(self.gmailUrl) without subdomains returned nil.")
24+
Issue.record("\(self.gmailUrl) without subdomains returned nil.")
2225
return
2326
}
2427

25-
XCTAssert(strippedGmailUrl == self.googleUrl, "Stripped Gmail URL returned \(strippedGmailUrl)")
28+
#expect(strippedGmailUrl == self.googleUrl)
2629
}
2730

28-
func testAbsoluteStringWithoutScheme() {
31+
@Test("Test AbsoluteString Without Scheme")
32+
func absoluteStringWithoutScheme() {
2933
guard let appleAuUrlWithoutScheme = self.appleAuUrl.absoluteStringWithoutScheme else {
30-
XCTAssert(false, "\(self.appleAuUrl) without scheme returned nil.")
34+
Issue.record("\(self.appleAuUrl) without subdomains returned nil.")
3135
return
3236
}
3337

34-
XCTAssert(appleAuUrlWithoutScheme == "apple.com/au", "Stripped AppleAu URL returned \(appleAuUrlWithoutScheme)")
38+
#expect(appleAuUrlWithoutScheme == "apple.com/au")
3539
}
3640
}

Tests/FaviconFinderTests/Toolbox/TestURLs.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
import Foundation
9-
import XCTest
9+
import Testing
1010

1111
enum TestURL: CaseIterable {
1212
case google
@@ -22,7 +22,7 @@ enum TestURL: CaseIterable {
2222
extension TestURL {
2323

2424
var url: URL {
25-
guard let url = try? XCTUnwrap(URL(string: self.urlStr)) else {
25+
guard let url = URL(string: self.urlStr) else {
2626
fatalError()
2727
}
2828
return url

0 commit comments

Comments
 (0)