From a7ea861a6e9d2d916f03cb204d11f57f10318aa6 Mon Sep 17 00:00:00 2001 From: Iustin Pop Date: Sat, 14 Jan 2012 23:26:58 +0100 Subject: [PATCH] Add test for CLI.parseYesNo MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also fix a tiny typo in the docstring. Signed-off-by: Iustin Pop Reviewed-by: René Nussbaumer --- htools/Ganeti/HTools/CLI.hs | 7 ++++--- htools/Ganeti/HTools/QC.hs | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/htools/Ganeti/HTools/CLI.hs b/htools/Ganeti/HTools/CLI.hs index bdee72f..cbc9160 100644 --- a/htools/Ganeti/HTools/CLI.hs +++ b/htools/Ganeti/HTools/CLI.hs @@ -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 diff --git a/htools/Ganeti/HTools/QC.hs b/htools/Ganeti/HTools/QC.hs index e5d8662..8ca45c0 100644 --- a/htools/Ganeti/HTools/QC.hs +++ b/htools/Ganeti/HTools/QC.hs @@ -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 ] -- 1.7.10.4