10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- #if canImport(TestSupport)
14
- import TestSupport
15
- #endif
16
-
17
13
#if FOUNDATION_FRAMEWORK
18
14
15
+ import Testing
16
+ import Foundation
17
+
19
18
fileprivate protocol PredicateCodingConfigurationProviding : EncodingConfigurationProviding , DecodingConfigurationProviding where EncodingConfiguration == PredicateCodableConfiguration , DecodingConfiguration == PredicateCodableConfiguration {
20
19
static var config : PredicateCodableConfiguration { get }
21
20
}
@@ -57,7 +56,8 @@ extension PredicateExpressions {
57
56
}
58
57
}
59
58
60
- final class PredicateCodableTests : XCTestCase {
59
+ @Suite ( " Predicate Codable " )
60
+ private struct PredicateCodableTests {
61
61
62
62
struct Object : Equatable , PredicateCodableKeyPathProviding {
63
63
var a : Int
@@ -192,163 +192,183 @@ final class PredicateCodableTests: XCTestCase {
192
192
return try decoder. decode ( T . self, from: data)
193
193
}
194
194
195
- func testBasicEncodeDecode ( ) throws {
195
+ @ Test func basicEncodeDecode ( ) throws {
196
196
let predicate = #Predicate< Object> {
197
197
$0. a == 2
198
198
}
199
199
200
200
let decoded = try _encodeDecode ( predicate, for: StandardConfig . self)
201
201
var object = Object . example
202
- XCTAssertEqual ( try predicate. evaluate ( object) , try decoded. evaluate ( object) )
202
+ #expect ( try predicate. evaluate ( object) == decoded. evaluate ( object) )
203
203
object. a = 2
204
- XCTAssertEqual ( try predicate. evaluate ( object) , try decoded. evaluate ( object) )
204
+ #expect ( try predicate. evaluate ( object) == decoded. evaluate ( object) )
205
205
object. a = 3
206
- XCTAssertEqual ( try predicate. evaluate ( object) , try decoded. evaluate ( object) )
206
+ #expect ( try predicate. evaluate ( object) == decoded. evaluate ( object) )
207
207
208
- XCTAssertThrowsError ( try _encodeDecode ( predicate, for: EmptyConfig . self) )
209
- XCTAssertThrowsError ( try _encodeDecode ( predicate) )
208
+ #expect( throws: ( any Error ) . self) {
209
+ try _encodeDecode ( predicate, for: EmptyConfig . self)
210
+ }
211
+ #expect( throws: ( any Error ) . self) {
212
+ try _encodeDecode ( predicate)
213
+ }
210
214
}
211
215
212
- func testDisallowedKeyPath ( ) throws {
216
+ @ Test func disallowedKeyPath ( ) throws {
213
217
var predicate = #Predicate< Object> {
214
218
$0. f
215
219
}
216
220
217
- XCTAssertThrowsError ( try _encodeDecode ( predicate) )
218
- XCTAssertThrowsError ( try _encodeDecode ( predicate, for: StandardConfig . self) )
221
+ #expect( throws: ( any Error ) . self) {
222
+ try _encodeDecode ( predicate)
223
+ }
224
+ #expect( throws: ( any Error ) . self) {
225
+ try _encodeDecode ( predicate, for: StandardConfig . self)
226
+ }
219
227
220
228
predicate = #Predicate< Object> {
221
229
$0. a == 1
222
230
}
223
- XCTAssertThrowsError ( try _encodeDecode ( predicate, encoding: StandardConfig . self, decoding: MinimalConfig . self) ) {
224
- guard let decodingError = $0 as? DecodingError else {
225
- XCTFail ( " Incorrect error thrown: \( $0) " )
226
- return
227
- }
228
- XCTAssertEqual ( decodingError. debugDescription, " A keypath for the 'Object.a' identifier is not in the provided allowlist " )
231
+ #expect {
232
+ try _encodeDecode ( predicate, encoding: StandardConfig . self, decoding: MinimalConfig . self)
233
+ } throws: {
234
+ let decodingError = try #require( $0 as? DecodingError )
235
+ return decodingError. debugDescription == " A keypath for the 'Object.a' identifier is not in the provided allowlist "
229
236
}
230
237
}
231
238
232
- func testKeyPathTypeMismatch ( ) throws {
239
+ @ Test func keyPathTypeMismatch ( ) throws {
233
240
let predicate = #Predicate< Object> {
234
241
$0. a == 2
235
242
}
236
243
237
244
try _encodeDecode ( predicate, for: StandardConfig . self)
238
- XCTAssertThrowsError ( try _encodeDecode ( predicate, encoding: StandardConfig . self, decoding: MismatchedKeyPathConfig . self) ) {
239
- guard let decodingError = $0 as? DecodingError else {
240
- XCTFail ( " Incorrect error thrown: \( $0) " )
241
- return
242
- }
243
- XCTAssertEqual ( decodingError. debugDescription, " Key path ' \\ Object.b' (KeyPath< \( _typeName ( Object . self) ) , Swift.String>) for identifier 'Object.a' did not match the expression's requirement for KeyPath< \( _typeName ( Object . self) ) , Swift.Int> " )
245
+ #expect {
246
+ try _encodeDecode ( predicate, encoding: StandardConfig . self, decoding: MismatchedKeyPathConfig . self)
247
+ } throws: {
248
+ let decodingError = try #require( $0 as? DecodingError )
249
+ return decodingError. debugDescription == " Key path ' \\ Object.b' (KeyPath< \( _typeName ( Object . self) ) , Swift.String>) for identifier 'Object.a' did not match the expression's requirement for KeyPath< \( _typeName ( Object . self) ) , Swift.Int> "
244
250
}
245
251
}
246
252
247
- func testDisallowedType ( ) throws {
253
+ @ Test func disallowedType ( ) throws {
248
254
let uuid = UUID ( )
249
255
let predicate = #Predicate< Object> { obj in
250
256
uuid == uuid
251
257
}
252
258
253
- XCTAssertThrowsError ( try _encodeDecode ( predicate) )
254
- XCTAssertThrowsError ( try _encodeDecode ( predicate, for: StandardConfig . self) )
255
- XCTAssertThrowsError ( try _encodeDecode ( predicate, encoding: UUIDConfig . self, decoding: MinimalConfig . self) ) {
256
- XCTAssertEqual ( String ( describing: $0) , " The 'Foundation.UUID' identifier is not in the provided allowlist (required by /PredicateExpressions.Equal/PredicateExpressions.Value) " )
259
+ #expect( throws: ( any Error ) . self) {
260
+ try _encodeDecode ( predicate)
261
+ }
262
+ #expect( throws: ( any Error ) . self) {
263
+ try _encodeDecode ( predicate, for: StandardConfig . self)
264
+ }
265
+ #expect {
266
+ try _encodeDecode ( predicate, encoding: UUIDConfig . self, decoding: MinimalConfig . self)
267
+ } throws: {
268
+ String ( describing: $0) == " The 'Foundation.UUID' identifier is not in the provided allowlist (required by /PredicateExpressions.Equal/PredicateExpressions.Value) "
257
269
}
258
270
259
271
let decoded = try _encodeDecode ( predicate, for: UUIDConfig . self)
260
- XCTAssertEqual ( try decoded. evaluate ( . example) , try predicate. evaluate ( . example) )
272
+ #expect ( try decoded. evaluate ( . example) == predicate. evaluate ( . example) )
261
273
}
262
274
263
- func testProvidedProperties ( ) throws {
275
+ @ Test func providedProperties ( ) throws {
264
276
var predicate = #Predicate< Object> {
265
277
$0. a == 2
266
278
}
267
279
268
- XCTAssertThrowsError ( try _encodeDecode ( predicate, for: ProvidedKeyPathConfig . self) )
269
- XCTAssertThrowsError ( try _encodeDecode ( predicate, for: RecursiveProvidedKeyPathConfig . self) )
280
+ #expect( throws: ( any Error ) . self) {
281
+ try _encodeDecode ( predicate, for: ProvidedKeyPathConfig . self)
282
+ }
283
+ #expect( throws: ( any Error ) . self) {
284
+ try _encodeDecode ( predicate, for: RecursiveProvidedKeyPathConfig . self)
285
+ }
270
286
271
287
predicate = #Predicate< Object> {
272
288
$0. f == false
273
289
}
274
290
275
291
var decoded = try _encodeDecode ( predicate, for: ProvidedKeyPathConfig . self)
276
- XCTAssertEqual ( try decoded. evaluate ( . example) , try predicate. evaluate ( . example) )
292
+ #expect ( try decoded. evaluate ( . example) == predicate. evaluate ( . example) )
277
293
decoded = try _encodeDecode ( predicate, for: RecursiveProvidedKeyPathConfig . self)
278
- XCTAssertEqual ( try decoded. evaluate ( . example) , try predicate. evaluate ( . example) )
294
+ #expect ( try decoded. evaluate ( . example) == predicate. evaluate ( . example) )
279
295
280
296
predicate = #Predicate< Object> {
281
297
$0. h. a == 1
282
298
}
283
299
284
- XCTAssertThrowsError ( try _encodeDecode ( predicate, for: ProvidedKeyPathConfig . self) )
300
+ #expect( throws: ( any Error ) . self) {
301
+ try _encodeDecode ( predicate, for: ProvidedKeyPathConfig . self)
302
+ }
285
303
decoded = try _encodeDecode ( predicate, for: RecursiveProvidedKeyPathConfig . self)
286
- XCTAssertEqual ( try decoded. evaluate ( . example) , try predicate. evaluate ( . example) )
304
+ #expect ( try decoded. evaluate ( . example) == predicate. evaluate ( . example) )
287
305
}
288
306
289
- func testDefaultAllowlist ( ) throws {
307
+ @ Test func defaultAllowlist ( ) throws {
290
308
var predicate = #Predicate< String> {
291
309
$0. isEmpty
292
310
}
293
311
var decoded = try _encodeDecode ( predicate)
294
- XCTAssertEqual ( try decoded. evaluate ( " Hello world " ) , try predicate. evaluate ( " Hello world " ) )
312
+ #expect ( try decoded. evaluate ( " Hello world " ) == predicate. evaluate ( " Hello world " ) )
295
313
296
314
predicate = #Predicate< String> {
297
315
$0. count > 2
298
316
}
299
317
decoded = try _encodeDecode ( predicate)
300
- XCTAssertEqual ( try decoded. evaluate ( " Hello world " ) , try predicate. evaluate ( " Hello world " ) )
318
+ #expect ( try decoded. evaluate ( " Hello world " ) == predicate. evaluate ( " Hello world " ) )
301
319
302
320
predicate = #Predicate< String> {
303
321
$0. contains ( /[a-z]/ )
304
322
}
305
323
decoded = try _encodeDecode ( predicate)
306
- XCTAssertEqual ( try decoded. evaluate ( " Hello world " ) , try predicate. evaluate ( " Hello world " ) )
324
+ #expect ( try decoded. evaluate ( " Hello world " ) == predicate. evaluate ( " Hello world " ) )
307
325
308
326
let predicate2 = #Predicate< Object> {
309
327
$0 == $0
310
328
}
311
329
let decoded2 = try _encodeDecode ( predicate2)
312
- XCTAssertEqual ( try decoded2. evaluate ( . example) , try predicate2. evaluate ( . example) )
330
+ #expect ( try decoded2. evaluate ( . example) == predicate2. evaluate ( . example) )
313
331
314
332
315
333
var predicate3 = #Predicate< Array< String>> {
316
334
$0. isEmpty
317
335
}
318
336
var decoded3 = try _encodeDecode ( predicate3)
319
- XCTAssertEqual ( try decoded3. evaluate ( [ " A " , " B " , " C " ] ) , try predicate3. evaluate ( [ " A " , " B " , " C " ] ) )
337
+ #expect ( try decoded3. evaluate ( [ " A " , " B " , " C " ] ) == predicate3. evaluate ( [ " A " , " B " , " C " ] ) )
320
338
321
339
predicate3 = #Predicate< Array< String>> {
322
340
$0. count == 2
323
341
}
324
342
decoded3 = try _encodeDecode ( predicate3)
325
- XCTAssertEqual ( try decoded3. evaluate ( [ " A " , " B " , " C " ] ) , try predicate3. evaluate ( [ " A " , " B " , " C " ] ) )
343
+ #expect ( try decoded3. evaluate ( [ " A " , " B " , " C " ] ) == predicate3. evaluate ( [ " A " , " B " , " C " ] ) )
326
344
327
345
var predicate4 = #Predicate< Dictionary< String, Int >> {
328
346
$0. isEmpty
329
347
}
330
348
var decoded4 = try _encodeDecode ( predicate4)
331
- XCTAssertEqual ( try decoded4. evaluate ( [ " A " : 1 , " B " : 2 , " C " : 3 ] ) , try predicate4. evaluate ( [ " A " : 1 , " B " : 2 , " C " : 3 ] ) )
349
+ #expect ( try decoded4. evaluate ( [ " A " : 1 , " B " : 2 , " C " : 3 ] ) == predicate4. evaluate ( [ " A " : 1 , " B " : 2 , " C " : 3 ] ) )
332
350
333
351
predicate4 = #Predicate< Dictionary< String, Int>> {
334
352
$0. count == 2
335
353
}
336
354
decoded4 = try _encodeDecode ( predicate4)
337
- XCTAssertEqual ( try decoded4. evaluate ( [ " A " : 1 , " B " : 2 , " C " : 3 ] ) , try predicate4. evaluate ( [ " A " : 1 , " B " : 2 , " C " : 3 ] ) )
355
+ #expect ( try decoded4. evaluate ( [ " A " : 1 , " B " : 2 , " C " : 3 ] ) == predicate4. evaluate ( [ " A " : 1 , " B " : 2 , " C " : 3 ] ) )
338
356
339
357
let predicate5 = #Predicate< Int> {
340
358
( 0 ..< 4 ) . contains ( $0)
341
359
}
342
360
let decoded5 = try _encodeDecode ( predicate5)
343
- XCTAssertEqual ( try decoded5. evaluate ( 2 ) , try predicate5. evaluate ( 2 ) )
361
+ #expect ( try decoded5. evaluate ( 2 ) == predicate5. evaluate ( 2 ) )
344
362
}
345
363
346
- func testMalformedData ( ) {
347
- func _malformedDecode< T: PredicateCodingConfigurationProviding > ( _ json: String , config: T . Type = StandardConfig . self, reason: String , file : StaticString = #filePath , line : UInt = #line ) {
364
+ @ Test func malformedData ( ) {
365
+ func _malformedDecode< T: PredicateCodingConfigurationProviding > ( _ json: String , config: T . Type = StandardConfig . self, reason: String , sourceLocation : SourceLocation = #_sourceLocation ) {
348
366
let data = Data ( json. utf8)
349
367
let decoder = JSONDecoder ( )
350
- XCTAssertThrowsError ( try decoder. decode ( CodableConfiguration < Predicate < Object > , T > . self, from: data) , file: file, line: line) {
351
- XCTAssertTrue ( String ( describing: $0) . contains ( reason) , " Error ' \( $0) ' did not contain reason ' \( reason) ' " , file: file, line: line)
368
+ #expect( sourceLocation: sourceLocation) {
369
+ try decoder. decode ( CodableConfiguration < Predicate < Object > , T > . self, from: data)
370
+ } throws: {
371
+ String ( describing: $0) . contains ( reason)
352
372
}
353
373
}
354
374
@@ -424,25 +444,29 @@ final class PredicateCodableTests: XCTestCase {
424
444
)
425
445
}
426
446
427
- func testBasicVariadic ( ) throws {
447
+ @Test func basicVariadic ( ) throws {
428
448
let predicate = #Predicate< Object, Object > {
429
449
$0. a == 2 && $1. a == 3
430
450
}
431
451
432
452
let decoded = try _encodeDecode ( predicate, for: StandardConfig . self)
433
453
var object = Object . example
434
454
let object2 = Object . example
435
- XCTAssertEqual ( try predicate. evaluate ( object, object2) , try decoded. evaluate ( object, object2) )
455
+ #expect ( try predicate. evaluate ( object, object2) == decoded. evaluate ( object, object2) )
436
456
object. a = 2
437
- XCTAssertEqual ( try predicate. evaluate ( object, object2) , try decoded. evaluate ( object, object2) )
457
+ #expect ( try predicate. evaluate ( object, object2) == decoded. evaluate ( object, object2) )
438
458
object. a = 3
439
- XCTAssertEqual ( try predicate. evaluate ( object, object2) , try decoded. evaluate ( object, object2) )
459
+ #expect ( try predicate. evaluate ( object, object2) == decoded. evaluate ( object, object2) )
440
460
441
- XCTAssertThrowsError ( try _encodeDecode ( predicate, for: EmptyConfig . self) )
442
- XCTAssertThrowsError ( try _encodeDecode ( predicate) )
461
+ #expect( throws: ( any Error ) . self) {
462
+ try _encodeDecode ( predicate, for: EmptyConfig . self)
463
+ }
464
+ #expect( throws: ( any Error ) . self) {
465
+ try _encodeDecode ( predicate)
466
+ }
443
467
}
444
468
445
- func testCapturedVariadicTypes ( ) throws {
469
+ @Test func capturedVariadicTypes ( ) throws {
446
470
struct A < each T > : Equatable , Codable {
447
471
init ( _: repeat ( each T ) . Type) { }
448
472
@@ -475,10 +499,10 @@ final class PredicateCodableTests: XCTestCase {
475
499
}
476
500
477
501
let decoded = try _encodeDecode ( predicate, for: CustomConfig . self)
478
- XCTAssertEqual ( try decoded. evaluate ( 2 ) , try predicate. evaluate ( 2 ) )
502
+ #expect ( try decoded. evaluate ( 2 ) == predicate. evaluate ( 2 ) )
479
503
}
480
504
481
- func testNestedPredicates ( ) throws {
505
+ @Test func nestedPredicates ( ) throws {
482
506
let predicateA = #Predicate< Object> {
483
507
$0. a == 3
484
508
}
@@ -498,11 +522,11 @@ final class PredicateCodableTests: XCTestCase {
498
522
]
499
523
500
524
for object in objects {
501
- XCTAssertEqual ( try decoded. evaluate ( object) , try predicateB. evaluate ( object) , " Evaluation failed to produce equal results for \( object) " )
525
+ #expect ( try decoded. evaluate ( object) == predicateB. evaluate ( object) , " Evaluation failed to produce equal results for \( object) " )
502
526
}
503
527
}
504
528
505
- func testNestedPredicateRestrictedConfiguration ( ) throws {
529
+ @Test func nestedPredicateRestrictedConfiguration ( ) throws {
506
530
struct RestrictedBox < each T > : Codable {
507
531
let predicate : Predicate < repeat each T >
508
532
@@ -541,24 +565,30 @@ final class PredicateCodableTests: XCTestCase {
541
565
}
542
566
543
567
// Throws an error because the sub-predicate's configuration won't contain anything in the allowlist
544
- XCTAssertThrowsError ( try _encodeDecode ( predicateB, for: CustomConfig . self) )
568
+ #expect( throws: ( any Error ) . self) {
569
+ try _encodeDecode ( predicateB, for: CustomConfig . self)
570
+ }
545
571
}
546
572
547
- func testExpression ( ) throws {
573
+ @Test func expression ( ) throws {
548
574
let expression = #Expression< Object, Int > {
549
575
$0. a
550
576
}
551
577
let decoded = try _encodeDecode ( expression, for: StandardConfig . self)
552
578
var object = Object . example
553
- XCTAssertEqual ( try expression. evaluate ( object) , try decoded. evaluate ( object) )
579
+ #expect ( try expression. evaluate ( object) == decoded. evaluate ( object) )
554
580
object. a = 2
555
- XCTAssertEqual ( try expression. evaluate ( object) , try decoded. evaluate ( object) )
581
+ #expect ( try expression. evaluate ( object) == decoded. evaluate ( object) )
556
582
object. a = 3
557
- XCTAssertEqual ( try expression. evaluate ( object) , try decoded. evaluate ( object) )
583
+ #expect ( try expression. evaluate ( object) == decoded. evaluate ( object) )
558
584
559
- XCTAssertThrowsError ( try _encodeDecode ( expression, for: EmptyConfig . self) )
560
- XCTAssertThrowsError ( try _encodeDecode ( expression) )
585
+ #expect( throws: ( any Error ) . self) {
586
+ try _encodeDecode ( expression, for: EmptyConfig . self)
587
+ }
588
+ #expect( throws: ( any Error ) . self) {
589
+ try _encodeDecode ( expression)
590
+ }
561
591
}
562
592
}
563
593
564
- #endif // FOUNDATION_FRAMEWORK
594
+ #endif
0 commit comments