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,47 @@ import SwiftFoundation
17
15
import SwiftXCTest
18
16
#endif
19
17
18
+ private struct Box : Equatable {
19
+ private let ns : NSCharacterSet
20
+ private 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
+ }
20
41
42
+ // MARK: Equatable
43
+
44
+ static func == ( lhs: Box , rhs: Box ) -> Bool {
45
+ return lhs. ns == rhs. ns
46
+ && lhs. swift == rhs. swift
47
+ && lhs. ns. _bridgeToSwift ( ) == rhs. ns. _bridgeToSwift ( )
48
+ && lhs. swift. _bridgeToObjectiveC ( ) == rhs. swift. _bridgeToObjectiveC ( )
49
+ && lhs. ns. isEqual ( rhs. ns)
50
+ && lhs. ns. isEqual ( rhs. swift)
51
+ && lhs. ns. isEqual ( rhs. ns. _bridgeToSwift ( ) )
52
+ && lhs. ns. isEqual ( rhs. swift. _bridgeToObjectiveC ( ) )
53
+ && lhs. swift. _bridgeToObjectiveC ( ) . isEqual ( rhs. ns)
54
+ && lhs. swift. _bridgeToObjectiveC ( ) . isEqual ( rhs. swift)
55
+ && lhs. swift. _bridgeToObjectiveC ( ) . isEqual ( rhs. ns. _bridgeToSwift ( ) )
56
+ && lhs. swift. _bridgeToObjectiveC ( ) . isEqual ( rhs. swift. _bridgeToObjectiveC ( ) )
57
+ }
58
+ }
21
59
22
60
class TestNSCharacterSet : XCTestCase {
23
61
@@ -283,28 +321,36 @@ class TestNSCharacterSet : XCTestCase {
283
321
let expected = CharacterSet ( charactersIn: " abc " )
284
322
XCTAssertEqual ( expected, symmetricDifference)
285
323
}
286
-
324
+
287
325
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
-
326
+ let equalPairs = [
327
+ ( " " , " " ) ,
328
+ ( " a " , " a " ) ,
329
+ ( " abcde " , " abcde " ) ,
330
+ ( " 12345 " , " 12345 " )
331
+ ]
332
+
297
333
/*
298
334
Tests disabled due to CoreFoundation bug?
299
335
These NSCharacterSet pairs are (wrongly?) evaluated to be equal. Same behaviour can be observed on macOS 10.12.
300
336
Interestingly, on iOS 11 Simulator, they are evaluted to be _not_ equal,
301
337
while on iOS 10.3.1 Simulator, they are evaluted to be equal.
302
338
*/
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"))
339
+ let notEqualPairs = [
340
+ ( " abc " , " 123 " ) ,
341
+ // ("ab", "abc"),
342
+ // ("abc", "")
343
+ ]
344
+
345
+ for pair in equalPairs {
346
+ XCTAssertEqual ( Box ( charactersIn: pair. 0 ) , Box ( charactersIn: pair. 1 ) )
347
+ }
348
+ XCTAssertEqual ( Box . alphanumerics, Box . alphanumerics)
349
+
350
+ for pair in notEqualPairs {
351
+ XCTAssertNotEqual ( Box ( charactersIn: pair. 0 ) , Box ( charactersIn: pair. 1 ) )
352
+ }
353
+ XCTAssertNotEqual ( Box . alphanumerics, Box . decimalDigits)
307
354
}
308
355
309
356
}
310
-
0 commit comments