public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
nu / test / test_dictionary.nu
100644 59 lines (51 sloc) 2.059 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
;; test_dictionary.nu
;; tests for Nu dictionary extensions.
;;
;; Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
 
(class TestDictionary is NuTestCase
     
     (imethod (id) testSet is
          (set d (NSMutableDictionary dictionary))
          (d set:(one:1
                  "two" 2
                  three:"three"))
          (assert_equal 3 (d count))
          (assert_equal 2 (d valueForKey:"two"))
          (assert_equal "three" (d valueForKey:"three")))
     
     (imethod (id) testCreate is
          (set d (NSMutableDictionary dictionaryWithList:(one:1
                                                          "two" 2
                                                          three:"three")))
          (assert_equal 3 (d count))
          (assert_equal 2 (d valueForKey:"two"))
          (assert_equal "three" (d valueForKey:"three")))
     
     (imethod (id) testAutomaticAccessor is
          (set d (dict "one" 1 two:2))
          (assert_equal 1 (d "one"))
          (assert_equal 2 (d "two")))
     
     (imethod (id) testEach is
          (set d (dict one:1 two:2 three:3 four:4 five:5 six:6))
          ;; test each: through everything
          (set count 0)
          (d each:
             (do (k v)
                 (assert_equal (d objectForKey:k) v)
                 (set count (+ count 1))))
          (assert_equal (d count) count)
          ;; test each: with break
          (set count 0)
          (d each:
             (do (k v)
                 (if (eq count 3) (break))
                 (set count (+ count 1))))
          (assert_equal 3 count)
          ;; test each: with continue
          (set count 0)
          (d each:
             (do (k v)
                 (if (eq v 3) (continue))
                 (set count (+ count 1))))
          (assert_equal (- (d count) 1) count))
     
     (imethod (id) testLookupWithDefault is
          (set d (dict "one" 1 two:2))
          (assert_equal 1 (d objectForKey:"one" withDefault:3))
          (assert_equal 3 (d objectForKey:"three" withDefault:3))))