mousescroll_spec.lua 4.0 KB

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