UNPKG

4.38 kBPlain TextView Raw
1;; A template def for template test
2(template testTemplate (one two three)
3 "1" one "2" two "3" three)
4
5;; Def testgroup name - lispyscript
6;; tests lispyscript expressions
7
8(testGroup lispyscript
9
10(assert (true? true) "(true? true)")
11(assert (false? false) "(false? false)")
12(assert (false? (true? {})) "(false? (true? {}))")
13(assert (undefined? undefined) "(undefined? undefined)")
14(assert (false? (undefined? null)) "(false? (undefined? null))")
15(assert (null? null) "(null? null)")
16(assert (false? (null? undefined)) "(false? (null? undefined))")
17(assert (zero? 0) "(zero? 0)")
18(assert (false? (zero? '')) "(false? (zero? ''))")
19(assert (boolean? true) "(boolean? true)")
20(assert (false? (boolean? 0)) "(false? (boolean? 0))")
21(assert (number? 1) "(number? 1)")
22(assert (false? (number? '')) "(false? (number? ''))")
23(assert (string? '') "(string? '')")
24(assert (array? []) "(array? []])")
25(assert (false? (array? {})) "(false? (array? {}))")
26(assert (object? {}) "(object? {})")
27(assert (object? []) "(object? [])")
28(assert (false? (object? null)) "(false? (object? null))")
29(assert
30 (= 10
31 (when true
32 (var ret 10)
33 ret)) "when test")
34(assert
35 (= 10
36 (unless false
37 (var ret 10)
38 ret)) "unless test")
39(assert
40 (= -10
41 (do
42 (var i -1)
43 (cond
44 (< i 0) -10
45 (zero? i) 0
46 (> i 0) 10))) "condition test less than")
47(assert
48 (= 10
49 (do
50 (var i 1)
51 (cond
52 (< i 0) -10
53 (zero? i) 0
54 (> i 0) 10))) "condition test greater than")
55(assert
56 (= 0
57 (do
58 (var i 0)
59 (cond
60 (< i 0) -10
61 (zero? i) 0
62 (> i 0) 10))) "condition test equal to")
63(assert
64 (= 10
65 (do
66 (var i Infinity)
67 (cond
68 (< i 0) -10
69 (zero? i) 0
70 true 10))) "condition test default")
71(assert
72 (= 10
73 (loop (i) (1)
74 (if (= i 10)
75 i
76 (recur ++i)))) "loop recur test")
77(assert
78 (= 10
79 (do
80 (var ret 0)
81 (each (array 1 2 3 4)
82 (function (val)
83 (set ret (+ ret val))))
84 ret)) "each test")
85(assert
86 (= 10
87 (do
88 (var ret 0)
89 (eachKey (object "a" 1 "b" 2 "c" 3 "d" 4)
90 (function (val)
91 (set ret (+ ret val))))
92 ret)) "eachKey test")
93(assert
94 (= 10
95 (reduce (array 1 2 3 4)
96 (function (accum val)
97 (+ accum val)) 0)) "reduce test with init")
98(assert
99 (= 10
100 (reduce (array 1 2 3 4)
101 (function (accum val)
102 (+ accum val)))) "reduce test without init")
103(assert
104 (= 20
105 (reduce (map (array 1 2 3 4) (function (val) (* val 2)))
106 (function (accum val)
107 (+ accum val)) 0)) "map test")
108(assert (= "112233" (testTemplate 1 2 3)) "template test")
109(assert (= "112233" (template-repeat-key {"1":1,"2":2,"3":3} key value)) "template repeat key test")
110(assert
111 (= 10
112 (try (var i 10) i (function (err)))) "try catch test - try block")
113(assert
114 (= 10
115 (try (throw 10) (function (err) err))) "try catch test - catch block")
116(assert
117 (= 3
118 (doMonad identityMonad (a 1 b (* a 2)) (+ a b))) "Identity Monad Test")
119(assert
120 (= 3
121 (doMonad maybeMonad (a 1 b (* a 2)) (+ a b))) "maybe Monad Test")
122(assert
123 (= null
124 (doMonad maybeMonad (a null b (* a 2)) (+ a b))) "maybe Monad null Test")
125(assert
126 (= 54
127 (reduce
128 (doMonad arrayMonad (a [1,2,3] b [3,4,5]) (+ a b))
129 (function (accum val) (+ accum val))
130 0)) "arrayMonad test")
131(assert
132 (= 32
133 (reduce
134 (doMonad arrayMonad (a [1,2,3] b [3,4,5]) (when (<= (+ a b) 6) (+ a b)))
135 (function (accum val) (+ accum val))
136 0)) "arrayMonad when test")
137(assert
138 (= 6
139 (reduce
140 (doMonad arrayMonad (a [1,2,0,null,3]) (when a a))
141 (function (accum val) (+ accum val))
142 0)) "arrayMonad when null values test")
143
144)
145
146;; Function for running on browser. This function is for
147;; the test.html file in the same folder.
148(var browserTest
149 (function ()
150 (var el (document.getElementById "testresult"))
151 (if el.outerHTML
152 (set el.outerHTML (str "<pre>" (testRunner lispyscript "LispyScript Testing") "</pre>"))
153 (set el.innerHTML (testRunner lispyscript "LispyScript Testing")))))
154
155;; If not running on browser
156;; call test runner with test group lispysript
157;; otherwise call browserTest
158(if (undefined? window)
159 (console.log (testRunner lispyscript "LispyScript Testing"))
160 (set window.onload browserTest))
161
162