commit
message
More tests for Sheet.ts, simplifying A1CellKey
author
Ben Vogt <[email protected]>
date
2016-12-31 21:20:15
stats
4 file(s) changed,
76 insertions(+),
26 deletions(-)
files
src/A1CellKey.ts
src/Sheet.ts
tests/A1CellKeyTest.ts
tests/SheetTest.ts
1diff --git a/src/A1CellKey.ts b/src/A1CellKey.ts
2index bc1e136..c905d32 100644
3--- a/src/A1CellKey.ts
4+++ b/src/A1CellKey.ts
5@@ -2,28 +2,21 @@
6 * A1-notation style cell id. Used to index the cells.
7 * */
8 class A1CellKey {
9- private column: string;
10- private row: number;
11 private x: number;
12 private y: number;
13+ private key: string;
14
15 constructor(key: string) {
16- this.row = parseInt(key.match(/\d+$/)[0], 10);
17- this.column = key.replace(this.row.toString(), '');
18- this.x = lettersToNumber(this.column);
19- this.y = this.row - 1;
20+ this.key = key;
21+ var row = parseInt(key.match(/\d+$/)[0], 10);
22+ this.x = lettersToNumber(key.replace(row.toString(), ''));
23+ this.y = row - 1;
24 }
25 static of(x: number, y: number): A1CellKey {
26 return new A1CellKey(numberToLetters(x+1) + (y+1).toString());
27 }
28 toString(): string {
29- return this.column + "" + this.row;
30- }
31- getColumn(): string {
32- return this.column;
33- }
34- getRow(): number {
35- return this.row;
36+ return this.key;
37 }
38 getX(): number {
39 return this.x;
40diff --git a/src/Sheet.ts b/src/Sheet.ts
41index 313f340..5db49f2 100644
42--- a/src/Sheet.ts
43+++ b/src/Sheet.ts
44@@ -476,8 +476,7 @@ var Sheet = (function () {
45 var cells = instance.utils.iterateCells.call(this, coordsStart, coordsEnd),
46 result = [];
47 //update dependencies
48- // instance.matrix.updateCellItem(new A1CellKey(origin), {deps: cells.index});
49- instance.matrix.getItem(new A1CellKey(origin)).updateDependencies([cells.index]);
50+ instance.matrix.getItem(new A1CellKey(origin)).updateDependencies(cells.index);
51
52 result.push(cells.value);
53 return result;
54diff --git a/tests/A1CellKeyTest.ts b/tests/A1CellKeyTest.ts
55index 3070412..1effb1e 100644
56--- a/tests/A1CellKeyTest.ts
57+++ b/tests/A1CellKeyTest.ts
58@@ -3,24 +3,21 @@ import { assertEquals } from "./utils/Asserts"
59
60 //Test constructor
61 var one = new A1CellKey("A1");
62-assertEquals("A", one.getColumn());
63-assertEquals(1, one.getRow());
64 assertEquals(0, one.getX());
65 assertEquals(0, one.getY());
66+assertEquals("A1", one.toString());
67 var two = new A1CellKey("Z4563534");
68-assertEquals("Z", two.getColumn());
69-assertEquals(4563534, two.getRow());
70 assertEquals(25, two.getX());
71 assertEquals(4563533, two.getY());
72+assertEquals("Z4563534", two.toString());
73+
74
75 //Test static constructor
76 var one = A1CellKey.of(0, 0);
77-assertEquals("A", one.getColumn());
78-assertEquals(1, one.getRow());
79 assertEquals(0, one.getX());
80 assertEquals(0, one.getY());
81+assertEquals("A1", one.toString());
82 var two = A1CellKey.of(25, 4563533);
83-assertEquals("Z", two.getColumn());
84-assertEquals(4563534, two.getRow());
85 assertEquals(25, two.getX());
86-assertEquals(4563533, two.getY());
87\ No newline at end of file
88+assertEquals(4563533, two.getY());
89+assertEquals("Z4563534", two.toString());
90\ No newline at end of file
91diff --git a/tests/SheetTest.ts b/tests/SheetTest.ts
92index b802c97..e34dfe2 100644
93--- a/tests/SheetTest.ts
94+++ b/tests/SheetTest.ts
95@@ -1,5 +1,5 @@
96 import { Sheet } from "../src/Sheet"
97-import { assertEquals } from "./utils/Asserts"
98+import {assertEquals, assertArrayEquals} from "./utils/Asserts"
99
100 // Test setCell, getCell, valid
101 var sheet = new Sheet();
102@@ -10,7 +10,68 @@ assertEquals(22, cell.value);
103 assertEquals("A2", cell.id);
104 assertEquals(1, cell.row);
105 assertEquals(0, cell.col);
106+assertEquals(null, cell.error);
107+assertArrayEquals([], cell.dependencies);
108
109 // Test getCell, null value
110 var nullCell = sheet.getCell("N1");
111 assertEquals(null, nullCell);
112+
113+// Test setCell, with formula
114+var sheet = new Sheet();
115+sheet.setCell("A1", "1");
116+sheet.setCell("A2", "20");
117+sheet.setCell("A3", "3.4");
118+sheet.setCell("A4", "45");
119+sheet.setCell("A5", "SUM(A1:A4)");
120+var A5 = sheet.getCell("A5");
121+assertEquals("69.4", A5.value);
122+assertEquals("SUM(A1:A4)", A5.formula);
123+assertEquals(null, cell.error);
124+assertArrayEquals(['A1', 'A2', 'A3', 'A4'], A5.dependencies);
125+
126+
127+// Test load
128+var sheet = new Sheet();
129+var SUM_FORMULA = "SUM(A1:D1, H1)";
130+var MAX_FORMULA = "MAX(A2:J2)";
131+var MIN_FORMULA = "MIN(A3:J3)";
132+var AVERAGE_FORMULA = "AVERAGE(A4:J4)";
133+var SUM_IF_FORMULA = "SUMIF(A5:J5,'>5')";
134+var SUM_REF_FORMULA = "SUM(K1, K2, K3, K4)";
135+sheet.load([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, SUM_FORMULA],
136+ [-1, -10, 2, 4, 100, 1, 50, 20, 200, -100, MAX_FORMULA],
137+ [-1, -40, -53, 1, 10, 30, 10, 301, -1, -20, MIN_FORMULA],
138+ [20, 50, 100, 20, 1, 5, 15, 25, 45, 23, AVERAGE_FORMULA],
139+ [0, 10, 1, 10, 2, 10, 3, 10, 4, 10, SUM_IF_FORMULA],
140+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, SUM_REF_FORMULA]]);
141+var K1 = sheet.getCell("K1");
142+assertEquals("18", K1.value);
143+assertEquals(SUM_FORMULA, K1.formula);
144+assertEquals(null, K1.error);
145+assertArrayEquals(['A1', 'B1', 'C1', 'D1', 'H1'], K1.dependencies);
146+var K2 = sheet.getCell("K2");
147+assertEquals("200", K2.value);
148+assertEquals(MAX_FORMULA, K2.formula);
149+assertEquals(null, K2.error);
150+assertArrayEquals(['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', 'I2', 'J2'], K2.dependencies);
151+var K3 = sheet.getCell("K3");
152+assertEquals("-53", K3.value);
153+assertEquals(MIN_FORMULA, K3.formula);
154+assertEquals(null, K3.error);
155+assertArrayEquals(['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', 'I3', 'J3'], K3.dependencies);
156+var K4 = sheet.getCell("K4");
157+assertEquals("30.4", K4.value);
158+assertEquals(AVERAGE_FORMULA, K4.formula);
159+assertEquals(null, K4.error);
160+assertArrayEquals(['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4', 'J4'], K4.dependencies);
161+var K5 = sheet.getCell("K5");
162+assertEquals("50", K5.value);
163+assertEquals(SUM_IF_FORMULA, K5.formula);
164+assertEquals(null, K5.error);
165+assertArrayEquals(['A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5', 'I5', 'J5'], K5.dependencies);
166+var K6 = sheet.getCell("K6");
167+assertEquals("195.4", K6.value);
168+assertEquals(SUM_REF_FORMULA, K6.formula);
169+assertEquals(null, K6.error);
170+assertArrayEquals(['K1', 'K2', 'K3', 'K4'], K6.dependencies);