/test/test_scriptjure.clj

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