public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
nu / test / test_operators.nu
100644 179 lines (166 sloc) 7.899 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
;; test_operators.nu
;; tests for Nu operators.
;;
;; Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
 
(class TestOperators is NuTestCase
     
     (imethod (id) testMinOperator is
          (assert_equal -5 (min (* 5 -1) 10 20 30 40 50000))
          (assert_equal 40.0 (min 40.0 400.0 19992.5))
          (assert_equal "a" (min "a" "b" "c" "d" "e"))
          (assert_equal 'x' (min 'x' 'y' 'z')))
     
     (imethod (id) testMaxOperator is
          (assert_equal 50000 (max -5 10 20 30 40 (* 500 100)))
          (assert_equal 19992.5 (max 40.0 400.0 19992.5))
          (assert_equal "e" (max "a" "b" "c" "d" "e"))
          (assert_equal 'z' (max 'x' 'y' 'z')))
     
     (imethod (id) testAddOperator is
          (assert_equal 3 (+ 1 1 -5 6))
          (assert_equal "hello, world" (+ "hello" "," " " "world")))
     
     (imethod (id) testSubtractOperator is
          (assert_equal -3 (- 3))
          (assert_equal 1 (- 4 2 1)))
     
     ;; turn this off by default; it's a good test, but it requires manual intervention
     (imethod (id) dontTestTheGetsOperator is
          (assert_not_equal nil gets)
          (puts "\nPlease don't enter anything.")
          (assert_equal (gets) ""))
     
     (imethod (id) testIfOperator is
          (set x 0)
          (assert_equal 'yes (if t 'yes))
          (assert_equal 'yes (if t (set x 1) 'yes))
          (assert_equal x 1)
          (assert_equal 'yes (if t 'yes (else (set x 'error))))
          (assert_equal x 1)
          (assert_equal 'yes (if t (then 'yes)))
          (assert_equal 'yes (if t (then (set x 2) 'yes)))
          (assert_equal x 2)
          (assert_equal 'yes (if t (then 'yes) (else (set x 'error))))
          (assert_equal x 2)
          (assert_equal 'no (if nil (else 'no)))
          (assert_equal 'no (if nil (else (set x 3) 'no)))
          (assert_equal x 3)
          (assert_equal 'no (if nil (set x 'error) (else 'no)))
          (assert_equal x 3)
          (assert_equal 'no (if nil (then (set x 'error)) (else 'no)))
          (assert_equal x 3))
     
     (imethod (id) testIfSugaredOperator is
          (set x 0)
          (assert_equal 'yes (if t then 'yes))
          (assert_equal 'yes (if t then (set x 1) 'yes))
          (assert_equal x 1)
          (assert_equal 'yes (if t then 'yes else (set x 'error)))
          (assert_equal x 1)
          (assert_equal 'yes (if t then 'yes (else (set x 'error))))
          (assert_equal x 1)
          (assert_equal 'no (if nil else 'no))
          (assert_equal 'no (if nil else (set x 2) 'no))
          (assert_equal x 2)
          (assert_equal 'no (if nil (set x 'error) else 'no))
          (assert_equal x 2)
          (assert_equal 'no (if nil then (set x 'error) else 'no))
          (assert_equal x 2)
          (assert_equal 'no (if nil (then (set x 'error)) else 'no))
          (assert_equal x 2))
     
     (imethod (id) testUnlessOperator is
          (set x 0)
          (assert_equal 'yes (unless nil 'yes))
          (assert_equal 'yes (unless nil (set x 1) 'yes))
          (assert_equal x 1)
          (assert_equal 'yes (unless nil 'yes (else (set x 'error))))
          (assert_equal x 1)
          (assert_equal 'yes (unless nil (then 'yes)))
          (assert_equal 'yes (unless nil (then (set x 2) 'yes)))
          (assert_equal x 2)
          (assert_equal 'yes (unless nil (then 'yes) (else (set x 'error))))
          (assert_equal x 2)
          (assert_equal 'no (unless t (else 'no)))
          (assert_equal 'no (unless t (else (set x 3) 'no)))
          (assert_equal x 3)
          (assert_equal 'no (unless t (set x 'error) (else 'no)))
          (assert_equal x 3)
          (assert_equal 'no (unless t (then (set x 'error)) (else 'no)))
          (assert_equal x 3))
     
     (imethod (id) testUnlessSugaredOperator is
          (set x 0)
          (assert_equal 'yes (unless nil then 'yes))
          (assert_equal 'yes (unless nil then (set x 1) 'yes))
          (assert_equal x 1)
          (assert_equal 'yes (unless nil then 'yes else (set x 'error)))
          (assert_equal x 1)
          (assert_equal 'yes (unless nil then 'yes (else (set x 'error))))
          (assert_equal x 1)
          (assert_equal 'no (unless t else 'no))
          (assert_equal 'no (unless t else (set x 2) 'no))
          (assert_equal x 2)
          (assert_equal 'no (unless t (set x 'error) else 'no))
          (assert_equal x 2)
          (assert_equal 'no (unless t then (set x 'error) else 'no))
          (assert_equal x 2)
          (assert_equal 'no (unless t (then (set x 'error)) else 'no))
          (assert_equal x 2))
     
     ;; support for elseif was removed because elseif is easily confused with elif (Python)
     ;; and elsif (Ruby), etc. and because cond does the job anyway.
     (imethod (id) dontTestIfElseifOperator is
          (set x 1)
          (set y 'true)
          (assert_equal 'one (if (eq x 0) 'none
                                 elseif (eq x 1) 'one
                                 else 'many))
          (assert_equal 'one (if (eq x 0) 'none
                                 elseif (eq x 2) 'two
                                 elseif (eq x 1) 'one
                                 else 'more))
          (assert_equal 'one (if (eq x 0) (set y 'none)
                                 elseif (eq x 1) 'one
                                 else (set y 'many)))
          (assert_equal y 'true)
          (assert_equal 'one (if (eq x 0) (set y 'none)
                                 elseif (eq x 2) (set y 'two)
                                 elseif (eq x 1) 'one
                                 else (set y 'more)))
          (assert_equal y 'true)
          (assert_equal 'one (if (eq x 0) 'none
                                 elseif (eq x 1)
                                 (set y 'one)
                                 'one
                                 else 'many))
          (assert_equal y 'one)
          (assert_equal 'one (if (eq x 0) 'none
                                 elseif (eq x 2) 'two
                                 elseif (eq x 1)
                                 (set y 'two)
                                 'one
                                 else 'more))
          (assert_equal y 'two))
     
     (imethod (id) dontTestUnlessElseifOperator is
          (set x 1)
          (set y 'true)
          (assert_equal 'one (unless ((not (eq x 0))) 'none
                                     elseif (eq x 1) 'one
                                     else 'many))
          (assert_equal 'one (unless ((not (eq x 0))) 'none
                                     elseif (eq x 2) 'two
                                     elseif (eq x 1) 'one
                                     else 'more))
          (assert_equal 'one (unless ((not (eq x 0))) (set y 'none)
                                     elseif (eq x 1) 'one
                                     else (set y 'many)))
          (assert_equal y 'true)
          (assert_equal 'one (unless ((not (eq x 0))) (set y 'none)
                                     elseif (eq x 2) (set y 'two)
                                     elseif (eq x 1) 'one
                                     else (set y 'more)))
          (assert_equal y 'true)
          (assert_equal 'one (unless ((not (eq x 0))) 'none
                                     elseif (eq x 1)
                                     (set y 'one)
                                     'one
                                     else 'many))
          (assert_equal y 'one)
          (assert_equal 'one (unless ((not (eq x 0))) 'none
                                     elseif (eq x 2) 'two
                                     elseif (eq x 1)
                                     (set y 'two)
                                     'one
                                     else 'more))
          (assert_equal y 'two)))