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