marick / Midje

Midje provides a migration path from clojure.test to a more flexible, readable, abstract, and gracious style of testing
MIT License
1.69k stars 129 forks source link

Fix `just` checker when expected result has maps that share keys #396

Closed philomates closed 7 years ago

philomates commented 7 years ago

Fix for https://github.com/marick/Midje/issues/348

just and contains checkers have some tricky logic that normalizes the expected and actual arguments. In a specific corner case, when actual is a map and expected is a list of maps, the normalization logic tries to turn expected into a map. This can lead to loss of information due to key merging, and hence incorrect check results.

For example, the following fact erroneously passes:

(fact
  {:a 2} => (just [{:a 1} {:a 2}])
; `just` will turn ` [{:a 1} {:a 2}]` into {:a 2} via `(into {} expected)`

Such merging in the context of just can never result in a true result, so my solution is to prevent such normalization, leaving expected as a list, hence causing the check to fail.

The same applies to contains because if actual is a map and expected is a list of maps, any merging means that the actual map can never contain the entries (which will have duplicates) in the expected list.

The last usage of standardized-arguments is for has-prefix, which doesn't have sensible semantics when actual is a map, so we don't need to worry about it.