File: TestExCommandParsers.hs

package info (click to toggle)
haskell-yi-keymap-vim 0.19.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,512 kB
  • sloc: haskell: 6,068; makefile: 6
file content (182 lines) | stat: -rw-r--r-- 5,941 bytes parent folder | download | duplicates (4)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
{-# LANGUAGE OverloadedStrings #-}
module Vim.TestExCommandParsers (tests) where

import           Data.List (inits)
import           Data.Maybe
import           Data.Monoid
import qualified Data.Text as T
import           Test.QuickCheck
import           Test.Tasty (TestTree, testGroup)
import           Test.Tasty.QuickCheck
import           Yi.Keymap.Vim.Common
import           Yi.Keymap.Vim.Ex
import qualified Yi.Keymap.Vim.Ex.Commands.Buffer as Buffer
import qualified Yi.Keymap.Vim.Ex.Commands.BufferDelete as BufferDelete
import qualified Yi.Keymap.Vim.Ex.Commands.Delete as Delete
import qualified Yi.Keymap.Vim.Ex.Commands.Registers as Registers
import qualified Yi.Keymap.Vim.Ex.Commands.Nohl as Nohl

data CommandParser = CommandParser
    { cpDescription  :: String
    , cpParser       :: String -> Maybe ExCommand
    , cpNames        :: [String]
    , cpAcceptsBang  :: Bool
    , cpAcceptsCount :: Bool
    , cpArgs         :: Gen String
    }

addingSpace :: Gen String -> Gen String
addingSpace = fmap (" " <>)

numberString :: Gen String
numberString = (\(NonNegative n) -> show n) <$> (arbitrary :: Gen (NonNegative Int))

-- | QuickCheck Generator of buffer identifiers.
--
-- A buffer identifier is either an empty string, a "%" character, a "#"
-- character, a string containing only numbers (optionally preceeded by
-- a space), or a string containing any chars preceeded by a space. E.g.,
--
--   ["", "%", "#", " myBufferName", " 45", "45"]
--
-- TODO Don't select "", "%", "#" half of the time.
bufferIdentifier :: Gen String
bufferIdentifier =
    oneof [ addingSpace arbitrary
          , addingSpace numberString
          , numberString
          , oneof [pure "%", pure " %"]
          , oneof [pure "#", pure " #"]
          , pure ""
          ]

-- | QuickCheck generator of strings suitable for use as register names in Vim
-- ex command lines. Does not include a preceding @"@.
registerName :: Gen String
registerName =
    (:[]) <$> oneof [ elements ['0'..'9']
                    , elements ['a'..'z']
                    , elements ['A'..'Z']
                    , elements ['"', '-', '=', '*', '+', '~', '_', '/']
                    -- TODO Should the read-only registers be included here?
                    -- , element [':', '.', '%', '#']
                    ]

-- | QuickCheck generator of strings suitable for use as counts in Vim ex
-- command lines
count :: Gen String
count = numberString

commandParsers :: [CommandParser]
commandParsers =
    [ CommandParser
          "Buffer.parse"
          (Buffer.parse . Ev . T.pack)
          ["buffer", "buf", "bu", "b"]
          True
          True
          bufferIdentifier

    , CommandParser
          "BufferDelete.parse"
          (BufferDelete.parse . Ev . T.pack)
          ["bdelete", "bdel", "bd"]
          True
          False
          (unwords <$> listOf bufferIdentifier)

    , CommandParser
          "Delete.parse"
          (Delete.parse . Ev . T.pack)
          ["delete", "del", "de", "d"]
          -- XXX TODO support these weird abbreviations too?
          -- :dl, :dell, :delel, :deletl, :deletel
          -- :dp, :dep, :delp, :delep, :deletp, :deletep
          True
          False
          (oneof [ pure ""
                 , addingSpace registerName
                 , addingSpace count
                 , (<>) <$> addingSpace registerName <*> addingSpace count
                 ])

    , CommandParser
          "Registers.parse"
          (Registers.parse . Ev . T.pack)
          [ "reg"
          , "regi"
          , "regis"
          , "regist"
          , "registe"
          , "register"
          , "registers"
          ]
          False
          False
          (pure "")

    , CommandParser
          "Nohl.parse"
          (Nohl.parse . Ev . T.pack)
          (drop 3 $ inits "nohlsearch")
          False
          False
          (pure "")
    ]


commandString :: CommandParser -> Gen String
commandString cp = do
    name <- elements $ cpNames cp
    bang <- if cpAcceptsBang cp
                then elements ["!", ""]
                else pure ""
    count' <- if cpAcceptsCount cp
                then count
                else pure ""
    args <- cpArgs cp
    return $ concat [count', name, bang, args]


expectedParserParses :: CommandParser -> TestTree
expectedParserParses commandParser =
    testProperty (cpDescription commandParser <> " parses expected input") $
        forAll (commandString commandParser)
               (isJust . cpParser commandParser)


expectedParserSelected :: CommandParser -> TestTree
expectedParserSelected expectedCommandParser =
    testProperty testName $
        forAll (commandString expectedCommandParser) $ \s ->
            let expectedName = expectedCommandName (Ev $ T.pack s)
                actualName   = actualCommandName (Ev $ T.pack s)
            in counterexample (errorMessage s actualName)
                              (expectedName == actualName)
  where
    unE = T.unpack . _unEv
    expectedCommandName = commandNameFor [cpParser expectedCommandParser . unE]
    actualCommandName   = commandNameFor defExCommandParsers
    commandNameFor parsers s =
        cmdShow <$> evStringToExCommand parsers s
    errorMessage s actualName =
        "Parsed " <> show s <> " to " <> show actualName <> " command"
    testName =
       cpDescription expectedCommandParser <> " selected for expected input"



-- | Tests for the Ex command parsers in the Vim Keymap.
--
-- Tests that the parsers parse the strings they are expected to and that
-- the expected parser is selected for string.
--
-- The actions of the ex commands are not tested here.
tests :: TestTree
tests =
    testGroup "Vim keymap ex command parsers"
        [ testGroup "Expected parser parses" $
              map expectedParserParses commandParsers
        , testGroup "Expected parser selected" $
              map expectedParserSelected commandParsers
        ]