Skip to content
This repository
tree: 0cd62d08f8
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 76 lines (65 sloc) 3.341 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
(ns name.choi.joshua.fnparse.test-parse-json
  (:use clojure.contrib.test-is)
  (:require [name.choi.joshua.fnparse.json :as j]))

(defstruct node-s :kind :content)
(defstruct state-s :remainder :column :line)
(def make-node (partial struct node-s))
(def make-state (partial struct state-s))

(deftest number-lit
  (is (= (j/number-lit (make-state "123]" 3 4))
         [(make-node :scalar 123) (make-state (seq "]") 6 4)]))
  (is (= (j/number-lit (make-state "-123]" 3 4))
         [(make-node :scalar -123) (make-state (seq "]") 7 4)]))
  (is (= (j/number-lit (make-state "-123e3]" 3 4))
         [(make-node :scalar -123e3) (make-state (seq "]") 9 4)]))
  (is (= (j/number-lit (make-state "-123.9e3]" 3 4))
         [(make-node :scalar -123.9e3) (make-state (seq "]") 11 4)])))
; (is (thrown-with-msg? Exception
; #"JSON error at line 4, column 10: in number literal, after an exponent sign, decimal digit expected where \"e\" is"
; (j/number-lit (make-state "-123.9ee3]" 3 4)))))

(deftest unicode-char-sequence
  (is (= (j/unicode-char-sequence (make-state "u11A3a\"]" 3 4))
         [\u11A3 (make-state (seq "a\"]") 8 4)]))
  (is (thrown-with-msg? Exception
        #"JSON error at line 4, column 7: hexadecimal digit expected where \"T\" is"
        (j/unicode-char-sequence (make-state "u11ATa\"]" 3 4)))))

(deftest escape-sequence
  (is (= (j/escape-sequence (make-state "\\\\a\"]" 3 4))
         [\\ (make-state (seq "a\"]") 5 4)]))
  (is (= (j/escape-sequence (make-state "\\u1111a\"]" 3 4))
         [\u1111 (make-state (seq "a\"]") 9 4)])))

(deftest string-lit
  (is (= (j/string-lit (make-state "\"hello\"]" 3 4))
         [(make-node :scalar "hello") (make-state (seq "]") 10 4)]))
  (is (= (j/string-lit (make-state "\"hello\\u1111\"]" 3 4))
         [(make-node :scalar "hello\u1111") (make-state (seq "]") 16 4)])))

(deftest entry
  (is (= (j/entry (make-state "\"hello\": 55}" 3 4))
         [[(make-node :scalar "hello") (make-node :scalar 55)]
          (make-state (seq "}") 14 4)])))

(deftest additional-entry
  (is (= (j/additional-entry (make-state ", \"hello\": 55}" 3 4))
         [[(make-node :scalar "hello") (make-node :scalar 55)]
          (make-state (seq "}") 16 4)])))

(deftest object
  (is (= (j/object (make-state "{\"hello\": 55}]" 3 4))
         [(make-node :object {(make-node :scalar "hello") (make-node :scalar 55)})
          (make-state (seq "]") 16 4)]))
  (is (= (j/object (make-state "{\"hello\": 55, \"B\": \"goodbye\"}]" 3 4))
         [(make-node :object {(make-node :scalar "hello") (make-node :scalar 55)
                              (make-node :scalar "B") (make-node :scalar "goodbye")})
          (make-state (seq "]") 32 4)])))

(deftest load-stream
  (is (= (j/load-stream "[1, 2, 3]") [1 2 3])
      "loading a flat array containing integers")
; (is (thrown-with-msg? Exception
; #"an array is unclosed; \"]\" expected where \"}\" is"
; (j/load-stream "[1, 2, 3}") [1 2 3])
; "loading an improperly closed array")
  (is (= (j/load-stream "[\"a\", \"b\\n\", \"\\u1234\"]")
         ["a" "b\n" "\u1234"])
      "loading a flat array containing strings")
  (is (= (j/load-stream "{\"a\": 1, \"b\\n\": 2, \"\\u1234\": 3}")
         {"a" 1, "b\n" 2, "\u1234" 3})
      "loading a flat object containing strings and integers"))

(time (run-tests))
Something went wrong with that request. Please try again.