blob: 7ad3b5f92d09cfb5c7ab3b8ddde8d4e58a84d79a [file] [log] [blame]
//===-- 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
import StdlibUnittest
import StdlibUnittestFoundationExtras
import Foundation
import CoreGraphics
#if os(iOS) || os(tvOS) || os(watchOS)
import UIKit
#endif
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
#if os(iOS) || os(tvOS) || os(watchOS)
${ 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") {
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<T> {
init() { }
}
let obj = GenericThingNotRootedInObjC<String>()
// 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<String>.self)
expectEqual(resString === obj, true) // ensure the value is exactly the same
let resInt = value.value(of: GenericThingNotRootedInObjC<Int>.self)
expectTrue(resInt == nil)
}
}
runAllTests()