Skip to content

Commit 6616fdb

Browse files
author
Dave Abrahams
committed
[ArrayBridge test] eliminate some FileCheck
1 parent 4e6f614 commit 6616fdb

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

test/1_stdlib/ArrayBridge.swift

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var nextBaseSerialNumber = 0
4141
func baz()
4242
}
4343

44+
/// A type that will be bridged verbatim to Objective-C
4445
class Base : NSObject, Fooable {
4546
func foo() { }
4647

@@ -85,6 +86,7 @@ class Derived : Base, Barable {
8586
var bridgeFromOperationCount = 0
8687
var bridgeToOperationCount = 0
8788

89+
/// A value type that's bridged using _ObjectiveCBridgeable
8890
struct BridgedSwift : CustomStringConvertible, _ObjectiveCBridgeable {
8991
func _bridgeToObjectiveC() -> Derived {
9092
bridgeToOperationCount += 1
@@ -151,30 +153,46 @@ class Thunks : NSObject {
151153
return Derived(value)
152154
}
153155

154-
@objc func acceptDerivedArray(_ x: [Derived]) {
155-
print("acceptDerivedArray(\(x))")
156+
@objc func acceptDerivedArray(
157+
_ x: [Derived],
158+
expecting expected: NSArray
159+
) {
160+
expectEqual(x.count, expected.count)
161+
for i in 0..<x.count {
162+
expectTrue(x[i] === expected[i])
163+
}
156164
}
157165

158-
@objc func produceDerivedArray(_ numItems: Int) -> [Derived] {
166+
@objc func produceDerivedArray(
167+
_ expectations: NSMutableArray
168+
) -> [Derived] {
159169
var array: [Derived] = []
160-
for i in 0..<numItems {
170+
for i in 0..<5 {
161171
array.append(Derived(i))
172+
expectations.add(array[i])
162173
}
163-
print("produceDerivedArray(\(array))")
164174
return array
165175
}
166176

167-
@objc func acceptBridgedSwiftArray(_ raw: NSArray) {
177+
@objc func checkProducedDerivedArray(
178+
_ produced: NSArray, expecting expected: NSArray
179+
) {
180+
expectEqual(produced.count, expected.count)
181+
for i in 0..<produced.count {
182+
expectTrue(produced[i] === expected[i])
183+
}
184+
}
185+
186+
@objc func acceptBridgedSwiftArray(_ raw: NSArray) -> AnyObject {
168187
let x = raw as! [BridgedSwift]
169-
print("acceptBridgedSwiftArray(\(x))")
188+
return Box(x)
170189
}
171190

172191
@objc func produceBridgedSwiftArray(_ numItems: Int) -> NSArray {
173192
var array: [BridgedSwift] = []
174193
for i in 0..<numItems {
175194
array.append(BridgedSwift(i))
176195
}
177-
print("produceBridgedSwiftArray(\(array))")
178196
return array as NSArray
179197
}
180198
}
@@ -270,22 +288,9 @@ tests.test("testBridgedVerbatim") {
270288
expectEmpty(derivedAsAnyObjectArray as? [protocol<Barable, Bazable>])
271289
}
272290

273-
func doTestDerived() {
274-
// CHECK: doTestDerived
275-
print("doTestDerived")
276-
277-
// CHECK-NEXT: produceDerivedArray([Derived[[A:#[0-9]+]](0), Derived[[B:#[0-9]+]](1), Derived[[C:#[0-9]+]](2), Derived[[D:#[0-9]+]](3), Derived[[E:#[0-9]+]](4)])
291+
tests.test("doTestDerived") {
278292
testDerived(Thunks())
279-
// CHECK-NEXT: 5 elements in the array
280-
// CHECK-NEXT: Derived[[A]](0)
281-
// CHECK-NEXT: Derived[[B]](1)
282-
// CHECK-NEXT: Derived[[C]](2)
283-
// CHECK-NEXT: Derived[[D]](3)
284-
// CHECK-NEXT: Derived[[E]](4)
285-
286-
// CHECK-NEXT: acceptDerivedArray([Derived[[A:#[0-9]+]](10), Derived[[B:#[0-9]+]](11), Derived[[C:#[0-9]+]](12), Derived[[D:#[0-9]+]](13), Derived[[E:#[0-9]+]](14)])
287293
}
288-
doTestDerived()
289294

290295
//===--- Explicitly Bridged -----------------------------------------------===//
291296
// BridgedSwift conforms to _ObjectiveCBridgeable
@@ -455,19 +460,12 @@ func testExplicitlyBridged() {
455460
// CHECK-NEXT: Correctly rejected downcast of nil array
456461
print("Correctly rejected downcast of nil array")
457462
}
463+
}
464+
testExplicitlyBridged()
458465

459-
// CHECK-NEXT: produceBridgedSwiftArray([BridgedSwift[[A:#[0-9]+]](0), BridgedSwift[[B:#[0-9]+]](1), BridgedSwift[[C:#[0-9]+]](2), BridgedSwift[[D:#[0-9]+]](3), BridgedSwift[[E:#[0-9]+]](4)])
466+
tests.test("testExplicitlyBridged") {
460467
testBridgedSwift(Thunks())
461-
// CHECK-NEXT: 5 elements in the array
462-
// CHECK-NEXT: Derived[[A:#[0-9]+]](0)
463-
// CHECK-NEXT: Derived[[B:#[0-9]+]](1)
464-
// CHECK-NEXT: Derived[[C:#[0-9]+]](2)
465-
// CHECK-NEXT: Derived[[D:#[0-9]+]](3)
466-
// CHECK-NEXT: Derived[[E:#[0-9]+]](4)
467-
468-
// CHECK-NEXT: acceptBridgedSwiftArray([BridgedSwift[[A:#[0-9]+]](10), BridgedSwift[[B:#[0-9]+]](11), BridgedSwift[[C:#[0-9]+]](12), BridgedSwift[[D:#[0-9]+]](13), BridgedSwift[[E:#[0-9]+]](14)])
469468
}
470-
testExplicitlyBridged()
471469

472470
tests.test("testRoundTrip") {
473471
class Test : NSObject {

test/1_stdlib/Inputs/ArrayBridge/ArrayBridge.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
@interface Thunks : NSObject
2020
- (id)createDerived:(NSInteger)value;
21-
- (void)acceptDerivedArray:(NSArray *)x;
22-
- (NSArray *)produceDerivedArray:(NSInteger)numItems;
23-
- (void)acceptBridgedSwiftArray:(NSArray *)x;
21+
- (id)acceptDerivedArray:(NSArray *)bridged expecting:(NSArray*)unbridged;
22+
- (NSArray *)produceDerivedArray:(NSMutableArray *)expectations;
23+
- (void)checkProducedDerivedArray:(NSArray *)produced expecting:(NSArray *)expected;
24+
- (id)acceptBridgedSwiftArray:(NSArray *)x;
2425
- (NSArray *)produceBridgedSwiftArray:(NSInteger)numItems;
2526
@end
2627

@@ -32,14 +33,12 @@ id arrayAsID(NSArray* a) {
3233
return a;
3334
}
3435

36+
// Call back into thunks, passing arrays in both directions
3537
void testDerived(id thunks) {
3638
// Retrieve an array from Swift.
37-
NSArray *fromObjCArr = [thunks produceDerivedArray: 5];
38-
printf("%d elements in the array\n", (int)fromObjCArr.count);
39-
40-
for (id obj in fromObjCArr) {
41-
printf("%s\n", [obj description].UTF8String);
42-
}
39+
NSMutableArray* expectations = [[NSMutableArray alloc] init];
40+
NSArray *fromObjCArr = [thunks produceDerivedArray:expectations];
41+
[thunks checkProducedDerivedArray:fromObjCArr expecting:expectations];
4342

4443
// Send an array to swift.
4544
NSMutableArray *toObjCArr = [[NSMutableArray alloc] init];
@@ -48,7 +47,8 @@ void testDerived(id thunks) {
4847
[toObjCArr addObject: [thunks createDerived:12]];
4948
[toObjCArr addObject: [thunks createDerived:13]];
5049
[toObjCArr addObject: [thunks createDerived:14]];
51-
[thunks acceptDerivedArray: toObjCArr];
50+
51+
[thunks acceptDerivedArray: toObjCArr expecting: toObjCArr];
5252
}
5353

5454
void testBridgedSwift(id thunks) {

0 commit comments

Comments
 (0)