Skip to content

Commit 1680cc4

Browse files
author
Dave Abrahams
committed
[ArrayBridge test] de-FileCheck- and grokk-ize
1 parent 58609bf commit 1680cc4

File tree

2 files changed

+89
-165
lines changed

2 files changed

+89
-165
lines changed

test/1_stdlib/ArrayBridge.swift

Lines changed: 84 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
// RUN: %target-clang %S/Inputs/ArrayBridge/ArrayBridge.m -c -o %t/ArrayBridgeObjC.o -g
1717
// RUN: %target-build-swift %s -I %S/Inputs/ArrayBridge/ -Xlinker %t/ArrayBridgeObjC.o -o %t/ArrayBridge
1818

19-
// RUN: %target-run %t/ArrayBridge > %t.txt
20-
// RUN: FileCheck %s < %t.txt
19+
// RUN: %target-run %t/ArrayBridge
2120
// REQUIRES: executable_test
2221
// REQUIRES: objc_interop
2322

@@ -58,15 +57,6 @@ class Base : NSObject, Fooable {
5857
serialNumber = -serialNumber
5958
}
6059

61-
override var description: String {
62-
assert(serialNumber > 0, "dead Base!")
63-
return "Base#\(serialNumber)(\(value))"
64-
}
65-
66-
func successor() -> Self {
67-
return self.dynamicType.init(self.value + 1)
68-
}
69-
7060
override func isEqual(_ other: AnyObject?) -> Bool {
7161
return (other as? Base)?.value == self.value
7262
}
@@ -77,17 +67,13 @@ class Base : NSObject, Fooable {
7767

7868
class Subclass : Base, Barable {
7969
func bar() { }
80-
override var description: String {
81-
assert(serialNumber > 0, "dead Base!")
82-
return "Subclass#\(serialNumber)(\(value))"
83-
}
8470
}
8571

8672
var bridgeFromOperationCount = 0
8773
var bridgeToOperationCount = 0
8874

8975
/// A value type that's bridged using _ObjectiveCBridgeable
90-
struct BridgeableValue : CustomStringConvertible, _ObjectiveCBridgeable {
76+
struct BridgeableValue : _ObjectiveCBridgeable, Equatable {
9177
func _bridgeToObjectiveC() -> Subclass {
9278
bridgeToOperationCount += 1
9379
return Subclass(trak.value)
@@ -126,19 +112,10 @@ struct BridgeableValue : CustomStringConvertible, _ObjectiveCBridgeable {
126112
return result!
127113
}
128114

129-
var description: String {
130-
assert(trak.serialNumber > 0, "dead Base!")
131-
return "BridgeableValue#\(trak.serialNumber)(\(trak.value))"
132-
}
133-
134115
init(_ value: Int) {
135116
self.trak = Base(value)
136117
}
137118

138-
func successor() -> BridgeableValue {
139-
return BridgeableValue(trak.value + 1)
140-
}
141-
142119
static func resetStats() {
143120
bridgeFromOperationCount = 0
144121
bridgeToOperationCount = 0
@@ -147,6 +124,10 @@ struct BridgeableValue : CustomStringConvertible, _ObjectiveCBridgeable {
147124
var trak: Base
148125
}
149126

127+
func == (lhs: BridgeableValue, rhs: BridgeableValue) -> Bool {
128+
return lhs.trak.value == rhs.trak.value
129+
}
130+
150131
// A class used to test various Objective-C thunks.
151132
class Thunks : NSObject {
152133
func createSubclass(_ value: Int) -> AnyObject {
@@ -157,10 +138,10 @@ class Thunks : NSObject {
157138
_ x: [Subclass],
158139
expecting expected: NSArray
159140
) {
160-
expectEqual(x.count, expected.count)
161-
for i in 0..<x.count {
162-
expectTrue(x[i] === expected[i])
163-
}
141+
expectEqualSequence(
142+
expected.lazy.map { ObjectIdentifier($0) },
143+
x.lazy.map { ObjectIdentifier($0) }
144+
)
164145
}
165146

166147
@objc func produceSubclassArray(
@@ -177,24 +158,30 @@ class Thunks : NSObject {
177158
@objc func checkProducedSubclassArray(
178159
_ produced: NSArray, expecting expected: NSArray
179160
) {
180-
expectEqual(produced.count, expected.count)
181-
for i in 0..<produced.count {
182-
expectTrue(produced[i] === expected[i])
183-
}
161+
expectEqualSequence(
162+
expected.lazy.map { ObjectIdentifier($0) },
163+
produced.lazy.map { ObjectIdentifier($0) }
164+
)
184165
}
185166

186167
@objc func acceptBridgeableValueArray(_ raw: NSArray) -> AnyObject {
187168
let x = raw as! [BridgeableValue]
188169
return Box(x)
189170
}
190171

191-
@objc func produceBridgeableValueArray(_ numItems: Int) -> NSArray {
172+
@objc func produceBridgeableValueArray() -> NSArray {
192173
var array: [BridgeableValue] = []
193-
for i in 0..<numItems {
174+
for i in 0..<5 {
194175
array.append(BridgeableValue(i))
195176
}
196177
return array as NSArray
197178
}
179+
180+
@objc func checkProducedBridgeableValueArray(_ produced: NSArray) {
181+
expectEqualSequence(
182+
0..<5,
183+
produced.lazy.map { ($0 as! Subclass).value })
184+
}
198185
}
199186

200187

@@ -209,9 +196,9 @@ tests.test("testBridgedVerbatim") {
209196
//===--- Implicit conversion to/from NSArray ------------------------------===//
210197

211198
let basesConvertedToNSArray = bases as NSArray
212-
expectEqual(
213-
"Base#1(100)",
214-
String(basesConvertedToNSArray.object(at: 0) as! Base))
199+
let firstBase = basesConvertedToNSArray.object(at: 0) as! Base
200+
expectEqual(100, firstBase.value)
201+
expectEqual(1, firstBase.serialNumber)
215202

216203
// Create an ordinary NSArray, not a native one
217204
let nsArrayOfBase: NSArray = NSArray(object: Base(42))
@@ -295,175 +282,116 @@ tests.test("doTestSubclass") {
295282
//===--- Explicitly Bridged -----------------------------------------------===//
296283
// BridgeableValue conforms to _ObjectiveCBridgeable
297284
//===----------------------------------------------------------------------===//
298-
func testExplicitlyBridged() {
299-
// CHECK-LABEL: testExplicitlyBridged()
300-
print("testExplicitlyBridged()")
301-
285+
tests.test("testExplicitlyBridged") {
286+
302287
let bridgeableValues = [BridgeableValue(42), BridgeableValue(17)]
303288

304289
let bridgeableValuesAsNSArray = bridgeableValues as NSArray
305-
// CHECK-NEXT: [Subclass#{{[0-9]+}}(42), Subclass#{{[0-9]+}}(17)]
306-
print("bridgeableValuesAsNSArray = \(bridgeableValuesAsNSArray as [AnyObject]))")
290+
expectEqual(2, bridgeableValuesAsNSArray.count)
291+
expectEqual(42, bridgeableValuesAsNSArray[0].value)
292+
expectEqual(17, bridgeableValuesAsNSArray[1].value)
307293

308294
// Make sure we can bridge back.
309-
let roundTripBridgeableValues
310-
= Swift._forceBridgeFromObjectiveC(bridgeableValuesAsNSArray,
311-
[BridgeableValue].self)
312-
// CHECK-NOT: [BridgeableValue#[[id00]](42), BridgeableValue#[[id01]](17)]
313-
// CHECK-NEXT: [BridgeableValue#[[id10:[0-9]+]](42), BridgeableValue#[[id11:[0-9]+]](17)]
314-
print("roundTripBridgeableValues = \(roundTripBridgeableValues))")
295+
let roundTrippedValues = Swift._forceBridgeFromObjectiveC(
296+
bridgeableValuesAsNSArray, [BridgeableValue].self)
297+
298+
// Expect equal values...
299+
expectEqualSequence(bridgeableValues, roundTrippedValues)
300+
// ...with the same identity for some reason? FIXME: understand why.
301+
expectFalse(
302+
zip(bridgeableValues, roundTrippedValues).contains { $0.trak !== $1.trak })
315303

316304
// Make a real Cocoa NSArray of these...
317-
let cocoaBridgeableValues = NSArray(array: bridgeableValuesAsNSArray as [AnyObject])
305+
let cocoaBridgeableValues = NSArray(
306+
array: bridgeableValuesAsNSArray as [AnyObject])
318307

319308
// ...and bridge *that* back
320-
let bridgedBackSwifts
321-
= Swift._forceBridgeFromObjectiveC(cocoaBridgeableValues, [BridgeableValue].self)
322-
// CHECK-NOT: [BridgeableValue#[[id00]](42), BridgeableValue#[[id01]](17)]
323-
// CHECK-NOT: [BridgeableValue#[[id10]](42), BridgeableValue#[[id11]](17)]
324-
// CHECK-NEXT: [BridgeableValue#{{[0-9]+}}(42), BridgeableValue#{{[0-9]+}}(17)]
325-
print("bridgedBackSwifts = \(bridgedBackSwifts)")
309+
let bridgedBackSwifts = Swift._forceBridgeFromObjectiveC(
310+
cocoaBridgeableValues, [BridgeableValue].self)
326311

327-
// all: verbatim, not, and doesn't bridge
328-
// implicit conversions to/from NSArray
329-
// [Base] -> [Subclass] and [Subclass] -> [Base] where Base can be AnyObject
330-
// defining @objc method taking [T] and returning [T]
312+
// Expect equal, but distinctly created instances
313+
expectEqualSequence(bridgeableValues, bridgedBackSwifts)
314+
expectFalse(
315+
zip(bridgedBackSwifts, roundTrippedValues).contains { $0.trak === $1.trak })
316+
expectFalse(
317+
zip(bridgedBackSwifts, bridgeableValues).contains { $0.trak === $1.trak })
318+
319+
let expectedSubclasses = [Subclass(42), Subclass(17)]
320+
let expectedBases = expectedSubclasses.lazy.map { $0 as Base }
331321

332322
// Up-casts.
333-
let bridgeableValuesAsSubclasss: [Subclass] = bridgeableValues
334-
// CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
335-
print(bridgeableValuesAsSubclasss[0])
336-
// CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
337-
print(bridgeableValuesAsSubclasss[1])
323+
let bridgeableValuesAsSubclasses: [Subclass] = bridgeableValues
324+
expectEqualSequence(expectedSubclasses, bridgeableValuesAsSubclasses)
338325

339326
let bridgeableValuesAsBases: [Base] = bridgeableValues
340-
// CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
341-
print(bridgeableValuesAsBases[0])
342-
// CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
343-
print(bridgeableValuesAsBases[1])
327+
expectEqualSequence(expectedBases, bridgeableValuesAsBases)
344328

345329
let bridgeableValuesAsAnyObjects: [AnyObject] = bridgeableValues
346-
// CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
347-
print(bridgeableValuesAsAnyObjects[0])
348-
// CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
349-
print(bridgeableValuesAsAnyObjects[1])
330+
expectEqualSequence(
331+
expectedBases,
332+
bridgeableValuesAsAnyObjects.lazy.map { $0 as! Base })
350333

351334
// Downcasts of non-verbatim bridged value types to objects.
352335
do {
353336
let downcasted = bridgeableValues as [Subclass]
354-
// CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
355-
print(downcasted[0])
356-
// CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
357-
print(downcasted[1])
337+
expectEqualSequence(expectedSubclasses, downcasted)
358338
}
359339

360340
do {
361341
let downcasted = bridgeableValues as [Base]
362-
// CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
363-
print(downcasted[0])
364-
// CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
365-
print(downcasted[1])
342+
expectEqualSequence(expectedBases, downcasted)
366343
}
367344

368345
do {
369346
let downcasted = bridgeableValues as [AnyObject]
370-
// CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
371-
print(downcasted[0])
372-
// CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
373-
print(downcasted[1])
347+
expectEqualSequence(expectedBases, downcasted.map { $0 as! Base })
374348
}
375349

376350
// Downcasts of up-casted arrays.
377-
if let downcasted = bridgeableValuesAsAnyObjects as? [Subclass] {
378-
// CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
379-
print(downcasted[0])
380-
// CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
381-
print(downcasted[1])
382-
} else {
383-
print("Could not downcast [AnyObject] to [Subclass]?")
351+
if let downcasted = expectNotEmpty(
352+
bridgeableValuesAsAnyObjects as? [Subclass]
353+
) {
354+
expectEqualSequence(expectedSubclasses, downcasted)
384355
}
385356

386357
if let downcasted = bridgeableValuesAsAnyObjects as? [Base] {
387-
// CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
388-
print(downcasted[0])
389-
// CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
390-
print(downcasted[1])
391-
} else {
392-
print("Could not downcast [AnyObject] to [Base]?")
358+
expectEqualSequence(expectedBases, downcasted)
393359
}
394360

395361
// Downcast of Cocoa array to an array of classes.
396362
let wrappedCocoaBridgeableValues = cocoaBridgeableValues as [AnyObject]
397363
if let downcasted = wrappedCocoaBridgeableValues as? [Subclass] {
398-
// CHECK-NEXT: Subclass#[[ID0:[0-9]+]](42)
399-
print(downcasted[0])
400-
// CHECK-NEXT: Subclass#[[ID1:[0-9]+]](17)
401-
print(downcasted[1])
402-
} else {
403-
print("Could not downcast [AnyObject] to [Subclass]?")
364+
expectEqualSequence(expectedSubclasses, downcasted)
404365
}
405366

406367
// Downcast of Cocoa array to an array of values.
407368
if let downcasted = wrappedCocoaBridgeableValues as? [BridgeableValue] {
408-
// CHECK-NEXT: BridgeableValue#[[ID0:[0-9]+]](42)
409-
print(downcasted[0])
410-
// CHECK-NEXT: BridgeableValue#[[ID1:[0-9]+]](17)
411-
print(downcasted[1])
412-
} else {
413-
print("Could not downcast [AnyObject] to [BridgeableValue]?")
369+
expectEqualSequence(bridgeableValues, downcasted)
414370
}
415371

416372
// Downcast of Cocoa array to an array of strings (which should fail)
417-
// CHECK-NEXT: Could not downcast [AnyObject] to [String]
418-
if let _ = wrappedCocoaBridgeableValues as? [String] {
419-
print("Shouldn't be able to downcast to an array of strings")
420-
} else {
421-
print("Could not downcast [AnyObject] to [String]")
422-
}
373+
expectEmpty(wrappedCocoaBridgeableValues as? [String])
423374

424375
// Downcast from an implicitly unwrapped optional array of AnyObjects.
425376
var wrappedCocoaBridgeableValuesIUO: [AnyObject]! = wrappedCocoaBridgeableValues
426-
if let downcasted = wrappedCocoaBridgeableValuesIUO as? [BridgeableValue] {
427-
// CHECK-NEXT: BridgeableValue#[[ID0:[0-9]+]](42)
428-
print(downcasted[0])
429-
// CHECK-NEXT: BridgeableValue#[[ID1:[0-9]+]](17)
430-
print(downcasted[1])
431-
} else {
432-
print("Could not downcast [AnyObject]! to [BridgeableValue]")
377+
if let downcasted = wrappedCocoaBridgeableValuesIUO as? [BridgeableValue] {
378+
expectEqualSequence(bridgeableValues, downcasted)
433379
}
434380

435381
// Downcast from a nil implicitly unwrapped optional array of AnyObjects.
436382
wrappedCocoaBridgeableValuesIUO = nil
437-
if let _ = wrappedCocoaBridgeableValuesIUO as? [BridgeableValue] {
438-
print("Cannot downcast from a nil array!")
439-
} else {
440-
// CHECK-NEXT: Correctly rejected downcast of nil array
441-
print("Correctly rejected downcast of nil array")
442-
}
383+
expectEmpty(wrappedCocoaBridgeableValuesIUO as? [BridgeableValue])
443384

444385
// Downcast from an optional array of AnyObjects.
445386
var wrappedCocoaBridgeableValuesOpt: [AnyObject]? = wrappedCocoaBridgeableValues
446-
if let downcasted = wrappedCocoaBridgeableValuesOpt as? [BridgeableValue] {
447-
// CHECK-NEXT: BridgeableValue#[[ID0:[0-9]+]](42)
448-
print(downcasted[0])
449-
// CHECK-NEXT: BridgeableValue#[[ID1:[0-9]+]](17)
450-
print(downcasted[1])
451-
} else {
452-
print("Could not downcast [AnyObject]! to [BridgeableValue]")
387+
if let downcasted = wrappedCocoaBridgeableValuesOpt as? [BridgeableValue] {
388+
expectEqualSequence(bridgeableValues, downcasted)
453389
}
454390

455391
// Downcast from a nil optional array of AnyObjects.
456392
wrappedCocoaBridgeableValuesOpt = nil
457-
if let _ = wrappedCocoaBridgeableValuesOpt as? [BridgeableValue] {
458-
print("Cannot downcast from a nil array!")
459-
} else {
460-
// CHECK-NEXT: Correctly rejected downcast of nil array
461-
print("Correctly rejected downcast of nil array")
462-
}
463-
}
464-
testExplicitlyBridged()
465-
466-
tests.test("testExplicitlyBridged") {
393+
expectEmpty(wrappedCocoaBridgeableValuesOpt as? [BridgeableValue])
394+
467395
testBridgeableValue(Thunks())
468396
}
469397

@@ -500,19 +428,18 @@ tests.test("testRoundTrip") {
500428

501429
struct X {}
502430

503-
/*
504-
let x: NSArray = arrayAsID(bases)!
505-
506-
print(x.objectAt(0) as Base)
507-
*/
508-
509-
func testMutableArray() {
431+
tests.test("testMutableArray") {
510432
let m = NSMutableArray(array: ["fu", "bar", "buzz"])
511433
let a = m as NSArray as! [NSString]
512-
print(a) // CHECK-NEXT: [fu, bar, buzz]
434+
435+
// Create distinct array storage with a copy of the elements in a
436+
let aCopy = a.map { $0 }
437+
438+
// Modify the original mutable array
513439
m.add("goop")
514-
print(a) // CHECK-NEXT: [fu, bar, buzz]
440+
441+
// Check that our [NSString] is unaffected
442+
expectEqualSequence(aCopy, a)
515443
}
516-
testMutableArray()
517444

518445
runAllTests()

0 commit comments

Comments
 (0)