spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Adding basic tests for supported formulas
author
Ben Vogt <[email protected]>
date
2017-01-07 18:34:08
stats
1 file(s) changed, 206 insertions(+), 4 deletions(-)
files
tests/SheetFormulaTest.ts
  1diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
  2index 43f8115..ec4cbab 100644
  3--- a/tests/SheetFormulaTest.ts
  4+++ b/tests/SheetFormulaTest.ts
  5@@ -1,5 +1,5 @@
  6 import { Sheet } from "../src/Sheet"
  7-import {assertEquals, assertArrayEquals} from "./utils/Asserts"
  8+import {assertEquals} from "./utils/Asserts"
  9 
 10 function testFormula(formula: string, expectation: any) {
 11   var sheet  = new Sheet();
 12@@ -9,21 +9,16 @@ function testFormula(formula: string, expectation: any) {
 13   assertEquals(expectation, cell.getValue());
 14 }
 15 
 16-//Test CONCATENATE formula
 17-var sheet  = new Sheet();
 18-sheet.setCell("A1", "Hello, ");
 19-sheet.setCell("A2", "World!");
 20-sheet.setCell("A3", "=CONCATENATE(A1, A2)");
 21-sheet.setCell("B1", "1000");
 22-sheet.setCell("B2", "=CONCATENATE(A1, B1)");
 23-var cell = sheet.getCell("A3");
 24-assertEquals("Hello, World!", cell.getValue());
 25-assertEquals(null, cell.getError());
 26-assertArrayEquals(['A1', 'A2'], cell.getDependencies());
 27-var cell = sheet.getCell("B2");
 28-assertEquals("Hello, 1000", cell.getValue());
 29-assertEquals(null, cell.getError());
 30-assertArrayEquals(['A1', 'B1'], cell.getDependencies());
 31+function testFormulaWithDependencies(formula: string, expectation: any, pairs: Array<Array<string>>) {
 32+  var sheet  = new Sheet();
 33+  for (var pair of pairs) {
 34+    sheet.setCell(pair[0], pair[1]);
 35+  }
 36+  sheet.setCell("A1", formula);
 37+  var cell = sheet.getCell("A1");
 38+  assertEquals(null, cell.getError());
 39+  assertEquals(expectation, cell.getValue());
 40+}
 41 
 42 // Test ABS formula
 43 testFormula("=ABS(-10)", 10);
 44@@ -71,3 +66,166 @@ testFormula("=AVERAGE(10, 20, 4.1)", 11.366666666666667);
 45 
 46 // Test AVERAGEA
 47 testFormula("=AVERAGEA(10, 20, 4.1)", 11.366666666666667);
 48+
 49+// Test AVERAGEIF
 50+testFormulaWithDependencies("=AVERAGEIF(B1:B3, '>2')", 7.5, [["B1", "1"], ["B2", "5"], ["B3", "10"]]);
 51+
 52+// Test BIN2DEC
 53+testFormula("=BIN2DEC(1010101010)", -342);
 54+
 55+// Test BIN2HEX
 56+testFormula("=BIN2HEX(1010101010)", "fffffffeaa");
 57+
 58+// Test BIN2OCT
 59+testFormula("=BIN2OCT(1010101010)", "7777777252");
 60+
 61+// Test BINOMDIST
 62+// TODO: This. FormulaJS implementation differs from GS.
 63+
 64+// Test CEIL
 65+testFormula("=CEILING(22.22, 0.1)", 22.3);
 66+
 67+// Test CHAR
 68+testFormula("=CHAR(97)", "a");
 69+
 70+// Test CODE
 71+testFormula("=CODE('a')", 97);
 72+
 73+// Test COMBIN
 74+testFormula("=COMBIN(4, 2)", 6);
 75+
 76+// Test CONCATENATE
 77+testFormula('=CONCATENATE("hey", " ", "there")', "hey there");
 78+
 79+// Test CONVERT
 80+testFormula('=CONVERT(5.1, "mm", "m")', 0.0050999999999999995);
 81+
 82+// Test CORREL
 83+testFormulaWithDependencies('=CORREL(B1:B2,B3:B4)', 1, [["B1", "9"], ["B2", "5"], ["B3", "10"], ["B4", "4"]]);
 84+
 85+// Test COS
 86+testFormula("=COS(PI())", -1);
 87+
 88+// Test COSH
 89+testFormula("=COSH(PI())", 11.591953275521522);
 90+
 91+// Test COUNT
 92+testFormulaWithDependencies('=COUNT(B1:B3)', 3, [["B1", "1"], ["B2", "5"], ["B3", "10"]]);
 93+
 94+// Test COUNTA
 95+testFormula("=COUNTA(10, 10, 22)", 3);
 96+
 97+// Test COUNTBLANK
 98+// TODO: Fix COUNTBLANK. Does not work properly.
 99+
100+// Test COUNTIF
101+testFormulaWithDependencies('=COUNTIF(B1:B3, ">4")', 2, [["B1", "1"], ["B2", "5"], ["B3", "10"]]);
102+
103+// Test COUNTIFS
104+testFormulaWithDependencies('=COUNTIFS(B1:B3, ">4", C1:C3, ">4")', 2, [["B1", "1"], ["B2", "5"], ["B3", "10"], ["C1", "1"], ["C2", "5"], ["C3", "10"]]);
105+
106+// Test COUNTUNIQUE
107+testFormulaWithDependencies('=COUNTUNIQUE(B1:B3)', 2, [["B1", "1"], ["B2", "1"], ["B3", "10"]]);
108+
109+// Test CUMIPMT
110+testFormula("=CUMIPMT(0.12, 12, 100, 1, 5, 0)", -54.39423242396348);
111+
112+// Test CUMPRINC
113+testFormula("=CUMPRINC(0.12, 12, 100, 1, 5, 0)", -26.324171373034403);
114+
115+// Test DATE
116+// TODO: DATE should parse dates correctly. Is this a display issue or a parsing issue?
117+// testFormula("=DATE(1992, 6, 24)", "6/24/1992");
118+
119+// Test DATEVALUE
120+// TODO: DATEVALUE should work.
121+// testFormula('=DATEVALUE("1992-6-24")', 33779);
122+
123+// Test DAY
124+// TODO: This should work
125+
126+// Test DAYS
127+// TODO: This should work
128+
129+// Test DAYS360
130+// TODO: This should work
131+
132+// Test DB
133+testFormula("=DB(100, 50, 10, 2, 12)", 6.2511);
134+
135+// Test DDB
136+testFormula("=DDB(100, 50, 10, 2, 2.25)", 17.4375);
137+
138+// Test DEC2BIN
139+testFormula('=DEC2BIN("100", 8)', "01100100");
140+
141+// Test DEC2HEX
142+testFormula('=DEC2HEX("100")', "64");
143+
144+// Test DEC2OCT
145+testFormula('=DEC2OCT("100")', "144");
146+
147+// Test DEGREES
148+testFormula('=DEGREES(PI())', 180);
149+
150+// Test DELTA
151+testFormula('=DELTA(2, 2)', 1);
152+
153+// Test DEVSQ
154+testFormula('=DEVSQ(1, 2)', 0.5);
155+
156+// Test DOLLAR
157+testFormula('=DOLLAR(1.2351, 4)', "$1.2351");
158+
159+// Test DOLLARDE
160+testFormula('=DOLLARDE(100.1, 32)', 100.3125);
161+
162+// Test DOLLARFR
163+testFormula('=DOLLARFR(100.1, 32)', 100.032);
164+
165+// Test AND
166+testFormula('=AND(10)', true);
167+
168+// Test EDATE
169+// TODO: This should work
170+
171+// Test EFFECT
172+testFormula('=EFFECT(0.99, 12)', 1.5890167507927795);
173+
174+// EOMONTH
175+// TODO: This should work
176+
177+// Test ERF
178+testFormula('=ERF(2)', 0.9953222650189527);
179+
180+// Test ERFC
181+testFormula('=ERFC(2)', 0.004677734981047288);
182+
183+// Test EVEN
184+testFormula('=EVEN(3)', 4);
185+
186+// Test EXACT
187+testFormula('=EXACT("m", "M")', false);
188+
189+// Test EXPONDIST
190+testFormula('=EXPONDIST(4, 0.5, false)', 0.06766764161830635);
191+
192+// Test FALSE
193+testFormula('=FALSE()', false);
194+
195+// Test FDIST
196+// TODO: This should work.
197+// testFormula('=FDIST(15.35, 7, 6)', 0.001930553432);
198+
199+// Test FINV
200+// TODO: This should work.
201+// testFormula('=FINV(0.88,1.013, 1.01)', 0.03638945475);
202+
203+// Test FISHER
204+testFormula('=FISHER(0.962)', 1.972066740199461);
205+
206+// Test FISHERINV
207+testFormula('=FISHERINV(0.962)', 0.7451676440945232);
208+
209+// Test IF
210+testFormula('=IF("m" = "m", "hit", "miss")', 'hit');