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

matcher-combinators in-any-order with timeout #28

Merged
merged 4 commits into from
Oct 8, 2020
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ A collection of test helpers.

## ns organisation

There is exactly 1 namespace meant for public consumption:
- `nedap.utils.test.api`
- `nedap.utils.test.api`
- `nedap.utils.test.matchers` matcher-combinators matchers

## Documentation

Expand Down
41 changes: 41 additions & 0 deletions src/nedap/utils/test/matchers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(:require
[clojure.string :as string]
[matcher-combinators.core :as matcher-combinators]
[matcher-combinators.matchers :as matchers]
[matcher-combinators.model :as model]
[matcher-combinators.result :as result]
[nedap.utils.test.impl :as impl]))
Expand Down Expand Up @@ -41,3 +42,43 @@
([] (gensym "G__"))
([prefix-string]
(map->Gensym {:expected prefix-string})))

#?(:clj
(defrecord InAnyOrder [expected timeout]
matcher-combinators/Matcher
(-matcher-for [this] this)
(-matcher-for [this _] this)
(-match [_this actual]
(let [result (deref
(future (matcher-combinators/match (matchers/in-any-order expected) actual))
timeout
::timeout)]
(if (#{::timeout} result)
(throw (ex-info "in-any-order timed out", {:expected expected
:actual actual}))
result)))))

#?(:clj
(defn in-any-order
"Matcher that will match when the given a list that is the same as the
`expected` list but with elements in a different order.

cancels evaluation when `:timeout` is reached (default 5000ms).

drop-in replacement for #'matcher-combinators.matchers/in-any-order"
[expected & {:keys [timeout]
:or {timeout 5000}}]
#?(:clj (map->InAnyOrder {:expected expected
:timeout timeout})
:cljs (matchers/in-any-order expected)))

:cljs
(defn in-any-order
"Matcher that will match when the given a list that is the same as the
`expected` list but with elements in a different order.

Similar to Midje's `(just expected :in-any-order)`.

equal to #'matcher-combinators.matchers/in-any-order"
[expected & {}]
(matchers/in-any-order expected)))
17 changes: 16 additions & 1 deletion test/integration/nedap/utils/test/matchers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#?(:clj [clojure.test :refer [deftest testing are is use-fixtures]] :cljs [cljs.test :refer-macros [deftest testing is are] :refer [use-fixtures]])
[matcher-combinators.model :as model]
[matcher-combinators.standalone :as standalone]
[nedap.utils.test.matchers :as sut]))
[nedap.utils.test.matchers :as sut])
#?(:clj (:import (clojure.lang ExceptionInfo))))

(deftest gensym-matcher
(are [input actual expected] (= expected
Expand All @@ -21,3 +22,17 @@
{:match/result :mismatch
:mismatch/detail {:a (model/map->Mismatch {:actual 'abc254810
:expected "abcd"})}}))

(deftest in-any-order
(testing "behaves like #'matcher-combinators.matchers/in-any-order"
(are [actual expected] (= expected (standalone/match (sut/in-any-order (range 8)) actual))
(reverse (range 8))
{:match/result :match}

(range 7)
{:match/result :mismatch, :mismatch/detail '(6 5 4 3 2 1 0 #matcher_combinators.model.Missing{:expected 7})}))

#?(:clj
(testing "will timeout after some time"
(is (thrown-with-msg? ExceptionInfo #"in-any-order timed out"
(standalone/match (sut/in-any-order (range 8) :timeout 50) (range 7)))))))