From 89298c046e8ab65bef4800018c30ade9b30d1b74 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Sun, 15 Jan 2012 00:26:37 +0100
Subject: [PATCH] Add some very basic options test
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Just tests that passing a string argument returns it correctly. We're
not interested in the flags themselves that much just that the
function itself parses some options at least correctly.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: RenΓ© Nussbaumer <rn@google.com>
---
 htools/Ganeti/HTools/QC.hs | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/htools/Ganeti/HTools/QC.hs b/htools/Ganeti/HTools/QC.hs
index 8ca45c0b1..e87aabbad 100644
--- a/htools/Ganeti/HTools/QC.hs
+++ b/htools/Ganeti/HTools/QC.hs
@@ -47,9 +47,11 @@ import Data.List (findIndex, intercalate, nub, isPrefixOf)
 import qualified Data.Set as Set
 import Data.Maybe
 import Control.Monad
+import qualified System.Console.GetOpt as GetOpt
 import qualified Text.JSON as J
 import qualified Data.Map
 import qualified Data.IntMap as IntMap
+
 import qualified Ganeti.OpCodes as OpCodes
 import qualified Ganeti.Jobs as Jobs
 import qualified Ganeti.Luxi
@@ -1488,8 +1490,30 @@ prop_CLI_parseYesNo def testval val =
               then result ==? Types.Ok (actual_val == "yes")
               else property $ Types.isBad result
 
+-- | Helper to check for correct parsing of string arg.
+checkStringArg val (opt, fn) =
+  let GetOpt.Option _ longs _ _ = opt
+  in case longs of
+       [] -> failTest "no long options?"
+       cmdarg:_ ->
+         case CLI.parseOptsInner ["--" ++ cmdarg ++ "=" ++ val] "prog" [opt] of
+           Left e -> failTest $ "Failed to parse option: " ++ show e
+           Right (options, _) -> fn options ==? Just val
+
+-- | Test a few string arguments.
+prop_CLI_StringArg argument =
+  let args = [ (CLI.oDataFile,      CLI.optDataFile)
+             , (CLI.oDynuFile,      CLI.optDynuFile)
+             , (CLI.oSaveCluster,   CLI.optSaveCluster)
+             , (CLI.oReplay,        CLI.optReplay)
+             , (CLI.oPrintCommands, CLI.optShowCmds)
+             , (CLI.oLuxiSocket,    CLI.optLuxi)
+             ]
+  in conjoin $ map (checkStringArg argument) args
+
 testSuite "CLI"
           [ 'prop_CLI_parseISpec
           , 'prop_CLI_parseISpecFail
           , 'prop_CLI_parseYesNo
+          , 'prop_CLI_StringArg
           ]
-- 
GitLab