ghcup-hs/test/optparse-test/Utils.hs

35 lines
1.0 KiB
Haskell
Raw Permalink Normal View History

2023-07-24 15:04:18 +00:00
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
2023-07-22 08:07:49 +00:00
module Utils where
import GHCup.OptParse as GHCup
import Options.Applicative
import Data.Bifunctor
2023-07-22 15:10:27 +00:00
import Test.Tasty
import Test.Tasty.HUnit
import Control.Monad.IO.Class
2023-07-22 08:07:49 +00:00
parseWith :: [String] -> IO Command
parseWith args =
optCommand <$> handleParseResult
(execParserPure defaultPrefs (info GHCup.opts fullDesc) args)
padLeft :: Int -> String -> String
padLeft desiredLength s = padding ++ s
where padding = replicate (desiredLength - length s) ' '
mapSecond :: (b -> c) -> [(a,b)] -> [(a,c)]
mapSecond = map . second
2023-07-22 09:14:49 +00:00
2023-07-22 15:10:27 +00:00
buildTestTree
:: (Eq a, Show a)
=> ([String] -> IO a) -- ^ The parse function
-> (String, [(String, a)]) -- ^ The check list @(test group, [(cli command, expected value)])@
-> TestTree
buildTestTree parse (title, checkList) =
testGroup title
$ zipWith (uncurry . check) [1 :: Int ..] checkList
where
check idx args expected = testCase (padLeft 2 (show idx) ++ "." ++ args) $ do
res <- parse (words args)
liftIO $ res @?= expected