Skip to content

Commit

Permalink
More reliably find specs for unqualified keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bhb committed Aug 29, 2021
1 parent 543572a commit 78be800
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
22 changes: 10 additions & 12 deletions src/expound/printer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
(s/or
:kw qualified-keyword?
:conj :expound.spec/spec-conjunction))
(s/def :expound.spec/key-spec
(s/def :expound.spec/keys-spec
(s/cat :keys #{'clojure.spec.alpha/keys
'cljs.spec.alpha/keys}
:clauses (s/*
Expand All @@ -37,7 +37,6 @@
:compound (s/cat
:op #{`or `and}
:clauses (s/+ :expound.spec/contains-key-pred))))

(declare format)

(defn ^:private str-width [lines]
Expand Down Expand Up @@ -126,7 +125,6 @@

;;;; private


(defn keywords [form]
(->> form
(tree-seq coll? seq)
Expand All @@ -137,20 +135,20 @@

(defn specs-from-form [via]
(let [form (some-> via last s/form)
conformed (s/conform :expound.spec/key-spec form)]
;; The containing spec might not be
;; a simple 'keys' call, in which case we give up
(if (and form
(not= ::s/invalid conformed))
(->> (:clauses conformed)
(map :specs)
keys-specs (->> (tree-seq coll? seq form)
(filter #(s/valid? :expound.spec/keys-spec %)))]
(if (empty? keys-specs)
#{}
(->> keys-specs
(map #(s/conform :expound.spec/keys-spec %))
(mapcat :clauses)
(mapcat :specs)
(tree-seq coll? seq)
(filter
(fn [x]
(and (vector? x) (= :kw (first x)))))
(map second)
set)
#{})))
set))))

(defn key->spec [keys problems]
(doseq [p problems]
Expand Down
50 changes: 47 additions & 3 deletions test/expound/alpha_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2866,9 +2866,9 @@ returned an invalid value
should contain key: :my-int
| key | spec |
|=========+===================================================|
| :my-int | <can't find spec for unqualified spec identifier> |
| key | spec |
|=========+==========|
| :my-int | pos-int? |
-------------------------
Detected 1 error
Expand Down Expand Up @@ -4304,3 +4304,47 @@ Detected 1 error
(expound/expound-str
(clojure.spec.alpha/coll-of (fn [x] (< x 9)))
(range 10))))))

;; https://github.com/bhb/expound/issues/215
(s/def :keys-within-operators.user/name string?)
(s/def :keys-within-operators.user/age pos-int?)

(deftest keys-within-operators

(is (= "-- Spec failed --------------------
{}
should contain keys: :age, :keys-within-operators.user/name
| key | spec |
|==================================+==========|
| :age | pos-int? |
|----------------------------------+----------|
| :keys-within-operators.user/name | string? |
-------------------------
Detected 1 error\n"
(expound/expound-str (s/and (s/keys :req [:keys-within-operators.user/name]
:req-un [:keys-within-operators.user/age])
#(contains? % :foo)) {} {:print-specs? false})))

(is (= "-- Spec failed --------------------
{}
should contain keys: :age, :foo, :keys-within-operators.user/name
| key | spec |
|==================================+===================================================|
| :age | pos-int? |
|----------------------------------+---------------------------------------------------|
| :foo | <can't find spec for unqualified spec identifier> |
|----------------------------------+---------------------------------------------------|
| :keys-within-operators.user/name | string? |
-------------------------
Detected 1 error\n"
(expound/expound-str (s/or :k1 (s/keys :req [:keys-within-operators.user/name]
:req-un [:keys-within-operators.user/age])
:k2 #(contains? % :foo)) {} {:print-specs? false}))))

0 comments on commit 78be800

Please sign in to comment.