robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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