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