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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
// RUN: %empty-directory(%t)
// RUN: %clang %target-cc-options -isysroot %sdk -fobjc-arc %S/Inputs/ObjCClasses/ObjCClasses.m -c -o %t/ObjCClasses.o
// RUN: %target-build-swift -I %S/Inputs/ObjCClasses/ %t/ObjCClasses.o %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out
// REQUIRES: executable_test
// REQUIRES: objc_interop
import Foundation
import StdlibUnittest
import ObjCClasses
class SwiftClass : ProtoWithClassProperty {
static var getCount = 0
static var setCount = 0
private static var _value: CInt = 0
@objc class func reset() {
getCount = 0
setCount = 0
_value = 0
}
@objc class var value: CInt {
get {
getCount += 1
return _value
}
set {
setCount += 1
_value = newValue
}
}
@objc class var optionalClassProp: Bool {
return true
}
}
class Subclass : ClassWithClassProperty {
static var getCount = 0
static var setCount = 0
override class func reset() {
getCount = 0
setCount = 0
super.reset()
}
override class var value: CInt {
get {
getCount += 1
return super.value
}
set {
setCount += 1
super.value = newValue
}
}
override class var optionalClassProp: Bool {
return true
}
}
var ClassProperties = TestSuite("ClassProperties")
ClassProperties.test("direct") {
ClassWithClassProperty.reset()
expectEqual(0, ClassWithClassProperty.value)
ClassWithClassProperty.value = 4
expectEqual(4, ClassWithClassProperty.value)
Subclass.reset()
expectEqual(0, Subclass.value)
Subclass.value = 4
expectEqual(4, Subclass.value)
expectEqual(2, Subclass.getCount)
expectEqual(1, Subclass.setCount)
}
func testExistential(_ e: ProtoWithClassProperty.Type) {
e.reset()
expectEqual(0, e.value)
e.value = 4
expectEqual(4, e.value)
}
ClassProperties.test("existentials") {
testExistential(ClassWithClassProperty.self)
testExistential(ObjCSubclassWithClassProperty.self)
testExistential(SwiftClass.self)
expectEqual(2, SwiftClass.getCount)
expectEqual(1, SwiftClass.setCount)
testExistential(Subclass.self)
expectEqual(2, Subclass.getCount)
expectEqual(1, Subclass.setCount)
}
func testGeneric<T: ProtoWithClassProperty>(_ e: T.Type) {
e.reset()
expectEqual(0, e.value)
e.value = 4
expectEqual(4, e.value)
}
ClassProperties.test("generics") {
testGeneric(ClassWithClassProperty.self)
testGeneric(ObjCSubclassWithClassProperty.self)
testGeneric(SwiftClass.self)
expectEqual(2, SwiftClass.getCount)
expectEqual(1, SwiftClass.setCount)
testGeneric(Subclass.self)
expectEqual(2, Subclass.getCount)
expectEqual(1, Subclass.setCount)
}
func testInheritance(_ e: ClassWithClassProperty.Type) {
e.reset()
expectEqual(0, e.value)
e.value = 4
expectEqual(4, e.value)
}
ClassProperties.test("inheritance") {
testInheritance(ClassWithClassProperty.self)
testInheritance(ObjCSubclassWithClassProperty.self)
testInheritance(Subclass.self)
expectEqual(2, Subclass.getCount)
expectEqual(1, Subclass.setCount)
}
func testInheritanceGeneric<T: ClassWithClassProperty>(_ e: T.Type) {
e.reset()
expectEqual(0, e.value)
e.value = 4
expectEqual(4, e.value)
}
ClassProperties.test("inheritance/generic") {
testInheritanceGeneric(ClassWithClassProperty.self)
testInheritanceGeneric(ObjCSubclassWithClassProperty.self)
testInheritanceGeneric(Subclass.self)
expectEqual(2, Subclass.getCount)
expectEqual(1, Subclass.setCount)
}
ClassProperties.test("optionalProp") {
let noProp: ProtoWithClassProperty.Type = ClassWithClassProperty.self
expectNil(noProp.optionalClassProp)
let hasProp: ProtoWithClassProperty.Type = Subclass.self
expectNotNil(hasProp.optionalClassProp)
expectEqual(true, hasProp.optionalClassProp!)
let hasOwnProp: ProtoWithClassProperty.Type = SwiftClass.self
expectNotNil(hasOwnProp.optionalClassProp)
expectEqual(true, hasOwnProp.optionalClassProp!)
let hasPropObjC: ProtoWithClassProperty.Type = ObjCSubclassWithClassProperty.self
expectNotNil(hasPropObjC.optionalClassProp)
expectEqual(true, hasPropObjC.optionalClassProp!)
}
class NamingConflictSubclass : PropertyNamingConflict {
override var prop: Any? { return nil }
override class var prop: Any? { return NamingConflictSubclass() }
}
ClassProperties.test("namingConflict") {
let obj = PropertyNamingConflict()
expectTrue(obj === obj.prop.map { $0 as AnyObject })
expectNil(type(of: obj).prop)
expectNil(PropertyNamingConflict.prop)
let sub = NamingConflictSubclass()
expectNil(sub.prop)
expectNotNil(type(of: sub).prop)
expectNotNil(NamingConflictSubclass.prop)
}
extension NamingConflictSubclass : PropertyNamingConflictProto {
var protoProp: Any? {
get { return self }
set {}
}
class var protoProp: Any? {
get { return nil }
set {}
}
}
ClassProperties.test("namingConflict/protocol") {
let obj: PropertyNamingConflictProto = NamingConflictSubclass()
expectTrue(obj === obj.protoProp.map { $0 as AnyObject })
expectNil(type(of: obj).protoProp)
let type: PropertyNamingConflictProto.Type = NamingConflictSubclass.self
expectNil(type.protoProp)
}
var global1: Int = 0
var global2: Int = 0
class Steak : NSObject {
@objc override var thickness: Int {
get { return global1 } set { global1 = newValue }
}
}
extension NSObject : HasThickness {
@objc var thickness: Int { get { return global2 } set { global2 = newValue } }
}
protocol HasThickness : class {
var thickness: Int { get set }
}
ClassProperties.test("dynamicOverride") {
// Calls NSObject.thickness
NSObject().thickness += 1
// Calls Steak.thickness
(Steak() as NSObject).thickness += 1
Steak().thickness += 1
(Steak() as HasThickness).thickness += 1
expectEqual(3, global1)
expectEqual(1, global2)
}
runAllTests()
|