Add test for CLI.parseYesNo
authorIustin Pop <iustin@google.com>
Sat, 14 Jan 2012 22:26:58 +0000 (23:26 +0100)
committerIustin Pop <iustin@google.com>
Mon, 16 Jan 2012 12:49:37 +0000 (13:49 +0100)
Also fix a tiny typo in the docstring.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: RenĂ© Nussbaumer <rn@google.com>

htools/Ganeti/HTools/CLI.hs
htools/Ganeti/HTools/QC.hs

index bdee72f..cbc9160 100644 (file)
@@ -31,6 +31,7 @@ module Ganeti.HTools.CLI
   ( Options(..)
   , OptType
   , parseOpts
+  , parseYesNo
   , parseISpecString
   , shTemplate
   , defaultLuxiSocket
@@ -411,14 +412,14 @@ oVerbose = Option "v" ["verbose"]
 -- * Functions
 
 -- | Helper for parsing a yes\/no command line flag.
-parseYesNo :: Bool         -- ^ Default whalue (when we get a @Nothing@)
+parseYesNo :: Bool         -- ^ Default value (when we get a @Nothing@)
            -> Maybe String -- ^ Parameter value
            -> Result Bool  -- ^ Resulting boolean value
 parseYesNo v Nothing      = return v
 parseYesNo _ (Just "yes") = return True
 parseYesNo _ (Just "no")  = return False
-parseYesNo _ (Just s)     = fail $ "Invalid choice '" ++ s ++
-                            "', pass one of 'yes' or 'no'"
+parseYesNo _ (Just s)     = fail ("Invalid choice '" ++ s ++
+                                  "', pass one of 'yes' or 'no'")
 
 -- | Usage info.
 usageHelp :: String -> [OptType] -> String
index e5d8662..8ca45c0 100644 (file)
@@ -1478,7 +1478,18 @@ prop_CLI_parseISpecFail descr =
        Types.Ok v -> failTest $ "Expected failure, got " ++ show v
        _ -> property True
 
+-- | Test parseYesNo.
+prop_CLI_parseYesNo def testval val =
+  forAll (elements [val, "yes", "no"]) $ \actual_val ->
+  if testval
+    then CLI.parseYesNo def Nothing ==? Types.Ok def
+    else let result = CLI.parseYesNo def (Just actual_val)
+         in if actual_val `elem` ["yes", "no"]
+              then result ==? Types.Ok (actual_val == "yes")
+              else property $ Types.isBad result
+
 testSuite "CLI"
           [ 'prop_CLI_parseISpec
           , 'prop_CLI_parseISpecFail
+          , 'prop_CLI_parseYesNo
           ]