Improve message for (==?) operator
authorIustin Pop <iustin@google.com>
Mon, 22 Oct 2012 12:07:06 +0000 (14:07 +0200)
committerIustin Pop <iustin@google.com>
Mon, 22 Oct 2012 15:19:24 +0000 (17:19 +0200)
After seeing how nice HUnit formats the error message on failed
'assertEqual', I think we can do better with ==?. Currently it says
(on one line): "Expected equality, but 1 /= 2".

This patch changes the code to format it similar to HUnit:

  Expected equality, but got mismatch
  expected: 1
   but got: 2

(on three lines). This makes it more clear what is the expected and
what is the wrong value.

A few tests have been modified to ensure that the expected value is
the second argument to ==?. This is different than HUnit, but makes
more sense in the operator version (I think).

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Helga Velroyen <helgav@google.com>

htest/Test/Ganeti/Confd/Utils.hs
htest/Test/Ganeti/HTools/PeerMap.hs
htest/Test/Ganeti/TestCommon.hs

index fea3dd9..f596ae3 100644 (file)
@@ -96,8 +96,8 @@ prop_bad_key salt crq =
   let signed = Confd.Utils.signMessage key_sign salt (J.encode crq)
       encoded = J.encode signed
   in printTestCase ("Accepted message signed with different key" ++ encoded) $
-    BasicTypes.Bad "HMAC verification failed" ==?
-     Confd.Utils.parseRequest key_verify encoded
+     Confd.Utils.parseRequest key_verify encoded ==?
+       BasicTypes.Bad "HMAC verification failed"
 
 testSuite "Confd/Utils"
   [ 'prop_req_sign
index c214271..810c1fd 100644 (file)
@@ -39,14 +39,14 @@ import qualified Ganeti.HTools.PeerMap as PeerMap
 prop_addIdempotent :: PeerMap.PeerMap
                    -> PeerMap.Key -> PeerMap.Elem -> Property
 prop_addIdempotent pmap key em =
-  fn puniq ==? fn (fn puniq)
+  fn (fn puniq) ==? fn puniq
     where fn = PeerMap.add key em
           puniq = PeerMap.accumArray const pmap
 
 -- | Make sure remove is idempotent.
 prop_removeIdempotent :: PeerMap.PeerMap -> PeerMap.Key -> Property
 prop_removeIdempotent pmap key =
-  fn puniq ==? fn (fn puniq)
+  fn (fn puniq) ==? fn puniq
     where fn = PeerMap.remove key
           puniq = PeerMap.accumArray const pmap
 
index a5f7490..38f9953 100644 (file)
@@ -73,14 +73,17 @@ maxOpCodes = 16
 
 -- * Helper functions
 
--- | Checks for equality with proper annotation.
+-- | Checks for equality with proper annotation. The first argument is
+-- the computed value, the second one the expected value.
 (==?) :: (Show a, Eq a) => a -> a -> Property
 (==?) x y = printTestCase
-            ("Expected equality, but '" ++
-             show x ++ "' /= '" ++ show y ++ "'") (x == y)
+            ("Expected equality, but got mismatch\nexpected: " ++
+             show x ++ "\n but got: " ++ show y) (x == y)
 infix 3 ==?
 
--- | Checks for inequality with proper annotation.
+-- | Checks for inequality with proper annotation. The first argument
+-- is the computed value, the second one the expected (not equal)
+-- value.
 (/=?) :: (Show a, Eq a) => a -> a -> Property
 (/=?) x y = printTestCase
             ("Expected inequality, but got equality: '" ++