Skip to content

Commit

Permalink
Collection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrink10 committed Feb 21, 2020
1 parent ab2e2fb commit 72d77c5
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 10 deletions.
50 changes: 41 additions & 9 deletions src/basilisp/edn.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
"Generic Python object used as a sentinel value for comments."
(python/object))

(def ^:private default-edn-data-readers
"Map of default data readers, which wrap the default readers and convert the
SyntaxErrors into standard ExceptionInfo types."
(reduce-kv (fn [m k tag-reader]
(assoc m k (fn [v]
(try
(tag-reader v)
(catch basilisp.lang.reader/SyntaxError e
(throw
(ex-info (get (.-args e) 0)
{:error :tag-reader-error})))))))
{}
(dissoc default-data-readers 'py)))

(def ^:private eof
"EOF marker if none is supplied."
(python/object))
Expand Down Expand Up @@ -186,7 +200,22 @@
(defmethod read-dispatch :set
[reader opts]
(assert-starts reader "{")
(read-coll reader set "}" "set"))
(letfn [(set-if-valid [coll]
(let [coll-set (set coll)]
(if (not= (count coll) (count coll-set))
(throw
(ex-info "Duplicate values in set"
{:error :duplicate-value-in-set
:value (->> coll
(reduce (fn [m k]
(if (contains? m k)
(update m k inc)
(assoc m k 1)))
{})
(filter (fn [[k n]] (> n 1)))
(ffirst))}))
coll-set)))]
(read-coll reader opts set-if-valid "}" "set")))

(defmethod read-dispatch :tag
[reader {readers :readers read-tag-default :default :as opts}]
Expand All @@ -197,7 +226,7 @@
{:error :reader-tag-not-symbol})))
(if-let [tag-reader (get (if (namespace tag)
readers
(dissoc default-data-readers 'py))
default-edn-data-readers)
tag)]
(tag-reader (read-next reader opts))
(if read-tag-default
Expand Down Expand Up @@ -329,15 +358,18 @@
(= c "}")
(do
(.next-token reader)
(apply hash-map objs))
(try
(->> objs
(remove #(or (identical? comment %)
(identical? eof %)))
(apply hash-map))
(catch python/IndexError _
(throw
(ex-info "Map should contain an even number of forms"
{:error :invalid-map-literal})))))

:else
(recur (->> #(read-next reader opts)
(repeatedly)
(remove #(or (identical? comment %)
(identical? eof %)))
(take 2)
(apply conj objs)))))))
(recur (conj objs (read-next reader opts)))))))

(defmethod read-next :keyword
[reader _]
Expand Down
61 changes: 60 additions & 1 deletion tests/basilisp/test_edn.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

(are [s] (thrown? basilisp.lang.exception/ExceptionInfo
(edn/read-string s))
"#_[]"))
"#_[]"
";;comment"))

(deftest read-numeric
(are [s v] (= v (edn/read-string s))
Expand Down Expand Up @@ -180,3 +181,61 @@
"#my.ns/nonexistant-tag []"

"#:kw []"))

(deftest read-map
(are [s v] (= v (edn/read-string s))
"{}" {}
"{:a 1}" {:a 1}
"{:a {:b 1}}" {:a {:b 1}}
"{:a #_{:b :c} :d}" {:a :d}
"{#_:a :b :c}" {:b :c})

(are [s] (thrown? basilisp.lang.exception/ExceptionInfo
(edn/read-string s))
"{"
"{:a"
"{:a}"
"{:a 1"
"{:a 1 :b}"))

(deftest read-list
(are [s v] (= v (edn/read-string s))
"()" '()
"(:a)" '(:a)
"([:a])" '([:a])
"(:a :b 3 \"4\")" '(:a :b 3 "4")
"(:a #_(:b) :c)" '(:a :c))

(are [s] (thrown? basilisp.lang.exception/ExceptionInfo
(edn/read-string s))
"("
"(:a"
"(:a :b"))

(deftest read-set
(are [s v] (= v (edn/read-string s))
"#{}" #{}
"#{:a}" #{:a}
"#{:a :b [:c]}" #{:a :b [:c]}
"#{:a #_#{} :b}" #{:a :b})

(are [s] (thrown? basilisp.lang.exception/ExceptionInfo
(edn/read-string s))
"#{"
"#{:a"
"#{:a :a}"
"#{:a :b"
"#{:a :a :b :b}"))

(deftest read-vector
(are [s v] (= v (edn/read-string s))
"[]" []
"[:a]" [:a]
"[:a :a :b :c]" [:a :a :b :c]
"[:a #_[:b] :c]" [:a :c])

(are [s] (thrown? basilisp.lang.exception/ExceptionInfo
(edn/read-string s))
"["
"[:a"
"[:a :b"))

0 comments on commit 72d77c5

Please sign in to comment.