PageRenderTime 48ms CodeModel.GetById 17ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 1ms

/test/test_scriptjure.clj

http://github.com/arohner/scriptjure
Clojure | 227 lines | 184 code | 43 blank | 0 comment | 5 complexity | e3336e1a8360a0430b24c57283dc36d8 MD5 | raw file
  1(ns test-scriptjure
  2  (:use clojure.test)
  3  (:require [clojure.string :as str])
  4  (:use com.reasonr.scriptjure))
  5
  6(defn strip-whitespace 
  7  "strip extraneous whitespace so tests don't fail because of differences in whitespace"
  8  [str]
  9  (str/trim (str/replace (str/replace str #"\n" " ") #"[ ]+" " ")))
 10
 11(deftest number-literal
 12  (is (= (js 42) "42"))
 13  (is (= (js 1/2) "0.5")))
 14
 15(deftest regex-literal
 16  (is (= "/^abc/" (js #"^abc"))))
 17
 18(deftest test-var-expr
 19  (is (= (strip-whitespace (js (var x)))) "var x;")
 20  (is (= (strip-whitespace (js (var x 42))) "var x; x = 42;"))
 21  (is (= (strip-whitespace (js (var x 1 y 2))) (strip-whitespace "var x, y; x = 1; y = 2;"))))
 22
 23(deftest test-invalid-variables-throw
 24  (is (= (js valid_symbol)) "valid_symbol")
 25  (is (thrown? Exception (js (var invalid-symbol 42)))))
 26
 27(deftest test-valid-keyword
 28  (is (= (js :foo)) "foo")
 29  (is (thrown? Exception (js :invalid-symbol))))
 30
 31(deftest test-simple-funcall
 32  (is (= (js (a b)) "a(b)")))
 33
 34(deftest test-funcall-multi-arg
 35  (is (= (js (a b c)) "a(b, c)")))
 36
 37(deftest test-arithmetic 
 38  (is (= (js (* x y)) "(x * y)"))
 39  (is (= (js (+ x y)) "(x + y)"))
 40  (is (= (js (* x y z a b c)) "(x * y * z * a * b * c)"))
 41  (is (= (js (+ x y z a b c)) "(x + y + z + a + b + c)")))
 42
 43(deftest test-prefix-unary
 44  (is (= (js (! x) "!x")))
 45  (is (= (js (! (+ x 1))) "!(x + 1)")))
 46
 47(deftest test-suffix-unary
 48  (is (= (js (++ x) "x++")))
 49  (is (= (js (++ (+ x 1)) "(x + 1)++")))
 50  (is (= (js (-- x) "x--"))))
 51
 52(deftest test-return
 53  (is (= (strip-whitespace (js (return 42))) "return 42;")))
 54
 55(deftest test-clj
 56  (let [foo 42]
 57    (is (= (js (clj foo)) "42"))))
 58
 59(deftest test-str
 60  (is (= (strip-whitespace (js (str "s" 1)))
 61	 "\"s\" + 1")))
 62
 63(deftest test-dot-fn-call
 64  (is (= (js (. foo bar :a :b)) "foo.bar(a, b)"))
 65  (is (= (js (. google.chart bar :a :b)) "google.chart.bar(a, b)")))
 66
 67(deftest test-dot-method-call
 68  (is (= (js (.bar google.chart :a :b)) "google.chart.bar(a, b)")))
 69
 70(deftest test-dotdot
 71  (is (= (js (.. google chart (bar :a :b))) "google.chart.bar(a, b)")))
 72
 73(deftest test-if
 74  (is (= (strip-whitespace (js (if (&& (= foo bar) (!= foo baz)) (.draw google.chart))))
 75         "if (((foo === bar) && (foo !== baz))) { google.chart.draw() }"))
 76  (is (= (strip-whitespace (js (if foo (do (var x 3) (foo x)) (do (var y 4) (bar y)))))
 77         "var x, y; if (foo) { x = 3; foo(x); } else { y = 4; bar(y); }")))
 78          
 79(deftest test-new-operator
 80  (is (= (js (new google.visualization.ColumnChart (.getElementById document "chart_div"))) "new google.visualization.ColumnChart(document.getElementById(\"chart_div\"))")))
 81
 82(deftest test-fn
 83  (is (= (strip-whitespace (js (fn foo [x] (foo a) (bar b)))) "var foo; foo = function (x) { foo(a); bar(b); }")))
 84
 85(deftest test-array
 86  (is (= (js [1 "2" :foo]) "[1, \"2\", foo]")))
 87
 88(deftest test-aget
 89  (is (= (js (aget foo 2)) "foo[2]"))
 90  (is (= (js (aget foo bar baz) "foo[bar][baz]")))
 91  (is (= (js (aget foo "bar" "baz") "foo[\"bar\"][\"baz\"]"))))
 92
 93(deftest test-map
 94  (is (= (strip-whitespace (js {:packages ["columnchart"]})) "{packages: [\"columnchart\"]}")))
 95
 96(deftest jquery
 97  (is (= (strip-whitespace (js (.ready ($j document) 
 98                                       (fn [] 
 99                                         (.bind ($j "div-id") "click" 
100                                                (fn [e] 
101                                                  (.cookie $j "should-display-make-public" true))))))) "$j(document).ready(function () { $j(\"div-id\").bind(\"click\", function (e) { $j.cookie(\"should-display-make-public\", true); }); })" )))
102
103(deftest test-do
104  (is (= (strip-whitespace 
105          (js 
106           (var x 3)
107           (var y 4)
108           (+ x y))) "var x, y; x = 3; y = 4; (x + y);")))
109
110(deftest test-doseq
111  (is (= (strip-whitespace (js (doseq [i [1 2 3]] (foo i))))
112         "for (i in [1, 2, 3]) { foo(i); }"))
113  (is (= (strip-whitespace (js (doseq [i [1 2 3] j [4 5]] (foo i j))))
114         "for (i in [1, 2, 3]) { for (j in [4, 5]) { foo(i, j); } }")))
115
116(deftest test-quote
117  (is (= (strip-whitespace (js (do (+ 1 1) (quote "alert()"))))
118         "(1 + 1); alert();")))
119
120(deftest test-combine-forms
121  (let [stuff (js* (do
122                       (var x 3)
123                       (var y 4)))]
124    (is (= (strip-whitespace (js (fn foo [x] (clj stuff))))
125           "var foo; foo = function (x) { var x, y; x = 3; y = 4; }"))))
126
127(deftest test-js*-adds-implicit-do
128  (let [one (js* (var x 3)
129                 (var y 4))
130        two (js* (do
131                   (var x 3)
132                   (var y 4)))]
133    (is (= (js (clj one)) (js (clj two))))))
134
135(deftest test-lazy-seq-expands-to-array-inside-clj
136  (let [vals (range 20)]
137    (is (= (js (clj vals)) "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"))))
138
139(deftest test-cljs
140  (let [foo 42]
141    (is (= (cljs foo) (js (clj foo))))))
142
143(deftest test-cljs*
144  (let [foo (fn [] (+ 1 2))]
145    (is (= (cljs* foo) (js* (clj foo))))))
146
147(deftest test-literal-fn-call
148  (is (= (strip-whitespace (js ((fn [x] (return x)) 1)))
149         "(function (x) { return x; })(1)"))
150  (is (= (strip-whitespace (js ((fn foo [x] (return x)) 1)))
151         "var foo; (foo = function (x) { return x; })(1)")))
152
153(deftest test-ternary-if
154  (is (= (strip-whitespace (js (? (= 1 2) 3 4)))
155         "(1 === 2) ? 3 : 4")))
156
157(deftest test-dec
158  (is (= (strip-whitespace(js (dec x)))
159         "(x - 1)")))
160
161(deftest test-inc
162  (is (= (strip-whitespace(js (inc x)))
163         "(x + 1)")))
164
165(deftest test-set!
166  (is (= (strip-whitespace (js (set! x 1)))
167         "x = 1;"))
168  (is (= (strip-whitespace(js (set! x 1 y 2)))
169         "x = 1; y = 2;")))
170
171(defjsmacro prn-hw [n]
172 (alert (str "hello world " (clj n))))
173
174(deftest custom-form-add
175         (is (get-custom 'prn-hw)))
176
177(deftest custom-form-use
178         (is (= (js (prn-hw "custom"))) "alert(\"hello world custom\")"))
179
180(deftest test-try
181  (testing "Normal cases for try / catch / finally"
182    (is (= (strip-whitespace (js (try (set! x 5)
183                                      (catch e (print (+ "BOOM: " e)))
184                                      (finally (print "saved!")))))
185           "try{ x = 5; } catch(e){ print((\"BOOM: \" + e)); } finally{ print(\"saved!\"); }")
186        "Try with catch and finally is OK")
187    (is (= (strip-whitespace (js (try (set! x 5)
188                                      (catch e (print (+ "BOOM: " e))))))
189           "try{ x = 5; } catch(e){ print((\"BOOM: \" + e)); }")
190        "Try with just a catch clause is OK")
191    (is (= (strip-whitespace (js (try (set! x 5)
192                                      (finally (print "saved!")))))
193           "try{ x = 5; } finally{ print(\"saved!\"); }")
194        "Try with just a finally clause is OK")
195    (is (= (strip-whitespace (js (try (set! x 5)
196                                      (print "doin' stuff")
197                                      (print "doin' more stuff")
198                                      (catch e
199                                             (print (+ "BOOM: " e))
200                                             (print "ouch"))
201                                      (finally (print "saved!")
202                                               (print "yippee!")))))
203           "try{ x = 5; print(\"doin' stuff\"); print(\"doin' more stuff\"); } catch(e){ print((\"BOOM: \" + e)); print(\"ouch\"); } finally{ print(\"saved!\"); print(\"yippee!\"); }")
204        "Try, catch, and finally all use implicit 'do' for multiple statements"))
205  (testing "Exceptional cases for try / catch / finally"
206    (is (thrown-with-msg? Exception
207          #"Must supply a catch or finally clause \(or both\) in a try statement! \(try \(set! x 5\)\)"
208          (js (try (set! x 5))))
209        "Try with no catch and no finally should throw an exception")
210    (is (thrown-with-msg? Exception
211          #"Multiple catch clauses in a try statement are not currently supported! \(try \(set! x 5\) \(catch e \(print \"foo\"\)\) \(catch ee \(print \"bar\"\)\)\)"
212          (js (try (set! x 5)
213                   (catch e (print "foo"))
214                   (catch ee (print "bar")))))
215        "Multiple catch clauses are not supported")
216    (is (thrown-with-msg? Exception
217          #"Cannot supply more than one finally clause in a try statement! \(try \(set! x 5\) \(finally \(print \"foo\"\)\) \(finally \(print \"bar\"\)\)\)"
218          (js (try (set! x 5)
219                   (finally (print "foo"))
220                   (finally (print "bar")))))
221        "Cannot supply more than one finally clause")))
222
223(deftest test-break
224  (is (= (strip-whitespace (js (break)))
225         "break;")))
226
227(run-tests)