From f91782fca234e61d0addaf6861d37cbb7948d9f3 Mon Sep 17 00:00:00 2001 From: Dhruva Sagar Date: Wed, 9 Apr 2014 08:09:28 +0530 Subject: [PATCH] Added tests for formula evaluation * Minor fixes as detected by the tests --- autoload/tablemode/spreadsheet.vim | 10 ++++---- t/fixtures/formula/formula.txt | 7 ++++++ t/fixtures/formula/sample.txt | 7 ++++++ t/spreadsheet.vim | 37 ++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 t/fixtures/formula/formula.txt create mode 100644 t/fixtures/formula/sample.txt diff --git a/autoload/tablemode/spreadsheet.vim b/autoload/tablemode/spreadsheet.vim index 5a046a8..48b830b 100644 --- a/autoload/tablemode/spreadsheet.vim +++ b/autoload/tablemode/spreadsheet.vim @@ -233,7 +233,7 @@ endfunction " tablemode#spreadsheet#GetCells(row) - Get values of all cells in a row as a List. " tablemode#spreadsheet#GetCells(0, col) - Get values of all cells in a column as a List. " tablemode#spreadsheet#GetCells(row, col) - Get the value of table cell by given row, col. -function! tablemode#spreadsheet#GetCells(line, ...) abort "{{{2 +function! tablemode#spreadsheet#GetCells(line, ...) abort let line = tablemode#utils#line(a:line) if tablemode#table#IsATableRow(line) @@ -281,12 +281,12 @@ endfunction function! tablemode#spreadsheet#GetCell(...) "{{{2 if a:0 == 0 - let [row, colm] = [tablemode#RowNr('.'), tablemode#spreadsheet#ColumnNr('.')] + let [row, colm] = [tablemode#spreadsheet#RowNr('.'), tablemode#spreadsheet#ColumnNr('.')] elseif a:0 == 2 let [row, colm] = [a:1, a:2] endif - return tablemode#spreadsheet#GetCells('.', row, col) + return tablemode#spreadsheet#GetCells('.', row, colm) endfunction function! tablemode#spreadsheet#GetRow(row, ...) abort "{{{2 @@ -418,8 +418,8 @@ function! tablemode#spreadsheet#Average(range, ...) abort "{{{2 return s:Average(call('tablemode#spreadsheet#GetCellRange', args)) endfunction -function! tablemode#spreadsheet#AddFormula() "{{{2 - let fr = input('f=') +function! tablemode#spreadsheet#AddFormula(...) "{{{2 + let fr = a:0 ? a:1 : input('f=') let row = tablemode#spreadsheet#RowNr('.') let colm = tablemode#spreadsheet#ColumnNr('.') let indent = indent('.') diff --git a/t/fixtures/formula/formula.txt b/t/fixtures/formula/formula.txt new file mode 100644 index 0000000..6c6a190 --- /dev/null +++ b/t/fixtures/formula/formula.txt @@ -0,0 +1,7 @@ +| Item | Cost | +|----------+-------| +| Bread | 20 | +| Tomatoes | 5 | +| Pasta | 100 | +| Total | 0 | +/* tmf: $5,2=Sum(1:-1) */ diff --git a/t/fixtures/formula/sample.txt b/t/fixtures/formula/sample.txt new file mode 100644 index 0000000..2920bbc --- /dev/null +++ b/t/fixtures/formula/sample.txt @@ -0,0 +1,7 @@ +| Item | Cost | +|----------+------| +| Bread | 20 | +| Tomatoes | 5 | +| Pasta | 100 | +| Total | | +| Test | | diff --git a/t/spreadsheet.vim b/t/spreadsheet.vim index 247cd18..2a61992 100644 --- a/t/spreadsheet.vim +++ b/t/spreadsheet.vim @@ -93,4 +93,41 @@ describe 'spreadsheet' Expect tablemode#spreadsheet#ColumnCount('.') == 1 end end + + describe 'Formulas' + describe 'Add Formula' + before + new + read t/fixtures/formula/sample.txt + end + + it 'should add a formula successfully' + call cursor(6, 15) + call tablemode#spreadsheet#AddFormula("Sum(1:4)") + Expect tablemode#spreadsheet#GetCell() == '125.0' + call cursor(8, 15) + Expect getline('.') == '/* tmf: $5,2=Sum(1:4) */' + + call cursor(7, 15) + call tablemode#spreadsheet#AddFormula("Sum(1:-1)") + Expect tablemode#spreadsheet#GetCell() == '250.0' + call cursor(8, 15) + Expect getline('.') == '/* tmf: $5,2=Sum(1:4) ; $6,2=Sum(1:-1) */' + end + end + + describe 'Evaluate Formula' + before + new + read t/fixtures/formula/formula.txt + end + + it 'should evaluate the formula successfull' + call cursor(6, 15) + call tablemode#spreadsheet#EvaluateFormulaLine() + Expect &modified == 1 + Expect tablemode#spreadsheet#GetCell() == '125.0' + end + end + end end