Skip to content

Commit 8261d6f

Browse files
committed
[Test] Go through the runtime for the "as? NSObject" check.
This prevents runtime differences on as? from affecting this test.
1 parent 5ed0641 commit 8261d6f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

test/stdlib/BridgeIdAsAny.swift.gyb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ testCases = [
199199
]
200200
}%
201201

202+
/// Whether this can be safely casted to NSObject
203+
func isNSObject<T>(_ value: T) -> Bool {
204+
return (value as? NSObject) != nil
205+
}
206+
202207
% for testName, type, valueExpr, testFunc, conformsToError, conformsToHashable in testCases:
203208
BridgeAnything.test("${testName}") {
204209
autoreleasepool {
@@ -210,7 +215,7 @@ BridgeAnything.test("${testName}") {
210215
let xInArray = [x]
211216
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as! [AnyObject])[0])
212217
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as? [AnyObject])![0])
213-
if (x as? NSObject) != nil {
218+
if isNSObject(x) {
214219
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as! [AnyObject])[0])
215220
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as? [AnyObject])![0])
216221
}
@@ -219,7 +224,7 @@ BridgeAnything.test("${testName}") {
219224
let xInDictValue = ["key" : x]
220225
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as! [String: AnyObject])["key"]!)
221226
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as? [String: AnyObject])!["key"]!)
222-
if (x as? NSObject) != nil {
227+
if isNSObject(x) {
223228
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as! [String: NSObject])["key"]!)
224229
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as? [String: NSObject])!["key"]!)
225230
}
@@ -231,7 +236,7 @@ BridgeAnything.test("${testName}") {
231236
// The NSObject version below can't test class LifetimeTracked.
232237
// ${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as! [(AnyObject & Hashable): String]).keys.first!)
233238
// ${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as? [(AnyObject & Hashable): String])!.keys.first!)
234-
if (x as? NSObject) != nil {
239+
if isNSObject(x) {
235240
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as! [NSObject: String]).keys.first!)
236241
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as? [NSObject: String])!.keys.first!)
237242
}

0 commit comments

Comments
 (0)