|
| 1 | +(ns clojure.core-test.val |
| 2 | + (:require [clojure.test :as t :refer [deftest testing is are]] |
| 3 | + [clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]])) |
| 4 | + |
| 5 | +(when-var-exists val |
| 6 | + (deftest test-val |
| 7 | + (testing "`val` on map-entry-like things" |
| 8 | + (is (= :v (val (first {:k :v})))) |
| 9 | + (is (= :v (val (first {:k :v, :one :two})))) |
| 10 | + ;; Note: the following may be built on shaky ground, per Rich: |
| 11 | + ;; https://groups.google.com/g/clojure/c/FVcrbHJpCW4/m/Fh7NsX_Yb7sJ |
| 12 | + (is (= 'v (val #?(:cljs (cljs.core/MapEntry. 'k 'v nil) |
| 13 | + :clj (clojure.lang.MapEntry/create 'k 'v))))) |
| 14 | + (is (= :b (val (first (sorted-map :a :b))))) |
| 15 | + (is (= :b (val (first (hash-map :a :b))))) |
| 16 | + (is (= :b (val (first (array-map :a :b)))))) |
| 17 | + (testing "`val` throws on lots of things" |
| 18 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 19 | + (val nil))) |
| 20 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 21 | + (val 0))) |
| 22 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 23 | + (val '()))) |
| 24 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 25 | + (val '(1 2)))) |
| 26 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 27 | + (val {}))) |
| 28 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 29 | + (val {1 2}))) |
| 30 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 31 | + (val []))) |
| 32 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 33 | + (val [1 2]))) |
| 34 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 35 | + (val #{}))) |
| 36 | + (is (thrown? #?(:cljs :default, :clj Exception, :cljr Exception) |
| 37 | + (val #{1 2})))))) |
0 commit comments