23
23
24
24
import Foundation
25
25
import ArrayBridgeObjC
26
-
27
- // CHECK: testing...
28
- print ( " testing... " )
26
+ import StdlibUnittest
27
+ let tests = TestSuite ( " ArrayBridge " )
29
28
30
29
var trackedCount = 0
31
30
var nextTrackedSerialNumber = 0
@@ -67,14 +66,14 @@ class Tracked : NSObject, Fooable {
67
66
return self . dynamicType. init ( self . value + 1 )
68
67
}
69
68
69
+ override func isEqual( _ other: AnyObject ? ) -> Bool {
70
+ return ( other as? Tracked ) ? . value == self . value
71
+ }
72
+
70
73
var value : Int
71
74
var serialNumber : Int
72
75
}
73
76
74
- func == ( x: Tracked , y: Tracked ) -> Bool {
75
- return x. value == y. value
76
- }
77
-
78
77
typealias Base = Tracked
79
78
class Derived : Base , Barable {
80
79
func bar( ) { }
@@ -201,14 +200,16 @@ class Thunks : NSObject {
201
200
// Base is "bridged verbatim"
202
201
//===----------------------------------------------------------------------===//
203
202
204
- func testBridgedVerbatim( ) {
203
+ tests. test ( " testBridgedVerbatim " ) {
204
+ nextTrackedSerialNumber = 0
205
205
let bases : [ Base ] = [ Base ( 100 ) , Base ( 200 ) , Base ( 300 ) ]
206
206
207
207
//===--- Implicit conversion to/from NSArray ------------------------------===//
208
208
209
- // CHECK-NEXT: Base#1(100)
210
209
let basesConvertedToNSArray = bases as NSArray
211
- print ( basesConvertedToNSArray. object ( at: 0 ) as! Base )
210
+ expectEqual (
211
+ " Base#1(100) " ,
212
+ String ( basesConvertedToNSArray. object ( at: 0 ) as! Base ) )
212
213
213
214
// Create an ordinary NSArray, not a native one
214
215
let nsArrayOfBase : NSArray = NSArray ( object: Base ( 42 ) )
@@ -217,107 +218,79 @@ func testBridgedVerbatim() {
217
218
let nsArrayOfBaseConvertedToAnyObjectArray = nsArrayOfBase as [ AnyObject ]
218
219
219
220
// Capture the representation of the first element
220
- // CHECK-NEXT: [[base42:Base.*42]]
221
- print ( nsArrayOfBase. object ( at: 0 ) as! Base )
221
+ let base42 : ObjectIdentifier
222
+ do {
223
+ let b = nsArrayOfBase. object ( at: 0 ) as! Base
224
+ expectEqual ( 42 , b. value)
225
+ base42 = ObjectIdentifier ( b)
226
+ }
222
227
223
228
// ...with the same elements
224
- // CHECK-NEXT: [[base42]]
225
- print ( nsArrayOfBaseConvertedToAnyObjectArray [ 0 ] as! Base )
229
+ expectEqual (
230
+ base42,
231
+ ObjectIdentifier ( nsArrayOfBaseConvertedToAnyObjectArray [ 0 ] as! Base ) )
226
232
227
233
// Verify that NSArray class methods are inherited by a Swift bridging class.
228
- // CHECK-NEXT: Swift.{{.*}}Array
229
- debugPrint ( basesConvertedToNSArray. dynamicType)
230
- // CHECK-NEXT: true
231
- print ( basesConvertedToNSArray. dynamicType. supportsSecureCoding)
234
+ let className = String ( reflecting: basesConvertedToNSArray. dynamicType)
235
+ expectTrue ( className. hasPrefix ( " Swift._ContiguousArrayStorage " ) )
236
+ expectTrue ( basesConvertedToNSArray. dynamicType. supportsSecureCoding)
232
237
233
238
//===--- Up- and Down-casts -----------------------------------------------===//
234
239
var derived : [ Derived ] = [ Derived ( 11 ) , Derived ( 22 ) ]
235
- // CHECK-NEXT: [[derived0:\[Derived#[0-9]+\(11\), Derived#[0-9]+\(22\)\]{1}]]
236
- print ( derived)
240
+ let derived0 = derived
237
241
238
242
// upcast is implicit
239
243
let derivedAsBases : [ Base ] = derived
240
-
241
- // CHECK-NEXT: [[derived0]]
242
- print ( derivedAsBases)
244
+ expectEqual ( derived. count, derivedAsBases. count)
245
+ for (x, y) in zip ( derived, derivedAsBases) {
246
+ expectTrue ( x === y)
247
+ }
243
248
244
249
// Arrays are logically distinct after upcast
245
250
derived [ 0 ] = Derived ( 33 )
246
-
247
- // CHECK-NEXT: {{\[Derived#[0-9]+\(33\), Derived#[0-9]+\(22\)]}}
248
- print ( derived)
249
- // CHECK-NEXT: [[derived0]]
250
- print ( derivedAsBases)
251
251
252
- // CHECK-NEXT: [[derived0]]
253
- if let roundTripDerived = derivedAsBases as? [ Derived ] {
254
- print ( roundTripDerived)
255
- }
256
- else {
257
- print ( " roundTripDerived upcast failed " )
258
- }
252
+ expectEqual ( [ Derived ( 33 ) , Derived ( 22 ) ] , derived)
253
+ expectEqual ( [ Derived ( 11 ) , Derived ( 22 ) ] , derivedAsBases)
254
+
255
+ expectEqual ( derived0, derivedAsBases as! [ Derived ] )
259
256
260
- // CHECK-NEXT: [[derived2:\[Derived#[0-9]+\(44\), Derived#[0-9]+\(55\)\]{1}]]
261
257
let derivedInBaseBuffer : [ Base ] = [ Derived ( 44 ) , Derived ( 55 ) ]
262
- print ( derivedInBaseBuffer)
258
+ let derived2 = derivedInBaseBuffer
263
259
264
- // CHECK-NEXT: Explicit downcast-ability is based on element type, not buffer type
265
- if let downcastBaseBuffer = derivedInBaseBuffer as? [ Derived ] {
266
- print ( " Explicit downcast-ability is based on element type, not buffer type " )
267
- }
268
- else {
269
- print ( " Unexpected downcast failure " )
270
- }
260
+ // Explicit downcast-ability is based on element type, not buffer type
261
+ expectNotEmpty ( derivedInBaseBuffer as? [ Derived ] )
271
262
272
263
// We can up-cast to array of AnyObject
273
- // CHECK-NEXT: [[derived2]]
274
264
let derivedAsAnyObjectArray : [ AnyObject ] = derivedInBaseBuffer
275
- print ( derivedAsAnyObjectArray)
265
+ expectEqual ( derived2 , derivedAsAnyObjectArray. map { $0 as! Base } )
276
266
277
- // CHECK-NEXT: downcastBackToBase = [[derived2]]
278
- if let downcastBackToBase = derivedAsAnyObjectArray as? [ Base ] {
279
- print ( " downcastBackToBase = \( downcastBackToBase) " )
280
- }
281
- else {
282
- print ( " downcastBackToBase failed " )
283
- }
267
+ let downcastBackToBase = derivedAsAnyObjectArray as? [ Base ]
268
+ expectNotEmpty ( downcastBackToBase)
284
269
285
- // CHECK-NEXT: downcastBackToDerived = [[derived2]]
286
- if let downcastBackToDerived = derivedAsAnyObjectArray as? [ Derived ] {
287
- print ( " downcastBackToDerived = \( downcastBackToDerived) " )
288
- }
289
- else {
290
- print ( " downcastBackToDerived failed " )
270
+ if let downcastBackToDerived = expectNotEmpty ( derivedAsAnyObjectArray as? [ Derived ] ) {
271
+ expectEqual ( derived2, downcastBackToDerived)
291
272
}
292
273
293
- // CHECK-NEXT: downcastToProtocols = [[derived2]]
294
- if let downcastToProtocols = derivedAsAnyObjectArray as? [ Fooable ] {
295
- print ( " downcastToProtocols = \( downcastToProtocols) " )
296
- } else {
297
- print ( " downcastToProtocols failed " )
274
+ if let downcastToProtocols = expectNotEmpty ( derivedAsAnyObjectArray as? [ Fooable ] ) {
275
+ expectEqual ( derived2, downcastToProtocols. map { $0 as! Derived } )
298
276
}
299
277
300
- // CHECK-NEXT: downcastToProtocols = [[derived2]]
301
- if let downcastToProtocols = derivedAsAnyObjectArray as? [ Barable ] {
302
- print ( " downcastToProtocols = \( downcastToProtocols) " )
303
- } else {
304
- print ( " downcastToProtocols failed " )
278
+ if let downcastToProtocols = expectNotEmpty ( derivedAsAnyObjectArray as? [ Barable ] ) {
279
+ expectEqual ( derived2, downcastToProtocols. map { $0 as! Derived } )
305
280
}
306
281
307
- // CHECK-NEXT: downcastToProtocols = [[derived2]]
308
- if let downcastToProtocols = derivedAsAnyObjectArray as? [ protocol < Barable , Fooable > ] {
309
- print ( " downcastToProtocols = \( downcastToProtocols) " )
310
- } else {
311
- print ( " downcastToProtocols failed " )
282
+ if let downcastToProtocols = expectNotEmpty ( derivedAsAnyObjectArray as? [ protocol < Barable , Fooable > ] ) {
283
+ expectEqual ( derived2, downcastToProtocols. map { $0 as! Derived } )
312
284
}
313
285
314
- // CHECK-NEXT: downcastToProtocols failed
315
- if let downcastToProtocols = derivedAsAnyObjectArray as? [ protocol < Barable , Bazable > ] {
316
- print ( " downcastToProtocols = \( downcastToProtocols) " )
317
- } else {
318
- print ( " downcastToProtocols failed " )
319
- }
286
+ expectEmpty ( derivedAsAnyObjectArray as? [ protocol < Barable , Bazable > ] )
287
+ }
320
288
289
+ func doTestBridgedObjC( ) {
290
+ // CHECK: doTestBridgedObjC
291
+ print ( " doTestBridgedObjC " )
292
+
293
+ testBridgedObjC ( Thunks ( ) )
321
294
// CHECK-NEXT: produceBridgedObjCArray([BridgedObjC[[A:#[0-9]+]](0), BridgedObjC[[B:#[0-9]+]](1), BridgedObjC[[C:#[0-9]+]](2), BridgedObjC[[D:#[0-9]+]](3), BridgedObjC[[E:#[0-9]+]](4)])
322
295
testBridgedObjC ( Thunks ( ) )
323
296
// CHECK-NEXT: 5 elements in the array
@@ -329,7 +302,7 @@ func testBridgedVerbatim() {
329
302
330
303
// CHECK-NEXT: acceptBridgedObjCArray([BridgedObjC[[A:#[0-9]+]](10), BridgedObjC[[B:#[0-9]+]](11), BridgedObjC[[C:#[0-9]+]](12), BridgedObjC[[D:#[0-9]+]](13), BridgedObjC[[E:#[0-9]+]](14)])
331
304
}
332
- testBridgedVerbatim ( )
305
+ doTestBridgedObjC ( )
333
306
334
307
//===--- Explicitly Bridged -----------------------------------------------===//
335
308
// BridgedSwift conforms to _ObjectiveCBridgeable
@@ -454,7 +427,7 @@ func testExplicitlyBridged() {
454
427
455
428
// Downcast of Cocoa array to an array of strings (which should fail)
456
429
// CHECK-NEXT: Could not downcast [AnyObject] to [String]
457
- if let downcasted = wrappedCocoaBridgedSwifts as? [ String ] {
430
+ if let _ = wrappedCocoaBridgedSwifts as? [ String ] {
458
431
print ( " Shouldn't be able to downcast to an array of strings " )
459
432
} else {
460
433
print ( " Could not downcast [AnyObject] to [String] " )
@@ -473,7 +446,7 @@ func testExplicitlyBridged() {
473
446
474
447
// Downcast from a nil implicitly unwrapped optional array of AnyObjects.
475
448
wrappedCocoaBridgedSwiftsIUO = nil
476
- if let downcasted = wrappedCocoaBridgedSwiftsIUO as? [ BridgedSwift ] {
449
+ if let _ = wrappedCocoaBridgedSwiftsIUO as? [ BridgedSwift ] {
477
450
print ( " Cannot downcast from a nil array! " )
478
451
} else {
479
452
// CHECK-NEXT: Correctly rejected downcast of nil array
@@ -493,7 +466,7 @@ func testExplicitlyBridged() {
493
466
494
467
// Downcast from a nil optional array of AnyObjects.
495
468
wrappedCocoaBridgedSwiftsOpt = nil
496
- if let downcasted = wrappedCocoaBridgedSwiftsOpt as? [ BridgedSwift ] {
469
+ if let _ = wrappedCocoaBridgedSwiftsOpt as? [ BridgedSwift ] {
497
470
print ( " Cannot downcast from a nil array! " )
498
471
} else {
499
472
// CHECK-NEXT: Correctly rejected downcast of nil array
@@ -517,12 +490,9 @@ func testRoundTrip() {
517
490
class Test : NSObject {
518
491
@objc dynamic func call( _ array: NSArray ) -> NSArray {
519
492
520
- // CHECK-NEXT: ---Passed array---
521
- print ( " ---Passed array--- " )
522
493
let result = array as! [ BridgedSwift ]
523
- // CHECK-NEXT: bridge operations (from, to) = (0, 0)
524
- BridgedSwift . printStats ( )
525
-
494
+ expectEqual ( 0 , bridgeFromOperationCount)
495
+ expectEqual ( 0 , bridgeToOperationCount)
526
496
527
497
// Clear out the stats before returning array
528
498
BridgedSwift . resetStats ( )
@@ -537,12 +507,10 @@ func testRoundTrip() {
537
507
BridgedSwift ( 40 ) , BridgedSwift ( 50 ) ]
538
508
539
509
BridgedSwift . resetStats ( )
540
- test. call ( array as NSArray )
510
+ _ = test. call ( array as NSArray )
541
511
542
- // CHECK-NEXT: ---Returned Array---
543
- print ( " ---Returned Array--- " )
544
- // CHECK-NEXT: bridge operations (from, to) = (0, 0)
545
- BridgedSwift . printStats ( )
512
+ expectEqual ( 0 , bridgeFromOperationCount)
513
+ expectEqual ( 0 , bridgeToOperationCount)
546
514
}
547
515
testRoundTrip ( )
548
516
//===--- Non-bridging -----------------------------------------------------===//
@@ -566,5 +534,4 @@ func testMutableArray() {
566
534
}
567
535
testMutableArray ( )
568
536
569
- // CHECK-NEXT: done.
570
- print ( " done. " )
537
+ runAllTests ( )
0 commit comments