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