Skip to content

Commit

Permalink
Add functionally predicate #88 and tests
Browse files Browse the repository at this point in the history
Make it work with `from-each`; update readme/changelog; prep for 2.2.0-beta1.
  • Loading branch information
seancorfield committed Feb 23, 2017
1 parent e96e2a2 commit c041568
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Changes in version 2.2.0

Add support for `clojure.test` via the new `expectations.clojure.test` namespace and the `defexpect` macro #82.

Add `approximately` predicate, to test if two floating point values are "equal" (within a given tolerance) #84.

Add `functionally` predicate, to test if two functions are "functionally equivalent" #88.

Numerous cljc platform bug fixes; documented testing process; updated change log (was stuck at 2.1.4).

# Changes in version 2.1.9

Allow `more-of` to expect an exception type #80.

# Changes in version 2.1.5-2.1.8

Switched from cljx to cljc (and fix various bugs that caused).

Improve handling of null values with regex expectations #75.

# Changes in version 2.1.4

Remove expect-let and expect-let focused.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Running the tests:

lein do clean, test-success, test-clojure

This will run the (successful) expectations for Clojure and the (unsuccessful) `clojure.test`-compatible tests for Clojure (currently 82/82 and 12/21 tests/assertions respectively, the latter should show 16 failures and 4 errors).
This will run the (successful) expectations for Clojure and the (unsuccessful) `clojure.test`-compatible tests for Clojure (currently 83/83 and 12/21 tests/assertions respectively, the latter should show 16 failures and 4 errors).

You can also run the ClojureScript tests interactively:

Expand All @@ -17,13 +17,13 @@ You can also run the ClojureScript tests interactively:
...
cljs.user=> (expectations.test/-main)

This will run the (successful) expectations that are compatible with ClojureScript (currently 68/68).
This will run the (successful) expectations that are compatible with ClojureScript (currently 69/69).

You can run _all_ expectations via:

lein do clean, expectations

This includes the deliberately failing expectations (used to visually confirm behavior for failing tests) and should run 127 assertions in total, of which 43 will fail and 2 will error.
This includes the deliberately failing expectations (used to visually confirm behavior for failing tests) and should run 128 assertions in total, of which 43 will fail and 2 will error.

## Donate to Jay C Fields, the creator of Expectations

Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject expectations "2.2.0-alpha1"
(defproject expectations "2.2.0-beta1"
:description "testing framework"
:jar-name "expectations.jar"
:jar-exclusions [#"\.swp|\.swo|\.DS_Store"]
Expand Down
40 changes: 31 additions & 9 deletions src/cljc/expectations.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,9 @@

(defmulti compare-expr (fn [e a _ _]
(cond
(satisfies? CustomPred e) ::custom-pred
(and (map? a) (not (sorted? a)) (contains? a ::from-each-flag)) ::from-each
(and (map? a) (not (sorted? a)) (contains? a ::in-flag)) ::in
(satisfies? CustomPred e) ::custom-pred
(and (map? e) (not (sorted? e)) (contains? e ::more)) ::more
(= e a) ::equals
(and (string? e) (string? a)) ::strings
Expand Down Expand Up @@ -476,17 +476,19 @@
:raw [str-e str-a]
:result ["regex" (pr-str e) "not found in" (pr-str a)]}))

(defmethod compare-expr ::strings [e a str-e str-a]
(defn strings-difference [e a]
(let [matches (->> (map vector e a) (take-while (partial apply =)) (map first) (apply str))
e-diverges (clojure.string/replace e matches "")
a-diverges (clojure.string/replace a matches "")]
{:type :fail :raw [str-e str-a]
:result ["expected:" (pr-str e)
"\n was:" (pr-str a)]
:message (str
"matches: " (pr-str matches)
"\n diverges: " (pr-str e-diverges)
"\n &: " (pr-str a-diverges))}))
(str " matches: " (pr-str matches)
"\n diverges: " (pr-str e-diverges)
"\n &: " (pr-str a-diverges))))

(defmethod compare-expr ::strings [e a str-e str-a]
{:type :fail :raw [str-e str-a]
:result ["expected:" (pr-str e)
"\n was:" (pr-str a)]
:message (strings-difference e a)})

(defmethod compare-expr ::expect-exception [e a str-e str-a]
(if (instance? e a)
Expand Down Expand Up @@ -704,3 +706,23 @@
([^double v] (approximately v 0.001))
([^double v ^double d]
(fn [x] (<= (- v (Math/abs d)) x (+ v (Math/abs d))))))

(defrecord Functionally [e-fn a-fn differ]
CustomPred
(expect-fn [e a] (= (e-fn a) (a-fn a)))
(expected-message [e a str-e str-a] (format "expected: %s" (e-fn a)))
(actual-message [e a str-e str-a] (format " actual: %s" (a-fn a)))
(message [e a str-e str-a]
(if differ
(differ (e-fn a) (a-fn a))
"not functionally equivalent")))

(defn functionally
"Given a pair of functions, return a custom predicate that checks that they
return the same result when applied to a value. May optionally accept a
'difference' function that should accept the result of each function and
return a string explaininhg how they actually differ.
For explaining strings, you could use expectations/strings-difference."
([expected-fn actual-fn] (->Functionally expected-fn actual-fn nil))
([expected-fn actual-fn difference-fn]
(->Functionally expected-fn actual-fn difference-fn)))
6 changes: 5 additions & 1 deletion test/cljc/success/success_examples.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns success.success-examples
(:require #?(:clj [expectations :refer :all]
:cljs [expectations
:refer [approximately in localize no-op]
:refer [approximately functionally in localize no-op
strings-difference]
:refer-macros [expanding
expect
expect-focused
Expand Down Expand Up @@ -268,3 +269,6 @@
(expect (approximately 1000 10) 1009)

(expect (approximately 0.333) 0.333)

(expect (functionally str name strings-difference)
(from-each [s ['a "b"]] s))

0 comments on commit c041568

Please sign in to comment.