diff --git a/autoload/tablemode.vim b/autoload/tablemode.vim index a5f0668..15c7e1c 100644 --- a/autoload/tablemode.vim +++ b/autoload/tablemode.vim @@ -101,7 +101,7 @@ endfunction function! tablemode#TableizeInsertMode() "{{{2 if tablemode#IsActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator . g:table_mode_separator) - call tablemode#table#AddHeaderBorder('.') + call tablemode#table#AddBorder('.') normal! A elseif tablemode#IsActive() && getline('.') =~# (tablemode#table#StartExpr() . g:table_mode_separator) let column = tablemode#utils#strlen(substitute(getline('.')[0:col('.')], '[^' . g:table_mode_separator . ']', '', 'g')) diff --git a/autoload/tablemode/align.vim b/autoload/tablemode/align.vim index 0b670e6..d7bb17f 100644 --- a/autoload/tablemode/align.vim +++ b/autoload/tablemode/align.vim @@ -93,7 +93,7 @@ endfunction function! tablemode#align#alignments(lnum, ncols) "{{{2 let alignments = [] - if tablemode#table#IsHeader(a:lnum+1) + if tablemode#table#IsBorder(a:lnum+1) let hcols = tablemode#align#Split(getline(a:lnum+1), '[' . g:table_mode_corner . g:table_mode_corner_corner . ']') for idx in range(len(hcols)) " Right align if header diff --git a/autoload/tablemode/spreadsheet.vim b/autoload/tablemode/spreadsheet.vim index 403b688..cf29f6c 100644 --- a/autoload/tablemode/spreadsheet.vim +++ b/autoload/tablemode/spreadsheet.vim @@ -50,10 +50,10 @@ function! tablemode#spreadsheet#GetFirstRow(line) "{{{2 if tablemode#table#IsRow(a:line) let line = tablemode#utils#line(a:line) - while tablemode#table#IsRow(line - 1) || tablemode#table#IsHeader(line - 1) + while tablemode#table#IsRow(line - 1) || tablemode#table#IsBorder(line - 1) let line -= 1 endwhile - if tablemode#table#IsHeader(line) | let line += 1 | endif + if tablemode#table#IsBorder(line) | let line += 1 | endif return line endif @@ -69,10 +69,10 @@ function! tablemode#spreadsheet#GetLastRow(line) "{{{2 if tablemode#table#IsRow(a:line) let line = tablemode#utils#line(a:line) - while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1) + while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1) let line += 1 endwhile - if tablemode#table#IsHeader(line) | let line -= 1 | endif + if tablemode#table#IsBorder(line) | let line -= 1 | endif return line endif @@ -89,7 +89,7 @@ function! tablemode#spreadsheet#LineNr(row) "{{{2 let line = tablemode#spreadsheet#GetFirstRow('.') let row_nr = 0 - while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1) + while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1) if tablemode#table#IsRow(line) let row_nr += 1 if row ==# row_nr | break | endif @@ -105,7 +105,7 @@ function! tablemode#spreadsheet#RowNr(line) "{{{2 let line = tablemode#utils#line(a:line) let rowNr = 0 - while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line) + while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line) if tablemode#table#IsRow(line) | let rowNr += 1 | endif let line -= 1 endwhile @@ -117,13 +117,13 @@ function! tablemode#spreadsheet#RowCount(line) "{{{2 let line = tablemode#utils#line(a:line) let [tline, totalRowCount] = [line, 0] - while tablemode#table#IsRow(tline) || tablemode#table#IsHeader(tline) + while tablemode#table#IsRow(tline) || tablemode#table#IsBorder(tline) if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif let tline -= 1 endwhile let tline = line + 1 - while tablemode#table#IsRow(tline) || tablemode#table#IsHeader(tline) + while tablemode#table#IsRow(tline) || tablemode#table#IsBorder(tline) if tablemode#table#IsRow(tline) | let totalRowCount += 1 | endif let tline += 1 endwhile @@ -166,27 +166,27 @@ function! tablemode#spreadsheet#MoveToStartOfCell() "{{{2 endif endfunction -function! tablemode#spreadsheet#GetLastRow(line) "{{{2 +function! tablemode#spreadsheet#GetFirstRow(line) "{{{2 if tablemode#table#IsRow(a:line) let line = tablemode#utils#line(a:line) - while tablemode#table#IsRow(line + 1) || tablemode#table#IsHeader(line + 1) - let line += 1 + while tablemode#table#IsRow(line - 1) || tablemode#table#IsBorder(line - 1) + let line -= 1 endwhile - if tablemode#table#IsHeader(line) | let line -= 1 | endif + if tablemode#table#IsBorder(line) | let line += 1 | endif return line endif endfunction -function! tablemode#spreadsheet#GetFirstRow(line) "{{{2 +function! tablemode#spreadsheet#GetLastRow(line) "{{{2 if tablemode#table#IsRow(a:line) let line = tablemode#utils#line(a:line) - while tablemode#table#IsRow(line - 1) || tablemode#table#IsHeader(line - 1) - let line -= 1 + while tablemode#table#IsRow(line + 1) || tablemode#table#IsBorder(line + 1) + let line += 1 endwhile - if tablemode#table#IsHeader(line) | let line += 1 | endif + if tablemode#table#IsBorder(line) | let line -= 1 | endif return line endif diff --git a/autoload/tablemode/spreadsheet/cell.vim b/autoload/tablemode/spreadsheet/cell.vim index 1d9e769..15fd0ec 100644 --- a/autoload/tablemode/spreadsheet/cell.vim +++ b/autoload/tablemode/spreadsheet/cell.vim @@ -85,7 +85,7 @@ function! tablemode#spreadsheet#cell#GetCells(line, ...) abort if row == 0 let values = [] let line = first_row - while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line) + while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line) if tablemode#table#IsRow(line) let row_line = getline(line)[stridx(getline(line), g:table_mode_separator):strridx(getline(line), g:table_mode_separator)] call add(values, tablemode#utils#strip(get(split(row_line, g:table_mode_separator), colm>0?colm-1:colm, ''))) @@ -97,7 +97,7 @@ function! tablemode#spreadsheet#cell#GetCells(line, ...) abort let row_nr = 0 let row_diff = row > 0 ? 1 : -1 let line = row > 0 ? first_row : last_row - while tablemode#table#IsRow(line) || tablemode#table#IsHeader(line) + while tablemode#table#IsRow(line) || tablemode#table#IsBorder(line) if tablemode#table#IsRow(line) let row_nr += row_diff if row ==# row_nr | break | endif @@ -127,10 +127,27 @@ function! tablemode#spreadsheet#cell#GetCell(...) "{{{2 endfunction function! tablemode#spreadsheet#cell#GetRow(row, ...) abort "{{{2 - let line = a:0 < 1 ? '.' : a:1 + let line = a:0 ? a:1 : '.' return tablemode#spreadsheet#cell#GetCells(line, a:row) endfunction +function! tablemode#spreadsheet#cell#GetRowColumn(col, ...) abort "{{{2 + let line = a:0 ? a:1 : '.' + let row = tablemode#spreadsheet#RowNr('.') + return tablemode#spreadsheet#cell#GetCells(line, row, a:col) +endfunction + +function! tablemode#spreadsheet#cell#GetColumn(col, ...) abort "{{{2 + let line = a:0 ? a:1 : '.' + return tablemode#spreadsheet#cell#GetCells(line, 0, a:col) +endfunction + +function! tablemode#spreadsheet#cell#GetColumnRow(row, ...) abort "{{{2 + let line = a:0 ? a:1 : '.' + let col = tablemode#spreadsheet#ColumnNr('.') + return tablemode#spreadsheet#cell#GetCells(line, a:row, col) +endfunction + function! tablemode#spreadsheet#cell#GetCellRange(range, ...) abort "{{{2 if a:0 < 1 let [line, colm] = ['.', tablemode#spreadsheet#ColumnNr('.')] @@ -169,23 +186,6 @@ function! tablemode#spreadsheet#cell#GetCellRange(range, ...) abort "{{{2 return values endfunction -function! tablemode#spreadsheet#cell#GetRowColumn(col, ...) abort "{{{2 - let line = a:0 < 1 ? '.' : a:1 - let row = tablemode#spreadsheet#RowNr('.') - return tablemode#spreadsheet#cell#GetCells(line, row, a:col) -endfunction - -function! tablemode#spreadsheet#cell#GetColumn(col, ...) abort "{{{2 - let line = a:0 < 1 ? '.' : a:1 - return tablemode#spreadsheet#cell#GetCells(line, 0, a:col) -endfunction - -function! tablemode#spreadsheet#cell#GetColumnRow(row, ...) abort "{{{2 - let line = a:0 < 1 ? '.' : a:1 - let col = tablemode#spreadsheet#ColumnNr('.') - return tablemode#spreadsheet#cell#GetCells(line, a:row, col) -endfunction - function! tablemode#spreadsheet#cell#SetCell(val, ...) "{{{2 if a:0 == 0 let [line, row, colm] = ['.', tablemode#spreadsheet#RowNr('.'), tablemode#spreadsheet#ColumnNr('.')] @@ -233,7 +233,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2 for ii in range(l:count) if a:direction ==# 'l' if tablemode#spreadsheet#IsLastCell() - if !tablemode#table#IsRow(line('.') + 1) && (tablemode#table#IsHeader(line('.') + 1) && !tablemode#table#IsRow(line('.') + 2)) + if !tablemode#table#IsRow(line('.') + 1) && (tablemode#table#IsBorder(line('.') + 1) && !tablemode#table#IsRow(line('.') + 2)) return endif call tablemode#spreadsheet#cell#Motion('j', 1) @@ -248,7 +248,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2 endif elseif a:direction ==# 'h' if tablemode#spreadsheet#IsFirstCell() - if !tablemode#table#IsRow(line('.') - 1) && (tablemode#table#IsHeader(line('.') - 1) && !tablemode#table#IsRow(line('.') - 2)) + if !tablemode#table#IsRow(line('.') - 1) && (tablemode#table#IsBorder(line('.') - 1) && !tablemode#table#IsRow(line('.') - 2)) return endif call tablemode#spreadsheet#cell#Motion('k', 1) @@ -265,7 +265,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2 if tablemode#table#IsRow(line('.') + 1) " execute 'normal! ' . 1 . 'j' normal! j - elseif tablemode#table#IsHeader(line('.') + 1) && tablemode#table#IsRow(line('.') + 2) + elseif tablemode#table#IsBorder(line('.') + 1) && tablemode#table#IsRow(line('.') + 2) " execute 'normal! ' . 2 . 'j' normal! 2j endif @@ -273,7 +273,7 @@ function! tablemode#spreadsheet#cell#Motion(direction, ...) "{{{2 if tablemode#table#IsRow(line('.') - 1) " execute 'normal! ' . 1 . 'k' normal! k - elseif tablemode#table#IsHeader(line('.') - 1) && tablemode#table#IsRow(line('.') - 2) + elseif tablemode#table#IsBorder(line('.') - 1) && tablemode#table#IsRow(line('.') - 2) " execute 'normal! ' . (1 + 1) . 'k' normal! 2k endif diff --git a/autoload/tablemode/spreadsheet/formula.vim b/autoload/tablemode/spreadsheet/formula.vim index 0851bd5..cd0cd63 100644 --- a/autoload/tablemode/spreadsheet/formula.vim +++ b/autoload/tablemode/spreadsheet/formula.vim @@ -39,7 +39,7 @@ function! tablemode#spreadsheet#formula#Add(...) "{{{2 if fr !=# '' let fr = '$' . row . ',' . colm . '=' . fr let fline = tablemode#spreadsheet#GetLastRow('.') + 1 - if tablemode#table#IsHeader(fline) | let fline += 1 | endif + if tablemode#table#IsBorder(fline) | let fline += 1 | endif let cursor_pos = [line('.'), col('.')] if getline(fline) =~# 'tmf: ' " Comment line correctly @@ -129,13 +129,13 @@ function! tablemode#spreadsheet#formula#EvaluateFormulaLine() abort "{{{2 if tablemode#table#IsRow('.') " We're inside the table let line = tablemode#spreadsheet#GetLastRow('.') let fline = line + 1 - if tablemode#table#IsHeader(fline) | let fline += 1 | endif + if tablemode#table#IsBorder(fline) | let fline += 1 | endif if getline(fline) =~# 'tmf: ' let exprs = split(matchstr(getline(fline), matchexpr), ';') endif elseif getline('.') =~# 'tmf: ' " We're on the formula line let line = line('.') - 1 - if tablemode#table#IsHeader(line) | let line -= 1 | endif + if tablemode#table#IsBorder(line) | let line -= 1 | endif if tablemode#table#IsRow(line) let exprs = split(matchstr(getline('.'), matchexpr), ';') endif diff --git a/autoload/tablemode/table.vim b/autoload/tablemode/table.vim index 6de40df..c2e2480 100644 --- a/autoload/tablemode/table.vim +++ b/autoload/tablemode/table.vim @@ -96,24 +96,6 @@ function! tablemode#table#scope() "{{{2 return s: endfunction -function! tablemode#table#StartCommentExpr() "{{{2 - let cstartexpr = tablemode#table#GetCommentStart() - if tablemode#utils#strlen(cstartexpr) > 0 - return '^\s*' . cstartexpr . '\s*' - else - return '' - endif -endfunction - -function! tablemode#table#EndCommentExpr() "{{{2 - let cendexpr = tablemode#table#GetCommentEnd() - if tablemode#utils#strlen(cendexpr) > 0 - return '.*\zs\s\+' . cendexpr . '\s*$' - else - return '' - endif -endfunction - function! tablemode#table#GetCommentStart() "{{{2 let cstring = &commentstring if tablemode#utils#strlen(cstring) > 0 @@ -123,6 +105,15 @@ function! tablemode#table#GetCommentStart() "{{{2 endif endfunction +function! tablemode#table#StartCommentExpr() "{{{2 + let cstartexpr = tablemode#table#GetCommentStart() + if tablemode#utils#strlen(cstartexpr) > 0 + return '^\s*' . cstartexpr . '\s*' + else + return '' + endif +endfunction + function! tablemode#table#GetCommentEnd() "{{{2 let cstring = &commentstring if tablemode#utils#strlen(cstring) > 0 @@ -137,6 +128,15 @@ function! tablemode#table#GetCommentEnd() "{{{2 endif endfunction +function! tablemode#table#EndCommentExpr() "{{{2 + let cendexpr = tablemode#table#GetCommentEnd() + if tablemode#utils#strlen(cendexpr) > 0 + return '.*\zs\s\+' . cendexpr . '\s*$' + else + return '' + endif +endfunction + function! tablemode#table#StartExpr() "{{{2 let cstart = tablemode#table#GetCommentStart() if tablemode#utils#strlen(cstart) > 0 @@ -155,15 +155,20 @@ function! tablemode#table#EndExpr() "{{{2 endif endfunction -function! tablemode#table#IsHeader(line) "{{{2 +function! tablemode#table#IsBorder(line) "{{{2 return getline(a:line) =~# s:HeaderBorderExpr() endfunction -function! tablemode#table#IsRow(line) "{{{2 - return !tablemode#table#IsHeader(a:line) && getline(a:line) =~# (tablemode#table#StartExpr() . g:table_mode_separator) +function! tablemode#table#IsHeader(line) "{{{2 + let line = tablemode#utils#line(a:line) + return tablemode#table#IsBorder(line+1) && !tablemode#table#IsRow(line-1) && !tablemode#table#IsRow(line-2) endfunction -function! tablemode#table#AddHeaderBorder(line) "{{{2 +function! tablemode#table#IsRow(line) "{{{2 + return !tablemode#table#IsBorder(a:line) && getline(a:line) =~# (tablemode#table#StartExpr() . g:table_mode_separator) +endfunction + +function! tablemode#table#AddBorder(line) "{{{2 call setline(a:line, s:GenerateHeaderBorder(a:line)) endfunction @@ -172,8 +177,8 @@ function! tablemode#table#Realign(line) "{{{2 let lines = [] let [lnum, blines] = [line, []] - while tablemode#table#IsRow(lnum) || tablemode#table#IsHeader(lnum) - if tablemode#table#IsHeader(lnum) + while tablemode#table#IsRow(lnum) || tablemode#table#IsBorder(lnum) + if tablemode#table#IsBorder(lnum) call insert(blines, lnum) let lnum -= 1 continue @@ -183,8 +188,8 @@ function! tablemode#table#Realign(line) "{{{2 endwhile let lnum = line + 1 - while tablemode#table#IsRow(lnum) || tablemode#table#IsHeader(lnum) - if tablemode#table#IsHeader(lnum) + while tablemode#table#IsRow(lnum) || tablemode#table#IsBorder(lnum) + if tablemode#table#IsBorder(lnum) call add(blines, lnum) let lnum += 1 continue @@ -200,6 +205,6 @@ function! tablemode#table#Realign(line) "{{{2 endfor for bline in blines - call tablemode#table#AddHeaderBorder(bline) + call tablemode#table#AddBorder(bline) endfor endfunction diff --git a/t/cell.vim b/t/cell.vim index d84a0bb..5412765 100644 --- a/t/cell.vim +++ b/t/cell.vim @@ -10,7 +10,7 @@ describe 'cell' read t/fixtures/sample.txt end - it 'should return the cells' + it 'should return the cells with GetCells' Expect tablemode#spreadsheet#cell#GetCells(2, 1, 1) ==# 'test11' " Get Rows Expect tablemode#spreadsheet#cell#GetCells(2, 1) == ['test11', 'test12'] @@ -20,7 +20,17 @@ describe 'cell' Expect tablemode#spreadsheet#cell#GetCells(2, 0, 2) == ['test12', 'test22'] end - it 'should return the cells in a range' + it 'should return the row with GetRow' + Expect tablemode#spreadsheet#cell#GetRow(1, 2) == ['test11', 'test12'] + Expect tablemode#spreadsheet#cell#GetRow(2, 2) == ['test21', 'test22'] + end + + it 'should return the column with GetColumn' + Expect tablemode#spreadsheet#cell#GetColumn(1, 2) == ['test11', 'test21'] + Expect tablemode#spreadsheet#cell#GetColumn(2, 2) == ['test12', 'test22'] + end + + it 'should return the cells in a range with GetCellRange' " Entire table as range Expect tablemode#spreadsheet#cell#GetCellRange('1,1:2,2', 2, 1) == [['test11', 'test21'], ['test12', 'test22']] @@ -95,7 +105,7 @@ describe 'cell' before new normal! ggdG - call tablemode#Enable() + call tablemode#TableModeEnable() normal i|test11|test12| |test21|test22| call cursor(1, 3) end diff --git a/t/fixtures/cell/sample.txt b/t/fixtures/cell/sample.txt new file mode 100644 index 0000000..9e670d5 --- /dev/null +++ b/t/fixtures/cell/sample.txt @@ -0,0 +1,2 @@ +| 1 | 2 | +| 3 | 4 | diff --git a/t/fixtures/table/sample.txt b/t/fixtures/table/sample.txt new file mode 100644 index 0000000..e447226 --- /dev/null +++ b/t/fixtures/table/sample.txt @@ -0,0 +1,4 @@ + +| test11 | test12 | +| test21 | test22 | + diff --git a/t/fixtures/table/sample_for_header.txt b/t/fixtures/table/sample_for_header.txt new file mode 100644 index 0000000..75f8f25 --- /dev/null +++ b/t/fixtures/table/sample_for_header.txt @@ -0,0 +1,3 @@ +| test11 | test12 | + +| test21 | test22 | diff --git a/t/fixtures/table/sample_header_realign_after.txt b/t/fixtures/table/sample_header_realign_after.txt new file mode 100644 index 0000000..ec77850 --- /dev/null +++ b/t/fixtures/table/sample_header_realign_after.txt @@ -0,0 +1,4 @@ +| Title | Message | +|------:+--------:| +| t1 | msg1 | +| t2 | msg2 | diff --git a/t/fixtures/table/sample_header_realign_before.txt b/t/fixtures/table/sample_header_realign_before.txt new file mode 100644 index 0000000..045fc67 --- /dev/null +++ b/t/fixtures/table/sample_header_realign_before.txt @@ -0,0 +1,4 @@ +| Title | Message | +|------:+--------:| +| t1 | msg1 | +| t2 | msg2 | diff --git a/t/fixtures/table/sample_realign_after.txt b/t/fixtures/table/sample_realign_after.txt new file mode 100644 index 0000000..e7fd9e1 --- /dev/null +++ b/t/fixtures/table/sample_realign_after.txt @@ -0,0 +1,2 @@ +| test11 | test12 | +| test21 | test22 | diff --git a/t/fixtures/table/sample_realign_before.txt b/t/fixtures/table/sample_realign_before.txt new file mode 100644 index 0000000..d1b3499 --- /dev/null +++ b/t/fixtures/table/sample_realign_before.txt @@ -0,0 +1,2 @@ +|test11|test12| +|test21|test22| diff --git a/t/fixtures/sample_with_header.txt b/t/fixtures/table/sample_with_header.txt similarity index 83% rename from t/fixtures/sample_with_header.txt rename to t/fixtures/table/sample_with_header.txt index e108e00..9213e7f 100644 --- a/t/fixtures/sample_with_header.txt +++ b/t/fixtures/table/sample_with_header.txt @@ -1,4 +1,4 @@ - +|--------+---------| | Title | Message | |--------+---------| | test11 | test12 | diff --git a/t/spreadsheet.vim b/t/spreadsheet.vim index bbb2708..47a0a3b 100644 --- a/t/spreadsheet.vim +++ b/t/spreadsheet.vim @@ -25,6 +25,27 @@ describe 'spreadsheet' Expect tablemode#spreadsheet#ColumnCount(3) == 2 end + it 'should return the column number' + call cursor(2,3) + Expect tablemode#spreadsheet#ColumnNr('.') == 1 + call cursor(2,12) + Expect tablemode#spreadsheet#ColumnNr('.') == 2 + end + + it 'should return true when in the first cell' + call cursor(2,3) + Expect tablemode#spreadsheet#IsFirstCell() to_be_true + call cursor(2,12) + Expect tablemode#spreadsheet#IsFirstCell() to_be_false + end + + it 'should return true when in the last cell' + call cursor(2,3) + Expect tablemode#spreadsheet#IsLastCell() to_be_false + call cursor(2,12) + Expect tablemode#spreadsheet#IsLastCell() to_be_true + end + it 'should return the line number of the first row' Expect tablemode#spreadsheet#GetFirstRow(2) == 2 Expect tablemode#spreadsheet#GetFirstRow(3) == 2 @@ -34,6 +55,33 @@ describe 'spreadsheet' Expect tablemode#spreadsheet#GetLastRow(2) == 3 Expect tablemode#spreadsheet#GetLastRow(3) == 3 end + + describe 'Math' + before + new + read t/fixtures/cell/sample.txt + end + + it 'should return the sum of cell range' + call cursor(1,3) + Expect tablemode#spreadsheet#Sum('1:2') == 4.0 + Expect tablemode#spreadsheet#Sum('1,1:1,2') == 3.0 + Expect tablemode#spreadsheet#Sum('1,1:2,2') == 10.0 + call cursor(2,7) + Expect tablemode#spreadsheet#Sum('1:2') == 6.0 + Expect tablemode#spreadsheet#Sum('2,1:2,2') == 7.0 + end + + it 'should return the average of cell range' + call cursor(1,3) + Expect tablemode#spreadsheet#Average('1:2') == 2.0 + Expect tablemode#spreadsheet#Average('1,1:1,2') == 1.5 + Expect tablemode#spreadsheet#Average('1,1:2,2') == 5.0 + call cursor(2,7) + Expect tablemode#spreadsheet#Average('1:2') == 3.0 + Expect tablemode#spreadsheet#Average('2,1:2,2') == 3.5 + end + end end describe 'Manipulations' diff --git a/t/table.vim b/t/table.vim index 86e48d0..8b84065 100644 --- a/t/table.vim +++ b/t/table.vim @@ -4,40 +4,96 @@ source t/config/options.vim call vspec#hint({'scope': 'tablemode#table#scope()', 'sid': 'tablemode#table#sid()'}) describe 'table' - describe 'API' - describe 'IsRow' + describe 'IsRow' + before + new + read t/fixtures/table/sample.txt + end + + it 'should be true when on a table row' + Expect tablemode#table#IsRow(2) to_be_true + Expect tablemode#table#IsRow(3) to_be_true + end + + it 'should be false when not on a table row' + Expect tablemode#table#IsRow(1) to_be_false + Expect tablemode#table#IsRow(4) to_be_false + end + end + + describe 'IsBorder' + before + new + read t/fixtures/table/sample_with_header.txt + end + + it 'should be true on a table border' + Expect tablemode#table#IsBorder(1) to_be_true + Expect tablemode#table#IsBorder(3) to_be_true + Expect tablemode#table#IsBorder(6) to_be_true + end + + it 'should be false when not on a table border' + Expect tablemode#table#IsBorder(2) to_be_false + Expect tablemode#table#IsBorder(4) to_be_false + Expect tablemode#table#IsBorder(5) to_be_false + end + end + + describe 'IsHeader' + before + new + read t/fixtures/table/sample_with_header.txt + end + + it 'should be true on the table header' + Expect tablemode#table#IsHeader(2) to_be_true + end + + it 'should be false anywhere else' + Expect tablemode#table#IsHeader(1) to_be_false + Expect tablemode#table#IsHeader(4) to_be_false + Expect tablemode#table#IsHeader(5) to_be_false + Expect tablemode#table#IsHeader(6) to_be_false + Expect tablemode#table#IsHeader(7) to_be_false + end + end + + describe 'AddBorder' + before + new + read t/fixtures/table/sample_for_header.txt + end + + it 'should add border to line' + call tablemode#table#AddBorder(2) + Expect tablemode#table#IsHeader(1) to_be_true + Expect tablemode#table#IsBorder(2) to_be_true + end + end + + describe 'Realign' + describe 'without header alignments' before new - read t/fixtures/sample.txt + read t/fixtures/table/sample_realign_before.txt end - it 'should return true when inside a table' - Expect tablemode#table#IsRow(2) to_be_true - Expect tablemode#table#IsRow(3) to_be_true - end - - it 'should return false when outside a table' - Expect tablemode#table#IsRow(1) to_be_false - Expect tablemode#table#IsRow(4) to_be_false + it 'should be aligned properly' + call tablemode#table#Realign(1) + Expect getline(1,'$') == readfile('t/fixtures/table/sample_realign_after.txt') end end - describe 'IsHeader' + describe 'with header alignments' before new - read t/fixtures/sample_with_header.txt + read t/fixtures/table/sample_header_realign_before.txt end - it 'should return true when on a table header' - Expect tablemode#table#IsHeader(3) to_be_true - Expect tablemode#table#IsHeader(6) to_be_true - end - - it 'should return false when not on a table header' - Expect tablemode#table#IsHeader(1) to_be_false - Expect tablemode#table#IsHeader(2) to_be_false - Expect tablemode#table#IsHeader(4) to_be_false - Expect tablemode#table#IsHeader(5) to_be_false + it 'should be aligned properly' + call tablemode#table#Realign(1) + Expect getline(1,'$') == readfile('t/fixtures/table/sample_header_realign_after.txt') end end end