blob: b8ac773f2681e4eee7583be24d19c32ed6cb1db7 (
plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
;;;; Objective-CL, an Objective-C bridge for Common Lisp.
;;;; Copyright (C) 2007 Matthias Andreas Benkard.
;;;;
;;;; This program is free software: you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License as
;;;; published by the Free Software Foundation, either version 3 of the
;;;; License, or (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful, but
;;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program. If not, see
;;;; <http://www.gnu.org/licenses/>.
(defpackage #:mulk.objective-cl.tests
(:nicknames #:objcl-tests #:objective-cl-tests #:mulk.objcl-tests)
(:use #:lift #:mulk.objective-cl #:cl)
(:export #:run-all-tests)
(:shadowing-import-from #:objcl
#:struct #:union #:pointer #:oneway #:out #:in
#:inout #:const #:parse-typespec #:objc-class
#:bit-field #:opaque #:bycopy #:byref
#:primitive-invoke))
(in-package #:mulk.objective-cl.tests)
(eval-when (:compile-toplevel)
(objcl:install-reader-syntax))
(defun run-all-tests ()
(objcl:initialise-runtime)
(run-tests :suite 'objective-cl))
(deftestsuite objective-cl ()
())
(defrandom-instance double objective-cl
(if (zerop (random 2))
(random most-positive-double-float)
(- (random (abs most-negative-double-float)))))
(deftestsuite base-functions (objective-cl)
()
(:equality-test #'objc-equal)
(:tests
((ensure-same (find-objc-class 'ns-object)
(find-objc-class "NSObject")))
((ensure-null (find-objc-class 'nsobject)))
((ensure-same (find-objc-class 'ns-method-invocation)
(find-objc-class "NSMethodInvocation")))
((ensure-null (find-selector "mulkyStuff:withMagic:")))
((ensure-same (find-selector "self")
(find-selector '(self))))
((ensure-same (find-selector "stringWithUTF8String:")
(find-selector '(:string-with-u-t-f-8-string))))
((ensure-same (find-selector "stringWithCString:encoding:")
(find-selector '(:string-with-c-string :encoding))))))
(deftestsuite primitive-method-invocation (objective-cl)
()
(:equality-test #'objc-equal)
(:tests
(#+(or) (ensure-error (primitive-invoke (find-objc-class 'ns-object)
'string 'id)))
((ensure-error (primitive-invoke 300 'self 'id)))
((ensure-error (primitive-invoke "abc" 'self 'id)))
(#+(or) (ensure-error (primitive-invoke (find-objc-class 'ns-object)
'selph 'id)))
((ensure-same (primitive-invoke (find-objc-class 'ns-object)
'self 'id)
(primitive-invoke (find-objc-class 'ns-object)
'class 'objc-class)))
((ensure-different (primitive-invoke (find-objc-class 'ns-object)
'self 'id)
(primitive-invoke (find-objc-class 'ns-number)
'self 'id)))
((ensure-same (primitive-invoke (find-objc-class 'ns-string)
:string-with-u-t-f-8-string 'id
"Mulk.")
(primitive-invoke (find-objc-class 'ns-string)
:string-with-u-t-f-8-string 'id
"Mulk.")))
((ensure-different (primitive-invoke (find-objc-class 'ns-string)
:string-with-u-t-f-8-string 'id
"Mulk.")
(primitive-invoke (find-objc-class 'ns-string)
:string-with-u-t-f-8-string 'id
"Klum.")))
((ensure-same +yes+
(primitive-invoke (find-objc-class 'ns-string)
:is-subclass-of-class :char
(find-objc-class 'ns-object))))
;; performSelector:withObject: cannot be used with non-id return
;; types.
#+(or)
((ensure-same +yes+
(primitive-invoke (find-objc-class 'ns-string)
'(:perform-selector :with-object) :char
(selector "isSubclassOfClass:")
(find-objc-class 'ns-object))))))
(deftestsuite method-invocation (objective-cl)
()
(:equality-test #'objc-equal)
(:tests
((ensure-error [NSObject 300]))
((ensure-error [300 self]))
((ensure-error ["abc" self]))
((ensure-error [NSObject selph]))
((ensure-same [NSObject self]
[NSObject class]))
((ensure-different [NSObject self]
[NSNumber self]))
((ensure-same [NSString stringWithUTF8String: "Mulk."]
[NSString stringWithUTF8String: "Mulk."]))
((ensure-different [NSString stringWithUTF8String: "Mulk."]
[NSString stringWithUTF8String: "Klum."]))
((ensure [NSString isSubclassOfClass: [NSObject class]]))
;; performSelector:withObject: cannot be used with non-id return
;; types.
#+(or)
((ensure [NSString performSelector:
(selector "stringWithUTF8String:")
withObject: [NSObject class]]))))
(deftestsuite parsing-typespecs (objective-cl)
()
(:equality-test #'equal)
(:tests
((ensure-same (parse-typespec "@0:4{_NSRange=II}8")
'(id ())))
((ensure-same (parse-typespec ":4{_NSRange=II}8")
'(selector ())))
((ensure-same (parse-typespec "{_NSRange=II}8")
'(struct () "_NSRange"
(:unsigned-int ())
(:unsigned-int ()))))
((ensure-same (parse-typespec "rnNoV^V[10rjd]4")
;; Actually, the order of the qualifiers doesn't
;; matter, which means that this test is dumber than
;; it ought to be.
'(pointer (oneway out inout in const)
(array (oneway)
10
(complex (const) (:double nil))))))
((ensure-same (parse-typespec "ROi")
;; Here, too, the order of the qualifiers is irrelevant.
'(:int (bycopy byref))))
((ensure-same (parse-typespec "(?=)")
'(union () "?")))
((ensure-same (parse-typespec "{?=rb123rjf456iii}")
'(struct () "?"
(bit-field (const) 123 456
(complex (const) (:float ())))
(:int ())
(:int ())
(:int ()))))
((ensure-same (parse-typespec "^[100{?=ii}]")
'(pointer ()
(array () 100
(struct () "?" (:int ()) (:int ()))))))
((ensure-same (parse-typespec "{?=BcCsSiIlLqQfd@#:*?}")
'(struct () "?"
(:boolean ())
(:char ())
(:unsigned-char ())
(:short ())
(:unsigned-short ())
(:int ())
(:unsigned-int ())
(:long ())
(:unsigned-long ())
(:long-long ())
(:unsigned-long-long ())
(:float ())
(:double ())
(id ()) (objc-class ()) (selector ())
(:string ())
(:unknown ()))))
((ensure-same (parse-typespec "{Mulk=*{Untermulk={Unteruntermulk=}}i}")
'(struct () "Mulk"
(:string ())
(struct () "Untermulk"
(struct () "Unteruntermulk"))
(:int ()))))
((ensure-same (parse-typespec "^^{OpaqueStruct}")
'(pointer ()
(pointer ()
(struct (opaque) "OpaqueStruct")))))))
(deftestsuite data-coercion (objective-cl)
()
(:equality-test #'objc-equal)
(:tests
((ensure-same [NSString stringWithUTF8String: "Mulk."]
[NSString stringWithCString: "Mulk." encoding: 4]))
((ensure-same [NSString respondsToSelector: (selector "new")]
[NSString respondsToSelector: 'new]))
((ensure-same [NSString respondsToSelector: (selector "new")]
[NSString respondsToSelector: "new"]))
((ensure (typep [NSString isEqual: [NSString self]] 'boolean)))
((ensure (typep [NSString isEqual: [NSObject self]] 'boolean)))))
(deftestsuite numbers (objective-cl)
()
(:equality-test #'objc-equal)
(:tests
((ensure-same [[NSDecimalNumber
decimalNumberWithString:
[NSString stringWithUTF8String:
"-12345"]]
doubleValue]
-12345d0))))
(deftestsuite exception-handling (objective-cl)
()
(:equality-test #'objc-equal)
(:tests
((ensure (typep (handler-case
[NSString selph]
(error (e) e))
'(or no-such-selector message-not-understood))))
((ensure (typep (handler-case
[NSObject string]
(error (e) e))
'message-not-understood)))))
(deftestsuite reader-syntax (objective-cl)
()
(:equality-test #'objc-equal)
(:tests
((ensure-same [NSObject self]
(find-objc-class 'ns-object)))
((ensure-same [NSString stringWithUTF8String: "Mulk."]
(invoke (find-objc-class 'ns-string)
:string-with-u-t-f-8-string "Mulk.")))
((ensure-same [NSString stringWithCString: "Mulk." encoding: 4]
(invoke (find-objc-class 'ns-string)
:string-with-c-string "Mulk." :encoding 4)))
;; performSelector:withObject: cannot be used with non-id return
;; types.
#+(or)
((ensure-same [NSString performSelector:
(selector "isSubclassOfClass:")
withObject: [NSObject self]]
(invoke (find-objc-class 'ns-string)
:perform-selector (selector "isSubclassOfClass:")
:with-object (invoke
(find-objc-class 'ns-object)
'self))))))
|