forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjc_class_properties.swift
211 lines (170 loc) · 5.05 KB
/
objc_class_properties.swift
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
// RUN: rm -rf %t && mkdir -p %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-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
expectEmpty(noProp.optionalClassProp)
let hasProp: ProtoWithClassProperty.Type = Subclass.self
expectNotEmpty(hasProp.optionalClassProp)
expectEqual(true, hasProp.optionalClassProp!)
let hasOwnProp: ProtoWithClassProperty.Type = SwiftClass.self
expectNotEmpty(hasOwnProp.optionalClassProp)
expectEqual(true, hasOwnProp.optionalClassProp!)
let hasPropObjC: ProtoWithClassProperty.Type = ObjCSubclassWithClassProperty.self
expectNotEmpty(hasPropObjC.optionalClassProp)
expectEqual(true, hasPropObjC.optionalClassProp!)
}
class NamingConflictSubclass : PropertyNamingConflict {
override var prop: AnyObject? { return nil }
override class var prop: AnyObject? { return NamingConflictSubclass() }
}
ClassProperties.test("namingConflict") {
let obj = PropertyNamingConflict()
expectTrue(obj === obj.prop)
expectEmpty(obj.dynamicType.prop)
expectEmpty(PropertyNamingConflict.prop)
let sub = NamingConflictSubclass()
expectEmpty(sub.prop)
expectNotEmpty(sub.dynamicType.prop)
expectNotEmpty(NamingConflictSubclass.prop)
}
extension NamingConflictSubclass : PropertyNamingConflictProto {
var protoProp: AnyObject? {
get { return self }
set {}
}
class var protoProp: AnyObject? {
get { return nil }
set {}
}
}
ClassProperties.test("namingConflict/protocol") {
let obj: PropertyNamingConflictProto = NamingConflictSubclass()
expectTrue(obj === obj.protoProp)
expectEmpty(obj.dynamicType.protoProp)
let type: PropertyNamingConflictProto.Type = NamingConflictSubclass.self
expectEmpty(type.protoProp)
}
runAllTests()