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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
;;;; 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 Lesser 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
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser 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 #:stefil #:mulk.objective-cl #:cl)
(:export #:run-all-tests #:objective-cl)
(:shadowing-import-from #:objcl
#:struct #:union #:pointer #:oneway #:out #:in
#:inout #:const #:parse-typespec #:objective-c-class
#:bit-field #:opaque #:bycopy #:byref
#:primitive-invoke #:print-typespec-to-string
#:nominally #:find-objc-meta-class
#:objcl-object-backed-by-lisp-class-p
#:foreign-class-registered-p))
(in-package #:mulk.objective-cl.tests)
(in-root-suite)
(eval-when (:compile-toplevel)
(objcl:install-reader-syntax))
(defun run-all-tests ()
(objective-cl))
(defsuite objective-cl ()
(objcl:initialise-runtime)
(run-child-tests)
(objcl:shutdown-runtime))
(in-suite objective-cl)
#.(prog1 nil
(defparameter *readtable-backup* *readtable*)
(setq *readtable* (copy-readtable))
(setf (readtable-case *readtable*) :invert))
(deftest base-functions ()
(is (objc-equal (find-objc-class 'ns-object)
(find-objc-class "NSObject")))
(is (null (find-objc-class 'nsobject)))
(is (objc-equal (find-objc-class 'ns-method-invocation)
(find-objc-class "NSMethodInvocation")))
(is (typep (find-selector "mulkyStuff:withMagic:" nil)
'(or null selector)))
(is (objc-equal (find-selector "self")
(find-selector '(self))))
(is (objc-equal (find-selector "stringWithUTF8String:")
(find-selector '(:string-with-u-t-f-8-string))))
(is (objc-equal (find-selector "stringWithCString:encoding:")
(find-selector '(:string-with-c-string :encoding))))
;; Case-sensitivity.
(is (objc-equal (find-selector "stringWithCString:encoding:")
(find-selector '(:stringWithCString :encoding))))
(is (objc-equal (find-selector "stringWithUTF8String:")
(find-selector :stringWithUTF8String))))
#.(prog1 nil
(setq *readtable* *readtable-backup*))
(deftest primitive-method-invocation ()
(#+(or) (signals error (primitive-invoke (find-objc-class 'ns-object)
'string 'id)))
(signals error (primitive-invoke 300 'self 'id))
(signals error (primitive-invoke "abc" 'self 'id))
(#+(or) (signals error (primitive-invoke (find-objc-class 'ns-object)
'selph 'id)))
(is (objc-equal (primitive-invoke (find-objc-class 'ns-object)
'self 'id)
(primitive-invoke (find-objc-class 'ns-object)
'class 'objective-c-class)))
(is (not (objc-equal (primitive-invoke (find-objc-class 'ns-object)
'self 'id)
(primitive-invoke (find-objc-class 'ns-number)
'self 'id))))
(is (objc-equal (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.")))
(is (not (objc-equal (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."))))
(is (primitive-invoke (find-objc-class 'ns-string)
:is-subclass-of-class
(first (parse-typespec "c" t))
(find-objc-class 'ns-object)))
;; performSelector:withObject: cannot be used with non-id return
;; types.
#+(or)
(is (primitive-invoke (find-objc-class 'ns-string)
'(:perform-selector :with-object) :char
(selector "isSubclassOfClass:")
(find-objc-class 'ns-object))))
(deftest method-invocation ()
(signals error [NSObject 300])
(signals error [300 self])
(signals error ["abc" self])
(signals error [NSObject selph])
(is (objc-equal [NSObject self]
[NSObject class]))
(is (not (objc-equal [NSObject self]
[NSNumber self])))
(is (objc-equal [NSString stringWithUTF8String: "Mulk."]
[NSString stringWithUTF8String: "Mulk."]))
(is (not (objc-equal [NSString stringWithUTF8String: "Mulk."]
[NSString stringWithUTF8String: "Klum."])))
(is [NSString isSubclassOfClass: [NSObject class]])
;; performSelector:withObject: cannot be used with non-id return
;; types.
#+(or)
(is [NSString performSelector: (selector "stringWithUTF8String:")
withObject: [NSObject class]]))
(deftest parsing-typespecs ()
(is (equal (parse-typespec "@0:4{_NSRange=II}8")
'(id ())))
(is (equal (parse-typespec ":4{_NSRange=II}8")
'(selector ())))
(is (equal (parse-typespec "{_NSRange=II}8")
'(struct () "_NSRange"
(:unsigned-int ())
(:unsigned-int ()))))
(is (equal (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))))))
(is (equal (parse-typespec "ROi")
;; Here, too, the order of the qualifiers is irrelevant.
'(:int (bycopy byref))))
(is (equal (parse-typespec "(?=)")
'(union () "?")))
(is (equal (parse-typespec "{?=rb123rjf456iii}")
(if (eq objcl::+runtime-type+ :gnu)
'(struct () "?"
(bit-field (const) 123 456
(complex (const) (:float ())))
(:int ())
(:int ())
(:int ()))
'(struct () "?"
(bit-field (const) nil 123)
(complex (const) (:float ()))
(:unrecognised () #\4)
(:unrecognised () #\5)
(:unrecognised () #\6)
(:int ())
(:int ())
(:int ())))))
(is (equal (parse-typespec "^[100{?=ii}]")
'(pointer ()
(array () 100
(struct () "?" (:int ()) (:int ()))))))
(is (equal (parse-typespec "{?=BiIlLqQfd@#:*?}")
'(struct () "?"
(:boolean ())
(:int ())
(:unsigned-int ())
(:long ())
(:unsigned-long ())
(:long-long ())
(:unsigned-long-long ())
(:float ())
(:double ())
(id ()) (objective-c-class ()) (selector ())
(:string ())
(:unknown ()))))
(let ((funky-spec (parse-typespec "{?=cC}")))
(is (member funky-spec
'((struct () "?"
(:char ())
(:unsigned-char ()))
(struct () "?"
(:int ((nominally :char)))
(:unsigned-int ((nominally :unsigned-char)))))
:test #'equalp)))
(let ((funky-spec (parse-typespec "{?=sS}")))
(is (member funky-spec
'((struct () "?"
(:short ())
(:unsigned-short ()))
(struct () "?"
(:int ((nominally :short)))
(:unsigned-int ((nominally :unsigned-short)))))
:test #'equalp)))
(is (equal (parse-typespec "{Mulk=*{Untermulk={Unteruntermulk=}}i}")
'(struct () "Mulk"
(:string ())
(struct () "Untermulk"
(struct () "Unteruntermulk"))
(:int ()))))
(is (equal (parse-typespec "^^{OpaqueStruct}")
'(pointer ()
(pointer ()
(struct (opaque) "OpaqueStruct"))))))
(deftest printing-typespecs ()
(is (equal (print-typespec-to-string '(id ()))
"@"))
(is (equal (print-typespec-to-string '(selector ()))
":"))
(is (equal (print-typespec-to-string '(struct () "_NSRange"
(:unsigned-int ())
(:unsigned-int ())))
"{_NSRange=II}"))
(is (equal (print-typespec-to-string '(pointer (oneway out inout in const)
(array (oneway)
10
(complex (const) (:double nil)))))
;; Actually, the order of the qualifiers doesn't
;; matter, which means that this test is dumber than
;; it ought to be.
"VoNnr^V[10rjd]"))
(is (equal (print-typespec-to-string '(:int (bycopy byref)))
;; Here, too, the order of the qualifiers is irrelevant.
"ORi"))
(is (equal (print-typespec-to-string '(union () "?"))
"(?=)"))
(is (equal (print-typespec-to-string
(if (eq objcl::+runtime-type+ :gnu)
'(struct () "?"
(bit-field (const) 123 456
(complex (const) (:float ())))
(:int ())
(:int ())
(:int ()))
'(struct () "?"
(bit-field (const) nil 123)
(complex (const) (:float ()))
(:unrecognised () #\4)
(:unrecognised () #\5)
(:unrecognised () #\6)
(:int ())
(:int ())
(:int ()))))
"{?=rb123rjf456iii}"))
(is (equal (print-typespec-to-string '(pointer ()
(array () 100
(struct () "?" (:int ()) (:int ())))))
"^[100{?=ii}]"))
(is (equal (print-typespec-to-string '(struct () "?"
(:boolean ())
(:int ())
(:unsigned-int ())
(:long ())
(:unsigned-long ())
(:long-long ())
(:unsigned-long-long ())
(:float ())
(:double ())
(id ()) (objective-c-class ()) (selector ())
(:string ())
(:unknown ())))
"{?=BiIlLqQfd@#:*?}"))
(is (equal (print-typespec-to-string '(struct () "?"
(:char ())
(:unsigned-char ())))
"{?=cC}"))
(is (equal (print-typespec-to-string '(struct () "?"
(:short ())
(:unsigned-short ())))
"{?=sS}"))
(is (equal (print-typespec-to-string
'(struct () "Mulk"
(:string ())
(struct () "Untermulk"
(struct () "Unteruntermulk"))
(:int ())))
"{Mulk=*{Untermulk={Unteruntermulk=}}i}"))
(is (equal (print-typespec-to-string
'(pointer ()
(pointer ()
(struct (opaque) "OpaqueStruct"))))
"^^{OpaqueStruct}")))
(deftest data-coercion ()
(is (objc-equal [NSString stringWithUTF8String: "Mulk."]
[NSString stringWithCString: "Mulk." encoding: 4]))
(is (objc-equal [NSString respondsToSelector: (selector "new")]
[NSString respondsToSelector: 'new]))
(is (objc-equal [NSString respondsToSelector: (selector "new")]
[NSString respondsToSelector: "new"]))
(is (typep [NSString isEqual: [NSString self]] 'boolean))
(is (typep [NSString isEqual: [NSObject self]] 'boolean)))
(deftest numbers ()
(is (objc-equal [[NSDecimalNumber decimalNumberWithString:
[NSString stringWithUTF8String: "-12345"]]
doubleValue]
-12345d0)))
(deftest exception-handling ()
(is (typep (handler-case [NSString selph]
(error (e) e))
'(or no-such-selector message-not-understood)))
(is (typep (handler-case [NSObject string]
(error (e) e))
'message-not-understood)))
(deftest reader-syntax ()
(is (objc-equal [NSObject self]
(find-objc-class 'ns-object)))
(is (objc-equal [NSString stringWithUTF8String: "Mulk."]
(invoke (find-objc-class 'ns-string)
:string-with-u-t-f-8-string "Mulk.")))
(is (objc-equal [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)
(is (objc-equal [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)))))
(defvar *class-counter* 0)
(deftest class-definition ()
(let ((class-name (intern (format nil "~A~D"
'#:mlk-test-string
(incf *class-counter*))))
(class nil)
(instance nil))
(setq class
(is (c2mop:ensure-class class-name
:direct-superclasses
(list (find-objc-class "NSObject"))
:direct-slots '((:name foos
:type list
:initargs (:foos))
(:name foo-count
:foreign-type (:int ())))
:metaclass (find-objc-meta-class "NSObject"))))
;; Class initialisation.
(is (not (foreign-class-registered-p class)))
;; Sanity checks.
(is (typep class 'objective-c-class))
(setq instance (is (invoke (invoke class 'alloc) 'init)))
;; Class finalisation. (Should be automatic upon instance
;; creation.)
(is (foreign-class-registered-p class))
;; Object identity preservation.
(is (eql instance
(invoke instance 'self)))
;; FIXME: What's wrong with the following line? It makes CMUCL
;; throw weird errors.
#+(or) (is (typep instance class-name))
;; Slot initialisation.
(is (not (slot-boundp instance 'foos)))
(is (slot-boundp instance 'foo-count)) ;foreign slots are always SLOT-BOUNDP
;; Slot handling.
(setf (slot-value instance 'foos) '(a b c))
(setf (slot-value instance 'foo-count) 3)
(is (slot-boundp instance 'foos))
;; Native slots.
(is (objcl-object-backed-by-lisp-class-p instance))
(is (equal (slot-value instance 'foos)
(slot-value (invoke instance 'self) 'foos)))
(is (typep (slot-value (invoke instance 'self) 'foos)
'list))
(is (equal (slot-value (invoke instance 'self) 'foos)
'(a b c)))
;; Foreign slots.
(is (equal (slot-value instance 'foo-count)
(slot-value (invoke instance 'self) 'foo-count)))
(is (typep (slot-value (invoke instance 'self) 'foo-count)
'integer))
(is (equal (slot-value (invoke instance 'self) 'foo-count)
3))))
|