Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More reliably find specs for unqualified keys in keys spec #223

Merged
merged 6 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
[orchestra "2020.09.18-1"]
[org.clojure/core.specs.alpha "0.2.44"]
[vvvvalvalval/scope-capture "0.3.2"]
[org.clojure/test.check "0.10.0"]
[org.clojure/test.check "1.1.0"]
[metosin/spec-tools "0.8.2"]
[ring/ring-core "1.6.3"] ; required to make ring-spec work, may cause issues with figwheel?
[ring/ring-spec "0.0.4"] ; to test specs
[org.onyxplatform/onyx-spec "0.13.0.0"] ; to test specs
[com.gfredericks/test.chuck "0.2.10"]
[cider/cider-nrepl "0.22.4"]
[cider/cider-nrepl "0.24.0"]
]
:injections [(require 'sc.api)]
:plugins [
Expand Down Expand Up @@ -83,7 +83,7 @@
:kaocha [:test-common
{:dependencies [[lambdaisland/kaocha "0.0-565"]
[lambdaisland/kaocha-cloverage "0.0-41"]]}]
:test-common {:dependencies [[org.clojure/test.check "0.10.0-alpha3"]
:test-common {:dependencies [[org.clojure/test.check "1.1.0"]
[pjstadig/humane-test-output "0.9.0"]
[com.gfredericks/test.chuck "0.2.10"]
[orchestra "2020.09.18-1"]
Expand Down
2 changes: 1 addition & 1 deletion src/expound/alpha.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@

(defn ^:private named? [x]
#?(:clj (instance? clojure.lang.Named x)
:cljs (implements? cljs.core.INamed x)))
:cljs (implements? INamed x)))

(defn ^:private pr-pred* [pred]
(cond
Expand Down
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
52 changes: 48 additions & 4 deletions test/expound/alpha_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2389,7 +2389,7 @@ Detected 2 errors\n"
(s/def :conformers-test/string-AB
(s/and
;; conform as sequence (seq function)
(s/conformer seq)
(s/conformer #(if (seqable? %) (seq %) %))
;; re-use previous sequence spec
:conformers-test/string-AB-seq))

Expand Down 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}))))