Skip to content

Commit 7659121

Browse files
committed
[Runtime] Add test for casting an Error type to NSObject
1 parent ab062fc commit 7659121

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/stdlib/ErrorBridged.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,4 +767,29 @@ ErrorBridgingTests.test("@objc error domains for nested types") {
767767
String(reflecting: NonPrintAsObjCError.self))
768768
}
769769

770+
@inline(never)
771+
@_optimize(none)
772+
func anyToAny<T, U>(_ a: T, _ : U.Type) -> U {
773+
return a as! U
774+
}
775+
776+
ErrorBridgingTests.test("error-to-NSObject casts") {
777+
let error = MyCustomizedError(code: 12345)
778+
779+
// Unconditional cast
780+
let nsErrorAsObject1 = unconditionalCast(error, to: NSObject.self)
781+
let nsError1 = unconditionalCast(nsErrorAsObject1, to: NSError.self)
782+
expectEqual("custom", nsError1.domain)
783+
expectEqual(12345, nsError1.code)
784+
785+
// Conditional cast
786+
let nsErrorAsObject2 = conditionalCast(error, to: NSObject.self)!
787+
let nsError2 = unconditionalCast(nsErrorAsObject2, to: NSError.self)
788+
expectEqual("custom", nsError2.domain)
789+
expectEqual(12345, nsError2.code)
790+
791+
// "is" check
792+
expectTrue(error is NSObject)
793+
}
794+
770795
runAllTests()

0 commit comments

Comments
 (0)