//===-- NSValueBridging.swift - Test bridging through NSValue -*- swift -*-===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// // RUN: %empty-directory(%t) // RUN: %gyb %s -o %t/NSValueBridging.swift // RUN: %target-build-swift -g -module-name a %t/NSValueBridging.swift -o %t.out // RUN: %target-codesign %t.out // RUN: %target-run %t.out // REQUIRES: executable_test // // REQUIRES: objc_interop // UNSUPPORTED: OS=watchos import StdlibUnittest import StdlibUnittestFoundationExtras import Foundation import CoreGraphics var nsValueBridging = TestSuite("NSValueBridging") func rangesEqual(_ x: NSRange, _ y: NSRange) -> Bool { return x.location == y.location && x.length == y.length } %{ def testCase(name, value, accessor, equal): if accessor: return """ nsValueBridging.test("{name}") {{ expectBridgeToNSValue({value}, nsValueInitializer: {{ NSValue({accessor}: $0) }}, nsValueGetter: {{ $0.{accessor}Value }}, equal: {equal}) }} """.format(name=name, value=value, accessor=accessor, equal=equal) else: return """ nsValueBridging.test("{name}") {{ expectBridgeToNSValue({value}, equal: {equal}) }} """.format(name=name, value=value, equal=equal) }% ${ testCase("NSRange", "NSRange(location: 17, length: 38)", "range", "rangesEqual") } // For historic reasons, macOS has different NSValue methods for the // CoreGraphics types from other Apple platforms. #if os(macOS) ${ testCase("CGRect", "CGRect(x: 17, y: 38, width: 6, height: 79)", "rect", "(==)") } ${ testCase("CGPoint", "CGPoint(x: 17, y: 38)", "point", "(==)") } ${ testCase("CGSize", "CGSize(width: 6, height: 79)", "size", "(==)") } ${ testCase("CGVector", "CGVector(dx: 6, dy: 79)", None, "(==)") } ${ testCase("CGAffineTransform", "CGAffineTransform(rotationAngle: .pi)", None, "(==)") } #endif // The last supported iOS version for 32-bit platforms is iOS 10.3, which didn't // ship with the UIKit overlay, so we cannot run NSValue bridging tests there. // // FIXME: Test back-deployment scenarios with the Swift 5.0 compatibility // runtime rather than a freshly built stdlib. (rdar://62694723) #if canImport(UIKit) && !(os(iOS) && (arch(arm) || arch(i386))) import UIKit ${ testCase("CGRect", "CGRect(x: 17, y: 38, width: 6, height: 79)", "cgRect", "(==)") } ${ testCase("CGPoint", "CGPoint(x: 17, y: 38)", "cgPoint", "(==)") } ${ testCase("CGSize", "CGSize(width: 6, height: 79)", "cgSize", "(==)") } ${ testCase("CGVector", "CGVector(dx: 6, dy: 79)", "cgVector", "(==)") } ${ testCase("CGAffineTransform", "CGAffineTransform(rotationAngle: .pi)", "cgAffineTransform", "(==)") } #endif nsValueBridging.test("NSValue can only be cast back to its original type") { let range = NSRange(location: 17, length: 38) let rangeObject: Any = NSValue(range: range) expectEqual((rangeObject as? NSRange)?.location, range.location) expectEqual((rangeObject as? NSRange)?.length, range.length) expectEqual(rangeObject as? CGRect, .none) expectCrashLater() _ = rangeObject as! CGRect } nsValueBridging.test("NSValue fetching method should be able to convert constructed values safely") { guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else { return } let range = NSRange(location: 17, length: 38) let value = NSValue(range: range) expectEqual(value.value(of: NSRange.self)?.location, range.location) expectEqual(value.value(of: NSRange.self)?.length, range.length) expectEqual(value.value(of: CGRect.self), .none) expectEqual(value.value(of: String.self), .none) expectTrue(value.value(of: AnyObject.self) == nil) class GenericThingNotRootedInObjC { init() { } } let obj = GenericThingNotRootedInObjC() // extend the lifetime to ensure that the non retained pointer is valid for the test duration withExtendedLifetime(obj) { () -> Void in let value = NSValue(nonretainedObject: obj) let resString = value.value(of: GenericThingNotRootedInObjC.self) expectEqual(resString === obj, true) // ensure the value is exactly the same let resInt = value.value(of: GenericThingNotRootedInObjC.self) expectTrue(resInt == nil) } } runAllTests()