From 32f2e1e1025ecbebebb380caeb3e421124a4f226 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Mon, 19 Nov 2012 16:57:24 +0100 Subject: [PATCH] Simplify a few test helpers Use more monadic combinators instead of explicit code. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Adeodato Simo <dato@google.com> --- htest/Test/Ganeti/TestCommon.hs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/htest/Test/Ganeti/TestCommon.hs b/htest/Test/Ganeti/TestCommon.hs index c108079be..b45480c74 100644 --- a/htest/Test/Ganeti/TestCommon.hs +++ b/htest/Test/Ganeti/TestCommon.hs @@ -125,9 +125,7 @@ checkPythonResult (py_code, py_stdout, py_stderr) = do newtype DNSChar = DNSChar { dnsGetChar::Char } instance Arbitrary DNSChar where - arbitrary = do - x <- elements (['a'..'z'] ++ ['0'..'9'] ++ "_-") - return (DNSChar x) + arbitrary = liftM DNSChar $ elements (['a'..'z'] ++ ['0'..'9'] ++ "_-") instance Show DNSChar where show = show . dnsGetChar @@ -148,11 +146,7 @@ getFQDN = do -- | Combinator that generates a 'Maybe' using a sub-combinator. getMaybe :: Gen a -> Gen (Maybe a) -getMaybe subgen = do - bool <- arbitrary - if bool - then Just <$> subgen - else return Nothing +getMaybe subgen = oneof [ pure Nothing, liftM Just subgen ] -- | Defines a tag type. newtype TagChar = TagChar { tagGetChar :: Char } @@ -163,9 +157,7 @@ tagChar :: String tagChar = ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'] ++ ".+*/:@-" instance Arbitrary TagChar where - arbitrary = do - c <- elements tagChar - return (TagChar c) + arbitrary = liftM TagChar $ elements tagChar -- | Generates a tag genTag :: Gen [TagChar] @@ -202,9 +194,7 @@ genUniquesList cnt = newtype SmallRatio = SmallRatio Double deriving Show instance Arbitrary SmallRatio where - arbitrary = do - v <- choose (0, 1) - return $ SmallRatio v + arbitrary = liftM SmallRatio $ choose (0, 1) -- | Checks for serialisation idempotence. testSerialisation :: (Eq a, Show a, J.JSON a) => a -> Property -- GitLab