Skip to content

Commit f95f8e9

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 8d0612d commit f95f8e9

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
@@ -196,6 +196,11 @@ testCases = [
196196
]
197197
}%
198198

199+
/// Whether this can be safely casted to NSObject
200+
func isNSObject<T>(_ value: T) -> Bool {
201+
return (value as? NSObject) != nil
202+
}
203+
199204
% for testName, type, valueExpr, testFunc, conformsToError, conformsToHashable in testCases:
200205
BridgeAnything.test("${testName}") {
201206
autoreleasepool {
@@ -207,7 +212,7 @@ BridgeAnything.test("${testName}") {
207212
let xInArray = [x]
208213
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as! [AnyObject])[0])
209214
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as? [AnyObject])![0])
210-
if (x as? NSObject) != nil {
215+
if isNSObject(x) {
211216
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as! [AnyObject])[0])
212217
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInArray) as? [AnyObject])![0])
213218
}
@@ -216,7 +221,7 @@ BridgeAnything.test("${testName}") {
216221
let xInDictValue = ["key" : x]
217222
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as! [String: AnyObject])["key"]!)
218223
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as? [String: AnyObject])!["key"]!)
219-
if (x as? NSObject) != nil {
224+
if isNSObject(x) {
220225
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as! [String: NSObject])["key"]!)
221226
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictValue) as? [String: NSObject])!["key"]!)
222227
}
@@ -228,7 +233,7 @@ BridgeAnything.test("${testName}") {
228233
// The NSObject version below can't test class LifetimeTracked.
229234
// ${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as! [(AnyObject & Hashable): String]).keys.first!)
230235
// ${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as? [(AnyObject & Hashable): String])!.keys.first!)
231-
if (x as? NSObject) != nil {
236+
if isNSObject(x) {
232237
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as! [NSObject: String]).keys.first!)
233238
${testFunc}(original: x, bridged: (_bridgeAnythingToObjectiveC(xInDictKey) as? [NSObject: String])!.keys.first!)
234239
}

0 commit comments

Comments
 (0)