1" Test for wordcount() function
2
3if !has('multi_byte')
4  finish
5endif
6
7func Test_wordcount()
8  let save_enc = &enc
9  set encoding=utf-8
10  set selection=inclusive fileformat=unix fileformats=unix
11
12  new
13
14  " Test 1: empty window
15  call assert_equal({'chars': 0, 'cursor_chars': 0, 'words': 0, 'cursor_words': 0,
16				\ 'bytes': 0, 'cursor_bytes': 0}, wordcount())
17
18  " Test 2: some words, cursor at start
19  call append(1, 'one two three')
20  call cursor([1, 1, 0])
21  call assert_equal({'chars': 15, 'cursor_chars': 1, 'words': 3, 'cursor_words': 0,
22				\ 'bytes': 15, 'cursor_bytes': 1}, wordcount())
23
24  " Test 3: some words, cursor at end
25  %d _
26  call append(1, 'one two three')
27  call cursor([2, 99, 0])
28  call assert_equal({'chars': 15, 'cursor_chars': 14, 'words': 3, 'cursor_words': 3,
29				\ 'bytes': 15, 'cursor_bytes': 14}, wordcount())
30
31  " Test 4: some words, cursor at end, ve=all
32  set ve=all
33  %d _
34  call append(1, 'one two three')
35  call cursor([2, 99, 0])
36  call assert_equal({'chars': 15, 'cursor_chars': 15, 'words': 3, 'cursor_words': 3,
37				\ 'bytes': 15, 'cursor_bytes': 15}, wordcount())
38  set ve=
39
40  " Test 5: several lines with words
41  %d _
42  call append(1, ['one two three', 'one two three', 'one two three'])
43  call cursor([4, 99, 0])
44  call assert_equal({'chars': 43, 'cursor_chars': 42, 'words': 9, 'cursor_words': 9,
45				\ 'bytes': 43, 'cursor_bytes': 42}, wordcount())
46
47  " Test 6: one line with BOM set
48  %d _
49  call append(1, 'one two three')
50  set bomb
51  w! Xtest
52  call cursor([2, 99, 0])
53  call assert_equal({'chars': 15, 'cursor_chars': 14, 'words': 3, 'cursor_words': 3,
54				\ 'bytes': 18, 'cursor_bytes': 14}, wordcount())
55  set nobomb
56  w!
57  call delete('Xtest')
58
59  " Test 7: one line with multibyte words
60  %d _
61  call append(1, ['Äne M¤ne Müh'])
62  call cursor([2, 99, 0])
63  call assert_equal({'chars': 14, 'cursor_chars': 13, 'words': 3, 'cursor_words': 3,
64				\ 'bytes': 17, 'cursor_bytes': 16}, wordcount())
65
66  " Test 8: several lines with multibyte words
67  %d _
68  call append(1, ['Äne M¤ne Müh', 'und raus bist dü!'])
69  call cursor([3, 99, 0])
70  call assert_equal({'chars': 32, 'cursor_chars': 31, 'words': 7, 'cursor_words': 7,
71				\ 'bytes': 36, 'cursor_bytes': 35}, wordcount())
72
73  " Visual map to capture wordcount() in visual mode
74  vnoremap <expr> <F2> execute("let g:visual_stat = wordcount()")
75
76  " Test 9: visual mode, complete buffer
77  let g:visual_stat = {}
78  %d _
79  call append(1, ['Äne M¤ne Müh', 'und raus bist dü!'])
80  " start visual mode and select the complete buffer
81  0
82  exe "normal V2j\<F2>y"
83  call assert_equal({'chars': 32, 'words': 7, 'bytes': 36, 'visual_chars': 32,
84				\ 'visual_words': 7, 'visual_bytes': 36}, g:visual_stat)
85
86  " Test 10: visual mode (empty)
87  %d _
88  call append(1, ['Äne M¤ne Müh', 'und raus bist dü!'])
89  " start visual mode and select the complete buffer
90  0
91  exe "normal v$\<F2>y"
92  call assert_equal({'chars': 32, 'words': 7, 'bytes': 36, 'visual_chars': 1,
93				\ 'visual_words': 0, 'visual_bytes': 1}, g:visual_stat)
94
95  " Test 11: visual mode, single line
96  %d _
97  call append(1, ['Äne M¤ne Müh', 'und raus bist dü!'])
98  " start visual mode and select the complete buffer
99  2
100  exe "normal 0v$\<F2>y"
101  call assert_equal({'chars': 32, 'words': 7, 'bytes': 36, 'visual_chars': 13,
102				\ 'visual_words': 3, 'visual_bytes': 16}, g:visual_stat)
103
104  set selection& fileformat& fileformats&
105  let &enc = save_enc
106  enew!
107  close
108endfunc
109