|
| 1 | +(ns active.clojure.record-runtime-test |
| 2 | + (:require #?(:clj [clojure.test :refer :all]) |
| 3 | + [active.clojure.record-runtime :as r :include-macros true] |
| 4 | + #?(:cljs [active.clojure.cljs.record]) |
| 5 | + #?(:cljs [cljs.test])) |
| 6 | + #?(:cljs |
| 7 | + (:require-macros [cljs.test :refer (is deftest run-tests testing)]))) |
| 8 | + |
| 9 | +(def rtd0 (r/make-record-type-descriptor `rtd0 nil [])) |
| 10 | +(def rtd1 (r/make-record-type-descriptor `rtd1 nil [(r/make-record-field "f1")])) |
| 11 | +(def rtd2 (r/make-record-type-descriptor `rtd2 nil [(r/make-record-field "f1") |
| 12 | + (r/make-record-field "f2")])) |
| 13 | +(def rtd100 (r/make-record-type-descriptor `rtd100 nil |
| 14 | + (map #(str "f" %) (range 100)))) |
| 15 | + |
| 16 | +(deftest t-make-record-simple |
| 17 | + (let [r0 (r/make-record rtd0) |
| 18 | + r1 (r/make-record rtd1 1) |
| 19 | + r2 (r/make-record rtd2 1 2)] |
| 20 | + (is (r/record? r0)) |
| 21 | + (is (r/record-of-type? r0 rtd0)) |
| 22 | + (is (not (r/record-of-type? r0 rtd1))) |
| 23 | + (is (r/record? r1)) |
| 24 | + (is (= 1 (r/record-get rtd1 r1 0))) |
| 25 | + (is (r/record? r2)) |
| 26 | + (is (= 1 (r/record-get rtd2 r2 0))) |
| 27 | + (is (= 2 (r/record-get rtd2 r2 1))))) |
| 28 | + |
| 29 | +(deftest t-make-record-100 |
| 30 | + (let [r100 (apply r/make-record rtd100 (range 100))] |
| 31 | + (doseq [i (range 100)] |
| 32 | + (is (= i (r/record-get rtd100 r100 i)))))) |
| 33 | + |
| 34 | +(deftest t-record-update |
| 35 | + (let [r2 (r/make-record rtd2 1 2) |
| 36 | + r2a (r/record-update rtd2 r2 1 5)] |
| 37 | + (is (= 1 (r/record-get rtd2 r2a 0))) |
| 38 | + (is (= 5 (r/record-get rtd2 r2a 1))))) |
| 39 | + |
| 40 | +(deftest t-record-check |
| 41 | + (let [r2 (r/make-record rtd2 1 2)] |
| 42 | + (is (thrown? #?(:clj Error :cljs js/Error) |
| 43 | + (r/record-get rtd0 r2 0))) |
| 44 | + (is (thrown? #?(:clj Error :cljs js/Error) |
| 45 | + (r/record-update rtd0 r2 0 :new))))) |
| 46 | + |
| 47 | +(deftest to-string-test |
| 48 | + (let [r0 (r/make-record rtd0) |
| 49 | + r1 (r/make-record rtd1 1) |
| 50 | + r2 (r/make-record rtd2 1 2)] |
| 51 | + (testing "str / toString" |
| 52 | + (is (= "active.clojure.record-runtime-test/rtd0{}" |
| 53 | + (str r0))) |
| 54 | + (is (= "active.clojure.record-runtime-test/rtd1{:f1 1}" |
| 55 | + (str r1))) |
| 56 | + (is (= "active.clojure.record-runtime-test/rtd2{:f1 1, :f2 2}" |
| 57 | + (str r2)))) |
| 58 | + (testing "pr-str / print-method" |
| 59 | + (is (= "active.clojure.record-runtime-test/rtd0{}" |
| 60 | + (pr-str r0))) |
| 61 | + (is (= "active.clojure.record-runtime-test/rtd1{:f1 1}" |
| 62 | + (pr-str r1))) |
| 63 | + (is (= "active.clojure.record-runtime-test/rtd2{:f1 1, :f2 2}" |
| 64 | + (pr-str r2)))))) |
| 65 | + |
| 66 | + |
| 67 | +(defrecord NotRTDRecord [a b]) |
| 68 | +(def rtd0-2 (r/make-record-type-descriptor `rtd0-2 nil [])) |
| 69 | +(def rtd2-2 (r/make-record-type-descriptor `rtd2-2 nil [(r/make-record-field "f1") |
| 70 | + (r/make-record-field "f2")])) |
| 71 | + |
| 72 | +(deftest record-check-rtd!-throws-when-wrong-record-type |
| 73 | + (testing "not even an rtd-record" |
| 74 | + (is (thrown-with-msg? |
| 75 | + #?(:clj Error :cljs js/Error) |
| 76 | + #"Not a record of the correct type \[\[active.clojure.record-runtime-test/rtd2]]" |
| 77 | + (r/record-check-rtd! ^RecordTypeDescriptor rtd2 4))) |
| 78 | + (is (thrown-with-msg? |
| 79 | + #?(:clj Error :cljs js/Error) |
| 80 | + #"Not a record of the correct type \[\[active.clojure.record-runtime-test/rtd2]]" |
| 81 | + (r/record-check-rtd! rtd2 "blub"))) |
| 82 | + (is (thrown-with-msg? |
| 83 | + #?(:clj Error :cljs js/Error) |
| 84 | + #"Not a record of the correct type \[\[active.clojure.record-runtime-test/rtd2]]" |
| 85 | + (r/record-check-rtd! rtd2 (->NotRTDRecord 1 2))))) |
| 86 | + (testing "wrong rtd-record" |
| 87 | + (is (thrown-with-msg? |
| 88 | + #?(:clj Error :cljs js/Error) |
| 89 | + #"Not a record of the correct type \[\[active.clojure.record-runtime-test/rtd2]]" |
| 90 | + (r/record-check-rtd! rtd2 (r/make-record rtd0)))) |
| 91 | + (is (thrown-with-msg? |
| 92 | + #?(:clj Error :cljs js/Error) |
| 93 | + #"Not a record of the correct type \[\[active.clojure.record-runtime-test/rtd2]]" |
| 94 | + (r/record-check-rtd! rtd2 (r/make-record rtd1)))) |
| 95 | + (is (thrown-with-msg? |
| 96 | + #?(:clj Error :cljs js/Error) |
| 97 | + #"Not a record of the correct type \[\[active.clojure.record-runtime-test/rtd2]]" |
| 98 | + (r/record-check-rtd! rtd2 (r/make-record rtd2-2)))) |
| 99 | + (is (thrown-with-msg? |
| 100 | + #?(:clj Error :cljs js/Error) |
| 101 | + #"Not a record of the correct type \[\[active.clojure.record-runtime-test/rtd0]]" |
| 102 | + (r/record-check-rtd! rtd0 (r/make-record rtd0-2))))) |
| 103 | + (testing "correct record, shouldnt throw" |
| 104 | + (is (nil? (r/record-check-rtd! rtd0 (r/make-record rtd0))))) |
| 105 | + (testing "correct record, shouldnt throw" |
| 106 | + (is (nil? (r/record-check-rtd! rtd2 (r/make-record rtd2 1 2)))))) |
0 commit comments