Skip to content

Commit 23948cf

Browse files
committed
fix: iterable NPE with empty choice-map
1 parent 5897d78 commit 23948cf

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/gen/dynamic/choice_map.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@
8787
(.iterator ^Iterable (map auto-get-choice m)))
8888

8989
Iterable
90-
(iterator [this] (.iterator ^Iterable (.seq this))))
90+
(iterator [this]
91+
(if-let [xs (.seq this)]
92+
(.iterator ^Iterable xs)
93+
(.iterator {}))))
9194

9295
(defn unwrap
9396
"If `m` is a [[Choice]] or [[ChoiceMap]], returns `m` stripped of its wrappers.

test/gen/dynamic/choice_map_test.clj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
(:refer-clojure :exclude [empty empty?])
33
(:require [clojure.core :as clojure]
44
[clojure.test :refer [deftest is]]
5+
[clojure.test.check.generators :as gen]
6+
[com.gfredericks.test.chuck.clojure-test :refer [checking]]
57
[gen.choice-map :as choice-map]
68
[gen.dynamic.choice-map :as dynamic.choice-map]))
79

10+
(def gen-choice-map
11+
(comp (partial gen/fmap dynamic.choice-map/choice-map)
12+
gen/map))
13+
814
(deftest choice
915
(is (dynamic.choice-map/choice? (dynamic.choice-map/choice nil)))
1016
(is (dynamic.choice-map/choice? #gen/choice nil))
@@ -30,3 +36,21 @@
3036
(is (clojure/empty? #gen/choice-map {}))
3137
#_{:clj-kondo/ignore [:not-empty?]}
3238
(is (not (clojure/empty? #gen/choice-map {:x 0}))))
39+
40+
(defn iterable-seq [^Iterable iter]
41+
(when (.hasNext iter)
42+
(lazy-seq
43+
(cons (.next iter)
44+
(iterable-seq iter)))))
45+
46+
(deftest interface-tests
47+
(checking "Interface tests for choice maps"
48+
[m (gen-choice-map gen/keyword gen/any-equatable)]
49+
(is (= (seq m)
50+
(iterable-seq
51+
(.iterator ^Iterable m)))
52+
"iterator impl matches seq")
53+
54+
(is (= m (dynamic.choice-map/choice-map
55+
(zipmap (keys m) (vals m))))
56+
"keys and vals work correctly")))

0 commit comments

Comments
 (0)