Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 474 lines (431 sloc) 13.283 kb
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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
# test_nu.rb
# Nu sanity tests. Requires Ruby, RubyObjC, and Test::Unit.
#
# Copyright (c) 2007 Tim Burks, Radtastical Inc.
# For more information about this file, visit http://programming.nu.

require 'test/unit'

unless defined? ObjC
  require 'rubygems'
  require 'objc'
end

ObjC::NSBundle.bundleWithPath_(File.dirname(__FILE__)+'/../Nu.framework').load

class ObjC::NuParser
  def eval(expr)
    value = parseEval_(expr)
    value ? value.to_s : nil
  end
end

class TestNu < Test::Unit::TestCase
  def setup
    @parser = ObjC::NuParser.alloc.init
  end

  def teardown
    @patterns.split("\n").each do |line|
      expression, value = line.split('===')
      expression = expression.strip
      value = value.chomp.strip if value
      value = nil if value == "nil"
      result = @parser.eval(expression)
      assert_equal(value, result, "error in the evaluation of #{expression}") if value
    end
  end

  def test_eval
    @patterns = <<-END
(eval '(+ 2 2)) === 4
END
  end
  
  def test_cadr
    @patterns = <<-END
(set a '(1 2 3)) === (1 2 3)
(set b '((1 2) 3)) === ((1 2) 3)
(car a) === 1
(car b) === (1 2)
(car (car b)) === 1
(cdr a) === (2 3)
(cdr b) === (3)
(car (cdr b)) === 3
END
  end

  def test_atom
    @patterns = <<-END
(atom '(1 2 3)) === ()
(atom 1) === t
(atom 'a) === t
END
  end
  
  def test_cond
    @patterns = <<-END
(cond (1 '1) (else '2)) === 1
(cond (0 '1) (else '2)) === 2
END
  end
  
  def test_cons
    @patterns = <<-END
(cons 1 '(2 3)) === (1 2 3)
(cons '(1 2) '(3)) === ((1 2) 3)
(cons 1 2) === (1 . 2)
(cons 1) === (1)
(cons nil 1) === (() . 1)
((cons 1 (cons 2 3))) === (1 2 . 3)
END
  end
  
  def test_positions
    @patterns = <<-END
(set a '(1 2 3 4 5 6))
(a first) === 1
(a second) === 2
(a third) === 3
(a fourth) === 4
(a fifth) === 5
END
  end
  
  def test_set
    @patterns = <<-END
(set a 1) === 1
(set b 2) === 2
a === 1
b === 2
(set c (set d 3)) === 3
c === 3
END
  end
  
  def test_do
    @patterns = <<-END
(set a 0) === 0
(set b 0) === 0
(set c 3) === 3
(def f (a b) (+ a b c)) === (do (a b) ((+ a b c)))
(set c 4) === 4
(f 1 2) === 6
(f 3 4) === 10
(def g (a b) (set c (+ a b))(* c c))
(g 2 3) === 25
c === 4
END
  end
  
  def test_factorial
    @patterns = <<-END
(def factorial (x)
(cond ((eq x 0) 1)
(else (* x (factorial (- x 1))))))
(factorial 0) === 1
(factorial 1) === 1
(factorial 2) === 2
(factorial 10) === 3628800
END
  end
  
  def test_fibonacci
    @patterns = <<-END
(def fib (x)
(cond ((eq x 0) 1)
((eq x 1) 1)
(else (+ (fib (- x 1)) (fib (- x 2))))))
(fib 0) === 1
(fib 1) === 1
(fib 2) === 2
(fib 3) === 3
(fib 4) === 5
(fib 5) === 8
(fib 6) === 13
(fib 7) === 21
(fib 8) === 34
(fib 9) === 55
END
  end
  
  def test_times
    @patterns = <<-END
(set a ((NSMutableArray alloc) init))
(3 times: (do (i)
(3 times: (do (j)
(a addObject: (+ (* i 3) j))
))
))
(a count) === 9
(a objectAtIndex: 4) === 4
(a objectAtIndex: 8) === 8
END
  end
  
  def test_messages
    @patterns = <<-END
((NuClass all) count) === #{ObjC::Class.to_a.length}
END
  end
  
  def test_bridge
    @patterns = <<-END
("scheme" commonPrefixWithString:"school" options:0) === sch
("scheme" capitalizedString) === Scheme
((NSArray arrayWithObject:2) objectAtIndex:0) === 2
END
  end
  
  def test_mutableset
    @patterns = <<-END
(set s ((NSMutableSet alloc) init))
(s addObject:1)
(s addObject:"two")
(s addObject:3.0)
(s count) === 3
(s member:1) === 1
(s member:"two") === two
(s member:3.0) === 3
(s member:4) === ()
END
  end
  
  def test_select
    @patterns = <<-END
(NSArray include: NuEnumerable)
(set m (NSString classMethods))
(((m select:(do (x) (eq (x name) "string"))) lastObject) name) === string
(set name-is (do (x) (do (y) (eq (y name) x))))
(((m select:(name-is "string")) lastObject) name) === string
(((m select:(name-is "stringWithCString:")) lastObject) name) === stringWithCString:
((m find:(name-is "stringWithCString:")) name) === stringWithCString:
END
  end

  def test_add_instance_method
    @patterns = <<-END
(NSObject addInstanceMethod:"beep" signature:"@@:" body:(do () ("beep!")))
(set o ((NSObject alloc) init))
(o beep) === beep!
END
  end
  
  def test_simple_subclass
    @patterns = <<-END
(NSObject createSubclassNamed:"MyChild")
(MyChild addInstanceMethod:"show" signature:"@@:" body:(do () (self class)))
(set o ((MyChild alloc) init))
(o show) === MyChild
END
  end
  
  def test_add_factorial_method
    @patterns = <<-END
(NSNumber addInstanceMethod:"factorial" signature:"i@:" body:
(do ()
(set x (self intValue))
(cond ((eq x 0) 1)
(else (* x ((- x 1) factorial))))))
(5 factorial) === #{5*4*3*2}
END
  end
  
  def test_addition_shocker
    @patterns = <<-END
(NSNumber addInstanceMethod:"+" signature:"d@:d" body:(do (x) (+ x (self doubleValue))) )
(1.2 + 3.4) === 4.6
END
  end
  
  def test_add_class_method
    @patterns = <<-END
(NSNumber addClassMethod:"three" signature:"@@:" body:(do () (+ 1 1 1)))
(NSNumber three) === 3
END
  end
  
  def test_add_ivar
    # try this for a few different classes. NSWindow crashes. :-(
    @patterns = <<-END
(NSWindowController createSubclassNamed: "Child")
(Child addInstanceVariable: "one" signature: "i")
(set c ((Child alloc) init))
(c valueForKey: "one") === 0
(c setValue: 33 forKey: "one")
(c valueForKey: "one") === 33
END
    # trying to read a non-existent ivar crashes too:
    # (c valueForKey: "two")
  end
  
  def test_add_ivar2
    @patterns = <<-END
(class Test is NSObject
(ivar (int) x)
(- (void) setX:(int) x is (set @x x))
(- (int) x is @x))
(set y ((Test alloc) init))
(y setX:33)
(y x) === 33
END
  end
  
  def test_auto_ivar
    @patterns = <<-END
(class Foo is NSObject
(ivars)
(- (id) incr is
(cond (@y (set @y (+ @y 1)))
(else (set @y 1)))))
(set f ((Foo alloc) init))
(f setValue:2 forIvar:"x")
(f valueForIvar:"x") === 2
(f incr) === 1
(f incr) === 2
(f incr) === 3
END
  end
  
  def test_let
    @patterns = <<-END
(let (abc 999) (+ 2 2) (+ 1 abc)) === 1000
(let ((a 1) (b 2)) (+ a b)) === 3
END
  end
  
  def test_list
    @patterns = <<-END
(list 1 2 3) === (1 2 3)
(list (list 2 3 4) 2) === ((2 3 4) 2)
END
  end
  
  def test_counter_class
    @patterns = <<-END
(class Counter is NSObject
(ivar (id) c)
(- (id) init is
(super init)
(set @c 0)
self)
(- (id) count is (@c))
(- (void) step is (set @c (+ @c 1))))
(set counter ((Counter alloc) init))
(counter count) === 0
(10 times: (do () (counter step)))
(counter count) === 10
END
  end
  
  def test_another_factorial
    @patterns = <<-END
(class NSNumber
(- (id) "!" is
(set n (self intValue))
(cond ((eq n 0) 1)
(else (* n ((- n 1)!))))))
(3 !) === 6
(4 !) === 24
(5 !) === 120
END
  end

  def test_append
    @patterns = <<-END
(append '(1 2 3) '() '(4 5 6) '(7) '(8 9)) === (1 2 3 4 5 6 7 8 9)
END
  end

  def test_extensions
@patterns = <<-END
(NSArray include: NuEnumerable)
(class NSMutableDictionary
(+ (id) dictionaryWithList: (id) list is
(let (d (self dictionary))
(list eachPair:
(do (key value)
(d setValue:value forKey:key)))
d)))
(class NSDictionary
(- (id) list is
((self allKeys) reduce:
(do (result key)
(cons key (cons (self objectForKey: key) result)))
from: nil)))
(class NSMutableArray
(+ (id) arrayWithList: (id) list is
(let (a (self array))
(list each: (do (item) (a addObject: item)))
a)))
(class NSArray
(- (id) list is
(self reduceLeft:(do (result item) (cons item result)) from: nil))
(- (id) sum is
(self reduce:(do (result item) (+ item result)) from: 0)))
(set d (NSMutableDictionary dictionaryWithList: '("a" 1 "b" 2 "c" 3)))
(d count) === 3
(d objectForKey:"a") === 1
(d objectForKey:"b") === 2
(d objectForKey:"c") === 3
(set e (NSMutableDictionary dictionaryWithList: (d list)))
(e count) === 3
(e objectForKey:"a") === 1
(e objectForKey:"b") === 2
(e objectForKey:"c") === 3
(set a (NSMutableArray arrayWithList: '(1 2 3 4 5 6)))
(a count) === 6
(a list) === (1 2 3 4 5 6)
(a sum) === 21
END
  end
  
  def test_super
    @patterns = <<-END
(class Level1 is NSObject
(- (int) depth is (1)))
(class Level2 is Level1
(- (int) depth is (+ 1 (super depth))))
(class Level3 is Level2
(- (int) depth is (+ 1 (super depth))))
(class Level4 is Level3
(- (int) depth is (+ 1 (super depth))))
(((Level1 alloc) init) depth) === 1
(((Level2 alloc) init) depth) === 2
(((Level3 alloc) init) depth) === 3
(((Level4 alloc) init) depth) === 4
END
  end
  
  def test_method_replacement
    @patterns = <<-END
(class NSObject (- (int) one is 1))
(((NSObject alloc) init) one) === 1
(class NSObject (- (int) one is 2))
(((NSObject alloc) init) one) === 2
END
    # this doesn't work, though:
    #(class NSObject (- (double) one is (1.1)))
    #(((NSObject alloc) init) one) === 1.1
    # it seems that we can't change the return type of a method
  end
  
  def test_case
    @patterns = <<-END
(set x 1)
(case x (2 y) (4 z) (11 2) (9 9 10 11) (x (set y 4) (* y x)) (2 (+ 1 1))) === 4
END
  end
  
  def test_if_unless
    @patterns = <<-END
(if (eq 1 1) 99) === 99
(if (eq 1 2) 99) === ()
(unless (eq 1 1) 99) === ()
(unless (eq 1 2) 99) === 99
END
  end
  
  def test_structs
    @patterns = <<-END
(def reverse (n)
(cond ((eq n nil) nil)
(else (append (reverse (cdr n)) (list (car n))))))
(class Structs is NSObject
(+ (NSPoint) makePointX:(double)x y:(double)y is (list x y))
(+ (NSSize) makeSizeW:(double)w h:(double)h is (list w h))
(+ (NSRect) makeRectX:(double)x y:(double)y w:(double)w h:(double)h is (list x y w h))
(+ (id) unpackPoint:(NSPoint) point is point)
(+ (id) unpackSize:(NSSize) size is size)
(+ (id) unpackRect:(NSRect) rect is rect)
(+ (NSPoint) passPoint:(NSPoint) point is point)
(+ (NSSize) passSize:(NSSize) size is size)
(+ (NSRange) passRange:(NSRange) range is range)
(+ (NSRect) passRect:(NSRect) rect is rect)
(+ (NSRect) flipRect:(NSRect) rect is (reverse rect)))
(Structs makePointX:3 y:5) === (3 5)
(Structs makeSizeW:9.9 h:110) === (9.9 110)
(Structs makeRectX:1 y:2.2 w:3.3 h:4.4) === (1 2.2 3.3 4.4)
(Structs unpackPoint:'(2 3)) === (2 3)
(Structs unpackSize:'(9 10)) === (9 10)
(Structs unpackRect:'(5 6 7 8)) === (5 6 7 8)
(Structs passPoint:'(1 2)) === (1 2)
(Structs passSize:'(0 0)) === (0 0)
(Structs passRange:'(10 20)) === (10 20)
(Structs passRect:'(1 2 3 4)) === (1 2 3 4)
(Structs flipRect:'(1 2 3 4)) === (4 3 2 1)
END
  end
  
  def test_list_map
    @patterns = <<-END
('(1 2 3 4 5) map: (do (i) (* i i))) === (1 4 9 16 25)
END
  end
  
  def test_kind_of
    @patterns = <<-END
(NSMutableArray isKindOfClass:NSArray) === 1
(NSMutableArray isKindOfClass:NSMutableArray) === 1
(NSMutableArray isKindOfClass:NSString) === 0
END
  end
end
Something went wrong with that request. Please try again.