diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/test_contrib/test_json.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,186 @@
     1.4 +(ns clojure.contrib.test-json
     1.5 +  (:use clojure.test clojure.contrib.json))
     1.6 +
     1.7 +(deftest can-read-from-pushback-reader
     1.8 +  (let [s (java.io.PushbackReader. (java.io.StringReader. "42"))]
     1.9 +    (is (= 42 (read-json s)))))
    1.10 +
    1.11 +(deftest can-read-from-reader
    1.12 +  (let [s (java.io.StringReader. "42")]
    1.13 +    (is (= 42 (read-json s)))))
    1.14 +
    1.15 +(deftest can-read-numbers
    1.16 +  (is (= 42 (read-json "42")))
    1.17 +  (is (= -3 (read-json "-3")))
    1.18 +  (is (= 3.14159 (read-json "3.14159")))
    1.19 +  (is (= 6.022e23 (read-json "6.022e23"))))
    1.20 +
    1.21 +(deftest can-read-null
    1.22 +  (is (= nil (read-json "null"))))
    1.23 +
    1.24 +(deftest can-read-strings
    1.25 +  (is (= "Hello, World!" (read-json "\"Hello, World!\""))))
    1.26 +
    1.27 +(deftest handles-escaped-slashes-in-strings
    1.28 +  (is (= "/foo/bar" (read-json "\"\\/foo\\/bar\""))))
    1.29 +
    1.30 +(deftest handles-unicode-escapes
    1.31 +  (is (= " \u0beb " (read-json "\" \\u0bEb \""))))
    1.32 +
    1.33 +(deftest handles-escaped-whitespace
    1.34 +  (is (= "foo\nbar" (read-json "\"foo\\nbar\"")))
    1.35 +  (is (= "foo\rbar" (read-json "\"foo\\rbar\"")))
    1.36 +  (is (= "foo\tbar" (read-json "\"foo\\tbar\""))))
    1.37 +
    1.38 +(deftest can-read-booleans
    1.39 +  (is (= true (read-json "true")))
    1.40 +  (is (= false (read-json "false"))))
    1.41 +
    1.42 +(deftest can-ignore-whitespace
    1.43 +  (is (= nil (read-json "\r\n   null"))))
    1.44 +
    1.45 +(deftest can-read-arrays
    1.46 +  (is (= [1 2 3] (read-json "[1,2,3]")))
    1.47 +  (is (= ["Ole" "Lena"] (read-json "[\"Ole\", \r\n \"Lena\"]"))))
    1.48 +
    1.49 +(deftest can-read-objects
    1.50 +  (is (= {:a 1, :b 2} (read-json "{\"a\": 1, \"b\": 2}"))))
    1.51 +
    1.52 +(deftest can-read-nested-structures
    1.53 +  (is (= {:a [1 2 {:b [3 "four"]} 5.5]}
    1.54 +         (read-json "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}"))))
    1.55 +
    1.56 +(deftest disallows-non-string-keys
    1.57 +  (is (thrown? Exception (read-json "{26:\"z\""))))
    1.58 +
    1.59 +(deftest disallows-barewords
    1.60 +  (is (thrown? Exception (read-json "  foo  "))))
    1.61 +
    1.62 +(deftest disallows-unclosed-arrays
    1.63 +  (is (thrown? Exception (read-json "[1, 2,  "))))
    1.64 +
    1.65 +(deftest disallows-unclosed-objects
    1.66 +  (is (thrown? Exception (read-json "{\"a\":1,  "))))
    1.67 +
    1.68 +(deftest can-get-string-keys
    1.69 +  (is (= {"a" [1 2 {"b" [3 "four"]} 5.5]}
    1.70 +         (read-json "{\"a\":[1,2,{\"b\":[3,\"four\"]},5.5]}" false true nil))))
    1.71 +
    1.72 +(declare *pass1-string*)
    1.73 +
    1.74 +(deftest pass1-test
    1.75 +  (let [input (read-json *pass1-string* false true nil)]
    1.76 +    (is (= "JSON Test Pattern pass1" (first input)))
    1.77 +    (is (= "array with 1 element" (get-in input [1 "object with 1 member" 0])))
    1.78 +    (is (= 1234567890 (get-in input [8 "integer"])))
    1.79 +    (is (= "rosebud" (last input)))))
    1.80 +
    1.81 +; from http://www.json.org/JSON_checker/test/pass1.json
    1.82 +(def *pass1-string*
    1.83 +     "[
    1.84 +    \"JSON Test Pattern pass1\",
    1.85 +    {\"object with 1 member\":[\"array with 1 element\"]},
    1.86 +    {},
    1.87 +    [],
    1.88 +    -42,
    1.89 +    true,
    1.90 +    false,
    1.91 +    null,
    1.92 +    {
    1.93 +        \"integer\": 1234567890,
    1.94 +        \"real\": -9876.543210,
    1.95 +        \"e\": 0.123456789e-12,
    1.96 +        \"E\": 1.234567890E+34,
    1.97 +        \"\":  23456789012E66,
    1.98 +        \"zero\": 0,
    1.99 +        \"one\": 1,
   1.100 +        \"space\": \" \",
   1.101 +        \"quote\": \"\\\"\",
   1.102 +        \"backslash\": \"\\\\\",
   1.103 +        \"controls\": \"\\b\\f\\n\\r\\t\",
   1.104 +        \"slash\": \"/ & \\/\",
   1.105 +        \"alpha\": \"abcdefghijklmnopqrstuvwyz\",
   1.106 +        \"ALPHA\": \"ABCDEFGHIJKLMNOPQRSTUVWYZ\",
   1.107 +        \"digit\": \"0123456789\",
   1.108 +        \"0123456789\": \"digit\",
   1.109 +        \"special\": \"`1~!@#$%^&*()_+-={':[,]}|;.</>?\",
   1.110 +        \"hex\": \"\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A\",
   1.111 +        \"true\": true,
   1.112 +        \"false\": false,
   1.113 +        \"null\": null,
   1.114 +        \"array\":[  ],
   1.115 +        \"object\":{  },
   1.116 +        \"address\": \"50 St. James Street\",
   1.117 +        \"url\": \"http://www.JSON.org/\",
   1.118 +        \"comment\": \"// /* <!-- --\",
   1.119 +        \"# -- --> */\": \" \",
   1.120 +        \" s p a c e d \" :[1,2 , 3
   1.121 +
   1.122 +,
   1.123 +
   1.124 +4 , 5        ,          6           ,7        ],\"compact\":[1,2,3,4,5,6,7],
   1.125 +        \"jsontext\": \"{\\\"object with 1 member\\\":[\\\"array with 1 element\\\"]}\",
   1.126 +        \"quotes\": \"&#34; \\u0022 %22 0x22 034 &#x22;\",
   1.127 +        \"\\/\\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?\"
   1.128 +: \"A key can be any string\"
   1.129 +    },
   1.130 +    0.5 ,98.6
   1.131 +,
   1.132 +99.44
   1.133 +,
   1.134 +
   1.135 +1066,
   1.136 +1e1,
   1.137 +0.1e1,
   1.138 +1e-1,
   1.139 +1e00,2e+00,2e-00
   1.140 +,\"rosebud\"]")
   1.141 +
   1.142 +
   1.143 +(deftest can-print-json-strings
   1.144 +  (is (= "\"Hello, World!\"" (json-str "Hello, World!")))
   1.145 +  (is (= "\"\\\"Embedded\\\" Quotes\"" (json-str "\"Embedded\" Quotes"))))
   1.146 +
   1.147 +(deftest can-print-unicode
   1.148 +  (is (= "\"\\u1234\\u4567\"" (json-str "\u1234\u4567"))))
   1.149 +
   1.150 +(deftest can-print-json-null
   1.151 +  (is (= "null" (json-str nil))))
   1.152 +
   1.153 +(deftest can-print-json-arrays
   1.154 +  (is (= "[1,2,3]" (json-str [1 2 3])))
   1.155 +  (is (= "[1,2,3]" (json-str (list 1 2 3))))
   1.156 +  (is (= "[1,2,3]" (json-str (sorted-set 1 2 3))))
   1.157 +  (is (= "[1,2,3]" (json-str (seq [1 2 3])))))
   1.158 +
   1.159 +(deftest can-print-java-arrays
   1.160 + (is (= "[1,2,3]" (json-str (into-array [1 2 3])))))
   1.161 +
   1.162 +(deftest can-print-empty-arrays
   1.163 +  (is (= "[]" (json-str [])))
   1.164 +  (is (= "[]" (json-str (list))))
   1.165 +  (is (= "[]" (json-str #{}))))
   1.166 +
   1.167 +(deftest can-print-json-objects
   1.168 +  (is (= "{\"a\":1,\"b\":2}" (json-str (sorted-map :a 1 :b 2)))))
   1.169 +
   1.170 +(deftest object-keys-must-be-strings
   1.171 +  (is (= "{\"1\":1,\"2\":2") (json-str (sorted-map 1 1 2 2))))
   1.172 +
   1.173 +(deftest can-print-empty-objects
   1.174 +  (is (= "{}" (json-str {}))))
   1.175 +
   1.176 +(deftest accept-sequence-of-nils
   1.177 +  (is (= "[null,null,null]" (json-str [nil nil nil]))))
   1.178 +
   1.179 +(deftest error-on-nil-keys
   1.180 +  (is (thrown? Exception (json-str {nil 1}))))
   1.181 +
   1.182 +(deftest characters-in-symbols-are-escaped
   1.183 +  (is (= "\"foo\\u1b1b\"" (json-str (symbol "foo\u1b1b")))))
   1.184 +
   1.185 +;;; Pretty-printer
   1.186 +
   1.187 +(deftest pretty-printing
   1.188 +  (let [x (read-json *pass1-string* false)]
   1.189 +    (is (= x (read-json (with-out-str (pprint-json x)) false)))))