Skip to content

Commit 004c181

Browse files
authored
empty_qmark,get-in,find,contains_qmark (#89)
* empty_qmark,get-in,find,contains_qmark * empty_qmark,get-in,find,contains_qmark
1 parent 9092d60 commit 004c181

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(ns clojure.core-test.contains-qmark
2+
(:require clojure.core
3+
[clojure.test :as t :refer [deftest testing is are]]
4+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
5+
6+
(when-var-exists clojure.core/contains?
7+
(deftest test-contains?
8+
(testing "common"
9+
(is (= false (contains? nil nil)))
10+
(is (= false (contains? {} nil)))
11+
(is (= false (contains? [] nil)))
12+
;; find by index
13+
(is (= true (contains? ["a" "b" "c"] 0)))
14+
(is (= false (contains? ["a" "b" "c"] 3)))
15+
(is (= true (contains? "abc" 0)))
16+
(is (= true (contains? "abc" 2)))
17+
(is (= false (contains? "abc" 3)))
18+
#?(:cljs (is (= false (contains? "abc" "a")))
19+
:clj (is (thrown? Exception (contains? "abc" "a"))))
20+
;; find by key
21+
(is (= true (contains? {:a 1 :b 1} :a)))
22+
(is (= false (contains? {:a 1 :b 1} :c)))
23+
(is (= true (contains? {:a 1 :b (range)} :a))))))
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(ns clojure.core-test.empty-qmark
2+
(:require clojure.core
3+
[clojure.test :as t :refer [deftest testing is are]]
4+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
5+
6+
(when-var-exists clojure.core/empty?
7+
(deftest test-empty?
8+
(testing "common"
9+
(is (= true (empty? nil)))
10+
(is (= true (empty? {})))
11+
(is (= true (empty? [])))
12+
(is (= true (empty? "")))
13+
(is (= true (empty? '())))
14+
(is (= true (empty? #{})))
15+
(is (= false (empty? [\a])))
16+
(is (= false (empty? '(nil))))
17+
(is (= false (empty? (range))))
18+
(is (= false (empty? "abc")))
19+
(is (= false (empty? #{0 \space "a"})))
20+
(is (= false (empty? [(repeat (range))])))
21+
#?@(:cljs [(is (= false (empty? \space)))
22+
(is (thrown? js/Error (empty? 0)))
23+
(is (thrown? js/Error (empty? 0.0)))]
24+
:clj [(is (thrown? Exception (empty? 0)))
25+
(is (thrown? Exception (empty? 0.0)))
26+
(is (thrown? Exception (empty? \space)))]))))

test/clojure/core_test/find.cljc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(ns clojure.core-test.find
2+
(:require clojure.core
3+
[clojure.test :as t :refer [deftest testing is are]]
4+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
5+
6+
(when-var-exists clojure.core/find
7+
(deftest test-find
8+
(testing "common"
9+
(is (= nil (find nil nil)))
10+
(is (= nil (find {} nil)))
11+
(is (= nil (find [] nil)))
12+
(is (= [:a 1] (find {:a 1 :b 2 :c 3} :a)))
13+
(is (= [:a 1] (find {:a 1 :b 2 :c (range)} :a)))
14+
(is (= nil (find {:a 1 :b 2 :c 3} :d)))
15+
(is (= [0 1] (find {0 1 :0 2 "0" 3} 0))))))

test/clojure/core_test/get_in.cljc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(ns clojure.core-test.get-in
2+
(:require clojure.core
3+
[clojure.test :as t :refer [deftest testing is are]]
4+
[clojure.core-test.portability #?(:cljs :refer-macros :default :refer) [when-var-exists]]))
5+
6+
(when-var-exists clojure.core/get-in
7+
(deftest test-get-in
8+
(testing "common"
9+
(is (= nil (get-in nil nil)))
10+
(is (= nil (get-in nil nil "not found")))
11+
;; maps
12+
(is (= {} (get-in {} nil)))
13+
(is (= {} (get-in {} nil "not found")))
14+
(is (= {} (get-in {} [])))
15+
(is (= {} (get-in {} [] "not found")))
16+
(is (= "not found" (get-in {} [nil] "not found")))
17+
(is (= \space (get-in {} [:a] \space)))
18+
(is (= 0 (get-in {} ["a"] 0)))
19+
(is (= 1 (get-in {:a 1} [:a])))
20+
(is (= {:a 1} (get-in {:a 1} nil)))
21+
(is (= {:a 1} (get-in {:a 1} [])))
22+
(is (= {:a 1} (get-in {:a 1} '())))
23+
(is (= {:b {:c :d}} (get-in {:a {:b {:c :d}}} [:a])))
24+
(is (= {:c :d} (get-in {:a {:b {:c :d}}} [:a :b])))
25+
(is (= :d (get-in {:a {:b {:c :d} :e (range)}} [:a :b :c])))
26+
(is (= nil (get-in {:a {:b {:c :d}}} [:a :b :c :d])))
27+
(is (= :d (get-in {:a {:b {:c :d} :e (range)}} [:a :b :c :d] :d)))
28+
;; vectors
29+
(is (= 0 (get-in [[0 1 2]
30+
[3 4 5]
31+
[6 7 8]] [0 0])))
32+
(is (= 4 (get-in [[0 1 2]
33+
[3 4 (range)]
34+
[6 7 8]] [1 1])))
35+
(is (= 9 (get-in [[0 1 2]
36+
[3 4 5]
37+
[6 7 (range)]] [2 3] 9)))
38+
39+
;; maps and vector mix
40+
(is (= "x" (get-in {:id 1
41+
:matrix [[0 1 2]
42+
[3 4 {:var "x"}]
43+
[6 7 8]]}
44+
45+
[:matrix 1 2 :var] "y"))))))

0 commit comments

Comments
 (0)