Refactor deftype tests

This commit is contained in:
Peter Taoussanis 2022-06-27 09:55:24 +02:00
parent 049197072c
commit 941ad15b0f

View file

@ -1,11 +1,11 @@
(ns taoensso.nippy.tests.main (ns taoensso.nippy.tests.main
(:require (:require
[clojure.test :as test :refer (is are deftest run-tests)] [clojure.test :as test :refer [deftest testing is]]
[clojure.test.check :as tc] [clojure.test.check :as tc]
[clojure.test.check.generators :as tc-gens] [clojure.test.check.generators :as tc-gens]
[clojure.test.check.properties :as tc-props] [clojure.test.check.properties :as tc-props]
[taoensso.encore :as enc :refer ()] [taoensso.encore :as enc :refer []]
[taoensso.nippy :as nippy :refer (freeze thaw)] [taoensso.nippy :as nippy :refer [freeze thaw]]
[taoensso.nippy.benchmarks :as benchmarks])) [taoensso.nippy.benchmarks :as benchmarks]))
(comment (test/run-tests)) (comment (test/run-tests))
@ -90,25 +90,30 @@
(defrecord MyRec [data]) (defrecord MyRec [data])
(deftest _types (deftest _types
;;; Extend to custom Type (testing "Extend to custom type"
(is (thrown? Exception ; No thaw extension yet (is (thrown? Exception ; No thaw extension yet
(do (alter-var-root #'nippy/*custom-readers* (constantly {})) (do (alter-var-root #'nippy/*custom-readers* (constantly {}))
(nippy/extend-freeze MyType 1 [x s] (.writeUTF s (.data x))) (nippy/extend-freeze MyType 1 [x s] (.writeUTF s (.data x)))
(thaw (freeze (MyType. "val")))))) (thaw (freeze (MyType. "val"))))))
(is (do (nippy/extend-thaw 1 [s] (MyType. (.readUTF s)))
(let [mt (MyType. "val")] (= (.data ^MyType mt)
(.data ^MyType (thaw (freeze mt)))))))
;;; Extend to custom Record (is (do (nippy/extend-thaw 1 [s] (MyType. (.readUTF s)))
(is (do (nippy/extend-freeze MyRec 2 [x s] (.writeUTF s (str "foo-" (:data x)))) (let [mt (MyType. "val")]
(nippy/extend-thaw 2 [s] (MyRec. (.readUTF s))) (=
(= (MyRec. "foo-val") (thaw (freeze (MyRec. "val")))))) (.data ^MyType mt)
(.data ^MyType (thaw (freeze mt))))))))
;;; Keyword (prefixed) extensions (testing "Extend to custom Record"
(is (is (do (nippy/extend-freeze MyRec 2 [x s] (.writeUTF s (str "foo-" (:data x))))
(do (nippy/extend-freeze MyRec :nippy-tests/MyRec [x s] (.writeUTF s (:data x))) (nippy/extend-thaw 2 [s] (MyRec. (.readUTF s)))
(nippy/extend-thaw :nippy-tests/MyRec [s] (MyRec. (.readUTF s))) (=
(let [mr (MyRec. "val")] (= mr (thaw (freeze mr))))))) (do (MyRec. "foo-val"))
(thaw (freeze (MyRec. "val")))))))
(testing "Keyword (prefixed) extensions"
(is
(do (nippy/extend-freeze MyRec :nippy-tests/MyRec [x s] (.writeUTF s (:data x)))
(nippy/extend-thaw :nippy-tests/MyRec [s] (MyRec. (.readUTF s)))
(let [mr (MyRec. "val")] (= mr (thaw (freeze mr))))))))
;;;; Caching ;;;; Caching