test_wordcount.vim 3.3 KB

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