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

Missing a "roughly" matcher for dates #112

Open
dchelimsky opened this issue Mar 30, 2020 · 2 comments
Open

Missing a "roughly" matcher for dates #112

dchelimsky opened this issue Mar 30, 2020 · 2 comments

Comments

@dchelimsky
Copy link
Contributor

No description provided.

@fernando-nubank
Copy link
Contributor

I guess we could create a roughly matcher that have different behaviors for different data types like in equals.

@philomates
Copy link
Collaborator

some examples of how to do this in user land:

(defn roughly-same-time [expected]
  (matcher-combinators.core/->PredMatcher
    (fn [actual]
      (and (instance? java.time.Instant actual)
           (.isAfter expected (.plus actual -5 java.time.temporal.ChronoUnit/SECONDS))
           (.isBefore expected (.plus actual 5 java.time.temporal.ChronoUnit/SECONDS))))
    (str "similar-time " expected " (+/- 5 seconds)")))

(is (match? {:scheduled-for-datetime (roughly-same-time this-month)}
            (some-expr)))

which gives mismatch info like this

FAIL
expected: (match? {:scheduled-for-datetime (roughly-same-time this-month)} (some-expr))
  actual: {:scheduled-for-datetime
 (mismatch
  (expected
   "similar-time 2022-08-30T16:36:25.588616447Z (+/- 5 seconds)")
  (actual #inst "2022-07-30T16:36:25.588Z"))}

or

(require '[matcher-combinators.matchers :as m])
(is (match? (m/via #(.toEpochMilli %)
                   (m/within-delta 100 (.toEpochMilli (Instant/now))))
            (some-expr)))

which gives mismatch info like this

FAIL
expected: (match? (m/via (fn* [p1__9809#] (.toEpochMilli p1__9809#)) (m/within-delta 100 (.toEpochMilli (Instant/now)))) (some-expr))
  actual: {:expected "within-delta 1671014561060 (+/- 100)" :actual 1671014560660}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants