Skip to content
Newer
Older
100644 38 lines (34 sloc) 1.52 KB
24a971f Closures on bindings.
Tim Burks authored
1 ;; test_closures.nu
2 ;; tests for Nu closures.
3 ;;
29db4cd @timburks Company name changed.
authored
4 ;; Copyright (c) 2007 Tim Burks, Radtastical Inc.
24a971f Closures on bindings.
Tim Burks authored
5
6 ;; These tests will fail with old-style (0.1.x) Nu closures,
7 ;; which can be enabled by defining CLOSE_ON_VALUES at compilation time.
8 ;; In Nu-0.2.0 and later, closures are made on name bindings.
9
10 (class TestClosures is NuTestCase
11
045f219 Replace "cmethod" and "imethod" with "+" and "-".
Tim Burks authored
12 (- (id) testAccumulator is
13 ;; The accumulator function from Paul Graham's
14 ;; "Revenge of the Nerds", http://www.paulgraham.com/icad.html
15 (function make-accumulator (n)
16 (do (i) (set n (+ n i))))
17 (set accumulator (make-accumulator 0))
18 (assert_equal 1 (accumulator 1))
19 (assert_equal 3 (accumulator 2))
20 (assert_equal 6 (accumulator 3))
21 (set accumulator (make-accumulator 5))
22 (assert_equal 6 (accumulator 1))
23 (assert_equal 8 (accumulator 2))
24 (assert_equal 11 (accumulator 3)))
24a971f Closures on bindings.
Tim Burks authored
25
045f219 Replace "cmethod" and "imethod" with "+" and "-".
Tim Burks authored
26 (- (id) testScoping is
27 (set x 0)
28 ;; Here we redefine x inside the let context, so
29 ;; assignments to x in the block do not affect the outer x
30 (10 times: (do (i) (let ((x x)) (set x (+ x 1)))))
31 (assert_equal 0 x)
32 ;; Here we refer to the outer binding of x, so
33 ;; assignments to x in the block do affect the outer x
34 (10 times: (do (i) (set x (+ x 1)) (set y x)))
35 (assert_equal 10 x)
36 (assert_equal 10 ((context) objectForKey:'x))
37 ;; but assignments to y are invisible in the outer context
38 (assert_equal nil ((context) objectForKey:'y))))
Something went wrong with that request. Please try again.