|
2 | 2 | //
|
3 | 3 | // Copyright (c) 2015-2024 Alexander Grebenyuk (github.com/kean).
|
4 | 4 |
|
5 |
| -import XCTest |
| 5 | +import Foundation |
| 6 | +import Testing |
6 | 7 | @testable import Nuke
|
7 | 8 |
|
8 |
| -class ImageDecoderTests: XCTestCase { |
9 |
| - func testDecodePNG() throws { |
| 9 | +@Suite struct ImageDecoderTests { |
| 10 | + @Test func decodePNG() throws { |
10 | 11 | // Given
|
11 | 12 | let data = Test.data(name: "fixture", extension: "png")
|
12 | 13 | let decoder = ImageDecoders.Default()
|
13 |
| - |
| 14 | + |
14 | 15 | // When
|
15 |
| - let container = try XCTUnwrap(decoder.decode(data)) |
16 |
| - |
| 16 | + let container = try #require(try decoder.decode(data)) |
| 17 | + |
17 | 18 | // Then
|
18 |
| - XCTAssertEqual(container.type, .png) |
19 |
| - XCTAssertFalse(container.isPreview) |
20 |
| - XCTAssertNil(container.data) |
21 |
| - XCTAssertTrue(container.userInfo.isEmpty) |
| 19 | + #expect(container.type == .png) |
| 20 | + #expect(!container.isPreview) |
| 21 | + #expect(container.data == nil) |
| 22 | + #expect(container.userInfo.isEmpty) |
22 | 23 | }
|
23 |
| - |
24 |
| - func testDecodeJPEG() throws { |
| 24 | + |
| 25 | + @Test func decodeJPEG() throws { |
25 | 26 | // Given
|
26 | 27 | let data = Test.data(name: "baseline", extension: "jpeg")
|
27 | 28 | let decoder = ImageDecoders.Default()
|
28 |
| - |
| 29 | + |
29 | 30 | // When
|
30 |
| - let container = try XCTUnwrap(decoder.decode(data)) |
31 |
| - |
| 31 | + let container = try #require(try decoder.decode(data)) |
| 32 | + |
32 | 33 | // Then
|
33 |
| - XCTAssertEqual(container.type, .jpeg) |
34 |
| - XCTAssertFalse(container.isPreview) |
35 |
| - XCTAssertNil(container.data) |
36 |
| - XCTAssertTrue(container.userInfo.isEmpty) |
| 34 | + #expect(container.type == .jpeg) |
| 35 | + #expect(!container.isPreview) |
| 36 | + #expect(container.data == nil) |
| 37 | + #expect(container.userInfo.isEmpty) |
37 | 38 | }
|
38 |
| - |
39 |
| - func testDecodingProgressiveJPEG() { |
| 39 | + |
| 40 | + @Test func decodingProgressiveJPEG() { |
40 | 41 | let data = Test.data(name: "progressive", extension: "jpeg")
|
41 | 42 | let decoder = ImageDecoders.Default()
|
42 |
| - |
| 43 | + |
43 | 44 | // Just before the Start Of Frame
|
44 |
| - XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...358])) |
45 |
| - XCTAssertEqual(decoder.numberOfScans, 0) |
46 |
| - |
| 45 | + #expect(decoder.decodePartiallyDownloadedData(data[0...358]) == nil) |
| 46 | + #expect(decoder.numberOfScans == 0) |
| 47 | + |
47 | 48 | // Right after the Start Of Frame
|
48 |
| - XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...359])) |
49 |
| - XCTAssertEqual(decoder.numberOfScans, 0) // still haven't finished the first scan |
50 |
| - |
| 49 | + #expect(decoder.decodePartiallyDownloadedData(data[0...359]) == nil) |
| 50 | + #expect(decoder.numberOfScans == 0) // still haven't finished the first scan // still haven't finished the first scan |
| 51 | + |
51 | 52 | // Just before the first Start Of Scan
|
52 |
| - XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...438])) |
53 |
| - XCTAssertEqual(decoder.numberOfScans, 0) // still haven't finished the first scan |
54 |
| - |
| 53 | + #expect(decoder.decodePartiallyDownloadedData(data[0...438]) == nil) |
| 54 | + #expect(decoder.numberOfScans == 0) // still haven't finished the first scan // still haven't finished the first scan |
| 55 | + |
55 | 56 | // Found the first Start Of Scan
|
56 |
| - XCTAssertNil(decoder.decodePartiallyDownloadedData(data[0...439])) |
57 |
| - XCTAssertEqual(decoder.numberOfScans, 1) |
58 |
| - |
| 57 | + #expect(decoder.decodePartiallyDownloadedData(data[0...439]) == nil) |
| 58 | + #expect(decoder.numberOfScans == 1) |
| 59 | + |
59 | 60 | // Found the second Start of Scan
|
60 | 61 | let scan1 = decoder.decodePartiallyDownloadedData(data[0...2952])
|
61 |
| - XCTAssertNotNil(scan1) |
62 |
| - XCTAssertEqual(scan1?.isPreview, true) |
| 62 | + #expect(scan1 != nil) |
| 63 | + #expect(scan1?.isPreview == true) |
63 | 64 | if let image = scan1?.image {
|
64 | 65 | #if os(macOS)
|
65 |
| - XCTAssertEqual(image.size.width, 450) |
66 |
| - XCTAssertEqual(image.size.height, 300) |
| 66 | + #expect(image.size.width == 450) |
| 67 | + #expect(image.size.height == 300) |
67 | 68 | #else
|
68 |
| - XCTAssertEqual(image.size.width * image.scale, 450) |
69 |
| - XCTAssertEqual(image.size.height * image.scale, 300) |
| 69 | + #expect(image.size.width * image.scale == 450) |
| 70 | + #expect(image.size.height * image.scale == 300) |
70 | 71 | #endif
|
71 | 72 | }
|
72 |
| - XCTAssertEqual(decoder.numberOfScans, 2) |
73 |
| - XCTAssertEqual(scan1?.userInfo[.scanNumberKey] as? Int, 2) |
74 |
| - |
| 73 | + #expect(decoder.numberOfScans == 2) |
| 74 | + #expect(scan1?.userInfo[.scanNumberKey] as? Int == 2) |
| 75 | + |
75 | 76 | // Feed all data and see how many scans are there
|
76 | 77 | // In practice the moment we finish receiving data we call
|
77 | 78 | // `decode(data: data, isCompleted: true)` so we might not scan all the
|
78 | 79 | // of the bytes and encounter all of the scans (e.g. the final chunk
|
79 | 80 | // of data that we receive contains multiple scans).
|
80 |
| - XCTAssertNotNil(decoder.decodePartiallyDownloadedData(data)) |
81 |
| - XCTAssertEqual(decoder.numberOfScans, 10) |
| 81 | + #expect(decoder.decodePartiallyDownloadedData(data) != nil) |
| 82 | + #expect(decoder.numberOfScans == 10) |
82 | 83 | }
|
83 |
| - |
84 |
| - func testDecodeGIF() throws { |
| 84 | + |
| 85 | + @Test func decodeGIF() throws { |
85 | 86 | // Given
|
86 | 87 | let data = Test.data(name: "cat", extension: "gif")
|
87 | 88 | let decoder = ImageDecoders.Default()
|
88 |
| - |
| 89 | + |
89 | 90 | // When
|
90 |
| - let container = try XCTUnwrap(decoder.decode(data)) |
91 |
| - |
| 91 | + let container = try #require(try decoder.decode(data)) |
| 92 | + |
92 | 93 | // Then
|
93 |
| - XCTAssertEqual(container.type, .gif) |
94 |
| - XCTAssertFalse(container.isPreview) |
95 |
| - XCTAssertNotNil(container.data) |
96 |
| - XCTAssertTrue(container.userInfo.isEmpty) |
| 94 | + #expect(container.type == .gif) |
| 95 | + #expect(!container.isPreview) |
| 96 | + #expect(container.data != nil) |
| 97 | + #expect(container.userInfo.isEmpty) |
97 | 98 | }
|
98 |
| - |
99 |
| - func testDecodeHEIC() throws { |
| 99 | + |
| 100 | + @Test func decodeHEIC() throws { |
100 | 101 | // Given
|
101 | 102 | let data = Test.data(name: "img_751", extension: "heic")
|
102 | 103 | let decoder = ImageDecoders.Default()
|
103 |
| - |
| 104 | + |
104 | 105 | // When
|
105 |
| - let container = try XCTUnwrap(decoder.decode(data)) |
106 |
| - |
| 106 | + let container = try #require(try decoder.decode(data)) |
| 107 | + |
107 | 108 | // Then
|
108 |
| - XCTAssertNil(container.type) // TODO: update when HEIF support is added |
109 |
| - XCTAssertFalse(container.isPreview) |
110 |
| - XCTAssertNil(container.data) |
111 |
| - XCTAssertTrue(container.userInfo.isEmpty) |
| 109 | + #expect(container.type == nil) // TODO: update when HEIF support is added // TODO: update when HEIF support is added |
| 110 | + #expect(!container.isPreview) |
| 111 | + #expect(container.data == nil) |
| 112 | + #expect(container.userInfo.isEmpty) |
112 | 113 | }
|
113 |
| - |
114 |
| - func testDecodingGIFDataAttached() throws { |
| 114 | + |
| 115 | + @Test func decodingGIFDataAttached() throws { |
115 | 116 | let data = Test.data(name: "cat", extension: "gif")
|
116 |
| - XCTAssertNotNil(try ImageDecoders.Default().decode(data).data) |
| 117 | + #expect(try ImageDecoders.Default().decode(data).data != nil) |
117 | 118 | }
|
118 |
| - |
119 |
| - func testDecodingGIFPreview() throws { |
| 119 | + |
| 120 | + @Test func decodingGIFPreview() throws { |
120 | 121 | let data = Test.data(name: "cat", extension: "gif")
|
121 |
| - XCTAssertEqual(data.count, 427672) // 427 KB |
| 122 | + #expect(data.count == 427672) // 427 KB // 427 KB |
122 | 123 | let chunk = data[...60000] // 6 KB
|
123 | 124 | let response = try ImageDecoders.Default().decode(chunk)
|
124 |
| - XCTAssertEqual(response.image.sizeInPixels, CGSize(width: 500, height: 279)) |
| 125 | + #expect(response.image.sizeInPixels == CGSize(width: 500, height: 279)) |
125 | 126 | }
|
126 |
| - |
127 |
| - func testDecodingGIFPreviewGeneratedOnlyOnce() throws { |
| 127 | + |
| 128 | + @Test func decodingGIFPreviewGeneratedOnlyOnce() throws { |
128 | 129 | let data = Test.data(name: "cat", extension: "gif")
|
129 |
| - XCTAssertEqual(data.count, 427672) // 427 KB |
| 130 | + #expect(data.count == 427672) // 427 KB // 427 KB |
130 | 131 | let chunk = data[...60000] // 6 KB
|
131 |
| - |
| 132 | + |
132 | 133 | let context = ImageDecodingContext.mock(data: chunk)
|
133 |
| - let decoder = try XCTUnwrap(ImageDecoders.Default(context: context)) |
134 |
| - |
135 |
| - XCTAssertNotNil(decoder.decodePartiallyDownloadedData(chunk)) |
136 |
| - XCTAssertNil(decoder.decodePartiallyDownloadedData(chunk)) |
| 134 | + let decoder = try #require(ImageDecoders.Default(context: context)) |
| 135 | + |
| 136 | + #expect(decoder.decodePartiallyDownloadedData(chunk) != nil) |
| 137 | + #expect(decoder.decodePartiallyDownloadedData(chunk) == nil) |
137 | 138 | }
|
138 |
| - |
139 |
| - func testDecodingPNGDataNotAttached() throws { |
| 139 | + |
| 140 | + @Test func decodingPNGDataNotAttached() throws { |
140 | 141 | let data = Test.data(name: "fixture", extension: "png")
|
141 | 142 | let container = try ImageDecoders.Default().decode(data)
|
142 |
| - XCTAssertNil(container.data) |
| 143 | + #expect(container.data == nil) |
143 | 144 | }
|
144 |
| - |
| 145 | + |
145 | 146 | #if os(iOS) || os(tvOS) || os(macOS) || os(visionOS)
|
146 |
| - func testDecodeBaselineWebP() throws { |
| 147 | + @Test func decodeBaselineWebP() throws { |
147 | 148 | if #available(OSX 11.0, iOS 14.0, watchOS 7.0, tvOS 999.0, *) {
|
148 | 149 | let data = Test.data(name: "baseline", extension: "webp")
|
149 | 150 | let container = try ImageDecoders.Default().decode(data)
|
150 |
| - XCTAssertEqual(container.image.sizeInPixels, CGSize(width: 550, height: 368)) |
151 |
| - XCTAssertNil(container.data) |
| 151 | + #expect(container.image.sizeInPixels == CGSize(width: 550, height: 368)) |
| 152 | + #expect(container.data == nil) |
152 | 153 | }
|
153 | 154 | }
|
154 | 155 | #endif
|
155 | 156 | }
|
156 | 157 |
|
157 |
| -class ImageTypeTests: XCTestCase { |
| 158 | +@Suite struct ImageTypeTests { |
158 | 159 | // MARK: PNG
|
159 |
| - |
160 |
| - func testDetectPNG() { |
| 160 | + |
| 161 | + @Test func detectPNG() { |
161 | 162 | let data = Test.data(name: "fixture", extension: "png")
|
162 |
| - XCTAssertNil(AssetType(data[0..<1])) |
163 |
| - XCTAssertNil(AssetType(data[0..<7])) |
164 |
| - XCTAssertEqual(AssetType(data[0..<8]), .png) |
165 |
| - XCTAssertEqual(AssetType(data), .png) |
| 163 | + #expect(AssetType(data[0..<1]) == nil) |
| 164 | + #expect(AssetType(data[0..<7]) == nil) |
| 165 | + #expect(AssetType(data[0..<8]) == .png) |
| 166 | + #expect(AssetType(data) == .png) |
166 | 167 | }
|
167 |
| - |
| 168 | + |
168 | 169 | // MARK: GIF
|
169 |
| - |
170 |
| - func testDetectGIF() { |
| 170 | + |
| 171 | + @Test func detectGIF() { |
171 | 172 | let data = Test.data(name: "cat", extension: "gif")
|
172 |
| - XCTAssertEqual(AssetType(data), .gif) |
| 173 | + #expect(AssetType(data) == .gif) |
173 | 174 | }
|
174 |
| - |
| 175 | + |
175 | 176 | // MARK: JPEG
|
176 |
| - |
177 |
| - func testDetectBaselineJPEG() { |
| 177 | + |
| 178 | + @Test func detectBaselineJPEG() { |
178 | 179 | let data = Test.data(name: "baseline", extension: "jpeg")
|
179 |
| - XCTAssertNil(AssetType(data[0..<1])) |
180 |
| - XCTAssertNil(AssetType(data[0..<2])) |
181 |
| - XCTAssertEqual(AssetType(data[0..<3]), .jpeg) |
182 |
| - XCTAssertEqual(AssetType(data), .jpeg) |
| 180 | + #expect(AssetType(data[0..<1]) == nil) |
| 181 | + #expect(AssetType(data[0..<2]) == nil) |
| 182 | + #expect(AssetType(data[0..<3]) == .jpeg) |
| 183 | + #expect(AssetType(data) == .jpeg) |
183 | 184 | }
|
184 |
| - |
185 |
| - func testDetectProgressiveJPEG() { |
| 185 | + |
| 186 | + @Test func detectProgressiveJPEG() { |
186 | 187 | let data = Test.data(name: "progressive", extension: "jpeg")
|
187 | 188 | // Not enough data
|
188 |
| - XCTAssertNil(AssetType(Data())) |
189 |
| - XCTAssertNil(AssetType(data[0..<2])) |
190 |
| - |
| 189 | + #expect(AssetType(Data()) == nil) |
| 190 | + #expect(AssetType(data[0..<2]) == nil) |
| 191 | + |
191 | 192 | // Enough to determine image format
|
192 |
| - XCTAssertEqual(AssetType(data[0..<3]), .jpeg) |
193 |
| - XCTAssertEqual(AssetType(data[0..<33]), .jpeg) |
194 |
| - |
| 193 | + #expect(AssetType(data[0..<3]) == .jpeg) |
| 194 | + #expect(AssetType(data[0..<33]) == .jpeg) |
| 195 | + |
195 | 196 | // Full image
|
196 |
| - XCTAssertEqual(AssetType(data), .jpeg) |
| 197 | + #expect(AssetType(data) == .jpeg) |
197 | 198 | }
|
198 |
| - |
| 199 | + |
199 | 200 | // MARK: WebP
|
200 |
| - |
201 |
| - func testDetectBaselineWebP() { |
| 201 | + |
| 202 | + @Test func detectBaselineWebP() { |
202 | 203 | let data = Test.data(name: "baseline", extension: "webp")
|
203 |
| - XCTAssertNil(AssetType(data[0..<1])) |
204 |
| - XCTAssertNil(AssetType(data[0..<2])) |
205 |
| - XCTAssertEqual(AssetType(data[0..<12]), .webp) |
206 |
| - XCTAssertEqual(AssetType(data), .webp) |
| 204 | + #expect(AssetType(data[0..<1]) == nil) |
| 205 | + #expect(AssetType(data[0..<2]) == nil) |
| 206 | + #expect(AssetType(data[0..<12]) == .webp) |
| 207 | + #expect(AssetType(data) == .webp) |
207 | 208 | }
|
208 | 209 | }
|
209 | 210 |
|
210 |
| -class ImagePropertiesTests: XCTestCase { |
| 211 | +@Suite struct ImagePropertiesTests { |
211 | 212 | // MARK: JPEG
|
212 |
| - |
213 |
| - func testDetectBaselineJPEG() { |
| 213 | + |
| 214 | + @Test func detectBaselineJPEG() { |
214 | 215 | let data = Test.data(name: "baseline", extension: "jpeg")
|
215 |
| - XCTAssertNil(ImageProperties.JPEG(data[0..<1])) |
216 |
| - XCTAssertNil(ImageProperties.JPEG(data[0..<2])) |
217 |
| - XCTAssertNil(ImageProperties.JPEG(data[0..<3])) |
218 |
| - XCTAssertEqual(ImageProperties.JPEG(data)?.isProgressive, false) |
| 216 | + #expect(ImageProperties.JPEG(data[0..<1]) == nil) |
| 217 | + #expect(ImageProperties.JPEG(data[0..<2]) == nil) |
| 218 | + #expect(ImageProperties.JPEG(data[0..<3]) == nil) |
| 219 | + #expect(ImageProperties.JPEG(data)?.isProgressive == false) |
219 | 220 | }
|
220 |
| - |
221 |
| - func testDetectProgressiveJPEG() { |
| 221 | + |
| 222 | + @Test func detectProgressiveJPEG() { |
222 | 223 | let data = Test.data(name: "progressive", extension: "jpeg")
|
223 | 224 | // Not enough data
|
224 |
| - XCTAssertNil(ImageProperties.JPEG(Data())) |
225 |
| - XCTAssertNil(ImageProperties.JPEG(data[0..<2])) |
226 |
| - |
| 225 | + #expect(ImageProperties.JPEG(Data()) == nil) |
| 226 | + #expect(ImageProperties.JPEG(data[0..<2]) == nil) |
| 227 | + |
227 | 228 | // Enough to determine image format
|
228 |
| - XCTAssertNil(ImageProperties.JPEG(data[0..<3])) |
229 |
| - XCTAssertNil(ImageProperties.JPEG(data[0...30])) |
230 |
| - |
| 229 | + #expect(ImageProperties.JPEG(data[0..<3]) == nil) |
| 230 | + #expect(ImageProperties.JPEG(data[0...30]) == nil) |
| 231 | + |
231 | 232 | // Just before the first scan
|
232 |
| - XCTAssertNil(ImageProperties.JPEG(data[0...358])) |
233 |
| - XCTAssertEqual(ImageProperties.JPEG(data[0...359])?.isProgressive, true) |
234 |
| - |
| 233 | + #expect(ImageProperties.JPEG(data[0...358]) == nil) |
| 234 | + #expect(ImageProperties.JPEG(data[0...359])?.isProgressive == true) |
| 235 | + |
235 | 236 | // Full image
|
236 |
| - XCTAssertEqual(ImageProperties.JPEG(data[0...359])?.isProgressive, true) |
| 237 | + #expect(ImageProperties.JPEG(data[0...359])?.isProgressive == true) |
237 | 238 | }
|
238 | 239 | }
|
0 commit comments