wordcount_spec.lua 5.6 KB

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