aboutsummaryrefslogtreecommitdiff
path: root/test/functional/autocmd/cmdline_spec.lua
blob: 8ec06dc14835a248199af03bf089fd0c92dd43ef (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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')

local clear = helpers.clear
local command = helpers.command
local eq = helpers.eq
local expect = helpers.expect
local eval = helpers.eval
local next_msg = helpers.next_msg
local feed = helpers.feed
local meths = helpers.meths

describe('cmdline autocommands', function()
  local channel
  before_each(function()
    clear()
    channel = meths.get_api_info()[1]
    meths.set_var("channel",channel)
    command("autocmd CmdlineEnter * call rpcnotify(g:channel, 'CmdlineEnter', v:event)")
    command("autocmd CmdlineLeave * call rpcnotify(g:channel, 'CmdlineLeave', v:event)")
    command("autocmd CmdWinEnter * call rpcnotify(g:channel, 'CmdWinEnter', v:event)")
    command("autocmd CmdWinLeave * call rpcnotify(g:channel, 'CmdWinLeave', v:event)")
  end)

  it('works', function()
    feed(':')
    eq({'notification', 'CmdlineEnter', {{cmdtype=':', cmdlevel=1}}}, next_msg())
    feed('redraw<cr>')
    eq({'notification', 'CmdlineLeave',
        {{cmdtype=':', cmdlevel=1, abort=false}}}, next_msg())

    feed(':')
    eq({'notification', 'CmdlineEnter', {{cmdtype=':', cmdlevel=1}}}, next_msg())

    -- note: feed('bork<c-c>') might not consume 'bork'
    -- due to out-of-band interrupt handling
    feed('bork<esc>')
    eq({'notification', 'CmdlineLeave',
        {{cmdtype=':', cmdlevel=1, abort=true}}}, next_msg())
  end)

  it('can abort cmdline', function()
    command("autocmd CmdlineLeave * let v:event.abort= len(getcmdline())>15")
    feed(":put! ='ok'<cr>")
    expect([[
      ok
      ]])

    feed(":put! ='blah blah'<cr>")
    expect([[
      ok
      ]])
  end)

  it('handles errors correctly', function()
    clear()
    local screen = Screen.new(72, 8)
    screen:attach()
    screen:set_default_attr_ids({
      [1] = {bold = true, foreground = Screen.colors.Blue1},
      [2] = {foreground = Screen.colors.Grey100, background = Screen.colors.Red},
      [3] = {bold = true, foreground = Screen.colors.SeaGreen4},
      [4] = {bold = true, reverse = true},
    })
    command("autocmd CmdlineEnter * echoerr 'FAIL'")
    command("autocmd CmdlineLeave * echoerr 'very error'")

    feed(':')
    screen:expect([[
                                                                              |
      {1:~                                                                       }|
      {1:~                                                                       }|
      {1:~                                                                       }|
      {4:                                                                        }|
      :                                                                       |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):FAIL}               |
      :^                                                                       |
    ]])

    feed("put ='lorem ipsum'<cr>")
    screen:expect([[
                                                                              |
      {4:                                                                        }|
      :                                                                       |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):FAIL}               |
      :put ='lorem ipsum'                                                     |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):very error}         |
                                                                              |
      {3:Press ENTER or type command to continue}^                                 |
    ]])

    -- cmdline was still executed
    feed('<cr>')
    screen:expect([[
                                                                              |
      ^lorem ipsum                                                             |
      {1:~                                                                       }|
      {1:~                                                                       }|
      {1:~                                                                       }|
      {1:~                                                                       }|
      {1:~                                                                       }|
                                                                              |
    ]])

    command("autocmd CmdlineChanged * echoerr 'change erreor'")

    -- history recall still works
    feed(":<c-p>")
    screen:expect([[
                                                                              |
      lorem ipsum                                                             |
      {4:                                                                        }|
      :                                                                       |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):FAIL}               |
      :put ='lorem ipsum'                                                     |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):change erreor}      |
      :put ='lorem ipsum'^                                                     |
    ]])

    feed("<left>")
    screen:expect([[
                                                                              |
      lorem ipsum                                                             |
      {4:                                                                        }|
      :                                                                       |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):FAIL}               |
      :put ='lorem ipsum'                                                     |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):change erreor}      |
      :put ='lorem ipsum^'                                                     |
    ]])

    -- edit still works
    feed(".")
    screen:expect([[
      {4:                                                                        }|
      :                                                                       |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):FAIL}               |
      :put ='lorem ipsum'                                                     |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):change erreor}      |
      :put ='lorem ipsum.'                                                    |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):change erreor}      |
      :put ='lorem ipsum.^'                                                    |
    ]])

    feed('<cr>')
    screen:expect([[
      :put ='lorem ipsum'                                                     |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):change erreor}      |
      :put ='lorem ipsum.'                                                    |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):change erreor}      |
      :put ='lorem ipsum.'                                                    |
      {2:E5500: autocmd has thrown an exception: Vim(echoerr):very error}         |
                                                                              |
      {3:Press ENTER or type command to continue}^                                 |
    ]])

    -- cmdline was still executed
    feed('<cr>')
    screen:expect([[
                                                                              |
      lorem ipsum                                                             |
      ^lorem ipsum.                                                            |
      {1:~                                                                       }|
      {1:~                                                                       }|
      {1:~                                                                       }|
      {1:~                                                                       }|
                                                                              |
    ]])
  end)

  it('works with nested cmdline', function()
    feed(':')
    eq({'notification', 'CmdlineEnter', {{cmdtype=':', cmdlevel=1}}}, next_msg())
    feed('<c-r>=')
    eq({'notification', 'CmdlineEnter', {{cmdtype='=', cmdlevel=2}}}, next_msg())
    feed('<c-f>')
    eq({'notification', 'CmdWinEnter', {{}}}, next_msg())
    feed(':')
    eq({'notification', 'CmdlineEnter', {{cmdtype=':', cmdlevel=3}}}, next_msg())
    feed('<c-c>')
    eq({'notification', 'CmdlineLeave', {{cmdtype=':', cmdlevel=3, abort=true}}}, next_msg())
    feed('<c-c>')
    eq({'notification', 'CmdWinLeave', {{}}}, next_msg())
    feed('1+2<cr>')
    eq({'notification', 'CmdlineLeave', {{cmdtype='=', cmdlevel=2, abort=false}}}, next_msg())
  end)

  it('supports CmdlineChanged' ,function()
    command("autocmd CmdlineChanged * call rpcnotify(g:channel, 'CmdlineChanged', v:event, getcmdline())")
    feed(':')
    eq({'notification', 'CmdlineEnter', {{cmdtype=':', cmdlevel=1}}}, next_msg())
    feed('l')
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "l"}}, next_msg())
    feed('e')
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "le"}}, next_msg())
    feed('t')
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "let"}}, next_msg())
    feed('<space>')
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "let "}}, next_msg())
    feed('x')
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "let x"}}, next_msg())
    feed('<space>')
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "let x "}}, next_msg())
    feed('=')
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "let x ="}}, next_msg())
    feed('<space>')
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "let x = "}}, next_msg())
    feed('<c-r>=')
    eq({'notification', 'CmdlineEnter', {{cmdtype='=', cmdlevel=2}}}, next_msg())
    feed('1')
    eq({'notification', 'CmdlineChanged', {{cmdtype='=', cmdlevel=2}, "1"}}, next_msg())
    feed('+')
    eq({'notification', 'CmdlineChanged', {{cmdtype='=', cmdlevel=2}, "1+"}}, next_msg())
    feed('1')
    eq({'notification', 'CmdlineChanged', {{cmdtype='=', cmdlevel=2}, "1+1"}}, next_msg())
    feed('<cr>')
    eq({'notification', 'CmdlineLeave', {{cmdtype='=', cmdlevel=2, abort=false}}}, next_msg())
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "let x = "}}, next_msg())
    eq({'notification', 'CmdlineChanged', {{cmdtype=':', cmdlevel=1}, "let x = 2"}}, next_msg())
    feed('<cr>')
    eq({'notification', 'CmdlineLeave', {{cmdtype=':', cmdlevel=1, abort=false}}}, next_msg())
    eq(2, eval('x'))
  end)
end)