|
10 | 10 | (assert= (json/read-string "{\"foo\": 42, \"bar\": [\"baz\"]}") |
11 | 11 | {"foo" 42, "bar" ["baz"]})) |
12 | 12 |
|
13 | | -(deftest test-write-string |
14 | | - (assert= (json/write-string {:strings {:a \a, :b "b", :c "", :d :d} |
15 | | - :numbers {:one 1, :two 2.0, :three 3/1} |
16 | | - :lists {:empty {:vector [], :list '(), :seq (take 0 (range))} |
17 | | - :non-empty {:vector [1 2 3], :list '(1 2 3), :seq (take 3 (range))}} |
18 | | - :maps {:empty {}, :non-empty {:this "is covered, right? ;)"}}}) |
19 | | - "{ \"numbers\": { \"three\": 3, \"two\": 2.000000, \"one\": 1 }, \"lists\": { \"empty\": { \"vector\": [], \"list\": [], \"seq\": [] }, \"non-empty\": { \"vector\": [ 1, 2, 3 ], \"list\": [ 1, 2, 3 ], \"seq\": [ 0, 1, 2 ] } }, \"maps\": { \"empty\": {}, \"non-empty\": { \"this\": \"is covered, right? ;)\" } }, \"strings\": { \"d\": \"d\", \"a\": \"a\", \"c\": \"\", \"b\": \"b\" } }")) |
| 13 | +(deftest test-strings |
| 14 | + (assert-table [x y] (assert= (json/write-string x) y) |
| 15 | + \a "\"a\"" |
| 16 | + "foo" "\"foo\"" |
| 17 | + :bar "\"bar\"" |
| 18 | + "" "\"\"")) |
| 19 | + |
| 20 | +(deftest test-numbers |
| 21 | + (assert-table [x y] (assert= (json/write-string x) y) |
| 22 | + 1 "1" |
| 23 | + 1.0 "1.000000" |
| 24 | + 0.1 "0.100000" |
| 25 | + 1.1 "1.100000" |
| 26 | + 1234.5678 "1234.567800" |
| 27 | + -1 "-1" |
| 28 | + -0.1 "-0.100000" |
| 29 | + -1.1 "-1.100000" |
| 30 | + -1234.5678 "-1234.567800" |
| 31 | + 1e1 "10.000000" |
| 32 | + 3/1 "3")) |
| 33 | + |
| 34 | +(deftest test-vectors |
| 35 | + (assert-table [x y] (assert= (json/write-string x) y) |
| 36 | + [] "[]" |
| 37 | + [nil] "[null]" |
| 38 | + [1 2] "[1, 2]" |
| 39 | + [1 1.0 nil] "[1, 1.000000, null]" |
| 40 | + ["foo" 42] "[\"foo\", 42]")) |
| 41 | + |
| 42 | +(deftest test-seqs |
| 43 | + (assert-table [x y] (assert= (json/write-string x) y) |
| 44 | + (take 0 (range)) "[]" |
| 45 | + (take 1 (repeat nil)) "[null]" |
| 46 | + (map identity [1 2]) "[1, 2]" |
| 47 | + (reduce + [1 2 3]) "6")) |
| 48 | + |
| 49 | +(deftest test-lists |
| 50 | + (assert-table [x y] (assert= (json/write-string x) y) |
| 51 | + '() "[]" |
| 52 | + '(nil) "[null]" |
| 53 | + '(1 2) "[1, 2]" |
| 54 | + '(1 1.0 nil) "[1, 1.000000, null]" |
| 55 | + '("foo" 42) "[\"foo\", 42]" |
| 56 | + (list) "[]" |
| 57 | + (list nil) "[null]" |
| 58 | + (list 1 2) "[1, 2]" |
| 59 | + (list 1 1.0 nil) "[1, 1.000000, null]" |
| 60 | + (list "foo" 42) "[\"foo\", 42]")) |
| 61 | + |
| 62 | +(deftest test-maps |
| 63 | + (assert-table [x y] (assert= (json/write-string x) y) |
| 64 | + {} "{}" |
| 65 | + {:foo 42} "{\"foo\": 42}" |
| 66 | + {"foo" 42} "{\"foo\": 42}" |
| 67 | + {"foo" 42, "bar" nil} "{\"foo\": 42, \"bar\": null}")) |
20 | 68 |
|
21 | 69 | (deftest test-round-trip |
22 | 70 | (let [data {"foo" 1, "bar" [2 3]}] ; won't work with keywords because the parser doesn't keywordise them (yet) |
23 | 71 | (assert= (-> data json/write-string json/read-string) |
24 | 72 | data)) |
25 | 73 |
|
26 | | - (let [string "{ \"foo\": [ 1, 2, 3 ] }"] |
| 74 | + (let [string "{\"foo\": [1, 2, 3]}"] |
27 | 75 | (assert= (-> string json/read-string json/write-string) |
28 | 76 | string))) |
0 commit comments