spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← All files
name: tests/Utils/Asserts.ts
-rw-r--r--
5173
  1import {
  2  Cell
  3} from "../../src/Cell";
  4
  5import {
  6  Sheet
  7} from "../../src/Sheet";
  8/**
  9 * Assert two params are equal using strict equality testing.
 10 * @param actual value
 11 * @param expected value
 12 */
 13function assertEquals(actual, expected) {
 14  if (actual instanceof Cell && expected instanceof Cell) {
 15    if (!expected.equals(actual)) {
 16      console.log("expected:", expected, " actual:", actual);
 17      console.trace();
 18    }
 19  } else if (actual instanceof Object && expected instanceof Object) {
 20    if (JSON.stringify(actual) !== JSON.stringify(expected)) {
 21      console.log("expected:", expected, " actual:", actual);
 22      console.trace();
 23    }
 24  } else {
 25    if (expected !== actual) {
 26      console.log("expected:", expected, " actual:", actual);
 27      console.trace();
 28    }
 29  }
 30}
 31
 32/**
 33 * Asserts value is equal to null.
 34 * @param actual - value to test.
 35 */
 36function assertIsNull(actual) {
 37  if (null !== actual) {
 38    console.log("expected:", null, " actual:", actual);
 39    console.trace();
 40  }
 41}
 42
 43
 44/**
 45 * Assert two arrays are equal using strict equality testing on individual items.
 46 * @param actual value
 47 * @param expected value
 48 */
 49function assertArrayEquals(actual: Array<any>, expected: Array<any>, ) {
 50  if (expected.length != actual.length) {
 51    console.log("expected: ", expected, " actual:", actual);
 52    console.trace();
 53    return;
 54  }
 55  for (let index in expected) {
 56    if (expected[index] != actual[index]) {
 57      console.log("expected: ", expected, " actual:", actual);
 58      console.trace();
 59    }
 60  }
 61}
 62
 63/**
 64 * Catch exceptions, check their name for an expected value
 65 * @param toExecute function to execute
 66 * @param expected error message
 67 */
 68function catchAndAssertEquals(toExecute : Function, expected) {
 69  let toThrow = null;
 70  try {
 71    toExecute();
 72    toThrow = true;
 73  } catch (actualError) {
 74    if (actualError.name !== expected) {
 75      console.log("expected:", expected, " actual:", actualError.name, actualError.message);
 76      console.trace();
 77    }
 78  }
 79  if (toThrow) {
 80    console.log("expected error: " + expected);
 81    console.trace();
 82  }
 83}
 84
 85/**
 86 * Print description of test, and run test.
 87 * @param {string} description - To print.
 88 * @param {Function} toRun - Test function to run.
 89 */
 90function test(description: string, toRun: Function) {
 91  console.log("Test:", description);
 92  toRun();
 93}
 94
 95
 96/**
 97 * Assert formula will result in a particular error.
 98 * @param {string} formula
 99 * @param {string} errorString
100 */
101function assertFormulaEqualsError(formula: string, errorString: string) {
102  let sheet  = new Sheet();
103  sheet.setCell("A1", formula);
104  let cell = sheet.getCell("A1");
105  assertEquals(cell.getError().name, errorString);
106  assertEquals(cell.getValue(), null);
107}
108
109/**
110 * Assert formula will result in a particular value.
111 * @param {string} formula
112 * @param expectation
113 */
114function assertFormulaEquals(formula: string, expectation: any) {
115  let sheet  = new Sheet();
116  sheet.setCell("A1", formula);
117  let cell = sheet.getCell("A1");
118  assertEquals(cell.getError(), null);
119  assertEquals(cell.getValue(), expectation);
120}
121
122/**
123 * Assert formula will equal a result, depends on a specific cell reference.
124 * @param {string} refId - Cell ID, eg: A1
125 * @param value - Value for refId.
126 * @param {string} formula - Formula to evaluate.
127 * @param expectation - Expected result.
128 */
129function assertFormulaEqualsDependsOnReference(refId: string, value: any, formula: string, expectation: any) {
130  let sheet  = new Sheet();
131  sheet.setCell(refId, value);
132  sheet.setCell("A1", formula);
133  let cell = sheet.getCell("A1");
134  assertEquals(cell.getError(), null);
135  assertEquals(cell.getValue(), expectation);
136}
137
138/**
139 * Assert that the evaluation of a formula results in a specific type.
140 * @param {string} formula
141 * @param {string} type
142 */
143function assertFormulaResultsInType(formula: string, type: string) {
144  let sheet  = new Sheet();
145  sheet.setCell("A1", formula);
146  let cell = sheet.getCell("A1");
147  assertEquals(cell.getError(), null);
148  assertEquals(typeof cell.getValue(), type);
149}
150
151/**
152 * Assert formula will result in a particular array.
153 * @param {string} formula
154 * @param expectation
155 */
156function assertFormulaEqualsArray(formula: string, expectation: any) {
157  let sheet  = new Sheet();
158  sheet.setCell("A1", formula);
159  let cell = sheet.getCell("A1");
160  assertEquals(null, cell.getError());
161  let values = cell.getValue();
162  for (let index in values) {
163    assertEquals(values[index], expectation[index]);
164  }
165}
166
167/**
168 * Lock in Date by overriding prototypes. WARNING: Should be used sparingly and cautiously.
169 * @param year
170 * @param month
171 * @param day
172 * @param hour
173 * @param minute
174 * @param second
175 */
176function lockDate(year, month, day, hour, minute, second)  {
177  let d = new Date(year, month, day, hour, minute, second);
178  Date.prototype.constructor = function () {
179    return d;
180  };
181  Date.now = function () {
182    return +(d);
183  };
184}
185
186
187export {
188  assertIsNull,
189  assertEquals,
190  assertArrayEquals,
191  assertFormulaEquals,
192  assertFormulaResultsInType,
193  assertFormulaEqualsArray,
194  assertFormulaEqualsError,
195  assertFormulaEqualsDependsOnReference,
196  catchAndAssertEquals,
197  test,
198  lockDate
199}