Skip to content

Commit

Permalink
fix: iterable NPE with empty choice-map
Browse files Browse the repository at this point in the history
  • Loading branch information
sritchie committed Aug 28, 2023
1 parent 5897d78 commit 23948cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/gen/dynamic/choice_map.clj
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@
(.iterator ^Iterable (map auto-get-choice m)))

Iterable
(iterator [this] (.iterator ^Iterable (.seq this))))
(iterator [this]
(if-let [xs (.seq this)]
(.iterator ^Iterable xs)
(.iterator {}))))

(defn unwrap
"If `m` is a [[Choice]] or [[ChoiceMap]], returns `m` stripped of its wrappers.
Expand Down
24 changes: 24 additions & 0 deletions test/gen/dynamic/choice_map_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
(:refer-clojure :exclude [empty empty?])
(:require [clojure.core :as clojure]
[clojure.test :refer [deftest is]]
[clojure.test.check.generators :as gen]
[com.gfredericks.test.chuck.clojure-test :refer [checking]]
[gen.choice-map :as choice-map]
[gen.dynamic.choice-map :as dynamic.choice-map]))

(def gen-choice-map
(comp (partial gen/fmap dynamic.choice-map/choice-map)
gen/map))

(deftest choice
(is (dynamic.choice-map/choice? (dynamic.choice-map/choice nil)))
(is (dynamic.choice-map/choice? #gen/choice nil))
Expand All @@ -30,3 +36,21 @@
(is (clojure/empty? #gen/choice-map {}))
#_{:clj-kondo/ignore [:not-empty?]}
(is (not (clojure/empty? #gen/choice-map {:x 0}))))

(defn iterable-seq [^Iterable iter]
(when (.hasNext iter)
(lazy-seq
(cons (.next iter)
(iterable-seq iter)))))

(deftest interface-tests
(checking "Interface tests for choice maps"
[m (gen-choice-map gen/keyword gen/any-equatable)]
(is (= (seq m)
(iterable-seq
(.iterator ^Iterable m)))
"iterator impl matches seq")

(is (= m (dynamic.choice-map/choice-map
(zipmap (keys m) (vals m))))
"keys and vals work correctly")))

0 comments on commit 23948cf

Please sign in to comment.