aboutsummaryrefslogtreecommitdiff
path: root/specs/Specs/FooSpec.hs
blob: 24545f9b512765d386ed36a8d64e71c1e2b7d28c (plain) (blame)
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
83
84
module Specs.FooSpec where

import Daffm.Action.Commands (parseCommand)
import Daffm.Keymap (matchKeySequence, parseKeySequence)
import Daffm.Types
import qualified Data.Map as Map
import qualified Graphics.Vty as K
import Test.Hspec

test :: SpecWith ()
test = do
  describe "matchKeySequence" $ do
    context "when key sequence is empty" $ do
      it "returns MatchFailure" $ do
        let keymap = Map.fromList [([K.KChar 'a', K.KChar 'b', K.KChar 'c'], CmdNoop)]
        matchKeySequence keymap [] `shouldBe` MatchFailure

    context "when keymap is empty" $ do
      it "returns MatchFailure" $ do
        matchKeySequence Map.empty [K.KChar 'a'] `shouldBe` MatchFailure

    context "when key sequence does not match any of the keymaps" $ do
      it "returns MatchPartial" $ do
        let keymap =
              Map.fromList
                [ ([K.KChar 'a', K.KChar 'b', K.KChar 'c'], CmdShell True "1"),
                  ([K.KChar 'a', K.KChar 'd', K.KChar 'e'], CmdShell True "2")
                ]
        let keys = [K.KChar 'a', K.KChar 'x']
        matchKeySequence keymap keys `shouldBe` MatchFailure

    context "when key sequence partially matches one of the keymaps" $ do
      it "returns MatchPartial" $ do
        let keymap =
              Map.fromList
                [ ([K.KChar 'a', K.KChar 'b', K.KChar 'c'], CmdShell True "1"),
                  ([K.KChar 'a', K.KChar 'd', K.KChar 'e'], CmdShell True "2")
                ]
        let keys = [K.KChar 'a', K.KChar 'b']
        matchKeySequence keymap keys `shouldBe` MatchPartial

    context "when key sequence matches with one of the keymaps" $ do
      it "returns MatchPartial" $ do
        let keymap =
              Map.fromList
                [ ([K.KChar 'a', K.KChar 'b', K.KChar 'c'], CmdShell True "1"),
                  ([K.KChar 'a', K.KChar 'd', K.KChar 'e'], CmdShell True "2")
                ]
        let keys = [K.KChar 'a', K.KChar 'd', K.KChar 'e']
        matchKeySequence keymap keys `shouldBe` MatchSuccess (CmdShell True "2")

  describe "parseCommand" $ do
    context "when given an invalid command" $ do
      it "returns Nothing" $ do
        parseCommand "aklsdjijm" `shouldBe` Nothing
    context "when empty command" $ do
      it "returns Nothing" $ do
        parseCommand "" `shouldBe` Nothing

    context "when command prefixed with !" $ do
      it "parses as shell command without wait for key" $ do
        parseCommand "!ls -la /" `shouldBe` Just (CmdShell False "ls -la /")

    context "when command prefixed with !!" $ do
      it "parses as shell command with wait for key" $ do
        parseCommand "!!ls -la /" `shouldBe` Just (CmdShell True "ls -la /")

    context "when given quit" $ do
      it "parses correctly" $ do
        parseCommand "quit invalid args" `shouldBe` Just CmdQuit
        parseCommand "quit" `shouldBe` Just CmdQuit

    context "when given set-cmdline" $ do
      it "parses correctly" $ do
        parseCommand "cmdline-set hello" `shouldBe` Just (CmdSetCmdline "hello")
        parseCommand "cmdline-set" `shouldBe` Just (CmdSetCmdline "")
        parseCommand "cmdline-set somespaces    " `shouldBe` Just (CmdSetCmdline "somespaces    ")

  describe "parseKey" $ do
    context "when given keys" $ do
      it "parses correctly" $ do
        parseKeySequence "gdl" `shouldBe` Just [K.KChar 'g', K.KChar 'd', K.KChar 'l']
        parseKeySequence "<tab>g<cr>" `shouldBe` Just [K.KChar '\t', K.KChar 'g', K.KEnter]
        parseKeySequence "<esc>22" `shouldBe` Just [K.KEsc, K.KChar '2', K.KChar '2']