robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40924 from justinmk/optdicte533ec5160

master
4.2 KiB177 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local command = n.command
6local clear = n.clear
7local eval = n.eval
8local eq = t.eq
9local pcall_err = t.pcall_err
10local feed = n.feed
11
12local scroll = function(direction)
13  return n.request('nvim_input_mouse', 'wheel', direction, '', 0, 2, 2)
14end
15
16local screenrow = function()
17  return n.call('screenrow')
18end
19
20local screencol = function()
21  return n.call('screencol')
22end
23
24describe("'mousescroll'", function()
25  local function should_fail(val, msg)
26    eq(msg, pcall_err(command, 'set mousescroll=' .. val))
27  end
28
29  local function should_succeed(val)
30    command('set mousescroll=' .. val)
31  end
32
33  before_each(function()
34    clear()
35    command('set nowrap lines=20 columns=20 virtualedit=all')
36    feed('100o<Esc>50G10|')
37  end)
38
39  it('handles invalid values', function()
40    -- empty string (no direction set)
41    should_fail('', 'Vim(set):E474: Invalid argument: mousescroll=')
42    -- unknown direction
43    should_fail('foo:123', "Vim(set):E474: Unknown item 'foo': mousescroll=foo:123")
44    -- integer overflow
45    should_fail(
46      'ver:99999999999999999999',
47      "Vim(set):E474: 'ver' number is out of range: mousescroll=ver:99999999999999999999"
48    )
49    -- expected digit
50    should_fail('ver:bar', "Vim(set):E474: 'ver' requires a number: mousescroll=ver:bar")
51    -- negative count
52    should_fail('ver:-1', "Vim(set):E474: 'ver' requires a number: mousescroll=ver:-1")
53  end)
54
55  it('handles valid values', function()
56    should_succeed('hor:1,ver:1') -- both directions set
57    should_succeed('hor:1') -- only horizontal
58    should_succeed('ver:1') -- only vertical
59    should_succeed('hor:0,ver:0') -- zero
60    should_succeed('hor:2147483647') -- large count
61    should_succeed('hor:1,hor:2') -- duplicate direction: last wins
62  end)
63
64  it('default set correctly', function()
65    eq('ver:3,hor:6', eval('&mousescroll'))
66
67    eq(10, screenrow())
68    scroll('up')
69    eq(13, screenrow())
70    scroll('down')
71    eq(10, screenrow())
72
73    eq(10, screencol())
74    scroll('right')
75    eq(4, screencol())
76    scroll('left')
77    eq(10, screencol())
78  end)
79
80  it('vertical scrolling falls back to default value', function()
81    command('set mousescroll=hor:1')
82    eq(10, screenrow())
83    scroll('up')
84    eq(13, screenrow())
85  end)
86
87  it('horizontal scrolling falls back to default value', function()
88    command('set mousescroll=ver:1')
89    eq(10, screencol())
90    scroll('right')
91    eq(4, screencol())
92  end)
93
94  it('count of zero disables mouse scrolling', function()
95    command('set mousescroll=hor:0,ver:0')
96
97    eq(10, screenrow())
98    scroll('up')
99    eq(10, screenrow())
100    scroll('down')
101    eq(10, screenrow())
102
103    eq(10, screencol())
104    scroll('right')
105    eq(10, screencol())
106    scroll('left')
107    eq(10, screencol())
108
109    -- vertical scrolling is still disabled with non-zero 'scrolloff' value
110    command('set scrolloff=1')
111
112    eq(10, screenrow())
113    scroll('up')
114    eq(10, screenrow())
115    scroll('down')
116    eq(10, screenrow())
117
118    -- also in insert mode
119    feed('i')
120
121    eq(10, screenrow())
122    scroll('up')
123    eq(10, screenrow())
124    scroll('down')
125    eq(10, screenrow())
126  end)
127
128  local test_vertical_scrolling = function()
129    eq(10, screenrow())
130
131    command('set mousescroll=ver:1')
132    scroll('up')
133    eq(11, screenrow())
134
135    command('set mousescroll=ver:2')
136    scroll('down')
137    eq(9, screenrow())
138
139    command('set mousescroll=ver:5')
140    scroll('up')
141    eq(14, screenrow())
142  end
143
144  it('controls vertical scrolling in normal mode', function()
145    test_vertical_scrolling()
146  end)
147
148  it('controls vertical scrolling in insert mode', function()
149    feed('i')
150    test_vertical_scrolling()
151  end)
152
153  local test_horizontal_scrolling = function()
154    eq(10, screencol())
155
156    command('set mousescroll=hor:1')
157    scroll('right')
158    eq(9, screencol())
159
160    command('set mousescroll=hor:3')
161    scroll('right')
162    eq(6, screencol())
163
164    command('set mousescroll=hor:2')
165    scroll('left')
166    eq(8, screencol())
167  end
168
169  it('controls horizontal scrolling in normal mode', function()
170    test_horizontal_scrolling()
171  end)
172
173  it('controls horizontal scrolling in insert mode', function()
174    feed('i')
175    test_horizontal_scrolling()
176  end)
177end)