​​
Version  0.9.39 of the Tupelo library <https://github.com/cloojure/tupelo>
has been released.

Tupelo is a library of helper and convenience functions to make working
with Clojure simpler, easier, and more bulletproof.

I've been working on some parsing & validation code recently and had
occasion to use a very handy function in the unit tests.  Often during
testing we wish to validate only part of a return value, and ignore certain
parts that are non-deterministic or otherwise non-diffable.  Think of
values like a timestamp, UUID, or the `#db/id` value in Datomic.  The value
of fields like these cannot be predicted in advance and are normally not
germane to problem under test.

In my problem, I have been making small "records" that look like this:


[:range {:low         low
         :high        high
         :fn-validate (fn [arg] (<= low arg high))}]


In this case, I can't just do an equals comparison in a unit test since one
cannot easily compare a function object to see if it is the expected value.
One could use `extract-keys` or similar and test a derived record, but that
is extra work (esp. for nested arrays & maps) and obscures the simple goal
of the test.

Enter the Tupelo function `wild-match`
<https://github.com/cloojure/tupelo#fast--simple-wild-card-matches>.  It
allows one to use a wild card :* to ignore values in a vector and/or map
that we don't care about.  In this case we can write the tests:

    (is (wild-match? [:range {:low  123 :high 456 :fn-validate :*}] (parse-
and-transform "123..456")))
    (is (wild-match? [:range {:low  123 :high 456 :fn-validate :*}] (parse-
and-transform "123 .. 456")))
    (is (wild-match? [:range {:low  123 :high 456 :fn-validate :*}] (parse-
and-transform "  123 .. 456 ")))
    (is (wild-match? [:range {:low -123 :high -45 :fn-validate :*}] (parse-
and-transform "-123..-45")))

We just use the :* wildcard to ignore the fields we don't care about, and
`wild-match?` takes care of the rest. Note that it works equally well on
vectors & on maps.  The function signature is:

(wild-match? pattern & values)


Note that, since we put the pattern first, we can match multiple data
values simultaneously:

(wild-match?  {:a :* :b 2}
              {:a 1  :b 2})         ;=> true

(wild-match?  [1 :* 3]
              [1 2  3]
              [1 9  3] ))           ;=> true

(wild-match?  {:a :*       :b 2}
              {:a [1 2 3]  :b 2})   ;=> true


Enjoy,
Alan

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to