Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 474 lines (431 sloc) 13.283 kB
506b8fa @timburks First public version.
authored
1 # test_nu.rb
2 # Nu sanity tests. Requires Ruby, RubyObjC, and Test::Unit.
3 #
29db4cd @timburks Company name changed.
authored
4 # Copyright (c) 2007 Tim Burks, Radtastical Inc.
506b8fa @timburks First public version.
authored
5 # For more information about this file, visit http://programming.nu.
6
7 require 'test/unit'
8
9 unless defined? ObjC
10 require 'rubygems'
11 require 'objc'
12 end
13
14 ObjC::NSBundle.bundleWithPath_(File.dirname(__FILE__)+'/../Nu.framework').load
15
16 class ObjC::NuParser
17 def eval(expr)
18 value = parseEval_(expr)
19 value ? value.to_s : nil
20 end
21 end
22
23 class TestNu < Test::Unit::TestCase
24 def setup
25 @parser = ObjC::NuParser.alloc.init
26 end
27
28 def teardown
29 @patterns.split("\n").each do |line|
30 expression, value = line.split('===')
31 expression = expression.strip
32 value = value.chomp.strip if value
33 value = nil if value == "nil"
34 result = @parser.eval(expression)
35 assert_equal(value, result, "error in the evaluation of #{expression}") if value
36 end
37 end
38
39 def test_eval
40 @patterns = <<-END
41 (eval '(+ 2 2)) === 4
42 END
43 end
44
45 def test_cadr
46 @patterns = <<-END
47 (set a '(1 2 3)) === (1 2 3)
48 (set b '((1 2) 3)) === ((1 2) 3)
49 (car a) === 1
50 (car b) === (1 2)
51 (car (car b)) === 1
52 (cdr a) === (2 3)
53 (cdr b) === (3)
54 (car (cdr b)) === 3
55 END
56 end
57
58 def test_atom
59 @patterns = <<-END
60 (atom '(1 2 3)) === ()
61 (atom 1) === t
62 (atom 'a) === t
63 END
64 end
65
66 def test_cond
67 @patterns = <<-END
bcfb80d @timburks Purged usage of "t" for truth from Nu library code and examples.
authored
68 (cond (1 '1) (else '2)) === 1
69 (cond (0 '1) (else '2)) === 2
506b8fa @timburks First public version.
authored
70 END
71 end
72
73 def test_cons
74 @patterns = <<-END
75 (cons 1 '(2 3)) === (1 2 3)
76 (cons '(1 2) '(3)) === ((1 2) 3)
77 (cons 1 2) === (1 . 2)
78 (cons 1) === (1)
79 (cons nil 1) === (() . 1)
80 ((cons 1 (cons 2 3))) === (1 2 . 3)
81 END
82 end
83
84 def test_positions
85 @patterns = <<-END
86 (set a '(1 2 3 4 5 6))
87 (a first) === 1
88 (a second) === 2
89 (a third) === 3
90 (a fourth) === 4
91 (a fifth) === 5
92 END
93 end
94
95 def test_set
96 @patterns = <<-END
97 (set a 1) === 1
98 (set b 2) === 2
99 a === 1
100 b === 2
101 (set c (set d 3)) === 3
102 c === 3
103 END
104 end
105
106 def test_do
107 @patterns = <<-END
108 (set a 0) === 0
109 (set b 0) === 0
110 (set c 3) === 3
111 (def f (a b) (+ a b c)) === (do (a b) ((+ a b c)))
112 (set c 4) === 4
113 (f 1 2) === 6
114 (f 3 4) === 10
115 (def g (a b) (set c (+ a b))(* c c))
116 (g 2 3) === 25
117 c === 4
118 END
119 end
120
121 def test_factorial
122 @patterns = <<-END
123 (def factorial (x)
124 (cond ((eq x 0) 1)
bcfb80d @timburks Purged usage of "t" for truth from Nu library code and examples.
authored
125 (else (* x (factorial (- x 1))))))
506b8fa @timburks First public version.
authored
126 (factorial 0) === 1
127 (factorial 1) === 1
128 (factorial 2) === 2
129 (factorial 10) === 3628800
130 END
131 end
132
133 def test_fibonacci
134 @patterns = <<-END
135 (def fib (x)
136 (cond ((eq x 0) 1)
137 ((eq x 1) 1)
bcfb80d @timburks Purged usage of "t" for truth from Nu library code and examples.
authored
138 (else (+ (fib (- x 1)) (fib (- x 2))))))
506b8fa @timburks First public version.
authored
139 (fib 0) === 1
140 (fib 1) === 1
141 (fib 2) === 2
142 (fib 3) === 3
143 (fib 4) === 5
144 (fib 5) === 8
145 (fib 6) === 13
146 (fib 7) === 21
147 (fib 8) === 34
148 (fib 9) === 55
149 END
150 end
151
152 def test_times
153 @patterns = <<-END
154 (set a ((NSMutableArray alloc) init))
155 (3 times: (do (i)
156 (3 times: (do (j)
157 (a addObject: (+ (* i 3) j))
158 ))
159 ))
160 (a count) === 9
161 (a objectAtIndex: 4) === 4
162 (a objectAtIndex: 8) === 8
163 END
164 end
165
166 def test_messages
167 @patterns = <<-END
168 ((NuClass all) count) === #{ObjC::Class.to_a.length}
169 END
170 end
171
172 def test_bridge
173 @patterns = <<-END
174 ("scheme" commonPrefixWithString:"school" options:0) === sch
175 ("scheme" capitalizedString) === Scheme
176 ((NSArray arrayWithObject:2) objectAtIndex:0) === 2
177 END
178 end
179
180 def test_mutableset
181 @patterns = <<-END
182 (set s ((NSMutableSet alloc) init))
183 (s addObject:1)
184 (s addObject:"two")
185 (s addObject:3.0)
186 (s count) === 3
187 (s member:1) === 1
188 (s member:"two") === two
189 (s member:3.0) === 3
190 (s member:4) === ()
191 END
192 end
193
194 def test_select
195 @patterns = <<-END
196 (NSArray include: NuEnumerable)
197 (set m (NSString classMethods))
198 (((m select:(do (x) (eq (x name) "string"))) lastObject) name) === string
199 (set name-is (do (x) (do (y) (eq (y name) x))))
200 (((m select:(name-is "string")) lastObject) name) === string
201 (((m select:(name-is "stringWithCString:")) lastObject) name) === stringWithCString:
202 ((m find:(name-is "stringWithCString:")) name) === stringWithCString:
203 END
204 end
205
206 def test_add_instance_method
207 @patterns = <<-END
208 (NSObject addInstanceMethod:"beep" signature:"@@:" body:(do () ("beep!")))
209 (set o ((NSObject alloc) init))
210 (o beep) === beep!
211 END
212 end
213
214 def test_simple_subclass
215 @patterns = <<-END
216 (NSObject createSubclassNamed:"MyChild")
217 (MyChild addInstanceMethod:"show" signature:"@@:" body:(do () (self class)))
218 (set o ((MyChild alloc) init))
219 (o show) === MyChild
220 END
221 end
222
223 def test_add_factorial_method
224 @patterns = <<-END
225 (NSNumber addInstanceMethod:"factorial" signature:"i@:" body:
226 (do ()
227 (set x (self intValue))
228 (cond ((eq x 0) 1)
bcfb80d @timburks Purged usage of "t" for truth from Nu library code and examples.
authored
229 (else (* x ((- x 1) factorial))))))
506b8fa @timburks First public version.
authored
230 (5 factorial) === #{5*4*3*2}
231 END
232 end
233
234 def test_addition_shocker
235 @patterns = <<-END
236 (NSNumber addInstanceMethod:"+" signature:"d@:d" body:(do (x) (+ x (self doubleValue))) )
237 (1.2 + 3.4) === 4.6
238 END
239 end
240
241 def test_add_class_method
242 @patterns = <<-END
243 (NSNumber addClassMethod:"three" signature:"@@:" body:(do () (+ 1 1 1)))
244 (NSNumber three) === 3
245 END
246 end
247
248 def test_add_ivar
249 # try this for a few different classes. NSWindow crashes. :-(
250 @patterns = <<-END
251 (NSWindowController createSubclassNamed: "Child")
252 (Child addInstanceVariable: "one" signature: "i")
253 (set c ((Child alloc) init))
254 (c valueForKey: "one") === 0
255 (c setValue: 33 forKey: "one")
256 (c valueForKey: "one") === 33
257 END
258 # trying to read a non-existent ivar crashes too:
259 # (c valueForKey: "two")
260 end
261
262 def test_add_ivar2
263 @patterns = <<-END
264 (class Test is NSObject
265 (ivar (int) x)
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
266 (- (void) setX:(int) x is (set @x x))
267 (- (int) x is @x))
506b8fa @timburks First public version.
authored
268 (set y ((Test alloc) init))
269 (y setX:33)
270 (y x) === 33
271 END
272 end
273
274 def test_auto_ivar
275 @patterns = <<-END
276 (class Foo is NSObject
277 (ivars)
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
278 (- (id) incr is
506b8fa @timburks First public version.
authored
279 (cond (@y (set @y (+ @y 1)))
bcfb80d @timburks Purged usage of "t" for truth from Nu library code and examples.
authored
280 (else (set @y 1)))))
506b8fa @timburks First public version.
authored
281 (set f ((Foo alloc) init))
282 (f setValue:2 forIvar:"x")
283 (f valueForIvar:"x") === 2
284 (f incr) === 1
285 (f incr) === 2
286 (f incr) === 3
287 END
288 end
289
290 def test_let
291 @patterns = <<-END
292 (let (abc 999) (+ 2 2) (+ 1 abc)) === 1000
293 (let ((a 1) (b 2)) (+ a b)) === 3
294 END
295 end
296
297 def test_list
298 @patterns = <<-END
299 (list 1 2 3) === (1 2 3)
300 (list (list 2 3 4) 2) === ((2 3 4) 2)
301 END
302 end
303
304 def test_counter_class
305 @patterns = <<-END
306 (class Counter is NSObject
307 (ivar (id) c)
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
308 (- (id) init is
506b8fa @timburks First public version.
authored
309 (super init)
310 (set @c 0)
311 self)
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
312 (- (id) count is (@c))
313 (- (void) step is (set @c (+ @c 1))))
506b8fa @timburks First public version.
authored
314 (set counter ((Counter alloc) init))
315 (counter count) === 0
316 (10 times: (do () (counter step)))
317 (counter count) === 10
318 END
319 end
320
321 def test_another_factorial
322 @patterns = <<-END
323 (class NSNumber
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
324 (- (id) "!" is
506b8fa @timburks First public version.
authored
325 (set n (self intValue))
326 (cond ((eq n 0) 1)
bcfb80d @timburks Purged usage of "t" for truth from Nu library code and examples.
authored
327 (else (* n ((- n 1)!))))))
506b8fa @timburks First public version.
authored
328 (3 !) === 6
329 (4 !) === 24
330 (5 !) === 120
331 END
332 end
333
334 def test_append
335 @patterns = <<-END
336 (append '(1 2 3) '() '(4 5 6) '(7) '(8 9)) === (1 2 3 4 5 6 7 8 9)
337 END
338 end
339
340 def test_extensions
341 @patterns = <<-END
342 (NSArray include: NuEnumerable)
343 (class NSMutableDictionary
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
344 (+ (id) dictionaryWithList: (id) list is
506b8fa @timburks First public version.
authored
345 (let (d (self dictionary))
346 (list eachPair:
347 (do (key value)
348 (d setValue:value forKey:key)))
349 d)))
350 (class NSDictionary
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
351 (- (id) list is
506b8fa @timburks First public version.
authored
352 ((self allKeys) reduce:
353 (do (result key)
354 (cons key (cons (self objectForKey: key) result)))
355 from: nil)))
356 (class NSMutableArray
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
357 (+ (id) arrayWithList: (id) list is
506b8fa @timburks First public version.
authored
358 (let (a (self array))
359 (list each: (do (item) (a addObject: item)))
360 a)))
361 (class NSArray
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
362 (- (id) list is
506b8fa @timburks First public version.
authored
363 (self reduceLeft:(do (result item) (cons item result)) from: nil))
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
364 (- (id) sum is
506b8fa @timburks First public version.
authored
365 (self reduce:(do (result item) (+ item result)) from: 0)))
366 (set d (NSMutableDictionary dictionaryWithList: '("a" 1 "b" 2 "c" 3)))
367 (d count) === 3
368 (d objectForKey:"a") === 1
369 (d objectForKey:"b") === 2
370 (d objectForKey:"c") === 3
371 (set e (NSMutableDictionary dictionaryWithList: (d list)))
372 (e count) === 3
373 (e objectForKey:"a") === 1
374 (e objectForKey:"b") === 2
375 (e objectForKey:"c") === 3
376 (set a (NSMutableArray arrayWithList: '(1 2 3 4 5 6)))
377 (a count) === 6
378 (a list) === (1 2 3 4 5 6)
379 (a sum) === 21
380 END
381 end
382
383 def test_super
384 @patterns = <<-END
385 (class Level1 is NSObject
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
386 (- (int) depth is (1)))
506b8fa @timburks First public version.
authored
387 (class Level2 is Level1
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
388 (- (int) depth is (+ 1 (super depth))))
506b8fa @timburks First public version.
authored
389 (class Level3 is Level2
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
390 (- (int) depth is (+ 1 (super depth))))
506b8fa @timburks First public version.
authored
391 (class Level4 is Level3
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
392 (- (int) depth is (+ 1 (super depth))))
506b8fa @timburks First public version.
authored
393 (((Level1 alloc) init) depth) === 1
394 (((Level2 alloc) init) depth) === 2
395 (((Level3 alloc) init) depth) === 3
396 (((Level4 alloc) init) depth) === 4
397 END
398 end
399
400 def test_method_replacement
401 @patterns = <<-END
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
402 (class NSObject (- (int) one is 1))
506b8fa @timburks First public version.
authored
403 (((NSObject alloc) init) one) === 1
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
404 (class NSObject (- (int) one is 2))
506b8fa @timburks First public version.
authored
405 (((NSObject alloc) init) one) === 2
406 END
407 # this doesn't work, though:
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
408 #(class NSObject (- (double) one is (1.1)))
506b8fa @timburks First public version.
authored
409 #(((NSObject alloc) init) one) === 1.1
410 # it seems that we can't change the return type of a method
411 end
412
413 def test_case
414 @patterns = <<-END
415 (set x 1)
416 (case x (2 y) (4 z) (11 2) (9 9 10 11) (x (set y 4) (* y x)) (2 (+ 1 1))) === 4
417 END
418 end
419
420 def test_if_unless
421 @patterns = <<-END
422 (if (eq 1 1) 99) === 99
423 (if (eq 1 2) 99) === ()
424 (unless (eq 1 1) 99) === ()
425 (unless (eq 1 2) 99) === 99
426 END
427 end
428
429 def test_structs
430 @patterns = <<-END
431 (def reverse (n)
432 (cond ((eq n nil) nil)
bcfb80d @timburks Purged usage of "t" for truth from Nu library code and examples.
authored
433 (else (append (reverse (cdr n)) (list (car n))))))
506b8fa @timburks First public version.
authored
434 (class Structs is NSObject
045f219 @timburks Replace "cmethod" and "imethod" with "+" and "-".
authored
435 (+ (NSPoint) makePointX:(double)x y:(double)y is (list x y))
436 (+ (NSSize) makeSizeW:(double)w h:(double)h is (list w h))
437 (+ (NSRect) makeRectX:(double)x y:(double)y w:(double)w h:(double)h is (list x y w h))
438 (+ (id) unpackPoint:(NSPoint) point is point)
439 (+ (id) unpackSize:(NSSize) size is size)
440 (+ (id) unpackRect:(NSRect) rect is rect)
441 (+ (NSPoint) passPoint:(NSPoint) point is point)
442 (+ (NSSize) passSize:(NSSize) size is size)
443 (+ (NSRange) passRange:(NSRange) range is range)
444 (+ (NSRect) passRect:(NSRect) rect is rect)
445 (+ (NSRect) flipRect:(NSRect) rect is (reverse rect)))
506b8fa @timburks First public version.
authored
446 (Structs makePointX:3 y:5) === (3 5)
447 (Structs makeSizeW:9.9 h:110) === (9.9 110)
448 (Structs makeRectX:1 y:2.2 w:3.3 h:4.4) === (1 2.2 3.3 4.4)
449 (Structs unpackPoint:'(2 3)) === (2 3)
450 (Structs unpackSize:'(9 10)) === (9 10)
451 (Structs unpackRect:'(5 6 7 8)) === (5 6 7 8)
452 (Structs passPoint:'(1 2)) === (1 2)
453 (Structs passSize:'(0 0)) === (0 0)
454 (Structs passRange:'(10 20)) === (10 20)
455 (Structs passRect:'(1 2 3 4)) === (1 2 3 4)
456 (Structs flipRect:'(1 2 3 4)) === (4 3 2 1)
457 END
458 end
459
460 def test_list_map
461 @patterns = <<-END
462 ('(1 2 3 4 5) map: (do (i) (* i i))) === (1 4 9 16 25)
463 END
464 end
465
466 def test_kind_of
467 @patterns = <<-END
468 (NSMutableArray isKindOfClass:NSArray) === 1
469 (NSMutableArray isKindOfClass:NSMutableArray) === 1
470 (NSMutableArray isKindOfClass:NSString) === 0
471 END
472 end
473 end
Something went wrong with that request. Please try again.