spreadsheet.vim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. " vim: fdm=indent
  2. source t/config/options.vim
  3. describe 'spreadsheet'
  4. describe 'API'
  5. before
  6. new
  7. read t/fixtures/sample.txt
  8. end
  9. it 'should return the row count'
  10. Expect tablemode#spreadsheet#RowCount(2) == 2
  11. Expect tablemode#spreadsheet#RowCount(3) == 2
  12. end
  13. it 'should return the row number'
  14. Expect tablemode#spreadsheet#RowNr(2) == 1
  15. Expect tablemode#spreadsheet#RowNr(3) == 2
  16. end
  17. it 'should return the column count'
  18. Expect tablemode#spreadsheet#ColumnCount(2) == 2
  19. Expect tablemode#spreadsheet#ColumnCount(3) == 2
  20. end
  21. it 'should return the column number'
  22. call cursor(2,3)
  23. Expect tablemode#spreadsheet#ColumnNr('.') == 1
  24. call cursor(2,12)
  25. Expect tablemode#spreadsheet#ColumnNr('.') == 2
  26. end
  27. it 'should return true when in the first cell'
  28. call cursor(2,3)
  29. Expect tablemode#spreadsheet#IsFirstCell() to_be_true
  30. call cursor(2,12)
  31. Expect tablemode#spreadsheet#IsFirstCell() to_be_false
  32. end
  33. it 'should return true when in the last cell'
  34. call cursor(2,3)
  35. Expect tablemode#spreadsheet#IsLastCell() to_be_false
  36. call cursor(2,12)
  37. Expect tablemode#spreadsheet#IsLastCell() to_be_true
  38. end
  39. it 'should return the line number of the first row'
  40. Expect tablemode#spreadsheet#GetFirstRow(2) == 2
  41. Expect tablemode#spreadsheet#GetFirstRow(3) == 2
  42. end
  43. it 'should return the line nuber of the last row'
  44. Expect tablemode#spreadsheet#GetLastRow(2) == 3
  45. Expect tablemode#spreadsheet#GetLastRow(3) == 3
  46. end
  47. describe 'Math'
  48. before
  49. new
  50. read t/fixtures/cell/sample.txt
  51. end
  52. it 'should return the sum of cell range'
  53. call cursor(1,3)
  54. Expect tablemode#spreadsheet#Sum('1:2') == 4.0
  55. Expect tablemode#spreadsheet#Sum('1,1:1,2') == 3.0
  56. Expect tablemode#spreadsheet#Sum('1,1:2,2') == 10.0
  57. call cursor(2,7)
  58. Expect tablemode#spreadsheet#Sum('1:2') == 6.0
  59. Expect tablemode#spreadsheet#Sum('2,1:2,2') == 7.0
  60. end
  61. it 'should return the average of cell range'
  62. call cursor(1,3)
  63. Expect tablemode#spreadsheet#Average('1:2') == 2.0
  64. Expect tablemode#spreadsheet#Average('1,1:1,2') == 1.5
  65. Expect tablemode#spreadsheet#Average('1,1:2,2') == 2.5
  66. call cursor(2,7)
  67. Expect tablemode#spreadsheet#Average('1:2') == 3.0
  68. Expect tablemode#spreadsheet#Average('2,1:2,2') == 3.5
  69. end
  70. it 'should return the min of cell range'
  71. call cursor(1,3)
  72. Expect tablemode#spreadsheet#Min('1:2') == 1.0
  73. Expect tablemode#spreadsheet#Min('1,1:1,2') == 1.0
  74. Expect tablemode#spreadsheet#Min('1,1:2,2') == 1.0
  75. call cursor(2,7)
  76. Expect tablemode#spreadsheet#Min('1:2') == 2.0
  77. Expect tablemode#spreadsheet#Min('2,1:2,2') == 3.0
  78. end
  79. it 'should return the max of cell range'
  80. call cursor(1,3)
  81. Expect tablemode#spreadsheet#Max('1:2') == 3.0
  82. Expect tablemode#spreadsheet#Max('1,1:1,2') == 2.0
  83. Expect tablemode#spreadsheet#Max('1,1:2,2') == 4.0
  84. call cursor(2,7)
  85. Expect tablemode#spreadsheet#Max('1:2') == 4.0
  86. Expect tablemode#spreadsheet#Max('2,1:2,2') == 4.0
  87. end
  88. end
  89. describe 'Count'
  90. before
  91. new
  92. read t/fixtures/cell/counts.txt
  93. end
  94. it 'should return the count of empty cell range'
  95. call cursor(1,3)
  96. Expect tablemode#spreadsheet#CountE('1:3') == 1
  97. Expect tablemode#spreadsheet#CountE('1,1:1,3') == 0
  98. Expect tablemode#spreadsheet#CountE('2,1:2,3') == 2
  99. Expect tablemode#spreadsheet#CountE('1,1:3,3') == 2
  100. call cursor(3,11)
  101. Expect tablemode#spreadsheet#CountE('1:3') == 1
  102. Expect tablemode#spreadsheet#CountE('3,1:3,3') == 0
  103. end
  104. it 'should return the count of not-empty cell range'
  105. call cursor(1,3)
  106. Expect tablemode#spreadsheet#CountNE('1:3') == 2
  107. Expect tablemode#spreadsheet#CountNE('1,1:1,3') == 3
  108. Expect tablemode#spreadsheet#CountNE('2,1:2,3') == 1
  109. Expect tablemode#spreadsheet#CountNE('1,1:3,3') == 7
  110. call cursor(3,11)
  111. Expect tablemode#spreadsheet#CountNE('1:3') == 2
  112. Expect tablemode#spreadsheet#CountNE('3,1:3,3') == 3
  113. end
  114. it 'should return the percent count of empty cell range'
  115. call cursor(1,3)
  116. Expect tablemode#spreadsheet#PercentE('1:3') == 33
  117. Expect tablemode#spreadsheet#PercentE('1,1:1,3') == 0
  118. Expect tablemode#spreadsheet#PercentE('2,1:2,3') == 66
  119. Expect tablemode#spreadsheet#PercentE('1,1:3,3') == 22
  120. call cursor(3,11)
  121. Expect tablemode#spreadsheet#PercentE('1:3') == 33
  122. Expect tablemode#spreadsheet#PercentE('3,1:3,3') == 0
  123. end
  124. it 'should return the percent count of not-empty cell range'
  125. call cursor(1,3)
  126. Expect tablemode#spreadsheet#PercentNE('1:3') == 66
  127. Expect tablemode#spreadsheet#PercentNE('1,1:1,3') == 100
  128. Expect tablemode#spreadsheet#PercentNE('2,1:2,3') == 33
  129. Expect tablemode#spreadsheet#PercentNE('1,1:3,3') == 77
  130. call cursor(3,11)
  131. Expect tablemode#spreadsheet#PercentNE('1:3') == 66
  132. Expect tablemode#spreadsheet#PercentNE('3,1:3,3') == 100
  133. end
  134. it 'should return the average of not-empty cell range'
  135. call cursor(1,3)
  136. Expect tablemode#spreadsheet#AverageNE('1:3') == 2.5
  137. Expect tablemode#spreadsheet#AverageNE('1,1:1,3') == 2.0
  138. Expect tablemode#spreadsheet#AverageNE('2,1:2,3') == 0.0
  139. Expect tablemode#spreadsheet#AverageNE('1,1:3,3') == 3.0
  140. call cursor(3,11)
  141. Expect tablemode#spreadsheet#AverageNE('1:3') == 4.5
  142. Expect tablemode#spreadsheet#AverageNE('3,1:3,3') == 5.0
  143. end
  144. end
  145. end
  146. describe 'Manipulations'
  147. before
  148. new
  149. normal! ggdG
  150. read t/fixtures/sample.txt
  151. call cursor(2, 3)
  152. end
  153. it 'should delete a row successfully'
  154. Expect tablemode#spreadsheet#RowCount('.') == 2
  155. call tablemode#spreadsheet#DeleteRow()
  156. Expect tablemode#spreadsheet#RowCount('.') == 1
  157. end
  158. it 'should successfully delete column'
  159. Expect tablemode#spreadsheet#ColumnCount('.') == 2
  160. call tablemode#spreadsheet#DeleteColumn()
  161. Expect tablemode#spreadsheet#ColumnCount('.') == 1
  162. end
  163. it 'should successfully insert a column before the cursor'
  164. Expect tablemode#spreadsheet#ColumnCount('.') == 2
  165. call tablemode#spreadsheet#InsertColumn(0)
  166. Expect tablemode#spreadsheet#ColumnCount('.') == 3
  167. Expect getline('.') == '| | test11 | test12 |'
  168. end
  169. it 'should successfully insert a column after the cursor'
  170. normal! $
  171. Expect tablemode#spreadsheet#ColumnCount('.') == 2
  172. call tablemode#spreadsheet#InsertColumn(1)
  173. Expect tablemode#spreadsheet#ColumnCount('.') == 3
  174. Expect getline('.') == '| test11 | test12 | |'
  175. end
  176. end
  177. describe 'Manipulation of tables with headers'
  178. before
  179. new
  180. normal! ggdG
  181. let g:table_mode_header_fillchar = '='
  182. read t/fixtures/complex_header.txt
  183. call cursor(4, 7)
  184. end
  185. it 'should successfully delete a row '
  186. Expect tablemode#spreadsheet#RowCount('.') == 5
  187. call tablemode#spreadsheet#DeleteRow()
  188. Expect tablemode#spreadsheet#RowCount('.') == 4
  189. Expect getline(4) == '| 2 | 8 | b | y |'
  190. end
  191. it 'should successfully delete a column'
  192. Expect tablemode#spreadsheet#ColumnCount('.') == 4
  193. call tablemode#spreadsheet#DeleteColumn()
  194. Expect tablemode#spreadsheet#ColumnCount('.') == 3
  195. Expect getline(4) == '| 9 | a | z |'
  196. end
  197. it 'should successfully insert a column before the cursor'
  198. Expect tablemode#spreadsheet#ColumnCount('.') == 4
  199. call tablemode#spreadsheet#InsertColumn(0)
  200. Expect tablemode#spreadsheet#ColumnCount('.') == 5
  201. Expect getline(4) == '| | 1 | 9 | a | z |'
  202. end
  203. it 'should successfully insert a column after the cursor'
  204. normal! $
  205. Expect tablemode#spreadsheet#ColumnCount('.') == 4
  206. call tablemode#spreadsheet#InsertColumn(1)
  207. Expect tablemode#spreadsheet#ColumnCount('.') == 5
  208. Expect getline(4) == '| 1 | 9 | a | z | |'
  209. end
  210. end
  211. describe 'Repeated Manipulations'
  212. before
  213. new
  214. normal! ggdG
  215. read t/fixtures/big_sample.txt
  216. call cursor(2, 3)
  217. end
  218. it 'should delete multiple rows correctly'
  219. Expect tablemode#spreadsheet#RowCount('.') == 5
  220. .,.+1 call tablemode#spreadsheet#DeleteRow()
  221. Expect tablemode#spreadsheet#RowCount('.') == 3
  222. end
  223. it 'should delete multiple columns correctly'
  224. Expect tablemode#spreadsheet#ColumnCount('.') == 4
  225. .,.+1 call tablemode#spreadsheet#DeleteColumn()
  226. Expect tablemode#spreadsheet#ColumnCount('.') == 2
  227. end
  228. it 'should insert multiple columns before the cursor correctly'
  229. call cursor(2, 7)
  230. Expect tablemode#spreadsheet#ColumnCount('.') == 4
  231. execute "normal! 2:\<C-u>call tablemode#spreadsheet#InsertColumn(0)\<CR>"
  232. Expect tablemode#spreadsheet#ColumnCount('.') == 6
  233. Expect getline('.') == '| 1 | | | 9 | a | z |'
  234. end
  235. it 'should insert multiple columns after the cursor correctly'
  236. call cursor(2, 7)
  237. Expect tablemode#spreadsheet#ColumnCount('.') == 4
  238. execute "normal! 2:\<C-u>call tablemode#spreadsheet#InsertColumn(1)\<CR>"
  239. Expect tablemode#spreadsheet#ColumnCount('.') == 6
  240. Expect getline('.') == '| 1 | 9 | | | a | z |'
  241. end
  242. end
  243. describe 'Unicode table separators'
  244. before
  245. new
  246. normal! ggdG
  247. read t/fixtures/table/sample_realign_unicode_after.txt
  248. call cursor(2, 19)
  249. end
  250. it 'should not prevent the deletion of rows'
  251. Expect tablemode#spreadsheet#RowCount('.') == 4
  252. call tablemode#spreadsheet#DeleteRow()
  253. Expect tablemode#spreadsheet#RowCount('.') == 3
  254. end
  255. it 'should not prevent the deletion of columns'
  256. Expect tablemode#spreadsheet#ColumnCount('.') == 3
  257. call tablemode#spreadsheet#DeleteColumn()
  258. Expect tablemode#spreadsheet#ColumnCount('.') == 2
  259. end
  260. it 'should not prevent the insertion of columns before the cursor'
  261. Expect tablemode#spreadsheet#ColumnCount('.') == 3
  262. call tablemode#spreadsheet#InsertColumn(1)
  263. Expect tablemode#spreadsheet#ColumnCount('.') == 4
  264. end
  265. it 'should not prevent the insertion of columns after the cursor'
  266. Expect tablemode#spreadsheet#ColumnCount('.') == 3
  267. call tablemode#spreadsheet#InsertColumn(1)
  268. Expect tablemode#spreadsheet#ColumnCount('.') == 4
  269. end
  270. end
  271. describe 'Escaped table separators'
  272. before
  273. new
  274. normal! ggdG
  275. read t/fixtures/escaped_seperator.txt
  276. call cursor(2, 3)
  277. end
  278. it 'should not prevent the deletion of rows'
  279. Expect tablemode#spreadsheet#RowCount('.') == 7
  280. call tablemode#spreadsheet#DeleteRow()
  281. Expect tablemode#spreadsheet#RowCount('.') == 6
  282. Expect getline('.') == '| a separator. | |'
  283. end
  284. it 'should not prevent the deletion of columns'
  285. Expect tablemode#spreadsheet#ColumnCount('.') == 2
  286. call tablemode#spreadsheet#DeleteColumn()
  287. Expect tablemode#spreadsheet#ColumnCount('.') == 1
  288. Expect getline('.') == '| It can be escaped by a \. |'
  289. end
  290. it 'should not prevent the insertion of columns'
  291. Expect tablemode#spreadsheet#ColumnCount('.') == 2
  292. call tablemode#spreadsheet#InsertColumn(1)
  293. Expect tablemode#spreadsheet#ColumnCount('.') == 3
  294. Expect getline('.') == '| The \| works as | | It can be escaped by a \. |'
  295. end
  296. end
  297. end