Add function testing parsers with QuickCheck
authorMichele Tartara <mtartara@google.com>
Wed, 19 Jun 2013 16:40:58 +0000 (16:40 +0000)
committerMichele Tartara <mtartara@google.com>
Mon, 1 Jul 2013 11:34:18 +0000 (11:34 +0000)
Parsing a string automatically generated by QuickCheck can be useful in many
cases. A function for doing this is added to the common test functions.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Helga Velroyen <helgav@google.com>

test/hs/Test/Ganeti/TestCommon.hs

index be014fd..4246ffe 100644 (file)
@@ -62,6 +62,7 @@ module Test.Ganeti.TestCommon
   , readTestData
   , genSample
   , testParser
+  , genPropParser
   , genNonNegative
   ) where
 
@@ -377,6 +378,13 @@ testParser parser fileName expectedContent = do
     Left msg -> HUnit.assertFailure $ "Parsing failed: " ++ msg
     Right obtained -> HUnit.assertEqual fileName expectedContent obtained
 
+-- | Generate a property test for parsers.
+genPropParser :: (Show a, Eq a) => Parser a -> String -> a -> Property
+genPropParser parser s expected =
+  case parseOnly parser $ pack s of
+    Left msg -> failTest $ "Parsing failed: " ++ msg
+    Right obtained -> expected ==? obtained
+
 -- | Generate an arbitrary non negative integer number
 genNonNegative :: Gen Int
 genNonNegative =