File: NSValueBridging.swift.gyb

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (125 lines) | stat: -rw-r--r-- 4,854 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//===-- 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

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<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()