Skip to content

Commit

Permalink
Make boolean options case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
UnkindPartition committed Feb 12, 2018
1 parent 7b34b68 commit b029403
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions core/Test/Tasty/Ingredients/ConsoleReporter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ newtype Quiet = Quiet Bool
deriving (Eq, Ord, Typeable)
instance IsOption Quiet where
defaultValue = Quiet False
parseValue = fmap Quiet . safeRead
parseValue = fmap Quiet . safeReadBool
optionName = return "quiet"
optionHelp = return "Do not produce any output; indicate success only by the exit code"
optionCLParser = mkFlagCLParser (short 'q') (Quiet True)
Expand All @@ -433,7 +433,7 @@ newtype HideSuccesses = HideSuccesses Bool
deriving (Eq, Ord, Typeable)
instance IsOption HideSuccesses where
defaultValue = HideSuccesses False
parseValue = fmap HideSuccesses . safeRead
parseValue = fmap HideSuccesses . safeReadBool
optionName = return "hide-successes"
optionHelp = return "Do not print tests that passed successfully"
optionCLParser = mkFlagCLParser mempty (HideSuccesses True)
Expand Down
2 changes: 1 addition & 1 deletion core/Test/Tasty/Ingredients/ListTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ newtype ListTests = ListTests Bool
deriving (Eq, Ord, Typeable)
instance IsOption ListTests where
defaultValue = ListTests False
parseValue = fmap ListTests . safeRead
parseValue = fmap ListTests . safeReadBool
optionName = return "list-tests"
optionHelp = return "Do not run the tests; just print their names"
optionCLParser = mkFlagCLParser (short 'l') (ListTests True)
Expand Down
13 changes: 12 additions & 1 deletion core/Test/Tasty/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ module Test.Tasty.Options
, mkFlagCLParser
, mkOptionCLParser
, safeRead
, safeReadBool
) where

import qualified Data.Map as Map
import Data.Map (Map)
import Data.Char (toLower)
import Data.Tagged
import Data.Proxy
import Data.Typeable
Expand All @@ -40,7 +42,8 @@ import qualified Data.Semigroup (Semigroup((<>)))
class Typeable v => IsOption v where
-- | The value to use if the option was not supplied explicitly
defaultValue :: v
-- | Try to parse an option value from a string
-- | Try to parse an option value from a string. Consider using
-- 'safeReadBool' for boolean options and 'safeRead' for numeric options.
parseValue :: String -> Maybe v
-- | The option name. It is used to form the command line option name, for
-- instance. Therefore, it had better not contain spaces or other fancy
Expand Down Expand Up @@ -155,3 +158,11 @@ safeRead :: Read a => String -> Maybe a
safeRead s
| [(x, "")] <- reads s = Just x
| otherwise = Nothing

-- | Parse a 'Bool' case-insensitively
safeReadBool :: String -> Maybe Bool
safeReadBool s =
case (map toLower s) of
"true" -> Just True
"false" -> Just False
_ -> Nothing
4 changes: 2 additions & 2 deletions quickcheck/Test/Tasty/QuickCheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ instance IsOption QuickCheckReplay where

instance IsOption QuickCheckShowReplay where
defaultValue = QuickCheckShowReplay False
parseValue = fmap QuickCheckShowReplay . safeRead
parseValue = fmap QuickCheckShowReplay . safeReadBool
optionName = return "quickcheck-show-replay"
optionHelp = return "Show a replay token for replaying tests"

Expand All @@ -130,7 +130,7 @@ instance IsOption QuickCheckMaxRatio where

instance IsOption QuickCheckVerbose where
defaultValue = QuickCheckVerbose False
parseValue = fmap QuickCheckVerbose . safeRead
parseValue = fmap QuickCheckVerbose . safeReadBool
optionName = return "quickcheck-verbose"
optionHelp = return "Show the generated test cases"
optionCLParser = mkFlagCLParser mempty (QuickCheckVerbose True)
Expand Down

0 comments on commit b029403

Please sign in to comment.