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