-
Notifications
You must be signed in to change notification settings - Fork 116
/
Tests.hs
82 lines (69 loc) · 2.46 KB
/
Tests.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{-# LANGUAGE TemplateHaskell #-}
module Main where
import qualified Examples.Hello as Hello
import qualified Examples.Commands as Commands
import qualified Examples.Cabal as Cabal
import qualified Examples.Alternatives as Alternatives
import Options.Applicative
import System.Exit
import Test.HUnit
import Test.Framework.Providers.HUnit
import Test.Framework.TH.Prime
run :: ParserInfo a -> [String] -> Either ParserFailure a
run = execParserPure (prefs idm)
assertLeft :: Show b => Either a b -> (a -> Assertion) -> Assertion
assertLeft x f = either f err x
where
err b = assertFailure $ "expected Left, got " ++ show b
checkHelpText :: Show a => String -> ParserInfo a -> [String] -> Assertion
checkHelpText name p args = do
let result = run p args
assertLeft result $ \(ParserFailure err code) -> do
expected <- readFile $ "tests/" ++ name ++ ".err.txt"
expected @=? err name
ExitFailure 1 @=? code
case_hello :: Assertion
case_hello = checkHelpText "hello" Hello.opts ["--help"]
case_modes :: Assertion
case_modes = checkHelpText "commands" Commands.opts ["--help"]
case_cabal :: Assertion
case_cabal = checkHelpText "cabal" Cabal.pinfo ["configure", "--help"]
case_args :: Assertion
case_args = do
let result = run Commands.opts ["hello", "foo", "bar"]
case result of
Left _ ->
assertFailure "unexpected parse error"
Right (Commands.Hello args) ->
["foo", "bar"] @=? args
Right Commands.Goodbye ->
assertFailure "unexpected result: Goodbye"
case_args_opts :: Assertion
case_args_opts = do
let result = run Commands.opts ["hello", "foo", "--bar"]
case result of
Left _ -> return ()
Right (Commands.Hello xs) ->
assertFailure $ "unexpected result: Hello " ++ show xs
Right Commands.Goodbye ->
assertFailure "unexpected result: Goodbye"
case_args_ddash :: Assertion
case_args_ddash = do
let result = run Commands.opts ["hello", "foo", "--", "--bar", "baz"]
case result of
Left _ ->
assertFailure "unexpected parse error"
Right (Commands.Hello args) ->
["foo", "--bar", "baz"] @=? args
Right Commands.Goodbye ->
assertFailure "unexpected result: Goodbye"
case_alts :: Assertion
case_alts = do
let result = run Alternatives.opts ["-b", "-a", "-b", "-a", "-a", "-b"]
case result of
Left _ -> assertFailure "unexpected parse error"
Right xs -> [b, a, b, a, a, b] @=? xs
where a = Alternatives.A
b = Alternatives.B
main :: IO ()
main = $(defaultMainGenerator)