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}
0 commit comments