1
- // This source file is part of the Swift.org open source project
1
+ // This source file is part of the Swift.org open source project
2
2
//
3
3
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
4
4
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8
8
//
9
9
10
-
11
-
12
10
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
13
11
import Foundation
14
12
import XCTest
@@ -17,7 +15,93 @@ import SwiftFoundation
17
15
import SwiftXCTest
18
16
#endif
19
17
18
+ private struct Box {
19
+ fileprivate let ns : NSCharacterSet
20
+ fileprivate let swift : CharacterSet
21
+
22
+ private init ( ns: NSCharacterSet , swift: CharacterSet ) {
23
+ self . ns = ns
24
+ self . swift = swift
25
+ }
26
+
27
+ init ( charactersIn string: String ) {
28
+ self . ns = NSCharacterSet ( charactersIn: string)
29
+ self . swift = CharacterSet ( charactersIn: string)
30
+ }
31
+
32
+ static var alphanumerics : Box {
33
+ return Box ( ns: NSCharacterSet . alphanumerics. _bridgeToObjectiveC ( ) ,
34
+ swift: CharacterSet . alphanumerics)
35
+ }
36
+
37
+ static var decimalDigits : Box {
38
+ return Box ( ns: NSCharacterSet . decimalDigits. _bridgeToObjectiveC ( ) ,
39
+ swift: CharacterSet . decimalDigits)
40
+ }
41
+ }
42
+
43
+ private func assertEqual( _ lhs: Box ,
44
+ _ rhs: Box ,
45
+ _ message: @autoclosure ( ) -> String = " " ,
46
+ file: StaticString = #file,
47
+ line: UInt = #line) {
48
+
49
+ assert ( equal: true , lhs, rhs, message, file: file, line: line)
50
+ }
51
+
52
+ private func assertNotEqual( _ lhs: Box ,
53
+ _ rhs: Box ,
54
+ _ message: @autoclosure ( ) -> String = " " ,
55
+ file: StaticString = #file,
56
+ line: UInt = #line) {
57
+
58
+ assert ( equal: false , lhs, rhs, message, file: file, line: line)
59
+ }
60
+
61
+ private func assert< T: Equatable > ( equal: Bool ,
62
+ _ lhs: T ,
63
+ _ rhs: T ,
64
+ _ message: @autoclosure ( ) -> String = " " ,
65
+ file: StaticString = #file,
66
+ line: UInt = #line) {
67
+
68
+ if equal {
69
+ XCTAssertEqual ( lhs, rhs, message, file: file, line: line)
70
+ }
71
+ else {
72
+ XCTAssertNotEqual ( lhs, rhs, message, file: file, line: line)
73
+ }
74
+ }
20
75
76
+ private func assert( equal: Bool ,
77
+ _ lhs: Box ,
78
+ _ rhs: Box ,
79
+ _ message: @autoclosure ( ) -> String = " " ,
80
+ file: StaticString = #file,
81
+ line: UInt = #line) {
82
+
83
+ for pair in [ ( lhs, rhs) , ( rhs, lhs) ] {
84
+ assert ( equal: equal, pair. 0 . ns, pair. 1 . ns, message, file: file, line: line)
85
+ assert ( equal: equal, pair. 0 . swift, pair. 1 . swift, message, file: file, line: line)
86
+
87
+ assert ( equal: equal,
88
+ pair. 0 . ns. _bridgeToSwift ( ) ,
89
+ pair. 1 . ns. _bridgeToSwift ( ) ,
90
+ message,
91
+ file: file,
92
+ line: line)
93
+
94
+ assert ( equal: equal,
95
+ pair. 0 . swift. _bridgeToObjectiveC ( ) ,
96
+ pair. 1 . swift. _bridgeToObjectiveC ( ) ,
97
+ message,
98
+ file: file,
99
+ line: line)
100
+
101
+ XCTAssertTrue ( pair. 0 . ns. isEqual ( pair. 1 . ns) == equal, message, file: file, line: line)
102
+ XCTAssertTrue ( pair. 0 . ns. isEqual ( pair. 1 . swift) == equal, message, file: file, line: line)
103
+ }
104
+ }
21
105
22
106
class TestNSCharacterSet : XCTestCase {
23
107
@@ -283,28 +367,36 @@ class TestNSCharacterSet : XCTestCase {
283
367
let expected = CharacterSet ( charactersIn: " abc " )
284
368
XCTAssertEqual ( expected, symmetricDifference)
285
369
}
286
-
370
+
287
371
func test_Equatable( ) {
288
- XCTAssertEqual ( NSCharacterSet ( charactersIn: " " ) , NSCharacterSet ( charactersIn: " " ) )
289
- XCTAssertEqual ( NSCharacterSet ( charactersIn: " a " ) , NSCharacterSet ( charactersIn: " a " ) )
290
- XCTAssertEqual ( NSCharacterSet ( charactersIn: " ab " ) , NSCharacterSet ( charactersIn: " ab " ) )
291
-
292
- XCTAssertNotEqual ( NSCharacterSet ( charactersIn: " abc " ) , NSCharacterSet ( charactersIn: " 123 " ) )
293
- XCTAssertNotEqual ( NSCharacterSet ( charactersIn: " 123 " ) , NSCharacterSet ( charactersIn: " abc " ) )
294
-
295
- XCTAssertNotEqual ( NSCharacterSet ( charactersIn: " " ) , nil )
296
-
372
+ let equalPairs = [
373
+ ( " " , " " ) ,
374
+ ( " a " , " a " ) ,
375
+ ( " abcde " , " abcde " ) ,
376
+ ( " 12345 " , " 12345 " )
377
+ ]
378
+
297
379
/*
298
380
Tests disabled due to CoreFoundation bug?
299
381
These NSCharacterSet pairs are (wrongly?) evaluated to be equal. Same behaviour can be observed on macOS 10.12.
300
382
Interestingly, on iOS 11 Simulator, they are evaluted to be _not_ equal,
301
383
while on iOS 10.3.1 Simulator, they are evaluted to be equal.
302
384
*/
303
- // XCTAssertNotEqual(NSCharacterSet(charactersIn: "ab"), NSCharacterSet(charactersIn: "abc"))
304
- // XCTAssertNotEqual(NSCharacterSet(charactersIn: "abc"), NSCharacterSet(charactersIn: "ab"))
305
- // XCTAssertNotEqual(NSCharacterSet(charactersIn: "abc"), NSCharacterSet(charactersIn: ""))
306
- // XCTAssertNotEqual(NSCharacterSet(charactersIn: ""), NSCharacterSet(charactersIn: "abc"))
385
+ let notEqualPairs = [
386
+ ( " abc " , " 123 " ) ,
387
+ // ("ab", "abc"),
388
+ // ("abc", "")
389
+ ]
390
+
391
+ for pair in equalPairs {
392
+ assertEqual ( Box ( charactersIn: pair. 0 ) , Box ( charactersIn: pair. 1 ) )
393
+ }
394
+ assertEqual ( Box . alphanumerics, Box . alphanumerics)
395
+
396
+ for pair in notEqualPairs {
397
+ assertNotEqual ( Box ( charactersIn: pair. 0 ) , Box ( charactersIn: pair. 1 ) )
398
+ }
399
+ assertNotEqual ( Box . alphanumerics, Box . decimalDigits)
307
400
}
308
401
309
402
}
310
-
0 commit comments