Hello Stuart & all!

As discussed in this thread:
test-is: generating and processing testing data
http://groups.google.com/group/clojure/browse_frm/thread/3e84efefd7c0bebc/3652a4a9a124cc6b

, sometimes it is necessary to test each value against each value. Example
is zeros-are-equal (as you suggested):

(deftest zeros-are-equal
  (doall (map (fn [[a b]] (is (= a b)))
              (combinations [0 0.0 0M] 2))))

Truth is that these combinations are more common than I thought. For
example, when testing equality of maps (test-equality in data_structures.clj
in test_clojure), values of maps equal, but their classes are not.

(= (sorted-map :a 1) (hash-map :a 1) (array-map :a 1)) => true

(class (sorted-map :a 1)) => clojure.lang.PersistentTreeMap
(class (hash-map :a 1)) => clojure.lang.PersistentHashMap
(class (array-map :a 1)) => clojure.lang.PersistentArrayMap

To test for this property, we can either write down all combinations (as I
did) or write deftest function for each case (equality and non-equality).
Each of these seem too verbose to me. I see 2 different solutions:

1) Allow deftest (new version of it?) to accept parameters:

(deftest each-equals-each [parameter]
  (doall (map (fn [[a b]] (is (= a b)))
              (combinations parameter 2))))

This way we could call it:
(each-equals-each [0 0.0 0M])
and
(each-equals-each [(sorted-map :a 1) (hash-map :a 1) (array-map :a 1)])

2) Create new version of 'are' - 'are-combinations' or more preferably
'are-all', which uses 'combinations' in itself:

(are-all (= _1 _2)
    0 0.0 0M)
=>
(is (= 0 0.0))
(is (= 0 0M))
(is (= 0.0 0M))

This way we can easily do:

(are-all (= _1 _2)
    (sorted-map :a 1) (hash-map :a 1) (array-map :a 1) )

and

(are-all (not= _1 _2)
    (class (sorted-map :a 1))
    (class (hash-map :a 1))
    (class (array-map :a 1)) )

or combined? ;-)

(are-all (and (= _1 _2) (not= (class _1) (class _2)))
    (sorted-map :a 1) (hash-map :a 1) (array-map :a 1) )

Personally, I prefer the second solution. It looks really elegant. The first
solution - tests with parameters - is useful too and we could use it under
different conditions.

Thank you, Frantisek

PS: What is the status of 'are' macro? Documentation still says
"Experimental. May be removed in the future.". Do you have any plans for it?
I really like it and use it heavily in test-clojure.

--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to