clementine 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +4 -2
  3. data/LICENSE.txt +22 -0
  4. data/README.md +11 -6
  5. data/Rakefile +22 -0
  6. data/clementine.gemspec +2 -1
  7. data/ext/clojure-clojurescript-bef56a7/.gitignore +13 -0
  8. data/ext/clojure-clojurescript-bef56a7/Clojurescript.iml +12 -0
  9. data/ext/clojure-clojurescript-bef56a7/README.md +29 -0
  10. data/ext/clojure-clojurescript-bef56a7/benchmark/cljs/benchmark_runner.cljs +155 -0
  11. data/ext/clojure-clojurescript-bef56a7/bin/cljsc +21 -0
  12. data/ext/clojure-clojurescript-bef56a7/bin/cljsc.bat +18 -0
  13. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/bin/cljsc.clj +0 -0
  14. data/ext/clojure-clojurescript-bef56a7/devnotes/README.org +35 -0
  15. data/ext/clojure-clojurescript-bef56a7/devnotes/bcrepl.org +13 -0
  16. data/ext/clojure-clojurescript-bef56a7/devnotes/cljs.org +500 -0
  17. data/ext/clojure-clojurescript-bef56a7/devnotes/corelib.org +583 -0
  18. data/ext/clojure-clojurescript-bef56a7/devnotes/day1.org +203 -0
  19. data/ext/clojure-clojurescript-bef56a7/devnotes/day2.org +44 -0
  20. data/ext/clojure-clojurescript-bef56a7/devnotes/talk.org +126 -0
  21. data/ext/clojure-clojurescript-bef56a7/devnotes/testing +13 -0
  22. data/ext/clojure-clojurescript-bef56a7/devnotes/todo.org +121 -0
  23. data/ext/clojure-clojurescript-bef56a7/epl-v10.html +261 -0
  24. data/ext/clojure-clojurescript-bef56a7/pom.template.xml +88 -0
  25. data/ext/clojure-clojurescript-bef56a7/samples/dom/.gitignore +2 -0
  26. data/ext/clojure-clojurescript-bef56a7/samples/dom/src/dom/test.cljs +48 -0
  27. data/ext/clojure-clojurescript-bef56a7/samples/dom/test.html +30 -0
  28. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/.gitignore +2 -0
  29. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/README.md +53 -0
  30. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/externed-lib.js +7 -0
  31. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/externs.js +3 -0
  32. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/hello-extern.html +14 -0
  33. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/hello-js-dev.html +18 -0
  34. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/hello-js.html +17 -0
  35. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/my-external-lib.js +3 -0
  36. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/src/hello-js/core.cljs +9 -0
  37. data/ext/clojure-clojurescript-bef56a7/samples/hello-js/src/hello-js/extern-example.cljs +5 -0
  38. data/ext/clojure-clojurescript-bef56a7/samples/hello/.gitignore +2 -0
  39. data/ext/clojure-clojurescript-bef56a7/samples/hello/README.md +34 -0
  40. data/ext/clojure-clojurescript-bef56a7/samples/hello/hello-dev.html +18 -0
  41. data/ext/clojure-clojurescript-bef56a7/samples/hello/hello.html +13 -0
  42. data/ext/clojure-clojurescript-bef56a7/samples/hello/src/hello/core.cljs +8 -0
  43. data/ext/clojure-clojurescript-bef56a7/samples/hello/src/hello/foo/bar.cljs +4 -0
  44. data/ext/clojure-clojurescript-bef56a7/samples/nodehello.cljs +18 -0
  45. data/ext/clojure-clojurescript-bef56a7/samples/nodels.cljs +17 -0
  46. data/ext/clojure-clojurescript-bef56a7/samples/repl/.gitignore +2 -0
  47. data/ext/clojure-clojurescript-bef56a7/samples/repl/README.md +101 -0
  48. data/ext/clojure-clojurescript-bef56a7/samples/repl/index.html +27 -0
  49. data/ext/clojure-clojurescript-bef56a7/samples/repl/src/repl/test.cljs +73 -0
  50. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/.gitignore +2 -0
  51. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/README.md +42 -0
  52. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/index-advanced.html +80 -0
  53. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/index.html +88 -0
  54. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/reset.css +48 -0
  55. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/anneal.cljs +66 -0
  56. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/core.cljs +307 -0
  57. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/dom-helpers.cljs +95 -0
  58. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/layout.cljs +100 -0
  59. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/leaderboard.cljs +40 -0
  60. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/radial.cljs +91 -0
  61. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/showgraph.cljs +121 -0
  62. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/src/twitterbuzz/timeline.cljs +39 -0
  63. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/style.css +301 -0
  64. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/test_data.txt +1 -0
  65. data/ext/clojure-clojurescript-bef56a7/samples/twitterbuzz/tweet_maps.txt +1 -0
  66. data/ext/clojure-clojurescript-bef56a7/script/benchmark +30 -0
  67. data/ext/clojure-clojurescript-bef56a7/script/bootstrap +70 -0
  68. data/ext/clojure-clojurescript-bef56a7/script/browser-repl +16 -0
  69. data/ext/clojure-clojurescript-bef56a7/script/build +59 -0
  70. data/ext/clojure-clojurescript-bef56a7/script/clean +5 -0
  71. data/ext/clojure-clojurescript-bef56a7/script/closure-library-release/google-closure-library-third-party.pom.template +59 -0
  72. data/ext/clojure-clojurescript-bef56a7/script/closure-library-release/google-closure-library.pom.template +54 -0
  73. data/ext/clojure-clojurescript-bef56a7/script/closure-library-release/make-closure-library-jars.sh +87 -0
  74. data/ext/clojure-clojurescript-bef56a7/script/compile +41 -0
  75. data/ext/clojure-clojurescript-bef56a7/script/repl +13 -0
  76. data/ext/clojure-clojurescript-bef56a7/script/repl.bat +13 -0
  77. data/ext/clojure-clojurescript-bef56a7/script/repljs +15 -0
  78. data/ext/clojure-clojurescript-bef56a7/script/repljs.bat +14 -0
  79. data/ext/clojure-clojurescript-bef56a7/script/test +38 -0
  80. data/ext/clojure-clojurescript-bef56a7/script/test-compile +30 -0
  81. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/analyzer.clj +975 -0
  82. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/clj/cljs/closure.clj +173 -73
  83. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/compiler.clj +1081 -0
  84. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/core.clj +1158 -0
  85. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/clj/cljs/repl.clj +51 -25
  86. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/repl/browser.clj +258 -0
  87. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/repl/reflect.clj +75 -0
  88. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/clj/cljs/repl/rhino.clj +6 -5
  89. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/repl/server.clj +173 -0
  90. data/ext/clojure-clojurescript-bef56a7/src/clj/cljs/tagged_literals.clj +30 -0
  91. data/ext/clojure-clojurescript-bef56a7/src/cljs/cljs/core.cljs +7197 -0
  92. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/cljs/nodejs.cljs +1 -1
  93. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/cljs/nodejs_externs.js +0 -0
  94. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/cljs/nodejscli.cljs +1 -1
  95. data/ext/clojure-clojurescript-bef56a7/src/cljs/cljs/reader.cljs +551 -0
  96. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/dom.cljs +59 -13
  97. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/event.cljs +0 -0
  98. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/net.cljs +8 -7
  99. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/browser/repl.cljs +2 -2
  100. data/ext/clojure-clojurescript-bef56a7/src/cljs/clojure/core/reducers.cljs +298 -0
  101. data/ext/clojure-clojurescript-bef56a7/src/cljs/clojure/data.cljs +162 -0
  102. data/ext/clojure-clojurescript-bef56a7/src/cljs/clojure/reflect.cljs +48 -0
  103. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/set.cljs +0 -0
  104. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/string.cljs +4 -10
  105. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/walk.cljs +0 -0
  106. data/{vendor/assets → ext/clojure-clojurescript-bef56a7}/src/cljs/clojure/zip.cljs +0 -0
  107. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/binding_test.cljs +7 -0
  108. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/binding_test_other_ns.cljs +3 -0
  109. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/core_test.cljs +1678 -0
  110. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/import_test.cljs +11 -0
  111. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/import_test/foo.cljs +5 -0
  112. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/letfn_test.cljs +19 -0
  113. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/macro_test.cljs +6 -0
  114. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/macro_test/macros.clj +5 -0
  115. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/ns_test.cljs +14 -0
  116. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/ns_test/bar.cljs +3 -0
  117. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/ns_test/foo.cljs +7 -0
  118. data/ext/clojure-clojurescript-bef56a7/test/cljs/cljs/reader_test.cljs +124 -0
  119. data/ext/clojure-clojurescript-bef56a7/test/cljs/clojure/data_test.cljs +22 -0
  120. data/ext/clojure-clojurescript-bef56a7/test/cljs/clojure/string_test.cljs +97 -0
  121. data/ext/clojure-clojurescript-bef56a7/test/cljs/foo/ns_shadow_test.cljs +9 -0
  122. data/ext/clojure-clojurescript-bef56a7/test/cljs/test_runner.cljs +26 -0
  123. data/lib/clementine.rb +3 -24
  124. data/lib/clementine/clojurescript_engine.rb +9 -48
  125. data/lib/clementine/clojurescript_engine/base.rb +15 -0
  126. data/lib/clementine/clojurescript_engine/jruby.rb +46 -0
  127. data/lib/clementine/{clojurescript_engine_mri.rb → clojurescript_engine/mri.rb} +17 -10
  128. data/lib/clementine/version.rb +1 -1
  129. data/test/clojurescript_engine_test.rb +36 -14
  130. metadata +177 -83
  131. data/vendor/assets/lib/clojure.jar +0 -0
  132. data/vendor/assets/lib/compiler.jar +0 -0
  133. data/vendor/assets/lib/goog.jar +0 -0
  134. data/vendor/assets/lib/js.jar +0 -0
  135. data/vendor/assets/src/clj/cljs/compiler.clj +0 -1341
  136. data/vendor/assets/src/clj/cljs/core.clj +0 -702
  137. data/vendor/assets/src/clj/cljs/repl/browser.clj +0 -341
  138. data/vendor/assets/src/cljs/cljs/core.cljs +0 -3330
  139. data/vendor/assets/src/cljs/cljs/reader.cljs +0 -360
