wordcount_spec.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. -- Test for wordcount() function
  2. local t = require('test.testutil')
  3. local n = require('test.functional.testnvim')()
  4. local feed, insert, source = n.feed, n.insert, n.source
  5. local clear, command = n.clear, n.command
  6. local eq, eval = t.eq, n.eval
  7. local poke_eventloop = n.poke_eventloop
  8. describe('wordcount', function()
  9. before_each(clear)
  10. it('is working', function()
  11. command('set selection=inclusive fileformat=unix fileformats=unix')
  12. insert([=[
  13. RESULT test:]=])
  14. poke_eventloop()
  15. command('new')
  16. source([=[
  17. function DoRecordWin(...)
  18. wincmd k
  19. if exists("a:1")
  20. call cursor(a:1)
  21. endif
  22. let result=[]
  23. call add(result, getline(1, '$'))
  24. call add(result, wordcount())
  25. wincmd j
  26. return result
  27. endfunction
  28. ]=])
  29. source([=[
  30. function PutInWindow(args)
  31. wincmd k
  32. %d _
  33. call append(1, a:args)
  34. wincmd j
  35. endfunction
  36. ]=])
  37. source([=[
  38. function! STL()
  39. if mode() =~? 'V'
  40. let g:visual_stat=wordcount()
  41. endif
  42. return string(wordcount())
  43. endfunction
  44. ]=])
  45. -- Test 1: empty window
  46. eq(
  47. eval([=[
  48. [[''], {'chars': 0, 'cursor_chars': 0, 'words': 0, 'cursor_words': 0, 'bytes': 0, 'cursor_bytes': 0}]
  49. ]=]),
  50. eval('DoRecordWin()')
  51. )
  52. -- Test 2: some words, cursor at start
  53. command([[call PutInWindow('one two three')]])
  54. eq(
  55. eval([=[
  56. [['', 'one two three'], {'chars': 15, 'cursor_chars': 1, 'words': 3, 'cursor_words': 0, 'bytes': 15, 'cursor_bytes': 1}]
  57. ]=]),
  58. eval('DoRecordWin([1, 1, 0])')
  59. )
  60. -- Test 3: some words, cursor at end
  61. command([[call PutInWindow('one two three')]])
  62. eq(
  63. eval([=[
  64. [['', 'one two three'], {'chars': 15, 'cursor_chars': 14, 'words': 3, 'cursor_words': 3, 'bytes': 15, 'cursor_bytes': 14}]
  65. ]=]),
  66. eval('DoRecordWin([2, 99, 0])')
  67. )
  68. -- Test 4: some words, cursor at end, ve=all
  69. command('set ve=all')
  70. command([[call PutInWindow('one two three')]])
  71. eq(
  72. eval([=[
  73. [['', 'one two three'], {'chars': 15, 'cursor_chars': 15, 'words': 3, 'cursor_words': 3, 'bytes': 15, 'cursor_bytes': 15}]
  74. ]=]),
  75. eval('DoRecordWin([2,99,0])')
  76. )
  77. command('set ve=')
  78. -- Test 5: several lines with words
  79. command([=[call PutInWindow(['one two three', 'one two three', 'one two three'])]=])
  80. eq(
  81. eval([=[
  82. [['', 'one two three', 'one two three', 'one two three'], {'chars': 43, 'cursor_chars': 42, 'words': 9, 'cursor_words': 9, 'bytes': 43, 'cursor_bytes': 42}]
  83. ]=]),
  84. eval('DoRecordWin([4,99,0])')
  85. )
  86. -- Test 6: one line with BOM set
  87. command([[call PutInWindow('one two three')]])
  88. command('wincmd k')
  89. command('set bomb')
  90. command('wincmd j')
  91. eq(
  92. eval([=[
  93. [['', 'one two three'], {'chars': 15, 'cursor_chars': 14, 'words': 3, 'cursor_words': 3, 'bytes': 18, 'cursor_bytes': 14}]
  94. ]=]),
  95. eval('DoRecordWin([2,99,0])')
  96. )
  97. command('wincmd k')
  98. command('set nobomb')
  99. command('wincmd j')
  100. -- Test 7: one line with multibyte words
  101. command([=[call PutInWindow(['Äne M¤ne Müh'])]=])
  102. eq(
  103. eval([=[
  104. [['', 'Äne M¤ne Müh'], {'chars': 14, 'cursor_chars': 13, 'words': 3, 'cursor_words': 3, 'bytes': 17, 'cursor_bytes': 16}]
  105. ]=]),
  106. eval('DoRecordWin([2,99,0])')
  107. )
  108. -- Test 8: several lines with multibyte words
  109. command([=[call PutInWindow(['Äne M¤ne Müh', 'und raus bist dü!'])]=])
  110. eq(
  111. eval([=[
  112. [['', 'Äne M¤ne Müh', 'und raus bist dü!'], {'chars': 32, 'cursor_chars': 31, 'words': 7, 'cursor_words': 7, 'bytes': 36, 'cursor_bytes': 35}]
  113. ]=]),
  114. eval('DoRecordWin([3,99,0])')
  115. )
  116. -- Test 9: visual mode, complete buffer
  117. command([=[call PutInWindow(['Äne M¤ne Müh', 'und raus bist dü!'])]=])
  118. command('wincmd k')
  119. command('set ls=2 stl=%{STL()}')
  120. -- -- Start visual mode quickly and select complete buffer.
  121. command('0')
  122. feed('V2jy<cr>')
  123. poke_eventloop()
  124. command('set stl= ls=1')
  125. command('let log=DoRecordWin([3,99,0])')
  126. command('let log[1]=g:visual_stat')
  127. eq(
  128. eval([=[
  129. [['', 'Äne M¤ne Müh', 'und raus bist dü!'], {'chars': 32, 'words': 7, 'bytes': 36, 'visual_chars': 32, 'visual_words': 7, 'visual_bytes': 36}]
  130. ]=]),
  131. eval('log')
  132. )
  133. -- Test 10: visual mode (empty)
  134. command([=[call PutInWindow(['Äne M¤ne Müh', 'und raus bist dü!'])]=])
  135. command('wincmd k')
  136. command('set ls=2 stl=%{STL()}')
  137. -- Start visual mode quickly and select complete buffer.
  138. command('0')
  139. feed('v$y<cr>')
  140. poke_eventloop()
  141. command('set stl= ls=1')
  142. command('let log=DoRecordWin([3,99,0])')
  143. command('let log[1]=g:visual_stat')
  144. eq(
  145. eval([=[
  146. [['', 'Äne M¤ne Müh', 'und raus bist dü!'], {'chars': 32, 'words': 7, 'bytes': 36, 'visual_chars': 1, 'visual_words': 0, 'visual_bytes': 1}]
  147. ]=]),
  148. eval('log')
  149. )
  150. -- Test 11: visual mode, single line
  151. command([=[call PutInWindow(['Äne M¤ne Müh', 'und raus bist dü!'])]=])
  152. command('wincmd k')
  153. command('set ls=2 stl=%{STL()}')
  154. -- Start visual mode quickly and select complete buffer.
  155. command('2')
  156. feed('0v$y<cr>')
  157. poke_eventloop()
  158. command('set stl= ls=1')
  159. command('let log=DoRecordWin([3,99,0])')
  160. command('let log[1]=g:visual_stat')
  161. eq(
  162. eval([=[
  163. [['', 'Äne M¤ne Müh', 'und raus bist dü!'], {'chars': 32, 'words': 7, 'bytes': 36, 'visual_chars': 13, 'visual_words': 3, 'visual_bytes': 16}]
  164. ]=]),
  165. eval('log')
  166. )
  167. end)
  168. end)