Add a small 'passTest' helper
[ganeti-local] / htest / Test / Ganeti / HTools / Types.hs
index 12a64cf..aebcc4c 100644 (file)
@@ -27,7 +27,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 -}
 
 module Test.Ganeti.HTools.Types
-  ( testTypes
+  ( testHTools_Types
   , Types.AllocPolicy(..)
   , Types.DiskTemplate(..)
   , Types.FailMode(..)
@@ -41,7 +41,6 @@ module Test.Ganeti.HTools.Types
 import Test.QuickCheck
 
 import Control.Applicative
-import qualified Text.JSON as J
 
 import Test.Ganeti.TestHelper
 import Test.Ganeti.TestCommon
@@ -57,17 +56,13 @@ allDiskTemplates = [minBound..maxBound]
 
 -- * Arbitrary instance
 
-instance Arbitrary Types.AllocPolicy where
-  arbitrary = elements [minBound..maxBound]
+$(genArbitrary ''Types.AllocPolicy)
 
-instance Arbitrary Types.DiskTemplate where
-  arbitrary = elements [minBound..maxBound]
+$(genArbitrary ''Types.DiskTemplate)
 
-instance Arbitrary Types.FailMode where
-  arbitrary = elements [minBound..maxBound]
+$(genArbitrary ''Types.FailMode)
 
-instance Arbitrary Types.EvacMode where
-  arbitrary = elements [minBound..maxBound]
+$(genArbitrary ''Types.EvacMode)
 
 instance Arbitrary a => Arbitrary (Types.OpResult a) where
   arbitrary = arbitrary >>= \c ->
@@ -127,38 +122,23 @@ instance Arbitrary Types.IPolicy where
 
 -- * Test cases
 
-prop_Types_AllocPolicy_serialisation :: Types.AllocPolicy -> Property
-prop_Types_AllocPolicy_serialisation apol =
-  case J.readJSON (J.showJSON apol) of
-    J.Ok p -> p ==? apol
-    J.Error s -> failTest $ "Failed to deserialise: " ++ s
-
-prop_Types_DiskTemplate_serialisation :: Types.DiskTemplate -> Property
-prop_Types_DiskTemplate_serialisation dt =
-  case J.readJSON (J.showJSON dt) of
-    J.Ok p -> p ==? dt
-    J.Error s -> failTest $ "Failed to deserialise: " ++ s
-
-prop_Types_ISpec_serialisation :: Types.ISpec -> Property
-prop_Types_ISpec_serialisation ispec =
-  case J.readJSON (J.showJSON ispec) of
-    J.Ok p -> p ==? ispec
-    J.Error s -> failTest $ "Failed to deserialise: " ++ s
-
-prop_Types_IPolicy_serialisation :: Types.IPolicy -> Property
-prop_Types_IPolicy_serialisation ipol =
-  case J.readJSON (J.showJSON ipol) of
-    J.Ok p -> p ==? ipol
-    J.Error s -> failTest $ "Failed to deserialise: " ++ s
-
-prop_Types_EvacMode_serialisation :: Types.EvacMode -> Property
-prop_Types_EvacMode_serialisation em =
-  case J.readJSON (J.showJSON em) of
-    J.Ok p -> p ==? em
-    J.Error s -> failTest $ "Failed to deserialise: " ++ s
-
-prop_Types_opToResult :: Types.OpResult Int -> Bool
-prop_Types_opToResult op =
+prop_AllocPolicy_serialisation :: Types.AllocPolicy -> Property
+prop_AllocPolicy_serialisation = testSerialisation
+
+prop_DiskTemplate_serialisation :: Types.DiskTemplate -> Property
+prop_DiskTemplate_serialisation = testSerialisation
+
+prop_ISpec_serialisation :: Types.ISpec -> Property
+prop_ISpec_serialisation = testSerialisation
+
+prop_IPolicy_serialisation :: Types.IPolicy -> Property
+prop_IPolicy_serialisation = testSerialisation
+
+prop_EvacMode_serialisation :: Types.EvacMode -> Property
+prop_EvacMode_serialisation = testSerialisation
+
+prop_opToResult :: Types.OpResult Int -> Bool
+prop_opToResult op =
   case op of
     Types.OpFail _ -> Types.isBad r
     Types.OpGood v -> case r of
@@ -166,8 +146,8 @@ prop_Types_opToResult op =
                         Types.Ok v' -> v == v'
   where r = Types.opToResult op
 
-prop_Types_eitherToResult :: Either String Int -> Bool
-prop_Types_eitherToResult ei =
+prop_eitherToResult :: Either String Int -> Bool
+prop_eitherToResult ei =
   case ei of
     Left _ -> Types.isBad r
     Right v -> case r of
@@ -175,12 +155,12 @@ prop_Types_eitherToResult ei =
                  Types.Ok v' -> v == v'
     where r = Types.eitherToResult ei
 
-testSuite "Types"
-            [ 'prop_Types_AllocPolicy_serialisation
-            , 'prop_Types_DiskTemplate_serialisation
-            , 'prop_Types_ISpec_serialisation
-            , 'prop_Types_IPolicy_serialisation
-            , 'prop_Types_EvacMode_serialisation
-            , 'prop_Types_opToResult
-            , 'prop_Types_eitherToResult
+testSuite "HTools/Types"
+            [ 'prop_AllocPolicy_serialisation
+            , 'prop_DiskTemplate_serialisation
+            , 'prop_ISpec_serialisation
+            , 'prop_IPolicy_serialisation
+            , 'prop_EvacMode_serialisation
+            , 'prop_opToResult
+            , 'prop_eitherToResult
             ]