@@ -0,0 +1,162 @@
1
+ ; Copyright (c) Rich Hickey. All rights reserved.
2
+ ; The use and distribution terms for this software are covered by the
3
+ ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
4
+ ; which can be found in the file epl-v10.html at the root of this distribution.
5
+ ; By using this software in any fashion, you are agreeing to be bound by
6
+ ; the terms of this license.
7
+ ; You must not remove this notice, or any other, from this software.
8
+
9
+ (ns
10
+ ^{:author "Stuart Halloway",
11
+ :doc "Non-core data functions."}
12
+ clojure.data
13
+ (:require [clojure.set :as set]))
14
+
15
+ (declare diff)
16
+
17
+ (defn- atom-diff
18
+ "Internal helper for diff."
19
+ [a b]
20
+ (if (= a b) [nil nil a] [a b nil]))
21
+
22
+ ;; for big things a sparse vector class would be better
23
+ (defn- vectorize
24
+ "Convert an associative-by-numeric-index collection into
25
+ an equivalent vector, with nil for any missing keys"
26
+ [m]
27
+ (when (seq m)
28
+ (reduce
29
+ (fn [result [k v]] (assoc result k v))
30
+ (vec (repeat (apply max (keys m)) nil))
31
+ m)))
32
+
33
+ (defn- diff-associative-key
34
+ "Diff associative things a and b, comparing only the key k."
35
+ [a b k]
36
+ (let [va (get a k)
37
+ vb (get b k)
38
+ [a* b* ab] (diff va vb)
39
+ in-a (contains? a k)
40
+ in-b (contains? b k)
41
+ same (and in-a in-b
42
+ (or (not (nil? ab))
43
+ (and (nil? va) (nil? vb))))]
44
+ [(when (and in-a (or (not (nil? a*)) (not same))) {k a*})
45
+ (when (and in-b (or (not (nil? b*)) (not same))) {k b*})
46
+ (when same {k ab})
47
+ ]))
48
+
49
+ (defn- diff-associative
50
+ "Diff associative things a and b, comparing only keys in ks (if supplied)."
51
+ ([a b]
52
+ (diff-associative a b (set/union (keys a) (keys b))))
53
+ ([a b ks]
54
+ (reduce
55
+ (fn [diff1 diff2]
56
+ (doall (map merge diff1 diff2)))
57
+ [nil nil nil]
58
+ (map
59
+ (partial diff-associative-key a b)
60
+ ks))))
61
+
62
+ (defn- diff-sequential
63
+ [a b]
64
+ (vec (map vectorize (diff-associative
65
+ (if (vector? a) a (vec a))
66
+ (if (vector? b) b (vec b))
67
+ (range (max (count a) (count b)))))))
68
+
69
+ (defn- diff-set
70
+ [a b]
71
+ [(not-empty (set/difference a b))
72
+ (not-empty (set/difference b a))
73
+ (not-empty (set/intersection a b))])
74
+
75
+ (defprotocol EqualityPartition
76
+ "Implementation detail. Subject to change."
77
+ (equality-partition [x] "Implementation detail. Subject to change."))
78
+
79
+ (defprotocol Diff
80
+ "Implementation detail. Subject to change."
81
+ (diff-similar [a b] "Implementation detail. Subject to change."))
82
+
83
+ (extend-protocol EqualityPartition
84
+ nil
85
+ (equality-partition [x] :atom)
86
+
87
+ string
88
+ (equality-partition [x] :atom)
89
+
90
+ number
91
+ (equality-partition [x] :atom)
92
+
93
+ array
94
+ (equality-partition [x] :sequential)
95
+
96
+ function
97
+ (equality-partition [x] :atom)
98
+
99
+ boolean
100
+ (equality-partition [x] :atom)
101
+
102
+ default
103
+ (equality-partition [x]
104
+ (cond
105
+ (satisfies? IMap x) :map
106
+ (satisfies? ISet x) :set
107
+ (satisfies? ISequential x) :sequential
108
+ :default :atom)))
109
+
110
+ (extend-protocol Diff
111
+ nil
112
+ (diff-similar [a b]
113
+ (atom-diff a b))
114
+
115
+ string
116
+ (diff-similar [a b]
117
+ (atom-diff a b))
118
+
119
+ number
120
+ (diff-similar [a b]
121
+ (atom-diff a b))
122
+
123
+ array
124
+ (diff-similar [a b]
125
+ (diff-sequential a b))
126
+
127
+ function
128
+ (diff-similar [a b]
129
+ (atom-diff a b))
130
+
131
+ boolean
132
+ (diff-similar [a b]
133
+ (atom-diff a b))
134
+
135
+ default
136
+ (diff-similar [a b]
137
+ ((case (equality-partition a)
138
+ :atom atom-diff
139
+ :set diff-set
140
+ :sequential diff-sequential
141
+ :map diff-associative)
142
+ a b)))
143
+
144
+ (defn diff
145
+ "Recursively compares a and b, returning a tuple of
146
+ [things-only-in-a things-only-in-b things-in-both].
147
+ Comparison rules:
148
+
149
+ * For equal a and b, return [nil nil a].
150
+ * Maps are subdiffed where keys match and values differ.
151
+ * Sets are never subdiffed.
152
+ * All sequential things are treated as associative collections
153
+ by their indexes, with results returned as vectors.
154
+ * Everything else (including strings!) is treated as
155
+ an atom and compared for equality."
156
+ [a b]
157
+ (if (= a b)
158
+ [nil nil a]
159
+ (if (= (equality-partition a) (equality-partition b))
160
+ (diff-similar a b)
161
+ (atom-diff a b))))
162
+
@@ -0,0 +1,48 @@
1
+ (ns clojure.reflect
2
+ (:refer-clojure :exclude [meta])
3
+ (:require [clojure.browser.net :as net]
4
+ [clojure.browser.event :as event]))
5
+
6
+ (defn- evaluate-javascript [block]
7
+ (let [result (try (js* "eval(~{block})")
8
+ (catch js/Error e
9
+ (.log js/console e)))]
10
+ result))
11
+
12
+ (defn- query-reflection
13
+ "Issues a GET to /reflect with a single query-parameter string.
14
+ Calls cb with the result."
15
+ [query-param cb]
16
+ (let [conn (net/xhr-connection)
17
+ url (str "/reflect?" query-param)]
18
+ (event/listen conn :success (fn [e]
19
+ (let [resp (.getResponseText e/currentTarget ())]
20
+ (cb resp))))
21
+ (event/listen conn :error #(println "Reflection query failed."))
22
+ (net/transmit conn url)))
23
+
24
+ (defn meta
25
+ "Queries the reflection api with a fully qualified symbol, then calls
26
+ callback fn cb with the evaluated cljs map containing that symbol's
27
+ meta information."
28
+ [sym cb]
29
+ (query-reflection (str "var=" (js/encodeURIComponent (str sym)))
30
+ #(cb (evaluate-javascript %))))
31
+
32
+ (defn macroexpand
33
+ "Queries the reflection api with a quoted macro form, then calls the
34
+ callback function with the macroexpanded form, as a string."
35
+ [form]
36
+ (query-reflection (str "macroform=" (js/encodeURIComponent (str form))) println))
37
+
38
+ (defn print-doc [{:keys [name method-params doc]}]
39
+ (when-not (empty? name)
40
+ (println name)
41
+ (println method-params)
42
+ (println doc)))
43
+
44
+ (defn doc
45
+ "Queries the reflection api with a fully qualified symbol, then prints
46
+ documentation information at the repl."
47
+ [sym]
48
+ (meta sym print-doc))
@@ -30,7 +30,7 @@
30
30
  (cond (string? match)
31
31
  (.replace s (js/RegExp. (gstring/regExpEscape match) "g") replacement)
32
32
  (.hasOwnProperty match "source")
33
- (.replace s (js/RegExp. (.source match) "g") replacement)
33
+ (.replace s (js/RegExp. (.-source match) "g") replacement)
34
34
  :else (throw (str "Invalid match arg: " match))))
35
35
 
36
36
  (defn replace-first
@@ -122,7 +122,7 @@
122
122
  "Removes all trailing newline \\n or return \\r characters from
123
123
  string. Similar to Perl's chomp."
124
124
  [s]
125
- (loop [index (.length s)]
125
+ (loop [index (.-length s)]
126
126
  (if (zero? index)
127
127
  ""
128
128
  (let [ch (get s (dec index))]
@@ -133,13 +133,7 @@
133
133
  (defn blank?
134
134
  "True is s is nil, empty, or contains only whitespace."
135
135
  [s]
136
- (let [s (str s)]
137
- (if (or
138
- (not s)
139
- (= "" s)
140
- (re-matches #"\s+" s))
141
- true
142
- false)))
136
+ (gstring/isEmptySafe s))
143
137
 
144
138
  (defn escape
145
139
  "Return a new string, using cmap to escape each character ch
@@ -149,7 +143,7 @@
149
143
  If (cmap ch) is non-nil, append (str (cmap ch)) instead."
150
144
  [s cmap]
151
145
  (let [buffer (gstring/StringBuffer.)
152
- length (.length s)]
146
+ length (.-length s)]
153
147
  (loop [index 0]
154
148
  (if (= length index)
155
149
  (. buffer (toString))
@@ -0,0 +1,7 @@
1
+ (ns cljs.binding-test
2
+ (:require [cljs.binding-test-other-ns :as o]))
3
+
4
+ (defn test-binding []
5
+ (binding [o/*foo* 2]
6
+ (assert (= o/*foo* 2)))
7
+ (assert (= o/*foo* 1)))
@@ -0,0 +1,3 @@
1
+ (ns cljs.binding-test-other-ns)
2
+
3
+ (def ^:dynamic *foo* 1)
@@ -0,0 +1,1678 @@
1
+ (ns cljs.core-test)
2
+
3
+ (defn test-stuff []
4
+ ;; js primitives
5
+ (let [keys #(vec (js-keys %))]
6
+ (assert (= [] (keys (js-obj)) (keys (apply js-obj []))))
7
+ (assert (= ["x"] (keys (js-obj "x" "y")) (keys (apply js-obj ["x" "y"])))))
8
+
9
+ ;; -equiv
10
+ (assert (= 1))
11
+ (assert (= 1 1))
12
+ (assert (= 1 1 1))
13
+ (assert (= 1 1 1 1))
14
+ (assert (not (= 1 2)))
15
+ (assert (not (= 1 2 1)))
16
+ (assert (not (= 1 1 2)))
17
+ (assert (not (= 1 1 2 1)))
18
+ (assert (not (= 1 1 1 2)))
19
+
20
+ ;; arithmetic
21
+ (assert (= (+) 0))
22
+ (assert (= (apply + []) 0))
23
+ (assert (= (+ 1) 1))
24
+ (assert (= (apply + [1]) 1))
25
+ (assert (= (+ 1 1) 2))
26
+ (assert (= (apply + [1 1]) 2))
27
+ (assert (= (+ 1 2 3) 6))
28
+ (assert (= (apply + [1 2 3]) 6))
29
+
30
+ (assert (= (- 1) -1))
31
+ (assert (= (apply - [1]) -1))
32
+ (assert (= (- 1 1) 0))
33
+ (assert (= (apply - [1 1]) 0))
34
+ (assert (= (- 3 2 1) 0))
35
+ (assert (= (apply - [3 2 1]) 0))
36
+
37
+ (assert (= (*) 1))
38
+ (assert (= (apply * []) 1))
39
+ (assert (= (* 2) 2))
40
+ (assert (= (apply * [2]) 2))
41
+ (assert (= (* 2 3) 6))
42
+ (assert (= (apply * [2 3]) 6))
43
+
44
+ (assert (= (/ 2) 0.5))
45
+ (assert (= (apply / [2]) 0.5))
46
+ (assert (= (/ 6 2) 3))
47
+ (assert (= (apply / [6 2]) 3))
48
+ (assert (= (/ 6 3 2) 1))
49
+ (assert (= (apply / [6 3 2]) 1))
50
+
51
+ (assert (= (< 1) true))
52
+ (assert (= (apply < [1]) true))
53
+ (assert (= (< 1 2) true))
54
+ (assert (= (apply < [1 2]) true))
55
+ (assert (= (< 1 1) false))
56
+ (assert (= (apply < [1 1]) false))
57
+ (assert (= (< 2 1) false))
58
+ (assert (= (apply < [2 1]) false))
59
+ (assert (= (< 1 2 3) true))
60
+ (assert (= (apply < [1 2 3]) true))
61
+ (assert (= (< 1 1 3) false))
62
+ (assert (= (apply < [1 1 3]) false))
63
+ (assert (= (< 3 1 1) false))
64
+ (assert (= (apply < [3 1 1]) false))
65
+
66
+ (assert (= (<= 1) true))
67
+ (assert (= (apply <= [1]) true))
68
+ (assert (= (<= 1 1) true))
69
+ (assert (= (apply <= [1 1]) true))
70
+ (assert (= (<= 1 2) true))
71
+ (assert (= (apply <= [1 2]) true))
72
+ (assert (= (<= 2 1) false))
73
+ (assert (= (apply <= [2 1]) false))
74
+ (assert (= (<= 1 2 3) true))
75
+ (assert (= (apply <= [1 2 3]) true))
76
+ (assert (= (<= 1 1 3) true))
77
+ (assert (= (apply <= [1 1 3]) true))
78
+ (assert (= (<= 3 1 1) false))
79
+ (assert (= (apply <= [3 1 1]) false))
80
+
81
+ (assert (= (> 1) true))
82
+ (assert (= (apply > [1]) true))
83
+ (assert (= (> 2 1) true))
84
+ (assert (= (apply > [2 1]) true))
85
+ (assert (= (> 1 1) false))
86
+ (assert (= (apply > [1 1]) false))
87
+ (assert (= (> 1 2) false))
88
+ (assert (= (apply > [1 2]) false))
89
+ (assert (= (> 3 2 1) true))
90
+ (assert (= (apply > [3 2 1]) true))
91
+ (assert (= (> 3 1 1) false))
92
+ (assert (= (apply > [3 1 1]) false))
93
+ (assert (= (> 1 1 3) false))
94
+ (assert (= (apply > [1 1 3]) false))
95
+
96
+ (assert (= (>= 1) true))
97
+ (assert (= (apply >= [1]) true))
98
+ (assert (= (>= 2 1) true))
99
+ (assert (= (apply >= [2 1]) true))
100
+ (assert (= (>= 1 1) true))
101
+ (assert (= (apply >= [1 1]) true))
102
+ (assert (= (>= 1 2) false))
103
+ (assert (= (apply >= [1 2]) false))
104
+ (assert (= (>= 3 2 1) true))
105
+ (assert (= (apply >= [3 2 1]) true))
106
+ (assert (= (>= 3 1 1) true))
107
+ (assert (= (apply >= [3 1 1]) true))
108
+ (assert (= (>= 3 1 2) false))
109
+ (assert (= (apply >= [3 1 2]) false))
110
+ (assert (= (>= 1 1 3) false))
111
+ (assert (= (apply >= [1 1 3]) false))
112
+
113
+ (assert (= (dec 1) 0))
114
+ (assert (= (apply dec [1]) 0))
115
+ (assert (= (inc 0) 1))
116
+ (assert (= (apply inc [0]) 1))
117
+
118
+ (assert (= (zero? 0) true))
119
+ (assert (= (apply zero? [0]) true))
120
+ (assert (= (zero? 1) false))
121
+ (assert (= (apply zero? [1]) false))
122
+ (assert (= (zero? -11) false))
123
+ (assert (= (apply zero? [-11]) false))
124
+ (assert (= (pos? 0) false))
125
+ (assert (= (apply pos? [0]) false))
126
+ (assert (= (pos? 1) true))
127
+ (assert (= (apply pos? [1]) true))
128
+ (assert (= (pos? -1) false))
129
+ (assert (= (apply pos? [-1]) false))
130
+ (assert (= (neg? -1) true))
131
+ (assert (= (apply neg? [-1]) true))
132
+
133
+ (assert (= (max 1) 1))
134
+ (assert (= (apply max [1]) 1))
135
+ (assert (= (max 1 2) 2))
136
+ (assert (= (apply max [1 2]) 2))
137
+ (assert (= (max 2 1) 2))
138
+ (assert (= (apply max [2 1]) 2))
139
+ (assert (= (max 1 2 3) 3))
140
+ (assert (= (apply max [1 2 3]) 3))
141
+ (assert (= (max 1 3 2) 3))
142
+ (assert (= (apply max [1 3 2]) 3))
143
+
144
+ (assert (= (min 1) 1))
145
+ (assert (= (apply min [1]) 1))
146
+ (assert (= (min 1 2) 1))
147
+ (assert (= (apply min [1 2]) 1))
148
+ (assert (= (min 2 1) 1))
149
+ (assert (= (apply min [2 1]) 1))
150
+ (assert (= (min 1 2 3) 1))
151
+ (assert (= (apply min [1 2 3]) 1))
152
+ (assert (= (min 2 1 3) 1))
153
+ (assert (= (apply min [3 1 3]) 1))
154
+
155
+ (assert (= (mod 4 2) 0))
156
+ (assert (= (apply mod [4 2]) 0))
157
+ (assert (= (mod 3 2) 1))
158
+ (assert (= (apply mod [3 2]) 1))
159
+
160
+ (assert (= [4 3 2 1 0] (loop [i 0 j ()]
161
+ (if (< i 5)
162
+ (recur (inc i) (conj j (fn [] i)))
163
+ (map #(%) j)))))
164
+
165
+ (assert (= [[1 1] [1 2] [1 3] [2 1] [2 2] [2 3]]
166
+ (map #(%) (for [i [1 2] j [1 2 3]] (fn [] [i j])))))
167
+
168
+ (assert (integer? 0))
169
+ (assert (integer? 42))
170
+ (assert (integer? -42))
171
+ (assert (not (integer? "")))
172
+ (assert (not (integer? 1e308)))
173
+ (assert (not (integer? js/Infinity)))
174
+ (assert (not (integer? (- js/Infinity))))
175
+ (assert (not (integer? js/NaN)))
176
+
177
+ (assert (= 42 (int 42.5)))
178
+ (assert (integer? (int 42.5)))
179
+
180
+ (assert (= 42 (long 42.5)))
181
+ (assert (integer? (long 42.5)))
182
+
183
+ (assert (= -1 (int -1.5)))
184
+ (assert (= -9 (long -9.8)))
185
+
186
+ (assert (= 2 (:b {:a 1 :b 2})))
187
+ (assert (= 2 ('b '{:a 1 b 2})))
188
+ (assert (= 2 ({:a 1 :b 2} :b)))
189
+ (assert (= 2 ({1 1 2 2} 2)))
190
+ (assert (= 2 (:a {:b 1} 2)))
191
+ (assert (= 2 (:a {} 2)))
192
+ (assert (= 2 ({:b 1} :a 2)))
193
+ (assert (= 2 ({} :a 2)))
194
+ (assert (= nil (:a {})))
195
+ (assert (= 2 (#{1 2 3} 2)))
196
+ (assert (zero? (hash (aget (js-obj) "foo"))))
197
+
198
+ (assert (= 1 (apply :a '[{:a 1 a 2}])))
199
+ (assert (= 1 (apply 'a '[{a 1 :b 2}])))
200
+ (assert (= 1 (apply {:a 1} [:a])))
201
+ (assert (= 2 (apply {:a 1} [:b 2])))
202
+
203
+ (assert (= "baz" (name 'foo/bar/baz)))
204
+ (assert (= "foo/bar" (namespace 'foo/bar/baz)))
205
+ (assert (= "baz" (name :foo/bar/baz)))
206
+ ;(assert (= "foo/bar" (namespace :foo/bar/baz)))
207
+
208
+ ; str
209
+ (assert (= ":hello" (str :hello)))
210
+ (assert (= "hello" (str 'hello)))
211
+ (assert (= "hello:world" (str "hello" :world)))
212
+ (assert (= ":helloworld" (str :hello 'world)))
213
+
214
+ ; symbol
215
+ (assert (= 'a (symbol 'a)))
216
+
217
+ ;; format
218
+ (assert (= "01: 2.000000" (format "%02d: %.6f" 1 2)))
219
+
220
+ (assert (= {:a :b} (get {[1 2 3] {:a :b}, 4 5} [1 2 3])))
221
+ (assert (= :a (nth [:a :b :c :d] 0)))
222
+ (assert (= :a (nth [:a :b :c :d] 0.1)) )
223
+ (assert (not (= {:a :b :c nil} {:a :b :d nil})))
224
+ (assert (= (list 3 2 1) [3 2 1]))
225
+ (assert (= [3 2 1] (seq (array 3 2 1))))
226
+ (assert (= 9 (reduce + (next (seq (array 1 2 3 4))))))
227
+ (assert (= () (rest nil)))
228
+ (assert (= () (rest ())))
229
+ (assert (= () (rest [1])))
230
+ (assert (= () (rest (array 1))))
231
+ (assert (= {"x" "y"} (meta ^{"x" "y"} [])))
232
+ (assert (= {:a :b} (dissoc {:a :b :c :d} :c)))
233
+ (assert (= (hash-map :foo 5)
234
+ (assoc (cljs.core.ObjMap. nil (array) (js-obj)) :foo 5)))
235
+
236
+ (assert (= "\"asdf\" \"asdf\"" (pr-str "asdf" "asdf")))
237
+ (assert (= "[1 true {:a 2, :b #\"x\\\"y\"} #<Array [3, 4]>]"
238
+ (pr-str [1 true {:a 2 :b #"x\"y"} (array 3 4)])))
239
+
240
+ (assert (= "\"asdf\"\n" (prn-str "asdf")))
241
+ (assert (= "[1 true {:a 2, :b 42} #<Array [3, 4]>]\n"
242
+ (prn-str [1 true {:a 2 :b 42} (array 3 4)])))
243
+
244
+ (assert (= "asdf" (print-str "asdf")))
245
+ (assert (= "asdf\n" (println-str "asdf")))
246
+
247
+ (assert (= "" (pr-str)))
248
+ (assert (= "\n" (prn-str)))
249
+
250
+ ;;this fails in v8 - why?
251
+ ;(assert (= "symbol\"'string" (pr-str (str 'symbol \" \' "string"))))
252
+
253
+ (assert (not (= "one" "two")))
254
+ (assert (= 3 (-count "abc")))
255
+ (assert (= 4 (-count (array 1 2 3 4))))
256
+ (assert (= "c" (-nth "abc" 2)))
257
+ (assert (= "quux" (-nth "abc" 3 "quux")))
258
+ (assert (= 1 (-nth (array 1 2 3 4) 0)))
259
+ (assert (= "val" (-nth (array 1 2 3 4) 4 "val")))
260
+ (assert (= "b" (-lookup "abc" 1)))
261
+ (assert (= "harriet" (-lookup "abcd" 4 "harriet")))
262
+ (assert (= 4 (-lookup (array 1 2 3 4) 3)))
263
+ (assert (= "zot" (-lookup (array 1 2 3 4) 4 "zot")))
264
+ (assert (= 10 (-reduce (array 1 2 3 4) +)))
265
+ (assert (= 20 (-reduce (array 1 2 3 4) + 10)))
266
+ (assert (= "cabd" (let
267
+ [jumble (fn [a b] (str (apply str (reverse (str a))) b))]
268
+ (-reduce "abcd" jumble))))
269
+ (assert (= "cafrogbd" (let
270
+ [jumble (fn [a b] (str (apply str (reverse (str a))) b))]
271
+ (-reduce "abcd" jumble "frog"))))
272
+ (assert (= [0 0 1 0 1]
273
+ [(bit-and 1 0)
274
+ (bit-and 0 0)
275
+ (bit-and 1 1)
276
+ (bit-and 42 1)
277
+ (bit-and 41 1)]))
278
+ (assert (= [1 0 1 43 41]
279
+ [(bit-or 1 0)
280
+ (bit-or 0 0)
281
+ (bit-or 1 1)
282
+ (bit-or 42 1)
283
+ (bit-or 41 1)]))
284
+ (assert (= [1 0 0 42 40]
285
+ [(bit-and-not 1 0)
286
+ (bit-and-not 0 0)
287
+ (bit-and-not 1 1)
288
+ (bit-and-not 42 1)
289
+ (bit-and-not 41 1)]))
290
+ (assert (= [0 2 968 16649 0]
291
+ [(bit-clear 1 0)
292
+ (bit-clear 2 0)
293
+ (bit-clear 1000 5)
294
+ (bit-clear 16713 6)
295
+ (bit-clear 1024 10)]))
296
+ (assert (= [0 0 992 18761 0]
297
+ [(bit-flip 1 0)
298
+ (bit-flip 2 1)
299
+ (bit-flip 1000 3)
300
+ (bit-flip 16713 11)
301
+ (bit-flip 1024 10)]))
302
+ (assert (= [-2 -3 999 -16714 -1025]
303
+ [(bit-not 1)
304
+ (bit-not 2)
305
+ (bit-not -1000)
306
+ (bit-not 16713)
307
+ (bit-not 1024)]))
308
+ (assert (= [1 2 1000 18761 1024]
309
+ [(bit-set 1 0)
310
+ (bit-set 2 1)
311
+ (bit-set 1000 3)
312
+ (bit-set 16713 11)
313
+ (bit-set 1024 10)]))
314
+ (assert (= [true true true false true]
315
+ [(bit-test 1 0)
316
+ (bit-test 2 1)
317
+ (bit-test 1000 3)
318
+ (bit-test 16713 11)
319
+ (bit-test 1024 10)]))
320
+ (assert (= [true false true false false false]
321
+ [(true? true)
322
+ (true? false)
323
+ (false? false)
324
+ (false? true)
325
+ (true? js/undefined)
326
+ (false? js/undefined)]))
327
+ ;; apply
328
+ (assert (= 0 (apply + nil)))
329
+ (assert (= 0 (apply + (list))))
330
+ (assert (= 1 (apply + (list 1))))
331
+ (assert (= 3 (apply + 1 (list 2))))
332
+ (assert (= 7 (apply + 1 2 (list 4))))
333
+ (assert (= 15 (apply + 1 2 4 (list 8))))
334
+ (assert (= 31 (apply + 1 2 4 8 (list 16))))
335
+ (assert (= 63 (apply + 1 2 4 8 16 (list 32))))
336
+ (assert (= 127 (apply + 1 2 4 8 16 (list 32 64))))
337
+ (assert (= 4950 (apply + (take 100 (iterate inc 0)))))
338
+ (assert (= () (apply list [])))
339
+ (assert (= [1 2 3] (apply list [1 2 3])))
340
+ (assert (= 6 (apply apply [+ [1 2 3]])))
341
+ ;; apply with infinite sequence
342
+ (assert (= 3 (apply (fn [& args]
343
+ (+ (nth args 0)
344
+ (nth args 1)
345
+ (nth args 2)))
346
+ (iterate inc 0))))
347
+ (assert (= [0 1 2 3 4] (take 5 (apply (fn [& m] m) (iterate inc 0)))))
348
+ (assert (= [1 2 3 4 5] (take 5 (apply (fn [x & m] m) (iterate inc 0)))))
349
+ (assert (= [2 3 4 5 6] (take 5 (apply (fn [x y & m] m) (iterate inc 0)))))
350
+ (assert (= [3 4 5 6 7] (take 5 (apply (fn [x y z & m] m) (iterate inc 0)))))
351
+ (assert (= [4 5 6 7 8] (take 5 (apply (fn [x y z a & m] m) (iterate inc 0)))))
352
+ (assert (= [5 6 7 8 9] (take 5 (apply (fn [x y z a b & m] m) (iterate inc 0)))))
353
+ ;; apply arity tests
354
+ (let [single-arity-non-variadic (fn [x y z] [z y x])
355
+ multiple-arity-non-variadic (fn ([x] x) ([x y] [y x]) ([x y z] [z y x]))
356
+ single-arity-variadic-fixedargs (fn [x y & more] [more y x])
357
+ single-arity-variadic-nofixedargs (fn [& more] more)
358
+ multiple-arity-variadic (fn ([x] x) ([x y] [y x]) ([x y & more] [more y x]))]
359
+ (assert (= [3 2 1] (apply single-arity-non-variadic [1 2 3])))
360
+ (assert (= [3 2 1] (apply single-arity-non-variadic 1 [2 3])))
361
+ (assert (= [3 2 1] (apply single-arity-non-variadic 1 2 [3])))
362
+ (assert (= 42 (apply multiple-arity-non-variadic [42])))
363
+ (assert (= [2 1] (apply multiple-arity-non-variadic [1 2])))
364
+ (assert (= [2 1] (apply multiple-arity-non-variadic 1 [2])))
365
+ (assert (= [3 2 1] (apply multiple-arity-non-variadic [1 2 3])))
366
+ (assert (= [3 2 1] (apply multiple-arity-non-variadic 1 [2 3])))
367
+ (assert (= [3 2 1] (apply multiple-arity-non-variadic 1 2 [3])))
368
+ (assert (= [[3 4 5] 2 1] (apply single-arity-variadic-fixedargs [1 2 3 4 5])))
369
+ (assert (= [[3 4 5] 2 1] (apply single-arity-variadic-fixedargs 1 [2 3 4 5])))
370
+ (assert (= [[3 4 5] 2 1] (apply single-arity-variadic-fixedargs 1 2 [3 4 5])))
371
+ (assert (= [[3 4 5] 2 1] (apply single-arity-variadic-fixedargs 1 2 3 [4 5])))
372
+ (assert (= [[3 4 5] 2 1] (apply single-arity-variadic-fixedargs 1 2 3 4 [5])))
373
+ (assert (= [3 4 5] (take 3 (first (apply single-arity-variadic-fixedargs (iterate inc 1))))))
374
+ (assert (= [2 1] (rest (apply single-arity-variadic-fixedargs (iterate inc 1)))))
375
+ (assert (= [1 2 3 4 5] (apply single-arity-variadic-nofixedargs [1 2 3 4 5])))
376
+ (assert (= [1 2 3 4 5] (apply single-arity-variadic-nofixedargs 1 [2 3 4 5])))
377
+ (assert (= [1 2 3 4 5] (apply single-arity-variadic-nofixedargs 1 2 [3 4 5])))
378
+ (assert (= [1 2 3 4 5] (apply single-arity-variadic-nofixedargs 1 2 3 [4 5])))
379
+ (assert (= [1 2 3 4 5] (apply single-arity-variadic-nofixedargs 1 2 3 4 [5])))
380
+ (assert (= [1 2 3 4 5] (take 5 (apply single-arity-variadic-nofixedargs (iterate inc 1)))))
381
+ (assert (= 42 (apply multiple-arity-variadic [42])))
382
+ (assert (= [2 1] (apply multiple-arity-variadic [1 2])))
383
+ (assert (= [2 1] (apply multiple-arity-variadic 1 [2])))
384
+ (assert (= [[3 4 5] 2 1] (apply multiple-arity-variadic [1 2 3 4 5])))
385
+ (assert (= [[3 4 5] 2 1] (apply multiple-arity-variadic 1 [2 3 4 5])))
386
+ (assert (= [[3 4 5] 2 1] (apply multiple-arity-variadic 1 2 [3 4 5])))
387
+ (assert (= [[3 4 5] 2 1] (apply multiple-arity-variadic 1 2 3 [4 5])))
388
+ (assert (= [[3 4 5] 2 1] (apply multiple-arity-variadic 1 2 3 4 [5])))
389
+ (assert (= [3 4 5] (take 3 (first (apply multiple-arity-variadic (iterate inc 1))))))
390
+ (assert (= [2 1] (rest (apply multiple-arity-variadic (iterate inc 1))))))
391
+
392
+ ;; CLJS-383
393
+ (let [f1 (fn f1 ([] 0) ([a] 1) ([a b] 2) ([a b c & more] 3))
394
+ f2 (fn f2 ([x] :foo) ([x y & more] (apply f1 y more)))]
395
+ (assert (= 1 (f2 1 2))))
396
+ (let [f (fn ([]) ([a & more] more))]
397
+ (assert (nil? (f :foo))))
398
+ (assert (nil? (array-seq (array 1) 1)))
399
+
400
+ (let [a (atom 0)]
401
+ (assert (= 0 (deref a)))
402
+ (assert (= 1 (swap! a inc)))
403
+ (assert (= false (compare-and-set! a 0 42)))
404
+ (assert (= true (compare-and-set! a 1 7)))
405
+ (assert (nil? (meta a)))
406
+ (assert (nil? (get-validator a))))
407
+ (let [a (atom 0)]
408
+ (assert (= 1 (swap! a + 1)))
409
+ (assert (= 4 (swap! a + 1 2)))
410
+ (assert (= 10 (swap! a + 1 2 3)))
411
+ (assert (= 20 (swap! a + 1 2 3 4))))
412
+ (let [a (atom [1] :validator coll? :meta {:a 1})]
413
+ (assert (= coll? (get-validator a)))
414
+ (assert (= {:a 1} (meta a)))
415
+ (alter-meta! a assoc :b 2)
416
+ (assert (= {:a 1 :b 2} (meta a))))
417
+ (assert (nil? (empty nil)))
418
+ (let [e-lazy-seq (empty (with-meta (lazy-seq (cons :a nil)) {:b :c}))]
419
+ (assert (seq? e-lazy-seq))
420
+ (assert (empty? e-lazy-seq))
421
+ (assert (= {:b :c} (meta e-lazy-seq))))
422
+ (let [e-list (empty '^{:b :c} (1 2 3))]
423
+ (assert (seq? e-list))
424
+ (assert (empty? e-list)))
425
+ (let [e-elist (empty '^{:b :c} ())]
426
+ (assert (seq? e-elist))
427
+ (assert (empty? e-elist))
428
+ (assert (= :c (get (meta e-elist) :b))))
429
+ (let [e-cons (empty (with-meta (cons :a nil) {:b :c}))]
430
+ (assert (seq? e-cons))
431
+ (assert (empty? e-cons))
432
+ (assert (= {:b :c} (meta e-cons))))
433
+ (let [e-vec (empty ^{:b :c} [:a :d :g])]
434
+ (assert (vector? e-vec))
435
+ (assert (empty? e-vec))
436
+ (assert (= {:b :c} (meta e-vec))))
437
+ (let [e-omap (empty ^{:b :c} {:a :d :g :h})]
438
+ (assert (map? e-omap))
439
+ (assert (empty? e-omap))
440
+ (assert (= {:b :c} (meta e-omap))))
441
+ (let [e-hmap (empty ^{:b :c} {[1 2] :d :g :h})]
442
+ (assert (map? e-hmap))
443
+ (assert (empty? e-hmap))
444
+ (assert (= {:b :c} (meta e-hmap))))
445
+
446
+ ;;this fails in v8 advanced mode - what's e?
447
+ #_(let [a (atom nil)]
448
+ (assert (= 1 (try* 1)))
449
+ (assert (= 2 (try* 1 (throw 3) (catch e 2))))
450
+ (assert (= 3 (try* 1 (throw 3) (catch e e))))
451
+ (assert (= 1 (try* 1 (finally (reset! a 42)))))
452
+ (assert (= 42 (deref a))))
453
+
454
+ (let [a (atom nil)]
455
+ (assert (= 1 (try 1)))
456
+ (assert (= 2 (try 1 (throw (js/Error.)) (catch js/Error e 2))))
457
+ (assert (= 2 (try 1 (throw (js/Error.)) (catch js/Error e 1 2))))
458
+ (assert (= 1 (try 1 (finally (reset! a 42)))))
459
+ (assert (= 42 (deref a))))
460
+
461
+ (assert (= [3] (nthnext [1 2 3] 2)))
462
+ (let [v [1 2 3]]
463
+ (assert (= v (for [e v] e)))
464
+ (assert (= [[1 1] [2 4] [3 9]] (for [e v :let [m (* e e)]] [e m])))
465
+ (assert (= [1 2] (for [e v :while (< e 3)] e)))
466
+ (assert (= [3] (for [e v :when (> e 2)] e)))
467
+ (assert (= [[1 1] [2 4]] (for [e v :while (< e 3) :let [m (* e e)]] [e m]))))
468
+ (assert (not= 1 2))
469
+ (assert (not (not= 1 1)))
470
+ (assert (not (not-empty [])))
471
+ (assert (boolean (not-empty [1 2 3])))
472
+ (assert (= "joel" (min-key count "joel" "tom servo" "crooooooooow")))
473
+ (assert (= "crooooooooow" (max-key count "joel" "tom servo" "crooooooooow")))
474
+ (assert (= (partition-all 4 [1 2 3 4 5 6 7 8 9])
475
+ [[1 2 3 4] [5 6 7 8] [9]]))
476
+ (assert (= (partition-all 4 2 [1 2 3 4 5 6 7 8 9])
477
+ [[1 2 3 4] [3 4 5 6] [5 6 7 8] [7 8 9] [9]]))
478
+ (assert (= [true true] (take-while true? [true true 2 3 4])))
479
+ (assert (= [[true true] [false false false] [true true]]
480
+ (partition-by true? [true true false false false true true])))
481
+ (assert (= [0 2 4 6 8 10] (take-nth 2 [0 1 2 3 4 5 6 7 8 9 10])))
482
+ (let [a10 (partial + 10)
483
+ a20 (partial + 10 10)
484
+ a21 (partial + 10 10 1)
485
+ a22 (partial + 10 5 4 3)
486
+ a23 (partial + 10 5 4 3 1)]
487
+ (assert (= 110 (a10 100)))
488
+ (assert (= 120 (a20 100)))
489
+ (assert (= 121 (a21 100)))
490
+ (assert (= 122 (a22 100)))
491
+ (assert (= 123 (a23 100))))
492
+ (let [n2 (comp first rest)
493
+ n3 (comp first rest rest)
494
+ n4 (comp first rest rest rest)
495
+ n5 (comp first rest rest rest rest)
496
+ n6 (comp first rest rest rest rest rest)]
497
+ (assert (= 2 (n2 [1 2 3 4 5 6 7])))
498
+ (assert (= 3 (n3 [1 2 3 4 5 6 7])))
499
+ (assert (= 4 (n4 [1 2 3 4 5 6 7])))
500
+ (assert (= 5 (n5 [1 2 3 4 5 6 7])))
501
+ (assert (= 6 (n6 [1 2 3 4 5 6 7]))))
502
+ (let [sf (some-fn number? keyword? symbol?)]
503
+ (assert (sf :foo 1))
504
+ (assert (sf :foo))
505
+ (assert (sf 'bar 1))
506
+ (assert (not (sf [] ()))))
507
+ (let [ep (every-pred number? zero?)]
508
+ (assert (ep 0 0 0))
509
+ (assert (not (ep 1 2 3 0))))
510
+ (assert ((complement number?) :foo))
511
+ (assert (= [1 [2 3] [1 2 3]] ((juxt first rest seq) [1 2 3])))
512
+ (assert (= 5 (max 1 2 3 4 5)))
513
+ (assert (= 5 (max 5 4 3 2 1)))
514
+ (assert (= 5.5 (max 1 2 3 4 5 5.5)))
515
+ (assert (= 1 (min 5 4 3 2 1)))
516
+ (assert (= 1 (min 1 2 3 4 5)))
517
+ (assert (= 0.5 (min 5 4 3 0.5 2 1)))
518
+ (let [x (array 1 2 3)]
519
+ (set! (.-foo x) :hello)
520
+ (assert (= (.-foo x) :hello)))
521
+
522
+ (assert (set []))
523
+ (assert (= #{} (set [])))
524
+ (assert (= #{} (hash-set)))
525
+ (assert (identical? cljs.core.PersistentHashSet (type (hash-set))))
526
+
527
+ (assert (= #{"foo"} (set ["foo"])))
528
+ (assert (= #{"foo"} (hash-set "foo")))
529
+ (assert (= #{1 2 3} #{1 3 2}))
530
+ (assert (= #{#{1 2 3} [4 5 6] {7 8} 9 10}
531
+ #{10 9 [4 5 6] {7 8} #{1 2 3}}))
532
+ (assert (not (= #{nil [] {} 0 #{}} #{})))
533
+ (assert (= (count #{nil [] {} 0 #{}}) 5))
534
+ (assert (= (conj #{1} 1) #{1}))
535
+ (assert (= (conj #{1} 2) #{2 1}))
536
+ (assert (= #{} (-empty #{1 2 3 4})))
537
+ (assert (= (reduce + #{1 2 3 4 5}) 15))
538
+ (assert (= 4 (get #{1 2 3 4} 4)))
539
+ (assert (contains? #{1 2 3 4} 4))
540
+ (assert (contains? #{[] nil 0 {} #{}} {}))
541
+ (assert (contains? #{[1 2 3]} [1 2 3]))
542
+ (assert (not (contains? (-disjoin #{1 2 3} 3) 3)))
543
+ (assert (neg? -1))
544
+ (assert (not (neg? 1)))
545
+ (assert (neg? -1.765))
546
+ (assert (not (neg? 0)))
547
+ (assert (= [true false true false true false true false]
548
+ (map integer?
549
+ [1 1.00001 0x7e7 [] (- 88 1001991881) :foo 0 "0"])))
550
+ (assert (= [true false true false true false]
551
+ (map odd? [1 2 3 4 -1 0])))
552
+ (assert (= [true false true false true true]
553
+ (map even? [2 3 4 5 -2 0])))
554
+ (assert (contains? {:a 1 :b 2} :a))
555
+ (assert (not (contains? {:a 1 :b 2} :z)))
556
+ (assert (contains? [5 6 7] 1))
557
+ (assert (contains? [5 6 7] 2))
558
+ (assert (not (contains? [5 6 7] 3)))
559
+ (assert (contains? (to-array [5 6 7]) 1))
560
+ (assert (contains? (to-array [5 6 7]) 2))
561
+ (assert (not (contains? (to-array [5 6 7]) 3)))
562
+ (assert (not (contains? nil 42)))
563
+ (assert (contains? "f" 0))
564
+ (assert (not (contains? "f" 55)))
565
+ (assert (distinct? 1 2 3))
566
+ (assert (not (distinct? 1 2 3 1)))
567
+
568
+ ;; distinct
569
+ (assert (= (distinct ()) ()))
570
+ (assert (= (distinct '(1)) '(1)))
571
+ (assert (= (distinct '(1 2 3 1 1 1)) '(1 2 3)))
572
+ (assert (= (distinct [1 1 1 2]) '(1 2)))
573
+ (assert (= (distinct [1 2 1 2]) '(1 2)))
574
+ (assert (= (distinct "a") ["a"]))
575
+ (assert (= (distinct "abcabab") ["a" "b" "c"]))
576
+ (assert (= (distinct ["abc" "abc"]) ["abc"]))
577
+ (assert (= (distinct [nil nil]) [nil]))
578
+ (assert (= (distinct [0.0 0.0]) [0.0]))
579
+ (assert (= (distinct ['sym 'sym]) '[sym]))
580
+ (assert (= (distinct [:kw :kw]) [:kw]))
581
+ (assert (= (distinct [42 42]) [42]))
582
+ (assert (= (distinct [[] []]) [[]]))
583
+ (assert (= (distinct ['(1 2) '(1 2)]) '[(1 2)]))
584
+ (assert (= (distinct [() ()]) [()]))
585
+ (assert (= (distinct [[1 2] [1 2]]) [[1 2]]))
586
+ (assert (= (distinct [{:a 1 :b 2} {:a 1 :b 2}]) [{:a 1 :b 2}]))
587
+ (assert (= (distinct [{} {}]) [{}]))
588
+ (assert (= (distinct [#{1 2} #{1 2}]) [#{1 2}]))
589
+ (assert (= (distinct [#{} #{}]) [#{}]))
590
+
591
+ ;;regexps
592
+ (assert (= (str (re-pattern "f(.)o")) (str (js* "/f(.)o/"))))
593
+ (assert (= (re-find (re-pattern "foo") "foo bar foo baz foo zot") "foo"))
594
+ (assert (= (re-find (re-pattern "f(.)o") "foo bar foo baz foo zot") ["foo" "o"]))
595
+ (assert (= (re-matches (re-pattern "foo") "foo") "foo"))
596
+ (assert (= (re-matches (re-pattern "foo") "foo bar foo baz foo zot") nil))
597
+ (assert (= (re-matches (re-pattern "foo.*") "foo bar foo baz foo zot") "foo bar foo baz foo zot"))
598
+ (assert (= (re-seq (re-pattern "foo") "foo bar foo baz foo zot") (list "foo" "foo" "foo")))
599
+ (assert (= (re-seq (re-pattern "f(.)o") "foo bar foo baz foo zot") (list ["foo" "o"] ["foo" "o"] ["foo" "o"])))
600
+ (assert (= (re-matches (re-pattern "(?i)foo") "Foo") "Foo"))
601
+
602
+ ;; destructuring
603
+ (assert (= [2 1] (let [[a b] [1 2]] [b a])))
604
+ (assert (= #{1 2} (let [[a b] [1 2]] #{a b})))
605
+ (assert (= [1 2] (let [{a :a b :b} {:a 1 :b 2}] [a b])))
606
+ (assert (= [1 2] (let [{:keys [a b]} {:a 1 :b 2}] [a b])))
607
+ (assert (= [1 2 [1 2]] (let [[a b :as v] [1 2]] [a b v])))
608
+ (assert (= [1 42] (let [{:keys [a b] :or {b 42}} {:a 1}] [a b])))
609
+ (assert (= [1 nil] (let [{:keys [a b] :or {c 42}} {:a 1}] [a b])))
610
+ (assert (= [2 1] (let [[a b] '(1 2)] [b a])))
611
+ (assert (= {1 2} (let [[a b] [1 2]] {a b})))
612
+ (assert (= [2 1] (let [[a b] (seq [1 2])] [b a])))
613
+
614
+ ;; update-in
615
+ (assert (= {:foo {:bar {:baz 1}}}
616
+ (update-in {:foo {:bar {:baz 0}}} [:foo :bar :baz] inc)))
617
+ (assert (= {:foo 1 :bar 2 :baz 10}
618
+ (update-in {:foo 1 :bar 2 :baz 3} [:baz] + 7)))
619
+ (assert (= [{:foo 1, :bar 2} {:foo 1, :bar 3}]
620
+ (update-in [{:foo 1 :bar 2}, {:foo 1 :bar 2}] [1 :bar] inc)))
621
+ (assert (= [{:foo {:bar 2}} {:foo {:bar 3}}]
622
+ (update-in [{:foo {:bar 2}}, {:foo {:bar 2}}] [1 :foo :bar] inc)))
623
+
624
+ ;; assoc-in
625
+ (assert (= {:foo {:bar {:baz 100}}}
626
+ (assoc-in {:foo {:bar {:baz 0}}} [:foo :bar :baz] 100)))
627
+ (assert (= {:foo 1 :bar 2 :baz 100}
628
+ (assoc-in {:foo 1 :bar 2 :baz 3} [:baz] 100)))
629
+ (assert (= [{:foo [{:bar 2} {:baz 3}]} {:foo [{:bar 2} {:baz 100}]}]
630
+ (assoc-in [{:foo [{:bar 2} {:baz 3}]}, {:foo [{:bar 2} {:baz 3}]}]
631
+ [1 :foo 1 :baz] 100)))
632
+ (assert (= [{:foo 1, :bar 2} {:foo 1, :bar 100}]
633
+ (assoc-in [{:foo 1 :bar 2}, {:foo 1 :bar 2}] [1 :bar] 100)))
634
+
635
+ ;; get-in
636
+ (assert (= 1 (get-in {:foo 1 :bar 2} [:foo])))
637
+ (assert (= 2 (get-in {:foo {:bar 2}} [:foo :bar])))
638
+ (assert (= 1 (get-in [{:foo 1}, {:foo 2}] [0 :foo])))
639
+ (assert (= 4 (get-in [{:foo 1 :bar [{:baz 1}, {:buzz 2}]}, {:foo 3 :bar [{:baz 3}, {:buzz 4}]}]
640
+ [1 :bar 1 :buzz])))
641
+
642
+ ;; arrays
643
+ (let [a (to-array [1 2 3])]
644
+ (assert (= [10 20 30] (seq (amap a i ret (* 10 (aget a i))))))
645
+ (assert (= 6 (areduce a i ret 0 (+ ret (aget a i)))))
646
+ (assert (= (seq a) (seq (to-array [1 2 3]))))
647
+ (assert (= 42 (aset a 0 42)))
648
+ (assert (not= (seq a) (seq (to-array [1 2 3]))))
649
+ (assert (not= a (aclone a))))
650
+
651
+ (let [a (array (array 1 2 3) (array 4 5 6))]
652
+ (assert (= (aget a 0 1) 2))
653
+ (assert (= (apply aget a [0 1]) 2))
654
+ (assert (= (aget a 1 1) 5))
655
+ (assert (= (apply aget a [1 1]) 5)))
656
+
657
+ ;; sort
658
+ (assert (= [1 2 3 4 5] (sort [5 3 1 4 2])))
659
+ (assert (= [1 2 3 4 5] (sort < [5 3 1 4 2])))
660
+ (assert (= [5 4 3 2 1] (sort > [5 3 1 4 2])))
661
+
662
+ ;; sort-by
663
+ (assert (= ["a" [ 1 2] "foo"] (sort-by count ["foo" "a" [1 2]])))
664
+ (assert (= ["foo" [1 2] "a"] (sort-by count > ["foo" "a" [1 2]])))
665
+
666
+ ;; shuffle
667
+ (let [coll [1 2 3 4 5 6 7 8 9 10]
668
+ ; while it is technically possible for this test to fail with a false negative,
669
+ ; it's _extraordinarily_ unlikely.
670
+ shuffles (filter #(not= coll %) (take 100 (iterate shuffle coll)))]
671
+ (assert (not (empty? shuffles))))
672
+
673
+ ;; js->clj
674
+ (assert (= {"a" 1, "b" 2} (js->clj (js* "{\"a\":1,\"b\":2}"))))
675
+ (assert (= {"a" nil} (js->clj (js* "{\"a\":null}"))))
676
+ (assert (= {"a" true, "b" false} (js->clj (js* "{\"a\":true,\"b\":false}"))))
677
+ (assert (= {:a 1, :b 2} (js->clj (js* "{\"a\":1,\"b\":2}") :keywordize-keys true)))
678
+ (assert (= [[{:a 1, :b 2} {:a 1, :b 2}]]
679
+ (js->clj (js* "[[{\"a\":1,\"b\":2}, {\"a\":1,\"b\":2}]]") :keywordize-keys true)))
680
+ (assert (= [[{:a 1, :b 2} {:a 1, :b 2}]]
681
+ (js->clj [[{:a 1, :b 2} {:a 1, :b 2}]])))
682
+
683
+ ;; last
684
+ (assert (= nil (last nil)))
685
+ (assert (= 3 (last [1 2 3])))
686
+
687
+ ;; dotimes
688
+ (let [s (atom [])]
689
+ (dotimes [n 5]
690
+ (swap! s conj n))
691
+ (assert (= [0 1 2 3 4] @s)))
692
+
693
+ ;; doseq
694
+ (let [v [1 2 3 4 5]
695
+ s (atom ())]
696
+ (doseq [n v] (swap! s conj n))
697
+ (assert (= @s (reverse v))))
698
+
699
+ ;; delay
700
+ (let [a (atom 0)
701
+ d (delay (swap! a inc))]
702
+ (assert (false? (realized? d)))
703
+ (assert (zero? @a)) ;; delay hasn't triggered yet
704
+ (assert (= 1 @d)) ;; trigger it
705
+ (assert (= 1 @a)) ;; make sure side effect has happened
706
+ (assert (true? (realized? d)))
707
+ (assert (= 1 @d)) ;; body doesn't happen again
708
+ (assert (= 1 @a)) ;; atom hasn't changed either
709
+ (assert (= (force d) @d))
710
+ (assert (= 1 (force 1)))) ;; you can safely force non-delays
711
+
712
+ ;; assoc
713
+ (assert (= {1 2 3 4} (assoc {} 1 2 3 4)))
714
+ (assert (= {1 2} (assoc {} 1 2)))
715
+ (assert (= [42 2] (assoc [1 2] 0 42)))
716
+
717
+ ;; dissoc
718
+ (assert (= {} (dissoc {1 2 3 4} 1 3)))
719
+ (assert (= {1 2} (dissoc {1 2 3 4} 3)))
720
+
721
+ ;; disj
722
+ (assert (= #{1 2 3} (disj #{1 2 3})))
723
+ (assert (= #{1 2} (disj #{1 2 3} 3)))
724
+ (assert (= #{1} (disj #{1 2 3} 2 3)))
725
+
726
+ ;; memoize
727
+ (let [f (memoize (fn [] (rand)))]
728
+ (f)
729
+ (assert (= (f) (f))))
730
+
731
+ ;; find
732
+ (assert (= (find {} :a) nil))
733
+ (assert (= (find {:a 1} :a) [:a 1]))
734
+ (assert (= (find {:a 1} :b) nil))
735
+ (assert (= (find {:a 1 :b 2} :a) [:a 1]))
736
+ (assert (= (find {:a 1 :b 2} :b) [:b 2]))
737
+ (assert (= (find {:a 1 :b 2} :c) nil))
738
+ (assert (= (find {} nil) nil))
739
+ (assert (= (find {:a 1} nil) nil))
740
+ (assert (= (find {:a 1 :b 2} nil) nil))
741
+ (assert (= (find [1 2 3] 0) [0 1]))
742
+
743
+ ;; mod,quot,rem
744
+ (assert (= (quot 4 2) 2))
745
+ (assert (= (quot 3 2) 1))
746
+ (assert (= (quot 6 4) 1))
747
+ (assert (= (quot 0 5) 0))
748
+ (assert (= (quot 42 5) 8))
749
+ (assert (= (quot 42 -5) -8))
750
+ (assert (= (quot -42 -5) 8))
751
+ (assert (= (quot 9 3) 3))
752
+ (assert (= (quot 9 -3) -3))
753
+ (assert (= (quot -9 3) -3))
754
+ (assert (= (quot 2 -5) 0))
755
+ (assert (= (quot -2 5) 0))
756
+ (assert (= (quot 0 3) 0))
757
+ (assert (= (quot 0 -3) 0))
758
+
759
+ (assert (= (mod 4 2) 0))
760
+ (assert (= (mod 3 2) 1))
761
+ (assert (= (mod 6 4) 2))
762
+ (assert (= (mod 0 5) 0))
763
+ (assert (= (mod 4.5 2.0) 0.5))
764
+ (assert (= (mod 42 5) 2))
765
+ (assert (= (mod 9 3) 0))
766
+ (assert (= (mod 9 -3) 0))
767
+ (assert (= (mod -9 3) 0))
768
+ (assert (= (mod -9 -3) 0))
769
+ (assert (= (mod 0 3) 0))
770
+ (assert (= (mod 3216478362187432 432143214) 120355456))
771
+
772
+ (assert (= (rem 4 2) 0))
773
+ (assert (= (rem 0 5) 0))
774
+ (assert (= (rem 4.5 2.0) 0.5))
775
+ (assert (= (rem 42 5) 2))
776
+ (assert (= (rem 2 5) 2))
777
+ (assert (= (rem 2 -5) 2))
778
+ (assert (= (rem 0 3) 0))
779
+
780
+ ;; range
781
+ (assert (= (range 10) (list 0 1 2 3 4 5 6 7 8 9)))
782
+ (assert (= (range 10 20) (list 10 11 12 13 14 15 16 17 18 19)))
783
+ (assert (= (range 10 20 2) (list 10 12 14 16 18)))
784
+ (assert (= (take 20 (range)) (list 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19)))
785
+
786
+ ;; group-by
787
+ (let [d (group-by second {:a 1 :b 2 :c 1 :d 4 :e 1 :f 2})]
788
+ (assert (= 3 (count (get d 1))))
789
+ (assert (= 2 (count (get d 2))))
790
+ (assert (= 1 (count (get d 4)))))
791
+
792
+ (assert (= {1 2 3 4 5 6} (merge {1 2} {3 4} {5 6})))
793
+ (assert (= {1 2 3 4} (merge {1 2} {3 4} nil)))
794
+
795
+ ;; frequencies
796
+ (assert (= {:a 3 :b 2} (frequencies [:a :b :a :b :a])))
797
+
798
+ ;; reductions
799
+ (assert (= [1 3 6 10 15] (reductions + [1 2 3 4 5])))
800
+
801
+ ;; keep
802
+ (assert (= [1 3 5 7 9] (keep #(if (odd? %) %) [1 2 3 4 5 6 7 8 9 10])))
803
+ (assert (= [2 4 6 8 10] (keep #(if (even? %) %) [1 2 3 4 5 6 7 8 9 10])))
804
+
805
+ ;; keep-indexed
806
+ (assert (= [1 3 5 7 9] (keep-indexed #(if (odd? %1) %2) [0 1 2 3 4 5 6 7 8 9 10])))
807
+ (assert (= [2 4 5] (keep-indexed #(if (pos? %2) %1) [-9 0 29 -7 45 3 -8])))
808
+
809
+ ;; map-indexed
810
+ (assert (= [[0 :a] [1 :b] [2 :c]] (map-indexed #(vector % %2) [:a :b :c])))
811
+
812
+ ;; merge-with
813
+ (assert (= '{"Foo" ("foo" "FOO" "fOo"), "Bar" ("bar" "BAR" "BAr"), "Baz" ["baz"], "Qux" ["qux" "quux"]}
814
+ (merge-with concat
815
+ {"Foo" ["foo" "FOO"]
816
+ "Bar" ["bar" "BAR"]
817
+ "Baz" ["baz"]}
818
+ {"Foo" ["fOo"]
819
+ "Bar" ["BAr"]
820
+ "Qux" ["qux" "quux"]})))
821
+ (assert (= {:a 111, :b 102, :c 13}
822
+ (merge-with +
823
+ {:a 1 :b 2 :c 3}
824
+ {:a 10 :c 10}
825
+ {:a 100 :b 100})))
826
+
827
+ (assert (= {:a 3, :b 102, :c 13}
828
+ (apply merge-with [+
829
+ {:a 1 :b 100}
830
+ {:a 1 :b 2 :c 3}
831
+ {:a 1 :c 10}])))
832
+
833
+ (assert (= '[a c e] (replace '[a b c d e] [0 2 4])))
834
+ (assert (= [:one :zero :two :zero]
835
+ (replace {0 :zero 1 :one 2 :two} '(1 0 2 0))))
836
+
837
+ ;; split-at
838
+ (assert (= [[1 2] [3 4 5]] (split-at 2 [1 2 3 4 5])))
839
+
840
+ ;; split-with
841
+ (assert (= [[1 2 3] [4 5]] (split-with (partial >= 3) [1 2 3 4 5])))
842
+
843
+ ;; trampoline
844
+ (assert (= 10000 (trampoline (fn f [n] (if (>= n 10000) n #(f (inc n)))) 0)))
845
+
846
+ ;; vary-meta
847
+ (assert (= {:a 1} (meta (vary-meta [] assoc :a 1))))
848
+ (assert (= {:a 1 :b 2} (meta (vary-meta (with-meta [] {:b 2}) assoc :a 1))))
849
+
850
+ ;; multi-methods
851
+ (swap! global-hierarchy make-hierarchy)
852
+
853
+ ;; hierarchy tests
854
+ (derive ::rect ::shape)
855
+ (derive ::square ::rect)
856
+
857
+ (assert (= #{:cljs.core-test/shape} (parents ::rect)))
858
+ (assert (= #{:cljs.core-test/rect :cljs.core-test/shape} (ancestors ::square)))
859
+ (assert (= #{:cljs.core-test/rect :cljs.core-test/square} (descendants ::shape)))
860
+ (assert (true? (isa? 42 42)))
861
+ (assert (true? (isa? ::square ::shape)))
862
+
863
+ (derive cljs.core.ObjMap ::collection)
864
+ (derive cljs.core.PersistentHashSet ::collection)
865
+ (assert (true? (isa? cljs.core.ObjMap ::collection)))
866
+ (assert (true? (isa? cljs.core.PersistentHashSet ::collection)))
867
+ (assert (false? (isa? cljs.core.IndexedSeq ::collection)))
868
+ ;; ?? (isa? String Object)
869
+ (assert (true? (isa? [::square ::rect] [::shape ::shape])))
870
+ ;; ?? (ancestors java.util.ArrayList)
871
+
872
+ ;; ?? isa? based dispatch tests
873
+
874
+ ;; prefer-method test
875
+ (defmulti bar (fn [x y] [x y]))
876
+ (defmethod bar [::rect ::shape] [x y] :rect-shape)
877
+ (defmethod bar [::shape ::rect] [x y] :shape-rect)
878
+
879
+ ;;(bar ::rect ::rect)
880
+ ;; -> java.lang.IllegalArgumentException:
881
+ ;; Multiple methods match dispatch value:
882
+ ;; [:cljs.core-test/rect :cljs.core-test/rect] -> [:cljs.core-test/rect :cljs.core-test/shape]
883
+ ;; and [:cljs.core-test/shape :cljs.core-test/rect],
884
+ ;; and neither is preferred
885
+
886
+ (assert (zero? (count (prefers bar))))
887
+ (prefer-method bar [::rect ::shape] [::shape ::rect])
888
+ (assert (= 1 (count (prefers bar))))
889
+ (assert (= :rect-shape (bar ::rect ::rect)))
890
+ (assert (= :rect-shape (apply (-get-method bar [::rect ::shape]) [::rect ::shape])))
891
+
892
+ ;; nested data structures tests
893
+ (defmulti nested-dispatch (fn [m] (-> m :a :b)))
894
+ (defmethod nested-dispatch :c [m] :nested-a)
895
+ (defmethod nested-dispatch :default [m] :nested-default)
896
+ (assert (= :nested-a (nested-dispatch {:a {:b :c}})))
897
+
898
+ (defmulti nested-dispatch2 ffirst)
899
+ (defmethod nested-dispatch2 :a [m] :nested-a)
900
+ (defmethod nested-dispatch2 :default [m] :nested-default)
901
+ (assert (= :nested-a (nested-dispatch2 [[:a :b]])))
902
+
903
+ ;; general tests
904
+ (defmulti foo1 (fn [& args] (first args)))
905
+ (defmethod foo1 :a [& args] :a-return)
906
+ (defmethod foo1 :default [& args] :default-return)
907
+ (assert (= :a-return (foo1 :a)))
908
+ (assert (= :default-return (foo1 1)))
909
+
910
+ (defmulti area :Shape)
911
+ (defn rect [wd ht] {:Shape :Rect :wd wd :ht ht})
912
+ (defn circle [radius] {:Shape :Circle :radius radius})
913
+ (defmethod area :Rect [r]
914
+ (* (:wd r) (:ht r)))
915
+ (defmethod area :Circle [c]
916
+ (* Math/PI (* (:radius c) (:radius c))))
917
+ (defmethod area :default [x] :oops)
918
+ (def r (rect 4 13))
919
+ (def c (circle 12))
920
+ (assert (= 52 (area r)))
921
+ (assert (= :oops (area {})))
922
+
923
+ ;; remove method tests
924
+ (assert (= 2 (count (methods bar))))
925
+ (remove-method bar [::rect ::shape])
926
+ (assert (= 1 (count (methods bar))))
927
+ (remove-all-methods bar)
928
+ (assert (zero? (count (methods bar))))
929
+
930
+ ;; test apply
931
+ (defmulti apply-multi-test (fn ([_] 0) ([_ _] 0) ([_ _ _] 0)))
932
+ (defmethod apply-multi-test 0
933
+ ([x] :one)
934
+ ([x y] :two)
935
+ ([x y & r] [:three r]))
936
+ (assert (= [:three '(2)] (apply apply-multi-test [0 1 2])))
937
+
938
+ ;; Range
939
+ (assert (= (range 0 10 3) (list 0 3 6 9)))
940
+ (assert (= (count (range 0 10 3)) 4))
941
+ (assert (= (range 0 -10 -3) (list 0 -3 -6 -9)))
942
+ (assert (= (count (range 0 -10 -3)) 4))
943
+ (assert (= (range -10 10 3) (list -10 -7 -4 -1 2 5 8)))
944
+ (assert (= (count (range -10 10 3)) 7))
945
+ (assert (= (range 0 1 1) (list 0)))
946
+ (assert (= (range 0 -3 -1) (list 0 -1 -2)))
947
+ (assert (= (range 3 0 -1) (list 3 2 1)))
948
+ (assert (= (range 0 10 -1) (list)))
949
+ (assert (= (range 0 1 0) (list)))
950
+ (assert (= (range 10 0 1) (list)))
951
+ (assert (= (range 0 0 0) (list)))
952
+ (assert (= (count (range 0 10 -1)) 0))
953
+ (assert (= (count (range 0 1 0)) 0))
954
+ (assert (= (count (range 10 0 1)) 0))
955
+ (assert (= (count (range 0 0 0)) 0))
956
+ (assert (= (take 3 (range 1 0 0)) (list 1 1 1)))
957
+ (assert (= (take 3 (range 3 1 0)) (list 3 3 3)))
958
+
959
+ ;; PersistentVector
960
+ (let [pv (vec (range 97))]
961
+ (assert (= (nth pv 96) 96))
962
+ (assert (= (nth pv 97 nil) nil))
963
+ (assert (= (pv 96) 96)))
964
+
965
+ (let [pv (vec (range 33))]
966
+ (assert (= pv
967
+ (-> pv
968
+ pop
969
+ pop
970
+ (conj 31)
971
+ (conj 32)))))
972
+
973
+ (let [stack1 (pop (vec (range 97)))
974
+ stack2 (pop stack1)]
975
+ (assert (= 95 (peek stack1)))
976
+ (assert (= 94 (peek stack2))))
977
+
978
+ ;; subvec
979
+ (let [v (vec (range 10))
980
+ s (subvec v 2 8)]
981
+ (assert (= s
982
+ (-> v
983
+ (subvec 2)
984
+ (subvec 0 6))
985
+ (->> v
986
+ (drop 2)
987
+ (take 6))))
988
+ (assert (= 6 (count s)))
989
+ (assert (= [2 3 4 5 6] (pop s)))
990
+ (assert (= 7 (peek s)))
991
+ (assert (= [2 3 4 5 6 7 1]
992
+ (assoc s 6 1)
993
+ (conj s 1)))
994
+ (assert (= 27 (reduce + s)))
995
+ (assert (= s (vec s))) ; pour into plain vector
996
+ (let [m {:x 1}] (assert (= m (meta (with-meta s m))))))
997
+
998
+ ;; TransientVector
999
+ (let [v1 (vec (range 15 48))
1000
+ v2 (vec (range 40 57))
1001
+ v1 (persistent! (assoc! (conj! (pop! (transient v1)) :foo) 0 :quux))
1002
+ v2 (persistent! (assoc! (conj! (transient v2) :bar) 0 :quux))
1003
+ v (into v1 v2)]
1004
+ (assert (= v (vec (concat [:quux] (range 16 47) [:foo]
1005
+ [:quux] (range 41 57) [:bar])))))
1006
+ (loop [v (transient [])
1007
+ xs (range 100)]
1008
+ (if-let [x (first xs)]
1009
+ (recur
1010
+ (condp #(%1 (mod %2 3)) x
1011
+ #{0 2} (conj! v x)
1012
+ #{1} (assoc! v (count v) x))
1013
+ (next xs))
1014
+ (assert (= (vec (range 100)) (persistent! v)))))
1015
+
1016
+ ;; PersistentHashMap & TransientHashMap
1017
+ (loop [m1 cljs.core.PersistentHashMap/EMPTY
1018
+ m2 (transient cljs.core.PersistentHashMap/EMPTY)
1019
+ i 0]
1020
+ (if (< i 100)
1021
+ (recur (assoc m1 i i) (assoc! m2 i i) (inc i))
1022
+ (let [m2 (persistent! m2)]
1023
+ (assert (= (count m1) 100))
1024
+ (assert (= (count m2) 100))
1025
+ (assert (= m1 m2))
1026
+ (loop [i 0]
1027
+ (if (< i 100)
1028
+ (do (assert (= (m1 i) i))
1029
+ (assert (= (m2 i) i))
1030
+ (assert (= (get m1 i) i))
1031
+ (assert (= (get m2 i) i))
1032
+ (assert (contains? m1 i))
1033
+ (assert (contains? m2 i))
1034
+ (recur (inc i)))))
1035
+ (assert (= (map vector (range 100) (range 100)) (sort-by first (seq m1))))
1036
+ (assert (= (map vector (range 100) (range 100)) (sort-by first (seq m2))))
1037
+ (assert (not (contains? (dissoc m1 3) 3))))))
1038
+ (let [m (-> (->> (interleave (range 10) (range 10))
1039
+ (apply assoc cljs.core.PersistentHashMap/EMPTY))
1040
+ (dissoc 3 5 7))]
1041
+ (assert (= (count m) 7))
1042
+ (assert (= m {0 0 1 1 2 2 4 4 6 6 8 8 9 9})))
1043
+ (let [m (-> (->> (interleave (range 10) (range 10))
1044
+ (apply assoc cljs.core.PersistentHashMap/EMPTY))
1045
+ (conj [:foo 1]))]
1046
+ (assert (= (count m) 11))
1047
+ (assert (= m {0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 :foo 1})))
1048
+ (let [m (-> (->> (interleave (range 10) (range 10))
1049
+ (apply assoc cljs.core.PersistentHashMap/EMPTY)
1050
+ transient)
1051
+ (conj! [:foo 1])
1052
+ persistent!)]
1053
+ (assert (= (count m) 11))
1054
+ (assert (= m {0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 :foo 1})))
1055
+ (let [tm (->> (interleave (range 10) (range 10))
1056
+ (apply assoc cljs.core.PersistentHashMap/EMPTY)
1057
+ transient)]
1058
+ (loop [tm tm ks [3 5 7]]
1059
+ (if-let [k (first ks)]
1060
+ (recur (dissoc! tm k) (next ks))
1061
+ (let [m (persistent! tm)]
1062
+ (assert (= (count m) 7))
1063
+ (assert (= m {0 0 1 1 2 2 4 4 6 6 8 8 9 9}))))))
1064
+ (let [tm (-> (->> (interleave (range 10) (range 10))
1065
+ (apply assoc cljs.core.PersistentHashMap/EMPTY))
1066
+ (dissoc 3 5 7)
1067
+ transient)]
1068
+ (doseq [k [0 1 2 4 6 8 9]]
1069
+ (assert (= k (get tm k))))
1070
+ (let [m (persistent! tm)]
1071
+ (assert (= 2 (try (dissoc! tm 1) 1 (catch js/Error e 2))))
1072
+ (assert (= 2 (try (assoc! tm 10 10) 1 (catch js/Error e 2))))
1073
+ (assert (= 2 (try (persistent! tm) 1 (catch js/Error e 2))))
1074
+ (assert (= 2 (try (count tm) 1 (catch js/Error e 2))))
1075
+ (assert (= m {0 0 1 1 2 2 4 4 6 6 8 8 9 9}))))
1076
+ (deftype FixedHash [h v]
1077
+ IHash
1078
+ (-hash [this] h)
1079
+ IEquiv
1080
+ (-equiv [this other]
1081
+ (and (instance? FixedHash other) (= v (.-v other)))))
1082
+ (def fixed-hash-foo (FixedHash. 0 :foo))
1083
+ (def fixed-hash-bar (FixedHash. 0 :bar))
1084
+ (let [m (assoc cljs.core.PersistentHashMap/EMPTY
1085
+ fixed-hash-foo 1
1086
+ fixed-hash-bar 2)]
1087
+ (assert (= (get m fixed-hash-foo) 1))
1088
+ (assert (= (get m fixed-hash-bar) 2))
1089
+ (assert (= (count m) 2))
1090
+ (let [m (dissoc m fixed-hash-foo)]
1091
+ (assert (= (get m fixed-hash-bar) 2))
1092
+ (assert (not (contains? m fixed-hash-foo)))
1093
+ (assert (= (count m) 1))))
1094
+ (let [m (into cljs.core.PersistentHashMap/EMPTY ; make sure we're testing
1095
+ (zipmap (range 100) (range 100))) ; the correct map type
1096
+ m (assoc m fixed-hash-foo 1 fixed-hash-bar 2)]
1097
+ (assert (= (count m) 102))
1098
+ (assert (= (get m fixed-hash-foo) 1))
1099
+ (assert (= (get m fixed-hash-bar) 2))
1100
+ (let [m (dissoc m 3 5 7 fixed-hash-foo)]
1101
+ (assert (= (get m fixed-hash-bar) 2))
1102
+ (assert (not (contains? m fixed-hash-foo)))
1103
+ (assert (= (count m) 98))))
1104
+ (let [m (into cljs.core.PersistentHashMap/EMPTY ; make sure we're testing
1105
+ (zipmap (range 100) (range 100))) ; the correct map type
1106
+ m (transient m)
1107
+ m (assoc! m fixed-hash-foo 1)
1108
+ m (assoc! m fixed-hash-bar 2)
1109
+ m (persistent! m)]
1110
+ (assert (= (count m) 102))
1111
+ (assert (= (get m fixed-hash-foo) 1))
1112
+ (assert (= (get m fixed-hash-bar) 2))
1113
+ (let [m (dissoc m 3 5 7 fixed-hash-foo)]
1114
+ (assert (= (get m fixed-hash-bar) 2))
1115
+ (assert (not (contains? m fixed-hash-foo)))
1116
+ (assert (= (count m) 98))))
1117
+
1118
+ ;; PersistentArrayMap & TransientArrayMap
1119
+ (def array-map-conversion-threshold
1120
+ cljs.core.PersistentArrayMap/HASHMAP_THRESHOLD)
1121
+ (loop [m1 cljs.core.PersistentArrayMap/EMPTY
1122
+ m2 (transient cljs.core.PersistentArrayMap/EMPTY)
1123
+ i 0]
1124
+ (if (< i array-map-conversion-threshold)
1125
+ (recur (assoc m1 i i) (assoc! m2 i i) (inc i))
1126
+ (let [m2 (persistent! m2)]
1127
+ (assert (= (count m1) array-map-conversion-threshold))
1128
+ (assert (= (count m2) array-map-conversion-threshold))
1129
+ (assert (= m1 m2))
1130
+ (loop [i 0]
1131
+ (if (< i array-map-conversion-threshold)
1132
+ (do (assert (= (m1 i) i))
1133
+ (assert (= (m2 i) i))
1134
+ (assert (= (get m1 i) i))
1135
+ (assert (= (get m2 i) i))
1136
+ (assert (contains? m1 i))
1137
+ (assert (contains? m2 i))
1138
+ (recur (inc i)))))
1139
+ (assert (= (map vector
1140
+ (range array-map-conversion-threshold)
1141
+ (range array-map-conversion-threshold))
1142
+ (sort-by first (seq m1))))
1143
+ (assert (= (map vector
1144
+ (range array-map-conversion-threshold)
1145
+ (range array-map-conversion-threshold))
1146
+ (sort-by first (seq m2))))
1147
+ (assert (not (contains? (dissoc m1 3) 3))))))
1148
+ (let [m (-> (->> (interleave (range 10) (range 10))
1149
+ (apply assoc cljs.core.PersistentArrayMap/EMPTY))
1150
+ (dissoc 3 5 7))]
1151
+ (assert (= (count m) 7))
1152
+ (assert (= m {0 0 1 1 2 2 4 4 6 6 8 8 9 9})))
1153
+ (let [m (-> (->> (interleave (range 10) (range 10))
1154
+ (apply assoc cljs.core.PersistentArrayMap/EMPTY))
1155
+ (conj [:foo 1]))]
1156
+ (assert (= (count m) 11))
1157
+ (assert (= m {0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 :foo 1})))
1158
+ (let [m (-> (->> (interleave (range 10) (range 10))
1159
+ (apply assoc cljs.core.PersistentArrayMap/EMPTY)
1160
+ transient)
1161
+ (conj! [:foo 1])
1162
+ persistent!)]
1163
+ (assert (= (count m) 11))
1164
+ (assert (= m {0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 :foo 1})))
1165
+ (let [tm (->> (interleave (range 10) (range 10))
1166
+ (apply assoc cljs.core.PersistentArrayMap/EMPTY)
1167
+ transient)]
1168
+ (loop [tm tm ks [3 5 7]]
1169
+ (if-let [k (first ks)]
1170
+ (recur (dissoc! tm k) (next ks))
1171
+ (let [m (persistent! tm)]
1172
+ (assert (= (count m) 7))
1173
+ (assert (= m {0 0 1 1 2 2 4 4 6 6 8 8 9 9}))))))
1174
+ (let [tm (-> (->> (interleave (range 10) (range 10))
1175
+ (apply assoc cljs.core.PersistentArrayMap/EMPTY))
1176
+ (dissoc 3 5 7)
1177
+ transient)]
1178
+ (doseq [k [0 1 2 4 6 8 9]]
1179
+ (assert (= k (get tm k))))
1180
+ (let [m (persistent! tm)]
1181
+ (assert (= 2 (try (dissoc! tm 1) 1 (catch js/Error e 2))))
1182
+ (assert (= 2 (try (assoc! tm 10 10) 1 (catch js/Error e 2))))
1183
+ (assert (= 2 (try (persistent! tm) 1 (catch js/Error e 2))))
1184
+ (assert (= 2 (try (count tm) 1 (catch js/Error e 2))))
1185
+ (assert (= m {0 0 1 1 2 2 4 4 6 6 8 8 9 9}))))
1186
+ (let [m (apply assoc cljs.core.PersistentArrayMap/EMPTY
1187
+ (interleave (range (* 2 array-map-conversion-threshold))
1188
+ (range (* 2 array-map-conversion-threshold))))]
1189
+ (assert (= (count m) (* 2 array-map-conversion-threshold)))
1190
+ (assert (= (m array-map-conversion-threshold) array-map-conversion-threshold))
1191
+ (assert (= m (into cljs.core.PersistentHashMap/EMPTY
1192
+ (map #(vector % %)
1193
+ (range (* 2 array-map-conversion-threshold)))))))
1194
+
1195
+ ;; literal maps
1196
+ (loop [m1 {} m2 {} i 0]
1197
+ (if (< i 100)
1198
+ (recur (assoc m1 i i) (assoc m2 (str "foo" i) i) (inc i))
1199
+ (do (assert (= m1 (into cljs.core.PersistentHashMap/EMPTY
1200
+ (map vector (range 100) (range 100)))))
1201
+ (assert (= m2 (into cljs.core.PersistentHashMap/EMPTY
1202
+ (map vector
1203
+ (map (partial str "foo") (range 100))
1204
+ (range 100)))))
1205
+ (assert (= (count m1) 100))
1206
+ (assert (= (count m2) 100)))))
1207
+
1208
+ ;; TransientHashSet
1209
+ (loop [s (transient #{})
1210
+ i 0]
1211
+ (if (< i 100)
1212
+ (recur (conj! s i) (inc i))
1213
+ (loop [s s i 0]
1214
+ (if (< i 100)
1215
+ (if (zero? (mod i 3))
1216
+ (recur (disj! s i) (inc i))
1217
+ (recur s (inc i)))
1218
+ (let [s (persistent! s)]
1219
+ (assert (= s (loop [s #{} xs (remove #(zero? (mod % 3)) (range 100))]
1220
+ (if-let [x (first xs)]
1221
+ (recur (conj s x) (next xs))
1222
+ s))))
1223
+ (assert (= s (set (remove #(zero? (mod % 3)) (range 100))))))))))
1224
+
1225
+ ;; PersistentTreeMap
1226
+ (let [m1 (sorted-map)
1227
+ c2 (comp - compare)
1228
+ m2 (sorted-map-by c2)]
1229
+ (assert (identical? cljs.core.PersistentTreeMap (type m1)))
1230
+ (assert (identical? cljs.core.PersistentTreeMap (type m2)))
1231
+ (assert (identical? compare (.-comp m1)))
1232
+ (assert (identical? c2 (.-comp m2)))
1233
+ (assert (zero? (count m1)))
1234
+ (assert (zero? (count m2)))
1235
+ (let [m1 (assoc m1 :foo 1 :bar 2 :quux 3)
1236
+ m2 (assoc m2 :foo 1 :bar 2 :quux 3)]
1237
+ (assert (= (count m1) 3))
1238
+ (assert (= (count m2) 3))
1239
+ (assert (= (seq m1) (list [:bar 2] [:foo 1] [:quux 3])))
1240
+ (assert (= (seq m2) (list [:quux 3] [:foo 1] [:bar 2])))
1241
+ (assert (= (seq m1) (rseq m2)))
1242
+ (assert (= (seq m2) (rseq m1)))
1243
+ (assert (= (conj m1 [:wibble 4]) {:foo 1 :bar 2 :quux 3 :wibble 4}))
1244
+ (assert (= (count (conj m1 [:wibble 4])) 4))
1245
+ (assert (= (conj m2 [:wibble 4]) {:foo 1 :bar 2 :quux 3 :wibble 4}))
1246
+ (assert (= (count (conj m2 [:wibble 4])) 4))
1247
+ (assert (= (map key (assoc m1 nil 4)) (list nil :bar :foo :quux)))
1248
+ (assert (= (map key (assoc m2 nil 4)) (list :quux :foo :bar nil)))))
1249
+ (let [m (->> [[0 10] [20 30] [10 20] [50 60] [30 40] [40 50]]
1250
+ (mapcat (partial apply range))
1251
+ (mapcat #(list % %))
1252
+ (apply sorted-map))
1253
+ s1 (map #(vector % %) (range 60))
1254
+ s2 (map #(vector % %) (range 59 -1 -1))]
1255
+ (assert (= (count m) 60))
1256
+ (assert (= (seq m) s1))
1257
+ (assert (= (rseq m) s2)))
1258
+ (let [m (sorted-map :foo 1 :bar 2 :quux 3)]
1259
+ (assert (= (dissoc m :foo) (hash-map :bar 2 :quux 3)))
1260
+ (assert (= (count (dissoc m :foo)) 2))
1261
+ (assert (= (hash m) (hash (hash-map :foo 1 :bar 2 :quux 3))))
1262
+ (assert (= (subseq m < :foo) (list [:bar 2])))
1263
+ (assert (= (subseq m <= :foo) (list [:bar 2] [:foo 1])))
1264
+ (assert (= (subseq m > :foo) (list [:quux 3])))
1265
+ (assert (= (subseq m >= :foo) (list [:foo 1] [:quux 3])))
1266
+ (assert (= (map #(reduce (fn [_ x] x) %) m) (list 2 1 3)))
1267
+ (assert (= (map #(reduce (fn [x _] x) 7 %) m) (list 7 7 7))))
1268
+
1269
+ ;; PersistentTreeSet
1270
+ (let [s1 (sorted-set)
1271
+ c2 (comp - compare)
1272
+ s2 (sorted-set-by c2)
1273
+ c3 #(compare (quot %1 2) (quot %2 2))
1274
+ s3 (sorted-set-by c3)]
1275
+ (assert (identical? cljs.core.PersistentTreeSet (type s1)))
1276
+ (assert (identical? cljs.core.PersistentTreeSet (type s2)))
1277
+ (assert (identical? compare (-comparator s1)))
1278
+ (assert (identical? c2 (-comparator s2)))
1279
+ (assert (zero? (count s1)))
1280
+ (assert (zero? (count s2)))
1281
+ (let [s1 (conj s1 1 2 3)
1282
+ s2 (conj s2 1 2 3)
1283
+ s3 (conj s3 1 2 3)]
1284
+ (assert (= (hash s1) (hash s2)))
1285
+ (assert (= (hash s1) (hash #{1 2 3})))
1286
+ (assert (= (seq s1) (list 1 2 3)))
1287
+ (assert (= (rseq s1) (list 3 2 1)))
1288
+ (assert (= (seq s2) (list 3 2 1)))
1289
+ (assert (= (rseq s2) (list 1 2 3)))
1290
+ (assert (= (count s1) 3))
1291
+ (assert (= (count s2) 3))
1292
+ (assert (= (count s3) 2))
1293
+ (assert (= (get s3 0) 1))
1294
+ (let [s1 (disj s1 2)
1295
+ s2 (disj s2 2)]
1296
+ (assert (= (seq s1) (list 1 3)))
1297
+ (assert (= (rseq s1) (list 3 1)))
1298
+ (assert (= (seq s2) (list 3 1)))
1299
+ (assert (= (rseq s2) (list 1 3)))
1300
+ (assert (= (count s1) 2))
1301
+ (assert (= (count s2) 2)))))
1302
+
1303
+ ;; defrecord
1304
+ (defrecord Person [firstname lastname])
1305
+ (def fred (Person. "Fred" "Mertz"))
1306
+ (assert (= (:firstname fred) "Fred"))
1307
+ (def fred-too (Person. "Fred" "Mertz"))
1308
+ (assert (= fred fred-too))
1309
+ (assert (false? (= fred nil)))
1310
+ (assert (false? (= nil fred)))
1311
+
1312
+ (def ethel (Person. "Ethel" "Mertz" {:married true} {:husband :fred}))
1313
+ (assert (= (meta ethel) {:married true}))
1314
+ (def ethel-too (Person. "Ethel" "Mertz" {:married true} {:husband :fred}))
1315
+ (assert (= ethel ethel-too))
1316
+
1317
+ (assert (= (map->Person {:firstname "Fred" :lastname "Mertz"}) fred))
1318
+ (assert (= (->Person "Fred" "Mertz") fred))
1319
+
1320
+ (assert (= (count fred) 2))
1321
+ (assert (= (count ethel) 3))
1322
+
1323
+ (defrecord A [])
1324
+ (assert (= {:foo 'bar} (meta (with-meta (A.) {:foo 'bar}))))
1325
+ (assert (= 'bar (:foo (assoc (A.) :foo 'bar))))
1326
+
1327
+ (defrecord C [a b c])
1328
+ (def letters (C. "a" "b" "c"))
1329
+ (assert (= (set (keys letters)) #{:a :b :c}))
1330
+ (def more-letters (assoc letters :d "d" :e "e" :f "f"))
1331
+ (assert (= (set (keys more-letters)) #{:a :b :c :d :e :f}))
1332
+ (assert (= (set (keys (dissoc more-letters :d))) #{:a :b :c :e :f}))
1333
+ (assert (= (set (keys (dissoc more-letters :d :e))) #{:a :b :c :f}))
1334
+ (assert (= (set (keys (dissoc more-letters :d :e :f))) #{:a :b :c}))
1335
+
1336
+ ;; ObjMap
1337
+ (let [ks (map (partial str "foo") (range 500))
1338
+ m (apply obj-map (interleave ks (range 500)))]
1339
+ (assert (instance? cljs.core.ObjMap m))
1340
+ (assert (= 500 (count m)))
1341
+ (assert (= 123 (m "foo123"))))
1342
+
1343
+ ;; dot
1344
+ (let [s "abc"]
1345
+ (assert (= 3 (.-length s)))
1346
+ (assert (= 3 (. s -length)))
1347
+ (assert (= 3 (. (str 138) -length)))
1348
+ (assert (= 3 (. "abc" -length)))
1349
+ (assert (= "bc" (.substring s 1)))
1350
+ (assert (= "bc" (.substring "abc" 1)))
1351
+ (assert (= "bc" ((memfn substring start) s 1)))
1352
+ (assert (= "bc" (. s substring 1)))
1353
+ (assert (= "bc" (. s (substring 1))))
1354
+ (assert (= "bc" (. s (substring 1 3))))
1355
+ (assert (= "bc" (.substring s 1 3)))
1356
+ (assert (= "ABC" (. s (toUpperCase))))
1357
+ (assert (= "ABC" (. "abc" (toUpperCase))))
1358
+ (assert (= "ABC" ((memfn toUpperCase) s)))
1359
+ (assert (= "BC" (. (. s (toUpperCase)) substring 1)))
1360
+ (assert (= 2 (.-length (. (. s (toUpperCase)) substring 1)))))
1361
+
1362
+ (assert (= (conj fred {:wife :ethel :friend :ricky})
1363
+ (map->Person {:firstname "Fred" :lastname "Mertz" :wife :ethel :friend :ricky})))
1364
+ (assert (= (conj fred {:lastname "Flintstone"})
1365
+ (map->Person {:firstname "Fred" :lastname "Flintstone"})))
1366
+ (assert (= (assoc fred :lastname "Flintstone")
1367
+ (map->Person {:firstname "Fred" :lastname "Flintstone"})))
1368
+ (assert (= (assoc fred :wife :ethel)
1369
+ (map->Person {:firstname "Fred" :lastname "Mertz" :wife :ethel})))
1370
+ (assert (= (dissoc ethel :husband)
1371
+ (map->Person {:firstname "Ethel" :lastname "Mertz"})))
1372
+
1373
+ (defrecord A [x])
1374
+ (defrecord B [x])
1375
+ (assert (not= (A. nil) (B. nil)))
1376
+
1377
+ (defprotocol IFoo (foo [this]))
1378
+ (assert (= (meta (with-meta (reify IFoo (foo [this] :foo)) {:foo :bar}))
1379
+ {:foo :bar}))
1380
+
1381
+ (defmulti foo2 identity)
1382
+ (defmethod foo2 0 [x] x)
1383
+ (assert (= foo2 (ffirst {foo2 1})))
1384
+
1385
+ (defprotocol IMutate
1386
+ (mutate [this]))
1387
+
1388
+ (deftype Mutate [^:mutable a]
1389
+ IMutate
1390
+ (mutate [_]
1391
+ (set! a 'foo)))
1392
+
1393
+ ;; IFn
1394
+ (deftype FnLike []
1395
+ IFn
1396
+ (-invoke [_] :a)
1397
+ (-invoke [_ a] :b)
1398
+ (-invoke [_ a b] :c))
1399
+
1400
+ (assert (= :a ((FnLike.))))
1401
+ (assert (= :b ((FnLike.) 1)))
1402
+ (assert (= :c ((FnLike.) 1 2)))
1403
+
1404
+ (assert (= [:b :b :b] (map (FnLike.) [0 0 0])))
1405
+
1406
+ (deftype FnLikeB [a]
1407
+ IFn
1408
+ (-invoke [_] a))
1409
+
1410
+ (assert (= 1 ((FnLikeB. 1))))
1411
+
1412
+ ;; hashing bug in many JS runtimes CLJ-118
1413
+ (let [g #{(conj #{:2} :alt)}
1414
+ h #{#{:2 :alt}}]
1415
+ (assert (= g h)))
1416
+ (assert (= (hash {:a 1 :b 2})
1417
+ (hash {:b 2 :a 1})))
1418
+ (assert (= (hash (hash-map :a 1 :b 2))
1419
+ (hash (hash-map :b 2 :a 1))))
1420
+ (assert (= (hash {:start 133 :end 134})
1421
+ (hash (apply hash-map [:start 133 :end 134]))))
1422
+
1423
+ (defprotocol IHasFirst
1424
+ (-get-first [this]))
1425
+
1426
+ (defprotocol IFindsFirst
1427
+ (-find-first [this other]))
1428
+
1429
+ (deftype First [xs]
1430
+ ISeqable
1431
+ (-seq [this] (seq xs))
1432
+ IIndexed
1433
+ (-nth [this i] (nth xs i))
1434
+ (-nth [this i not-found] (nth xs i not-found))
1435
+ IFn
1436
+ (-invoke [[x]] x)
1437
+ (-invoke [this x] this)
1438
+ Object
1439
+ (toString [[x]] (str x))
1440
+ IHasFirst
1441
+ (-get-first [[x]] x)
1442
+ IFindsFirst
1443
+ (-find-first [_ [x]] x))
1444
+
1445
+ (let [fv (First. [1 2 3])
1446
+ fs (First. "asdf")]
1447
+ (assert (= (fv) 1))
1448
+ (assert (= (fs) \a))
1449
+ (assert (= (str fs) \a))
1450
+ (assert (= (-get-first fv) 1))
1451
+ (assert (= (-get-first fs) \a))
1452
+ (assert (= (-find-first fv [1]) 1))
1453
+ (assert (identical? (fv 1) fv)))
1454
+
1455
+ (deftype DestructuringWithLocals [a]
1456
+ IFindsFirst
1457
+ (-find-first [_ [x y]]
1458
+ [x y a]))
1459
+
1460
+ (let [t (DestructuringWithLocals. 1)]
1461
+ (assert (= [2 3 1] (-find-first t [2 3]))))
1462
+
1463
+ (let [x 1]
1464
+ (assert (= (case x 1 :one) :one)))
1465
+ (let [x 1]
1466
+ (assert (= (case x 2 :two :default) :default)))
1467
+ (let [x 1]
1468
+ (assert (= (try
1469
+ (case x 3 :three)
1470
+ (catch js/Error e
1471
+ :fail))
1472
+ :fail)))
1473
+ (let [x 1]
1474
+ (assert (= (case x
1475
+ (1 2 3) :ok
1476
+ :fail)
1477
+ :ok)))
1478
+
1479
+ (let [x [:a :b]]
1480
+ (assert (= (case x
1481
+ [:a :b] :ok)
1482
+ :ok)))
1483
+
1484
+ (let [a 'a]
1485
+ (assert (= (case a
1486
+ nil nil
1487
+ & :amp
1488
+ :none)
1489
+ :none)))
1490
+
1491
+ (let [a '&]
1492
+ (assert (= (case a
1493
+ nil nil
1494
+ & :amp
1495
+ :none)
1496
+ :amp)))
1497
+
1498
+ (let [foo 'a]
1499
+ (assert (= (case foo
1500
+ (a b c) :sym
1501
+ :none)
1502
+ :sym))
1503
+ (assert (= (case foo
1504
+ (b c d) :sym
1505
+ :none)
1506
+ :none)))
1507
+
1508
+ ;; IComparable
1509
+ (assert (= 0 (compare false false)))
1510
+ (assert (= -1 (compare false true)))
1511
+ (assert (= 1 (compare true false)))
1512
+
1513
+ (assert (= -1 (compare 0 1)))
1514
+ (assert (= -1 (compare -1 1)))
1515
+ (assert (= 0 (compare 1 1)))
1516
+ (assert (= 1 (compare 1 0)))
1517
+ (assert (= 1 (compare 1 -1)))
1518
+
1519
+ (assert (= 0 (compare "cljs" "cljs")))
1520
+ (assert (= 0 (compare :cljs :cljs)))
1521
+ (assert (= 0 (compare 'cljs 'cljs)))
1522
+ (assert (= -1 (compare "a" "b")))
1523
+ (assert (= -1 (compare :a :b)))
1524
+ (assert (= -1 (compare 'a 'b)))
1525
+ ;; cases involving ns
1526
+ (assert (= -1 (compare :b/a :c/a)))
1527
+ #_(assert (= -1 (compare :c :a/b)))
1528
+ #_(assert (= 1 (compare :a/b :c)))
1529
+ (assert (= -1 (compare 'b/a 'c/a)))
1530
+ #_(assert (= -1 (compare 'c 'a/b)))
1531
+ #_(assert (= 1 (compare 'a/b 'c)))
1532
+
1533
+ ;; This is different from clj. clj gives -2 next 3 tests
1534
+ (assert (= -1 (compare "a" "c")))
1535
+ (assert (= -1 (compare :a :c)))
1536
+ (assert (= -1 (compare 'a 'c)))
1537
+
1538
+ (assert (= -1 (compare [1 2] [1 1 1])))
1539
+ (assert (= -1 (compare [1 2] [1 2 1])))
1540
+ (assert (= -1 (compare [1 1] [1 2])))
1541
+ (assert (= 0 (compare [1 2] [1 2])))
1542
+ (assert (= 1 (compare [1 2] [1 1])))
1543
+ (assert (= 1 (compare [1 1 1] [1 2])))
1544
+ (assert (= 1 (compare [1 1 2] [1 1 1])))
1545
+
1546
+ (assert (= -1 (compare (subvec [1 2 3] 1) (subvec [1 2 4] 1))))
1547
+ (assert (= 0 (compare (subvec [1 2 3] 1) (subvec [1 2 3] 1))))
1548
+ (assert (= 1 (compare (subvec [1 2 4] 1) (subvec [1 2 3] 1))))
1549
+
1550
+ ;; RSeq
1551
+
1552
+ (assert (= '(3 2 1) (reverse (seq (array 1 2 3)))))
1553
+ (assert (= '(3 2 1) (reverse [1 2 3])))
1554
+ (assert (= '(4 3 2 1) (cons 4 (reverse [1 2 3]))))
1555
+ (assert (= 6 (reduce + (reverse [1 2 3]))))
1556
+ (assert (= '(4 3 2) (map inc (reverse [1 2 3]))))
1557
+ (assert (= '(4 2) (filter even? (reverse [1 2 3 4]))))
1558
+
1559
+ ;; Chunked Sequences
1560
+
1561
+ (assert (= (hash (seq [1 2])) (hash (seq [1 2]))))
1562
+ (assert (= 6 (reduce + (array-chunk (array 1 2 3)))))
1563
+ (assert (instance? ChunkedSeq (seq [1 2 3])))
1564
+ (assert (= '(1 2 3) (seq [1 2 3])))
1565
+ (assert (= '(2 3 4) (map inc [1 2 3])))
1566
+ (assert (= '(2) (filter even? [1 2 3])))
1567
+ (assert (= '(1 3) (filter odd? [1 2 3])))
1568
+ (assert (= '(1 2 3) (concat [1] [2] [3])))
1569
+
1570
+ ;; INext
1571
+
1572
+ (assert (= nil (next nil)))
1573
+ (assert (= nil (next (seq (array 1)))))
1574
+ (assert (= '(2 3) (next (seq (array 1 2 3)))))
1575
+ (assert (= nil (next (reverse (seq (array 1))))))
1576
+ (assert (= '(2 1) (next (reverse (seq (array 1 2 3))))))
1577
+ (assert (= nil (next (cons 1 nil))))
1578
+ (assert (= '(2 3) (next (cons 1 (cons 2 (cons 3 nil))))))
1579
+ (assert (= nil (next (lazy-seq (cons 1 nil)))))
1580
+ (assert (= '(2 3) (next (lazy-seq
1581
+ (cons 1
1582
+ (lazy-seq
1583
+ (cons 2
1584
+ (lazy-seq (cons 3 nil)))))))))
1585
+ (assert (= nil (next (list 1))))
1586
+ (assert (= '(2 3) (next (list 1 2 3))))
1587
+ (assert (= nil (next [1])))
1588
+ (assert (= '(2 3) (next [1 2 3])))
1589
+ (assert (= nil (next (range 1 2))))
1590
+ (assert (= '(2 3) (next (range 1 4))))
1591
+
1592
+ ;; UUID
1593
+
1594
+ (assert (= (UUID. "550e8400-e29b-41d4-a716-446655440000")
1595
+ (UUID. "550e8400-e29b-41d4-a716-446655440000")))
1596
+
1597
+ (assert (not (identical? (UUID. "550e8400-e29b-41d4-a716-446655440000")
1598
+ (UUID. "550e8400-e29b-41d4-a716-446655440000"))))
1599
+
1600
+ (assert (= 42 (get {(UUID. "550e8400-e29b-41d4-a716-446655440000") 42}
1601
+ (UUID. "550e8400-e29b-41d4-a716-446655440000")
1602
+ :not-at-all-found)))
1603
+
1604
+ (assert (= :not-at-all-found
1605
+ (get {(UUID. "550e8400-e29b-41d4-a716-446655440000") 42}
1606
+ (UUID. "666e8400-e29b-41d4-a716-446655440000")
1607
+ :not-at-all-found)))
1608
+
1609
+ ;; Reader literals
1610
+ (assert (= #queue [1] (into cljs.core.PersistentQueue/EMPTY [1])))
1611
+ (assert (not= #queue [1 2] (into cljs.core.PersistentQueue/EMPTY [1])))
1612
+
1613
+ (assert (= #inst "2010-11-12T18:14:15.666-00:00"
1614
+ #inst "2010-11-12T13:14:15.666-05:00"))
1615
+
1616
+ (assert (= #uuid "550e8400-e29b-41d4-a716-446655440000"
1617
+ #uuid "550e8400-e29b-41d4-a716-446655440000"))
1618
+
1619
+ (assert (= 42
1620
+ (get {#uuid "550e8400-e29b-41d4-a716-446655440000" 42}
1621
+ #uuid "550e8400-e29b-41d4-a716-446655440000")))
1622
+
1623
+ ;; pr-str
1624
+
1625
+ (assert (= (pr-str 1) "1"))
1626
+ (assert (= (pr-str -1) "-1"))
1627
+ (assert (= (pr-str -1.5) "-1.5"))
1628
+ (assert (= (pr-str [3 4]) "[3 4]"))
1629
+ (assert (= (pr-str "foo") "\"foo\""))
1630
+ (assert (= (pr-str :hello) ":hello"))
1631
+ (assert (= (pr-str 'goodbye) "goodbye"))
1632
+ (assert (= (pr-str #{1 2 3}) "#{1 2 3}"))
1633
+ (assert (= (pr-str '(7 8 9)) "(7 8 9)"))
1634
+ (assert (= (pr-str '(deref foo)) "(deref foo)"))
1635
+ (assert (= (pr-str '(quote bar)) "(quote bar)"))
1636
+ (assert (= (pr-str 'foo/bar) "foo/bar"))
1637
+ (assert (= (pr-str \a) "\"a\""))
1638
+ (assert (= (pr-str :foo/bar) ":foo/bar"))
1639
+ (assert (= (pr-str nil) "nil"))
1640
+ (assert (= (pr-str true) "true"))
1641
+ (assert (= (pr-str false) "false"))
1642
+ (assert (= (pr-str "string") "\"string\""))
1643
+ (assert (= (pr-str "escape chars \t \r \n \\ \" \b \f") "\"escape chars \\t \\r \\n \\\\ \\\" \\b \\f\""))
1644
+
1645
+ ;;; pr-str records
1646
+
1647
+ (defrecord PrintMe [a b])
1648
+ (assert (= (pr-str (PrintMe. 1 2)) "#PrintMe{:a 1, :b 2}"))
1649
+
1650
+ ;;; pr-str backwards compatibility
1651
+
1652
+ (deftype PrintMeBackwardsCompat [a b]
1653
+ IPrintable
1654
+ (-pr-seq [_ _] (list (str "<<<" a " " b ">>>"))))
1655
+
1656
+ (assert (= (pr-str (PrintMeBackwardsCompat. 1 2)) "<<<1 2>>>"))
1657
+
1658
+ ;;; pr-str inst
1659
+
1660
+ (assert (= (pr-str (js/Date. "2010-11-12T13:14:15.666-05:00"))
1661
+ "#inst \"2010-11-12T18:14:15.666-00:00\""))
1662
+
1663
+ (doseq [month (range 1 13) day (range 1 29) hour (range 1 23)]
1664
+ (let [pad (fn [n]
1665
+ (if (< n 10)
1666
+ (str "0" n)
1667
+ n))
1668
+ inst (str "2010-" (pad month) "-" (pad day) "T" (pad hour) ":14:15.666-00:00")]
1669
+ (assert (= (pr-str (js/Date. inst)) (str "#inst \"" inst "\"")))))
1670
+
1671
+ ;;; pr-str uuid
1672
+
1673
+ (let [uuid-str "550e8400-e29b-41d4-a716-446655440000"
1674
+ uuid (UUID. uuid-str)]
1675
+ (assert (= (pr-str uuid) (str "#uuid \"" uuid-str "\""))))
1676
+
1677
+ :ok
1678
+ )