Skip to content

Commit

Permalink
fixes #41 by deprecating implicit expect
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <[email protected]>
  • Loading branch information
seancorfield committed Dec 12, 2024
1 parent cc192ed commit 50a36ca
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 25 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
.settings
.socket-repl-port
.sw*
.vscode
/checkouts
/classes
/cljs-test-runner-out
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

Only accretive/fixative changes will be made from now on.

* 2.1.next in progress
* 2.2.next in progress
* Address [#41](https://github.com/clojure-expectations/clojure-test/issues/41) by deprecating the implicit `expect` form of `defexpect` and printing an obnoxious warning when it is used. This form of `defexpect` will be removed in a future release!
* PR [#40](https://github.com/clojure-expectations/clojure-test/pull/40) [@NoahTheDuke](https://github.com/NoahTheDuke) reduces the amount of code generated for `?=` (and, in turn, for `more-of`), allowing for more complex tests.

* 2.1.208 -- 2024-11-21
Expand Down
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,20 @@ What follows is an example REPL session showing some of what this library provid
(defexpect regex-1
(expect #"foo" "It's foobar!"))

;; since that has only a single expectation, it can be written more succinctly:

(defexpect regex-2 #"foo" "It's foobar!")

;; the expected outcome can be an exception type:

(defexpect divide-by-zero ArithmeticException (/ 12 0))
(defexpect divide-by-zero
(expect ArithmeticException (/ 12 0)))

;; the expected outcome can be a predicate:

(defexpect no-elements empty? (list))
(defexpect no-elements
(expect empty? (list)))

;; the expected outcome can be a type:

(defexpect named String (name :foo))
(defexpect named
(expect String (name :foo)))

;; the expected outcome can be a Spec:

Expand Down Expand Up @@ -151,7 +150,7 @@ nil
If the test passes, nothing is printed, and `nil` is returned. Let's look at a failing test:

```clojure
user=> (defexpect inequality (* 2 21) (+ 13 13 13))
user=> (defexpect inequality (expect (* 2 21) (+ 13 13 13)))
#'user/inequality
user=> (inequality)

Expand All @@ -167,7 +166,7 @@ that allows for Expectations style of predicate-or-equality testing (based on
whether the "expected" expression resolves to a function or some other value):

```clojure
user=> (defexpect not-at-all-odd odd? (+ 1 1))
user=> (defexpect not-at-all-odd (expect odd? (+ 1 1)))
#'user/not-at-all-odd
user=> (not-at-all-odd)

Expand Down
2 changes: 1 addition & 1 deletion build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
[deps-deploy.deps-deploy :as dd]))

(def lib 'com.github.seancorfield/expectations)
(defn- the-version [patch] (format "2.1.%s" patch))
(defn- the-version [patch] (format "2.2.%s" patch))
(def version (the-version (b/git-count-revs nil)))
(def snapshot (the-version "999-SNAPSHOT"))
(def class-dir "target/classes")
Expand Down
12 changes: 6 additions & 6 deletions doc/getting-started-cljs.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ Classes are all different in ClojureScript, and in some cases things
that would be a class in Clojure are different in ClojureScript. For
instance, lists are a class:
```clojure
(defexpect class-test cljs.core/List '(a b c))
(defexpect class-test (expect cljs.core/List '(a b c)))
```
and this test passes. Strings, however, don't have an easily
discoverable type or class, and are better handled with a predicate:
```clojure
(defexpect string-class-test string? "abc")
(defexpect string-class-test (expect string? "abc"))
```
In general, the classes in ClojureScript will not be the same as
the classes in Clojure. You can do this to write a test that
Expand All @@ -139,7 +139,7 @@ will work in both environments:
```
but you cannot write this:
```
(defexpect bad-both-class-test (type "abc") (type "def"))
(defexpect bad-both-class-test (expect (type "abc") (type "def")))
```
because `(type "abc")` yields something that tests positive as a
`fn?`, causing expectations to think it is a predicate. Which,
Expand All @@ -151,12 +151,12 @@ Exceptions are very different in ClojureScript from Clojure.

The Clojure example:
```clojure
(defexpect divide-by-zero ArithmeticException (/ 12 0))
(defexpect divide-by-zero (expect ArithmeticException (/ 12 0)))
```
doesn't even throw an exception -- it returns `##Inf`.
You can do this for that situation:
```clojure
(defexpect divide-by-zero ##Inf (/ 12 0))
(defexpect divide-by-zero (expect ##Inf (/ 12 0)))
```
but be careful putting `##Inf` in a reader conditional, as some versions of
Clojure don't handle that well. But all of this is a bit off-topic,
Expand All @@ -168,7 +168,7 @@ Clojurecript to distinguish things that can be thrown from anything
else. The only exception supported in `expectations/clojure-test`
in ClojureScript is where the exception is: `js/Error`. For example:
```clojure
(defexpect exception js/Error (count 5))
(defexpect exception (expect js/Error (count 5)))
```
will pass, because `(count 5)` throws `js/Error`.

Expand Down
10 changes: 4 additions & 6 deletions src/expectations/clojure/test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,14 @@

(defmacro defexpect
"Given a name (a symbol that may include metadata) and a test body,
produce a standard `clojure.test` test var (using `deftest`).
`(defexpect name expected actual)` is a special case shorthand for
`(defexpect name (expect expected actual))` provided as an easy way to migrate
legacy Expectation tests to the 'clojure.test' compatibility version."
produce a standard `clojure.test` test var (using `deftest`)."
[n & body]
(if (and (= (count body) 2)
(not (some contains-expect? body)))
;; treat (defexpect my-name pred (expr)) as a special case
`(t/deftest ~n (expect ~@body))
`(t/deftest ~n (do
(println "DEPRECATED: implicit 'expect' in defexpect" '~n)
(expect ~@body)))
;; #13 match deftest behavior starting in 2.0.0
`(t/deftest ~n ~@body)))

Expand Down
2 changes: 1 addition & 1 deletion test/expectations/clojure/test_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@

(def ^:private control (atom 0))
;; this will succeed on its own
(sut/defexpect control-test-1 zero? @control)
(sut/defexpect control-test-1 (sut/expect zero? @control))
;; then retest with a different control value
(deftest control-test-2
(try
Expand Down

0 comments on commit 50a36ca

Please sign in to comment.