-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Address review comments, support expanded defn specs
- Loading branch information
Showing
5 changed files
with
57 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(ns nedap.utils.spec.impl.spec-assertion | ||
(:require | ||
#?(:cljs [cljs.core :refer [ExceptionInfo]]) | ||
[clojure.test :as test] | ||
[nedap.utils.spec.impl.parsing :refer [infer-spec-from-symbol]]) | ||
#?(:clj (:import (clojure.lang ExceptionInfo)))) | ||
|
||
(defn assert-spec-failure [msg spec-sym body] | ||
`(try | ||
(with-out-str ~@body) ; silently execute body | ||
(test/do-report {:type :fail, :message ~msg :expected '~spec-sym, :actual nil}) | ||
(catch ExceptionInfo e# | ||
(let [spec# (:spec (ex-data e#)) | ||
inferred-spec# (:spec (infer-spec-from-symbol true ~spec-sym))] | ||
|
||
;; rethrow if no spec failure is found | ||
(when-not [spec#] | ||
(throw e#)) | ||
|
||
(if (or (= ~spec-sym spec#) | ||
(= inferred-spec# spec#)) | ||
(test/do-report {:type :pass, :message ~msg :expected '~spec-sym, :actual nil}) | ||
(test/do-report {:type :fail, :message ~msg :expected '~spec-sym, :actual spec#})) | ||
e#)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
(ns unit.nedap.utils.speced.spec-assertion | ||
(:require [clojure.test :refer :all] | ||
[nedap.utils.spec.api :as sut] | ||
[nedap.utils.speced :as speced] | ||
[clojure.spec.alpha :as s])) | ||
|
||
(s/def ::age number?) | ||
(speced/defn accepts-age [^::age x] x) | ||
(speced/defn ^::age returns-age [x] x) | ||
(speced/defn accepts-number [^number? x] x) | ||
(speced/defn ^number? returns-number [x] x) | ||
|
||
(deftest spec-assertion-thrown?-defmethod | ||
(is (spec-assertion-thrown? 'string? (sut/check! string? 123))) | ||
(is (spec-assertion-thrown? 'number? (sut/check! number? "123"))) | ||
(is (spec-assertion-thrown? ::age (accepts-age "1234"))) | ||
(is (spec-assertion-thrown? ::age (returns-age "1234"))) | ||
(is (spec-assertion-thrown? 'number? (accepts-number "1234"))) | ||
(is (spec-assertion-thrown? 'number? (returns-number "1234")))) |