neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

wordcount_spec.lua (5773B)


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