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

Make multimethods public, check for explicit problem type #97

Merged
merged 2 commits into from
May 29, 2018
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
7 changes: 5 additions & 2 deletions src/expound/alpha.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@
(pr-str retag)
(pr-str (if retag (retag (problems/value-in val path)) nil)))))

(defmulti ^:private problem-group-str (fn [type spec-name _val _path _problems _opts] type))
(defmulti ^:private expected-str (fn [type spec-name _val _path _problems _opts] type))
(defmulti ^:no-doc problem-group-str (fn [type spec-name _val _path _problems _opts] type))
(defmulti ^:no-doc expected-str (fn [type spec-name _val _path _problems _opts] type))

(defn ^:private explain-missing-keys [problems]
(let [missing-keys (map #(printer/missing-key (:pred %)) problems)]
Expand Down Expand Up @@ -381,6 +381,9 @@

(defn ^:private problem-type [failure problem]
(cond
(get-method problem-group-str (:expound.spec.problem/type problem))
(:expound.spec.problem/type problem)

(insufficient-input? failure problem)
:problem/insufficient-input

Expand Down
46 changes: 46 additions & 0 deletions test/expound/alpha_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2695,3 +2695,49 @@ should satisfy
")
(binding [s/*explain-out* (expound/custom-printer {:theme :figwheel-theme})]
(readable-ansi (s/explain-str :colorized-output/strings ["" :a ""]))))))

(defmethod expound/problem-group-str ::test-problem1 [_type spec-name val path problems opts]
"fake-problem-group-str")

(defmethod expound/problem-group-str ::test-problem2 [type spec-name val path problems opts]
(str "fake-problem-group-str\n"
(expound/expected-str type spec-name val path problems opts)))

(defmethod expound/expected-str ::test-problem2 [_type spec-name val path problems opts]
"fake-expected-str")

(deftest extensibility-test
(testing "can overwrite entire message"
(let [printer-str #'expound/printer-str
ed (assoc-in (s/explain-data int? "")
[::s/problems 0 :expound.spec.problem/type]
::test-problem1)]

(is (= "fake-problem-group-str\n\n-------------------------\nDetected 1 error\n"
(printer-str {:print-specs? false} ed)))))
(testing "can overwrite 'expected' str"
(let [printer-str #'expound/printer-str
ed (assoc-in (s/explain-data int? "")
[::s/problems 0 :expound.spec.problem/type]
::test-problem2)]

(is (= "fake-problem-group-str\nfake-expected-str\n\n-------------------------\nDetected 1 error\n"
(printer-str {:print-specs? false} ed)))))
(testing "if type has no mm implemented, behavior is normal expound behavior"
(let [printer-str #'expound/printer-str
ed (assoc-in (s/explain-data int? "")
[::s/problems 0 :expound.spec.problem/type]
::test-problem3)]

(is (= "-- Spec failed --------------------

\"\"

should satisfy

int?

-------------------------
Detected 1 error
"
(printer-str {:print-specs? false} ed))))))