comparison src/clojure/contrib/test_contrib/test_json.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
comparison
equal deleted inserted replaced
9:35cf337adfcf 10:ef7dbbd6452c
1 (ns clojure.contrib.test-json
2 (:use clojure.test clojure.contrib.json))
3
4 (deftest can-read-from-pushback-reader
5 (let [s (java.io.PushbackReader. (java.io.StringReader. "42"))]
6 (is (= 42 (read-json s)))))
7
8 (deftest can-read-from-reader
9 (let [s (java.io.StringReader. "42")]
10 (is (= 42 (read-json s)))))
11
12 (deftest can-read-numbers
13 (is (= 42 (read-json "42")))
14 (is (= -3 (read-json "-3")))
15 (is (= 3.14159 (read-json "3.14159")))
16 (is (= 6.022e23 (read-json "6.022e23"))))
17
18 (deftest can-read-null
19 (is (= nil (read-json "null"))))
20
21 (deftest can-read-strings
22 (is (= "Hello, World!" (read-json "\"Hello, World!\""))))
23
24 (deftest handles-escaped-slashes-in-strings
25 (is (= "/foo/bar" (read-json "\"\\/foo\\/bar\""))))
26
27 (deftest handles-unicode-escapes
28 (is (= " \u0beb " (read-json "\" \\u0bEb \""))))
29
30 (deftest handles-escaped-whitespace
31 (is (= "foo\nbar" (read-json "\"foo\\nbar\"")))
32 (is (= "foo\rbar" (read-json "\"foo\\rbar\"")))
33 (is (= "foo\tbar" (read-json "\"foo\\tbar\""))))
34
35 (deftest can-read-booleans
36 (is (= true (read-json "true")))
37 (is (= false (read-json "false"))))
38
39 (deftest can-ignore-whitespace
40 (is (= nil (read-json "\r\n null"))))
41
42 (deftest can-read-arrays
43 (is (= [1 2 3] (read-json "[1,2,3]")))
44 (is (= ["Ole" "Lena"] (read-json "[\"Ole\", \r\n \"Lena\"]"))))
45
46 (deftest can-read-objects
47 (is (= {:a 1, :b 2} (read-json "{\"a\": 1, \"b\": 2}"))))
48
49 (deftest can-read-nested-structures
50 (is (= {:a [1 2 {:b [3 "four"]} 5.5]}
51 (read-json "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}"))))
52
53 (deftest disallows-non-string-keys
54 (is (thrown? Exception (read-json "{26:\"z\""))))
55
56 (deftest disallows-barewords
57 (is (thrown? Exception (read-json " foo "))))
58
59 (deftest disallows-unclosed-arrays
60 (is (thrown? Exception (read-json "[1, 2, "))))
61
62 (deftest disallows-unclosed-objects
63 (is (thrown? Exception (read-json "{\"a\":1, "))))
64
65 (deftest can-get-string-keys
66 (is (= {"a" [1 2 {"b" [3 "four"]} 5.5]}
67 (read-json "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}" false true nil))))
68
69 (declare *pass1-string*)
70
71 (deftest pass1-test
72 (let [input (read-json *pass1-string* false true nil)]
73 (is (= "JSON Test Pattern pass1" (first input)))
74 (is (= "array with 1 element" (get-in input [1 "object with 1 member" 0])))
75 (is (= 1234567890 (get-in input [8 "integer"])))
76 (is (= "rosebud" (last input)))))
77
78 ; from http://www.json.org/JSON_checker/test/pass1.json
79 (def *pass1-string*
80 "[
81 \"JSON Test Pattern pass1\",
82 {\"object with 1 member\":[\"array with 1 element\"]},
83 {},
84 [],
85 -42,
86 true,
87 false,
88 null,
89 {
90 \"integer\": 1234567890,
91 \"real\": -9876.543210,
92 \"e\": 0.123456789e-12,
93 \"E\": 1.234567890E+34,
94 \"\": 23456789012E66,
95 \"zero\": 0,
96 \"one\": 1,
97 \"space\": \" \",
98 \"quote\": \"\\\"\",
99 \"backslash\": \"\\\\\",
100 \"controls\": \"\\b\\f\\n\\r\\t\",
101 \"slash\": \"/ & \\/\",
102 \"alpha\": \"abcdefghijklmnopqrstuvwyz\",
103 \"ALPHA\": \"ABCDEFGHIJKLMNOPQRSTUVWYZ\",
104 \"digit\": \"0123456789\",
105 \"0123456789\": \"digit\",
106 \"special\": \"`1~!@#$%^&*()_+-={':[,]}|;.</>?\",
107 \"hex\": \"\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A\",
108 \"true\": true,
109 \"false\": false,
110 \"null\": null,
111 \"array\":[ ],
112 \"object\":{ },
113 \"address\": \"50 St. James Street\",
114 \"url\": \"http://www.JSON.org/\",
115 \"comment\": \"// /* <!-- --\",
116 \"# -- --> */\": \" \",
117 \" s p a c e d \" :[1,2 , 3
118
119 ,
120
121 4 , 5 , 6 ,7 ],\"compact\":[1,2,3,4,5,6,7],
122 \"jsontext\": \"{\\\"object with 1 member\\\":[\\\"array with 1 element\\\"]}\",
123 \"quotes\": \"&#34; \\u0022 %22 0x22 034 &#x22;\",
124 \"\\/\\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?\"
125 : \"A key can be any string\"
126 },
127 0.5 ,98.6
128 ,
129 99.44
130 ,
131
132 1066,
133 1e1,
134 0.1e1,
135 1e-1,
136 1e00,2e+00,2e-00
137 ,\"rosebud\"]")
138
139
140 (deftest can-print-json-strings
141 (is (= "\"Hello, World!\"" (json-str "Hello, World!")))
142 (is (= "\"\\\"Embedded\\\" Quotes\"" (json-str "\"Embedded\" Quotes"))))
143
144 (deftest can-print-unicode
145 (is (= "\"\\u1234\\u4567\"" (json-str "\u1234\u4567"))))
146
147 (deftest can-print-json-null
148 (is (= "null" (json-str nil))))
149
150 (deftest can-print-json-arrays
151 (is (= "[1,2,3]" (json-str [1 2 3])))
152 (is (= "[1,2,3]" (json-str (list 1 2 3))))
153 (is (= "[1,2,3]" (json-str (sorted-set 1 2 3))))
154 (is (= "[1,2,3]" (json-str (seq [1 2 3])))))
155
156 (deftest can-print-java-arrays
157 (is (= "[1,2,3]" (json-str (into-array [1 2 3])))))
158
159 (deftest can-print-empty-arrays
160 (is (= "[]" (json-str [])))
161 (is (= "[]" (json-str (list))))
162 (is (= "[]" (json-str #{}))))
163
164 (deftest can-print-json-objects
165 (is (= "{\"a\":1,\"b\":2}" (json-str (sorted-map :a 1 :b 2)))))
166
167 (deftest object-keys-must-be-strings
168 (is (= "{\"1\":1,\"2\":2") (json-str (sorted-map 1 1 2 2))))
169
170 (deftest can-print-empty-objects
171 (is (= "{}" (json-str {}))))
172
173 (deftest accept-sequence-of-nils
174 (is (= "[null,null,null]" (json-str [nil nil nil]))))
175
176 (deftest error-on-nil-keys
177 (is (thrown? Exception (json-str {nil 1}))))
178
179 (deftest characters-in-symbols-are-escaped
180 (is (= "\"foo\\u1b1b\"" (json-str (symbol "foo\u1b1b")))))
181
182 ;;; Pretty-printer
183
184 (deftest pretty-printing
185 (let [x (read-json *pass1-string* false)]
186 (is (= x (read-json (with-out-str (pprint-json x)) false)))))