spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← All files
name: tests/CellTest.ts
-rw-r--r--
4955
  1import {
  2  Cell,
  3  CELL_ID_ERROR
  4} from "../src/Cell";
  5import {
  6  assertArrayEquals,
  7  assertEquals,
  8  assertIsNull,
  9  catchAndAssertEquals,
 10  test
 11} from "./Utils/Asserts";
 12
 13test("Cell.constructor", function(){
 14  let cell = new Cell("A1");
 15  assertEquals(cell.getColumn(), 0);
 16  assertEquals(cell.getRow(), 0);
 17  assertArrayEquals(cell.getDependencies(), []);
 18  assertEquals(cell.getId(), "A1");
 19  assertIsNull(cell.getFormula());
 20  assertEquals(cell.hasFormula(), false);
 21  catchAndAssertEquals(function () {
 22    new Cell("C");
 23  }, CELL_ID_ERROR)
 24  catchAndAssertEquals(function () {
 25    new Cell("111");
 26  }, CELL_ID_ERROR)
 27  catchAndAssertEquals(function () {
 28    new Cell("BAD11BAD");
 29  }, CELL_ID_ERROR)
 30});
 31
 32test("Cell.updateDependencies", function(){
 33  let one = new Cell("A1");
 34  one.updateDependencies(["B2", "C1", "D12", "D13"]);
 35  assertArrayEquals(one.getDependencies(), ["B2", "C1", "D12", "D13"]);
 36  one.updateDependencies(["M4"]);
 37  assertArrayEquals(one.getDependencies(), ["B2", "C1", "D12", "D13", "M4"]);
 38});
 39
 40test("Cell.getDependencies", function(){
 41  let one = new Cell("A1");
 42  assertArrayEquals(one.getDependencies(), []);
 43  one.updateDependencies(["M4"]);
 44  assertArrayEquals(one.getDependencies(), ["M4"]);
 45});
 46
 47test("Cell.getColumn", function(){
 48  assertEquals(Cell.BuildFrom("A1", 0).getColumn(), 0);
 49  assertEquals(Cell.BuildFrom("B1", 0).getColumn(), 1);
 50  assertEquals(Cell.BuildFrom("M1", 0).getColumn(), 12);
 51  assertEquals(Cell.BuildFrom("N1", 0).getColumn(), 13);
 52  assertEquals(Cell.BuildFrom("AA1", 0).getColumn(), 26);
 53  assertEquals(Cell.BuildFrom("AB1", 0).getColumn(), 27);
 54  assertEquals(Cell.BuildFrom("AM1", 0).getColumn(), 38);
 55});
 56
 57test("Cell.getRow", function(){
 58  assertEquals(Cell.BuildFrom("A1", 0).getRow(), 0);
 59  assertEquals(Cell.BuildFrom("B2", 0).getRow(), 1);
 60  assertEquals(Cell.BuildFrom("M13", 0).getRow(), 12);
 61  assertEquals(Cell.BuildFrom("N14", 0).getRow(), 13);
 62  assertEquals(Cell.BuildFrom("AA27", 0).getRow(), 26);
 63  assertEquals(Cell.BuildFrom("AB28", 0).getRow(), 27);
 64  assertEquals(Cell.BuildFrom("AM39", 0).getRow(), 38);
 65});
 66
 67test("Cell.getId", function(){
 68  assertEquals(Cell.BuildFrom("A1", 0).getId(), "A1");
 69  assertEquals(Cell.BuildFrom("M1400", 0).getId(), "M1400");
 70});
 71
 72test("Cell.setValue, Cell.getValue", function(){
 73  let v = new Cell("A1");
 74  v.setValue("100");
 75  assertEquals(v.getValue(), "100");
 76  v = new Cell("A1");
 77  v.setValue("=SUM(10, 10)");
 78  assertIsNull(v.getValue());
 79});
 80
 81test("Cell.clearValue", function(){
 82  let v = Cell.BuildFrom("A1", 100);
 83  assertEquals(v.getValue(), 100);
 84  v.clearValue();
 85  assertIsNull(v.getValue());
 86});
 87
 88test("Cell.setError, Cell.getError", function(){
 89  let v = Cell.BuildFrom("A1", 100);
 90  let e = new Error("e");
 91  assertIsNull(v.getError());
 92  v.setError(e);
 93  assertEquals(v.getError(), e);
 94});
 95
 96test("Cell.hasError", function(){
 97  let v = Cell.BuildFrom("A1", 100);
 98  let e = new Error("e");
 99  assertEquals(v.hasError(), false);
100  v.setError(e);
101  assertEquals(v.hasError(), true);
102});
103
104test("Cell.getFormula", function(){
105  assertIsNull(Cell.BuildFrom("A1", "100").getFormula());
106  assertEquals(Cell.BuildFrom("A1", "=SUM(1, 2)").getFormula(), "SUM(1, 2)");
107  assertEquals(Cell.BuildFrom("A1", "= 100,000,000").getFormula(), " 100,000,000");
108});
109
110test("Cell.hasFormula", function(){
111  assertEquals(Cell.BuildFrom("A1", "100").hasFormula(), false);
112  assertEquals(Cell.BuildFrom("A1", "=SUM(1, 2)").hasFormula(), true);
113  assertEquals(Cell.BuildFrom("A1", "= 100,000,000").hasFormula(), true);
114});
115
116test("Cell.getRawFormulaText", function(){
117  assertIsNull(Cell.BuildFrom("A1", "100").getRawFormulaText());
118  assertEquals(Cell.BuildFrom("A1", "=10e1").getRawFormulaText(), "10e1");
119  assertEquals(Cell.BuildFrom("A1", "=SUM(1, 2)").getRawFormulaText(), "SUM(1, 2)");
120  assertEquals(Cell.BuildFrom("A1", "= 100,000,000").getRawFormulaText(), " 100,000,000");
121});
122
123test("Cell.isBlank", function(){
124  let v = new Cell("A1");
125  assertIsNull(v.getValue());
126  assertIsNull(v.getError());
127  assertEquals(v.isBlank(), true);
128});
129
130test("Cell.BuildFrom", function(){
131  let v = Cell.BuildFrom("A1", 10);
132  assertEquals(v.getValue(), 10);
133  assertIsNull(v.getError());
134  assertEquals(v.isBlank(), false);
135});
136
137test("Cell.tosString", function(){
138  assertEquals(new Cell("A1").toString(), "id=A1, value=null, rawFormulaText=null, error=null");
139  assertEquals(Cell.BuildFrom("A1", 100).toString(), "id=A1, value=100, rawFormulaText=null, error=null");
140  assertEquals(Cell.BuildFrom("A1", "100").toString(), "id=A1, value=100, rawFormulaText=null, error=null");
141  assertEquals(Cell.BuildFrom("A1", "=SUM(A1:A10)").toString(), "id=A1, value=null, rawFormulaText=SUM(A1:A10), error=null");
142});
143
144test("Cell.equals", function(){
145  assertEquals(new Cell("A1").equals(new Cell("A1")), true);
146  assertEquals(new Cell("M100").equals(new Cell("A1")), false);
147  assertEquals(Cell.BuildFrom("A1", 100).equals(Cell.BuildFrom("A1", 100)), true);
148});