File: EnvironmentSpec.hs

package info (click to toggle)
haskell-hspec-core 2.11.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 656 kB
  • sloc: haskell: 8,945; makefile: 5
file content (71 lines) | stat: -rw-r--r-- 2,555 bytes parent folder | download
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
{-# OPTIONS_GHC -fno-warn-missing-fields #-}
{-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}
module GetOpt.Declarative.EnvironmentSpec (spec) where

import           Prelude ()
import           Helper

import           GetOpt.Declarative.Types
import           GetOpt.Declarative.Environment

spec :: Spec
spec = do
  describe "parseEnvironmentOption" $ do
    context "with NoArg" $ do
      let
        option :: Option Bool
        option = Option {
          optionName = "some-flag"
        , optionSetter = NoArg $ const True
        }
      it "accepts 'yes'" $ do
        parseEnvironmentOption "FOO" [("FOO_SOME_FLAG", "yes")] False option `shouldBe` Right True

      it "rejects other values" $ do
        parseEnvironmentOption "FOO" [("FOO_SOME_FLAG", "no")] False option `shouldBe` invalidValue "FOO_SOME_FLAG" "no"

    context "with Flag" $ do
      let
        option :: Option Bool
        option = Option {
          optionName = "some-flag"
        , optionSetter = Flag $ \ value _ -> value
        }
      it "accepts 'yes'" $ do
        parseEnvironmentOption "FOO" [("FOO_SOME_FLAG", "yes")] False option `shouldBe` Right True

      it "accepts 'no'" $ do
        parseEnvironmentOption "FOO" [("FOO_SOME_FLAG", "no")] True option `shouldBe` Right False

      it "rejects other values" $ do
        parseEnvironmentOption "FOO" [("FOO_SOME_FLAG", "nay")] True option `shouldBe` invalidValue "FOO_SOME_FLAG" "nay"

    context "with OptArg" $ do
      let
        option :: Option String
        option = Option {
          optionName = "some-flag"
        , optionSetter = OptArg undefined $ \ (Just arg) _ -> guard (arg == "yes") >> Just arg
        }

      it "accepts valid values" $ do
        parseEnvironmentOption "FOO" [("FOO_SOME_FLAG", "yes")] "" option `shouldBe` Right "yes"

      it "rejects invalid values" $ do
        parseEnvironmentOption "FOO" [("FOO_SOME_FLAG", "no")] "" option `shouldBe` invalidValue "FOO_SOME_FLAG" "no"

    context "with Arg" $ do
      let
        option :: Option String
        option = Option {
          optionName = "some-flag"
        , optionSetter = Arg undefined $ \ arg _ -> guard (arg == "yes") >> Just arg
        }

      it "accepts valid values" $ do
        parseEnvironmentOption "FOO" [("FOO_SOME_FLAG", "yes")] "" option `shouldBe` Right "yes"

      it "rejects invalid values" $ do
        parseEnvironmentOption "FOO" [("FOO_SOME_FLAG", "no")] "" option `shouldBe` invalidValue "FOO_SOME_FLAG" "no"
  where
    invalidValue name = Left . InvalidValue name