1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
4 {-| Unittests for the 'Ganeti.Common' module.
10 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 module Test.Ganeti.Common
36 import Test.QuickCheck hiding (Result)
39 import qualified System.Console.GetOpt as GetOpt
42 import Test.Ganeti.TestHelper
43 import Test.Ganeti.TestCommon
45 import Ganeti.BasicTypes
48 -- | Helper to check for correct parsing of an option.
49 checkOpt :: (StandardOptions b) =>
50 (a -> Maybe String) -- ^ Converts the value into a cmdline form
51 -> b -- ^ The default options
52 -> (String -> c) -- ^ Fail test function
53 -> (String -> d -> d -> c) -- ^ Check for equality function
54 -> (a -> d) -- ^ Transforms the value to a compare val
55 -> (a, GenericOptType b, b -> d) -- ^ Triple of value, the
56 -- option, function to
57 -- extract the set value
60 checkOpt repr defaults failfn eqcheck valfn
61 (val, opt@(GetOpt.Option _ longs _ _), fn) =
63 [] -> failfn "no long options?"
65 case parseOptsInner defaults
66 ["--" ++ cmdarg ++ maybe "" ("=" ++) (repr val)]
68 Left e -> failfn $ "Failed to parse option '" ++ cmdarg ++ ": " ++
70 Right (options, _) -> eqcheck ("Wrong value in option " ++
71 cmdarg ++ "?") (valfn val) (fn options)
73 -- | Helper to check for correct and incorrect parsing of an option.
74 passFailOpt :: (StandardOptions b) =>
75 b -- ^ The default options
76 -> (String -> c) -- ^ Fail test function
77 -> c -- ^ Pass function
78 -> (GenericOptType b, String, String)
79 -- ^ The list of enabled options, fail value and pass value
81 passFailOpt defaults failfn passfn
82 (opt@(GetOpt.Option _ longs _ _), bad, good) =
83 let prefix = "--" ++ head longs ++ "="
84 good_cmd = prefix ++ good
85 bad_cmd = prefix ++ bad in
86 case (parseOptsInner defaults [bad_cmd] "prog" [opt],
87 parseOptsInner defaults [good_cmd] "prog" [opt]) of
88 (Left _, Right _) -> passfn
89 (Right _, Right _) -> failfn $ "Command line '" ++ bad_cmd ++
90 "' succeeded when it shouldn't"
91 (Left _, Left _) -> failfn $ "Command line '" ++ good_cmd ++
92 "' failed when it shouldn't"
94 failfn $ "Command line '" ++ bad_cmd ++
95 "' succeeded when it shouldn't, while command line '" ++
96 good_cmd ++ "' failed when it shouldn't"
98 -- | Helper to test that a given option is accepted OK with quick exit.
99 checkEarlyExit :: (StandardOptions a) =>
100 a -> String -> [GenericOptType a] -> Assertion
101 checkEarlyExit defaults name options =
103 case parseOptsInner defaults [param] name options of
105 assertEqual ("Program " ++ name ++
106 " returns invalid code " ++ show code ++
107 " for option " ++ param) ExitSuccess code
108 _ -> assertFailure $ "Program " ++ name ++
109 " doesn't consider option " ++
110 param ++ " as early exit one"
111 ) ["-h", "--help", "-V", "--version"]
113 -- | Test parseYesNo.
114 prop_parse_yes_no :: Bool -> Bool -> String -> Property
115 prop_parse_yes_no def testval val =
116 forAll (elements [val, "yes", "no"]) $ \actual_val ->
118 then parseYesNo def Nothing ==? Ok def
119 else let result = parseYesNo def (Just actual_val)
120 in if actual_val `elem` ["yes", "no"]
121 then result ==? Ok (actual_val == "yes")
122 else property $ isBad result