Skip to content
Newer
Older
100644 335 lines (287 sloc) 12.2 KB
09f59f7 @dakrone move testing namespace and reformat for whitespace
authored
1 (ns cheshire.test.core
fc4b6f2 @dakrone test keywordizing on stream parsing
authored
2 (:use [clojure.test]
027c545 @dakrone add a better test for inputstream parsing
authored
3 [clojure.java.io :only [file reader]])
45339a4 @dakrone allow re-bindable json/smile factories, with options
authored
4 (:require [cheshire.core :as json]
e02a9ae @dakrone A bit of re-org
authored
5 [cheshire.generate :as gen]
b4ab8e3 @dakrone Allow parsing floats into BigDecimals to retain precision, fixes #16
authored
6 [cheshire.factory :as fact]
7 [cheshire.parse :as parse])
e02a9ae @dakrone A bit of re-org
authored
8 (:import (com.fasterxml.jackson.core JsonGenerationException)
9 (java.io FileInputStream StringReader StringWriter
1e21b7d @dakrone add stream handling (encode/decode)
authored
10 BufferedReader BufferedWriter)
0b2317d @dakrone encode java.sql.Timestamps for easier working with sql, fixes #6
authored
11 (java.sql Timestamp)
04b2ce4 @dakrone add Date and UUID encoding (date encoding uses either the default date
authored
12 (java.util Date UUID)))
74eb30e @dakrone initial commit
authored
13
2fc71f8 @dakrone tests for large numbers now pass, fix the bigint test, update readme …
authored
14 (def test-obj {"int" 3 "long" (long -2147483647) "boolean" true
15 "LongObj" (Long/parseLong "2147483647") "double" 1.23
16 "nil" nil "string" "string" "vec" [1 2 3] "map" {"a" "b"}
6087d7f @dakrone Add support for encoding Short and Byte objects
authored
17 "list" (list "a" "b") "short" (short 21) "byte" (byte 3)})
2fc71f8 @dakrone tests for large numbers now pass, fix the bigint test, update readme …
authored
18
7525dea @dakrone Added support for clojure Ratio type.
authored
19 (deftest t-ratio
20 (let [n 1/2]
21 (is (= (double n) (:num (json/decode (json/encode {:num n}) true))))))
22
20545fa @dakrone Mirror the Long test from custom to core
authored
23 (deftest t-long-wrap-around
24 (is (= 2147483648 (json/decode (json/encode 2147483648)))))
25
2fc71f8 @dakrone tests for large numbers now pass, fix the bigint test, update readme …
authored
26 (deftest t-bigint
27 (let [n 9223372036854775808]
28 (is (= n (:num (json/decode (json/encode {:num n}) true))))))
fca4fbb @dakrone support encoding #{} and clojure symbols
authored
29
db755c4 @dakrone handle BigInteger encoding on clojure 1.3
authored
30 (deftest t-biginteger
31 (let [n (BigInteger. "42")]
32 (is (= n (:num (json/decode (json/encode {:num n}) true))))))
33
de849ce @CmdrDats Add support for BigDecimal
CmdrDats authored
34 (deftest t-bigdecimal
35 (let [n (BigDecimal. "42.5")]
b4ab8e3 @dakrone Allow parsing floats into BigDecimals to retain precision, fixes #16
authored
36 (is (= (.doubleValue n) (:num (json/decode (json/encode {:num n}) true))))
37 (binding [parse/*use-bigdecimals?* true]
38 (is (= n (:num (json/decode (json/encode {:num n}) true)))))))
de849ce @CmdrDats Add support for BigDecimal
CmdrDats authored
39
74eb30e @dakrone initial commit
authored
40 (deftest test-string-round-trip
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
41 (is (= test-obj (json/decode (json/encode test-obj)))))
74eb30e @dakrone initial commit
authored
42
43 (deftest test-generate-accepts-float
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
44 (is (= "3.14" (json/encode 3.14))))
09f59f7 @dakrone move testing namespace and reformat for whitespace
authored
45
fca4fbb @dakrone support encoding #{} and clojure symbols
authored
46 (deftest test-keyword-encode
47 (is (= {"key" "val"}
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
48 (json/decode (json/encode {:key "val"})))))
fca4fbb @dakrone support encoding #{} and clojure symbols
authored
49
50 (deftest test-generate-set
51 (is (= {"set" ["a" "b"]}
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
52 (json/decode (json/encode {"set" #{"a" "b"}})))))
fca4fbb @dakrone support encoding #{} and clojure symbols
authored
53
10062af @dlebrero empty sets are serialized to empty arrays instead of null. This is co…
dlebrero authored
54 (deftest test-generate-empty-set
55 (is (= {"set" []}
56 (json/decode (json/encode {"set" #{}})))))
57
58 (deftest test-generate-empty-array
59 (is (= {"array" []}
60 (json/decode (json/encode {"array" []})))))
61
74eb30e @dakrone initial commit
authored
62 (deftest test-key-coercion
63 (is (= {"foo" "bar" "1" "bat" "2" "bang" "3" "biz"}
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
64 (json/decode
65 (json/encode
09f59f7 @dakrone move testing namespace and reformat for whitespace
authored
66 {:foo "bar" 1 "bat" (long 2) "bang" (bigint 3) "biz"})))))
74eb30e @dakrone initial commit
authored
67
68 (deftest test-keywords
69 (is (= {:foo "bar" :bat 1}
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
70 (json/decode (json/encode {:foo "bar" :bat 1}) true))))
74eb30e @dakrone initial commit
authored
71
fca4fbb @dakrone support encoding #{} and clojure symbols
authored
72 (deftest test-symbols
73 (is (= {"foo" "clojure.core/map"}
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
74 (json/decode (json/encode {"foo" 'clojure.core/map})))))
fca4fbb @dakrone support encoding #{} and clojure symbols
authored
75
d2a9ad8 Add support for java.util.{Map,List,Set}
Nick Dimiduk authored
76 (deftest test-accepts-java-map
77 (is (= {"foo" 1}
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
78 (json/decode
79 (json/encode (doto (java.util.HashMap.) (.put "foo" 1)))))))
d2a9ad8 Add support for java.util.{Map,List,Set}
Nick Dimiduk authored
80
81 (deftest test-accepts-java-list
82 (is (= [1 2 3]
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
83 (json/decode (json/encode (doto (java.util.ArrayList. 3)
84 (.add 1)
85 (.add 2)
86 (.add 3)))))))
d2a9ad8 Add support for java.util.{Map,List,Set}
Nick Dimiduk authored
87
88 (deftest test-accepts-java-set
89 (is (= {"set" [1 2 3]}
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
90 (json/decode (json/encode {"set" (doto (java.util.HashSet. 3)
91 (.add 1)
92 (.add 2)
93 (.add 3))})))))
d2a9ad8 Add support for java.util.{Map,List,Set}
Nick Dimiduk authored
94
10062af @dlebrero empty sets are serialized to empty arrays instead of null. This is co…
dlebrero authored
95 (deftest test-accepts-empty-java-set
96 (is (= {"set" []}
3998b55 @dakrone remove extra test that was doing nothing (being overwritten)
authored
97 (json/decode (json/encode {"set" (java.util.HashSet. 3)})))))
10062af @dlebrero empty sets are serialized to empty arrays instead of null. This is co…
dlebrero authored
98
98ff87b @zk Added guard against NPE in cheshire.core/parse-string.
zk authored
99 (deftest test-nil
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
100 (is (nil? (json/decode nil true))))
98ff87b @zk Added guard against NPE in cheshire.core/parse-string.
zk authored
101
74eb30e @dakrone initial commit
authored
102 (deftest test-parsed-seq
103 (let [br (BufferedReader. (StringReader. "1\n2\n3\n"))]
104 (is (= (list 1 2 3) (json/parsed-seq br)))))
105
106 (deftest test-smile-round-trip
fca4fbb @dakrone support encoding #{} and clojure symbols
authored
107 (is (= test-obj (json/parse-smile (json/generate-smile test-obj)))))
76186a8 @dakrone helpers for people coming from clojure-json
authored
108
109 (deftest test-aliases
110 (is (= {"foo" "bar" "1" "bat" "2" "bang" "3" "biz"}
111 (json/decode
112 (json/encode
113 {:foo "bar" 1 "bat" (long 2) "bang" (bigint 3) "biz"})))))
04b2ce4 @dakrone add Date and UUID encoding (date encoding uses either the default date
authored
114
115 (deftest test-date
116 (is (= {"foo" "1970-01-01T00:00:00Z"}
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
117 (json/decode (json/encode {:foo (Date. (long 0))}))))
04b2ce4 @dakrone add Date and UUID encoding (date encoding uses either the default date
authored
118 (is (= {"foo" "1970-01-01"}
20ebaa8 @drewr Add optional default pretty-printer to json generator.
drewr authored
119 (json/decode (json/encode {:foo (Date. (long 0))}
a164a75 @dakrone Don't auto-convert multi-args into a map, just pass a map
authored
120 {:date-format "yyyy-MM-dd"})))
04b2ce4 @dakrone add Date and UUID encoding (date encoding uses either the default date
authored
121 "encode with given date format"))
122
0b2317d @dakrone encode java.sql.Timestamps for easier working with sql, fixes #6
authored
123 (deftest test-sql-timestamp
124 (is (= {"foo" "1970-01-01T00:00:00Z"}
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
125 (json/decode (json/encode {:foo (Timestamp. (long 0))}))))
0b2317d @dakrone encode java.sql.Timestamps for easier working with sql, fixes #6
authored
126 (is (= {"foo" "1970-01-01"}
20ebaa8 @drewr Add optional default pretty-printer to json generator.
drewr authored
127 (json/decode (json/encode {:foo (Timestamp. (long 0))}
a164a75 @dakrone Don't auto-convert multi-args into a map, just pass a map
authored
128 {:date-format "yyyy-MM-dd"})))
0b2317d @dakrone encode java.sql.Timestamps for easier working with sql, fixes #6
authored
129 "encode with given date format"))
130
04b2ce4 @dakrone add Date and UUID encoding (date encoding uses either the default date
authored
131 (deftest test-uuid
132 (let [id (UUID/randomUUID)
133 id-str (str id)]
eba96f9 @dakrone prefer encode/decode over generate-string/parse-string in tests
authored
134 (is (= {"foo" id-str} (json/decode (json/encode {:foo id}))))))
1e21b7d @dakrone add stream handling (encode/decode)
authored
135
8d6123d @dakrone Add default encoder for java.lang.Character
authored
136 (deftest test-char-literal
137 (is (= "{\"foo\":\"a\"}" (json/encode {:foo \a}))))
138
1e21b7d @dakrone add stream handling (encode/decode)
authored
139 (deftest test-streams
140 (is (= {"foo" "bar"}
141 (json/parse-stream
142 (BufferedReader. (StringReader. "{\"foo\":\"bar\"}\n")))))
143 (let [sw (StringWriter.)
144 bw (BufferedWriter. sw)]
145 (json/generate-stream {"foo" "bar"} bw)
fc4b6f2 @dakrone test keywordizing on stream parsing
authored
146 (is (= "{\"foo\":\"bar\"}" (.toString sw))))
147 (is (= {(keyword "foo baz") "bar"}
148 (with-open [rdr (StringReader. "{\"foo baz\":\"bar\"}\n")]
149 (json/parse-stream rdr true)))))
1e21b7d @dakrone add stream handling (encode/decode)
authored
150
81e517f @kostafey Add sequential writing
kostafey authored
151 (deftest serial-writing
4014983 @kostafey Add more tests for sequential writing.
kostafey authored
152 (is (= "[\"foo\",\"bar\"]"
153 (.toString
154 (json/with-writer [(StringWriter.) nil]
155 (json/write [] :start)
156 (json/write "foo")
157 (json/write "bar")
158 (json/write [] :end)))))
159 (is (= "[1,[2,3],4]"
160 (.toString
161 (json/with-writer [(StringWriter.) nil]
162 (json/write [1 [2]] :start-inner)
163 (json/write 3)
164 (json/write [] :end)
165 (json/write 4)
166 (json/write [] :end)))))
9270b16 @kostafey Fix test for sequential writing.
kostafey authored
167 (is (= "{\"a\":1,\"b\":2,\"c\":3}"
4014983 @kostafey Add more tests for sequential writing.
kostafey authored
168 (.toString
169 (json/with-writer [(StringWriter.) nil]
170 (json/write {:a 1} :start)
171 (json/write {:b 2} :bare)
9270b16 @kostafey Fix test for sequential writing.
kostafey authored
172 (json/write {:c 3} :end)))))
4014983 @kostafey Add more tests for sequential writing.
kostafey authored
173 (is (= (str "[\"start\",\"continue\",[\"implicitly-nested\"],"
174 "[\"explicitly-nested\"],\"flatten\",\"end\"]")
175 (.toString
176 (json/with-writer [(StringWriter.) nil]
177 (json/write ["start"] :start)
178 (json/write "continue")
179 (json/write ["implicitly-nested"])
180 (json/write ["explicitly-nested"] :all)
181 (json/write ["flatten"] :bare)
182 (json/write ["end"] :end)))))
81e517f @kostafey Add sequential writing
kostafey authored
183 (is (= "{\"head\":\"head info\",\"data\":[1,2,3],\"tail\":\"tail info\"}"
184 (.toString
185 (json/with-writer [(StringWriter.) nil]
4014983 @kostafey Add more tests for sequential writing.
kostafey authored
186 (json/write {:head "head info" :data []} :start-inner)
81e517f @kostafey Add sequential writing
kostafey authored
187 (json/write 1)
188 (json/write 2)
189 (json/write 3)
190 (json/write [] :end)
191 (json/write {:tail "tail info"} :end))))))
192
cb1b989 @dakrone Add a test for parsing multiple objects in a stream
authored
193 (deftest test-multiple-objs-in-file
194 (is (= {"one" 1, "foo" "bar"}
195 (first (json/parsed-seq (reader "test/multi.json")))))
196 (is (= {"two" 2, "foo" "bar"}
027c545 @dakrone add a better test for inputstream parsing
authored
197 (second (json/parsed-seq (reader "test/multi.json")))))
198 (with-open [s (FileInputStream. (file "test/multi.json"))]
199 (let [r (reader s)]
200 (is (= [{"one" 1, "foo" "bar"} {"two" 2, "foo" "bar"}]
201 (json/parsed-seq r))))))
cb1b989 @dakrone Add a test for parsing multiple objects in a stream
authored
202
1e21b7d @dakrone add stream handling (encode/decode)
authored
203 (deftest test-jsondotorg-pass1
204 (let [string (slurp "test/pass1.json")
205 decoded-json (json/decode string)
206 encoded-json (json/encode decoded-json)
207 re-decoded-json (json/decode encoded-json)]
208 (is (= decoded-json re-decoded-json))))
0e20493 @hiredman time tests
hiredman authored
209
8c686c2 @dakrone write namespace for qualified keywords; :user/foo -> "user/foo"
authored
210 (deftest test-namespaced-keywords
211 (is (= "{\"foo\":\"user/bar\"}"
9b2c3f4 @dakrone fix test suites, back to test parity between core and custom, better
authored
212 (json/encode {:foo :user/bar})))
213 (is (= {:foo/bar "baz/eggplant"}
214 (json/decode (json/encode {:foo/bar :baz/eggplant}) true))))
434099d @sbtourist Implemented function-based custom array coercion.
sbtourist authored
215
216 (deftest test-array-coerce-fn
ba11b2f @dakrone Test that array coercion doesn't interfere with maps
authored
217 (is (= {"set" #{"a" "b"} "array" ["a" "b"] "map" {"a" 1}}
b0f7e28 @dakrone 80-character line fix
authored
218 (json/decode
ba11b2f @dakrone Test that array coercion doesn't interfere with maps
authored
219 (json/encode {"set" #{"a" "b"} "array" ["a" "b"] "map" {"a" 1}}) false
b0f7e28 @dakrone 80-character line fix
authored
220 (fn [field-name] (if (= "set" field-name) #{} []))))))
eaee529 @dakrone Encode symbols that are non-resolvable as strings
authored
221
222 (deftest t-symbol-encoding-for-non-resolvable-symbols
c7b8a10 @dakrone Bump jackson and clojure deps and fix tests for new hashing in clojur…
authored
223 (is (= "{\"bar\":\"clojure.core/pam\",\"foo\":\"clojure.core/map\"}"
224 (json/encode (sorted-map :foo 'clojure.core/map :bar 'clojure.core/pam))))
225 (is (= "{\"bar\":\"clojure.core/pam\",\"foo\":\"foo.bar/baz\"}"
226 (json/encode (sorted-map :foo 'foo.bar/baz :bar 'clojure.core/pam)))))
45339a4 @dakrone allow re-bindable json/smile factories, with options
authored
227
228 (deftest t-bindable-factories
229 (binding [fact/*json-factory* (fact/make-json-factory
230 {:allow-non-numeric-numbers true})]
231 (is (= (type Double/NaN)
232 (type (:foo (json/decode "{\"foo\":NaN}" true)))))))
e16ec6a @dakrone Add a test for Clojure's PersistentQueue
authored
233
234 (deftest t-persistent-queue
235 (let [q (conj (clojure.lang.PersistentQueue/EMPTY) 1 2 3)]
236 (is (= q (json/decode (json/encode q))))))
3b0c869 @dakrone add tests for namespaced keywords
authored
237
20ebaa8 @drewr Add optional default pretty-printer to json generator.
drewr authored
238 (deftest t-pretty-print
c7b8a10 @dakrone Bump jackson and clojure deps and fix tests for new hashing in clojur…
authored
239 (is (= (str "{\n \"bar\" : [ {\n \"baz\" : 2\n }, "
240 "\"quux\", [ 1, 2, 3 ] ],\n \"foo\" : 1\n}")
241 (json/encode (sorted-map :foo 1 :bar [{:baz 2} :quux [1 2 3]])
a164a75 @dakrone Don't auto-convert multi-args into a map, just pass a map
authored
242 {:pretty true}))))
bacb3ac @dakrone Add the ability to specify non-ascii escaping if desired
authored
243
244 (deftest t-unicode-escaping
245 (is (= "{\"foo\":\"It costs \\u00A3100\"}"
246 (json/encode {:foo "It costs £100"} {:escape-non-ascii true}))))
52a700b @dakrone allow a custom keyword fn to munge JSON keys
authored
247
248 (deftest t-custom-keyword-fn
249 (is (= {:FOO "bar"} (json/decode "{\"foo\": \"bar\"}"
250 (fn [k] (keyword (.toUpperCase k))))))
251 (is (= {"foo" "bar"} (json/decode "{\"foo\": \"bar\"}" nil)))
252 (is (= {"foo" "bar"} (json/decode "{\"foo\": \"bar\"}" false)))
253 (is (= {:foo "bar"} (json/decode "{\"foo\": \"bar\"}" true))))
e02a9ae @dakrone A bit of re-org
authored
254
4c13026 @goodwink Fix namespaced keyword conversion, add test and benchmark.
goodwink authored
255 (deftest t-custom-encode-key-fn
256 (is (= "{\"FOO\":\"bar\"}"
4f817c0 @dakrone whitespace and formatting fixes
authored
257 (json/encode {:foo :bar}
258 {:key-fn (fn [k] (.toUpperCase (name k)))}))))
4c13026 @goodwink Fix namespaced keyword conversion, add test and benchmark.
goodwink authored
259
e02a9ae @dakrone A bit of re-org
authored
260 (deftest test-add-remove-encoder
261 (gen/remove-encoder java.net.URL)
262 (gen/add-encoder java.net.URL gen/encode-str)
263 (is (= "\"http://foo.com\""
264 (json/encode (java.net.URL. "http://foo.com"))))
265 (gen/remove-encoder java.net.URL)
266 (is (thrown? JsonGenerationException
267 (json/encode (java.net.URL. "http://foo.com")))))
268
6f1854c @dakrone Allow custom encoders to take precedence for protocols; addresses add…
authored
269 (defprotocol TestP
270 (foo [this] "foo method"))
271
272 (defrecord TestR [state])
273
274 (extend TestR
275 TestP
276 {:foo (constantly "bar")})
277
278 (deftest t-custom-protocol-encoder
279 (let [rec (TestR. :quux)]
280 (is (= {:state "quux"} (json/decode (json/encode rec) true)))
281 (gen/add-encoder cheshire.test.core.TestR
282 (fn [obj jg]
283 (.writeString jg (foo obj))))
284 (is (= "bar" (json/decode (json/encode rec))))
285 (gen/remove-encoder cheshire.test.core.TestR)
286 (is (= {:state "quux"} (json/decode (json/encode rec) true)))))
7abc16c @dakrone add tests for all custom helpers
authored
287
288 (defprotocol CTestP
289 (thing [this] "thing method"))
290 (defrecord CTestR [state])
291 (extend CTestR
292 CTestP
293 {:thing (constantly "thing")})
294
295 (deftest t-custom-helpers
296 (let [thing (CTestR. :state)
297 remove #(gen/remove-encoder CTestR)]
298 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-nil nil jg)))
299 (is (= nil (json/decode (json/encode thing) true)))
300 (remove)
301 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-str "foo" jg)))
302 (is (= "foo" (json/decode (json/encode thing) true)))
303 (remove)
304 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-number 5 jg)))
305 (is (= 5 (json/decode (json/encode thing) true)))
306 (remove)
307 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-long 4 jg)))
308 (is (= 4 (json/decode (json/encode thing) true)))
309 (remove)
310 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-int 3 jg)))
311 (is (= 3 (json/decode (json/encode thing) true)))
312 (remove)
313 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-ratio 1/2 jg)))
314 (is (= 0.5 (json/decode (json/encode thing) true)))
315 (remove)
316 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-seq [:foo :bar] jg)))
317 (is (= ["foo" "bar"] (json/decode (json/encode thing) true)))
318 (remove)
998bf7a @dakrone fix "YYYY" not being a valid date format on openjdk 6
authored
319 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-date (Date. (long 0)) jg)))
320 (binding [gen/*date-format* "yyyy-MM-dd'T'HH:mm:ss'Z'"]
321 (is (= "1970-01-01T00:00:00Z" (json/decode (json/encode thing) true))))
7abc16c @dakrone add tests for all custom helpers
authored
322 (remove)
323 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-bool true jg)))
324 (is (= true (json/decode (json/encode thing) true)))
325 (remove)
326 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-named :foo jg)))
327 (is (= "foo" (json/decode (json/encode thing) true)))
328 (remove)
329 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-map {:foo "bar"} jg)))
330 (is (= {:foo "bar"} (json/decode (json/encode thing) true)))
331 (remove)
332 (gen/add-encoder CTestR (fn [obj jg] (gen/encode-symbol 'foo jg)))
333 (is (= "foo" (json/decode (json/encode thing) true)))
334 (remove)))
Something went wrong with that request. Please try again.