name:
src/test/js/testutils/TestUtils.ts
-rw-r--r--
3038
1import { assert } from "chai";
2import { F7ExceptionName } from "../../../main/js/errors/F7ExceptionName";
3import { CodeExecutor } from "../../../main/js/execution/CodeExecutor";
4import { FormulaCaller } from "../../../main/js/formulas/FormulaCaller";
5import { CollateralLookupFunction, LookupFunction } from "../../../main/js/models/common/Types";
6import { Converters } from "../../../main/js/utils/Converters";
7import { CommonModels } from "./CommonModels";
8import { SpreadsheetTestRunner } from "./SpreadsheetTestRunner";
9import { AbstractStandardError } from "../../../main/js/common/errors/StandardError";
10
11export function run(code: string) {
12 const executor: CodeExecutor = new CodeExecutor(
13 {},
14 null,
15 null,
16 new FormulaCaller(
17 (value) => value,
18 (origin, value) => value
19 )
20 );
21 return executor.execute(CommonModels.A1, { f: code, t: "n" });
22}
23
24export function runWithCustomFormulas(
25 code: string,
26 customFormulas: { [name: string]: (a: unknown, b?: unknown, c?: unknown) => unknown }
27) {
28 const executor: CodeExecutor = new CodeExecutor(
29 {},
30 null,
31 null,
32 new FormulaCaller(
33 (value) => value,
34 (origin, value) => value,
35 customFormulas
36 )
37 );
38 return executor.execute(CommonModels.A1, { f: code, t: "n" });
39}
40
41export function runWithLookups(
42 code: string,
43 lookup: LookupFunction,
44 collateralLookup: CollateralLookupFunction
45) {
46 const executor: CodeExecutor = new CodeExecutor(
47 {},
48 lookup,
49 collateralLookup,
50 new FormulaCaller(lookup, collateralLookup)
51 );
52 return executor.execute(CommonModels.A1, { f: code, t: "n" });
53}
54
55export function runner(): SpreadsheetTestRunner {
56 return new SpreadsheetTestRunner();
57}
58
59/**
60 * Cast the value as an F7Exception, and assert on the name.
61 * @param value - Value to cast.
62 * @param expectedName - Expected name for assertion.
63 */
64export function assertF7ExceptionByName(value: any, expectedName: F7ExceptionName) {
65 assert.deepEqual(Converters.castAsF7Exception(value).name, expectedName);
66}
67
68/**
69 * When ensuring that a function throws a particular error it's a pain to wrap each assertion.
70 * This just accepts a Function, runs it, and returns the exception if one was thrown, or
71 * returns null if it didn't throw anything.
72 * @param f - Function to run.
73 */
74export function tryAndCatchError(f: (a?: any) => any): any {
75 try {
76 f();
77 } catch (e) {
78 return e;
79 }
80 return null;
81}
82
83/**
84 * Quickly casting value as common error.
85 * @param value - to cast.
86 */
87export function asCommonError(value: any): AbstractStandardError {
88 return value as AbstractStandardError;
89}
90
91export function describe(description: string, specDefinitions: () => void): void {
92 console.log(description);
93 specDefinitions();
94}
95
96export function it(expectation: string, assertion: () => void, timeout?: number): void {
97 internalSetup();
98 console.log(`\t ${expectation}`);
99 assertion();
100}
101
102let internalSetup: any = undefined;
103
104export function beforeEach(setup: () => void): void {
105 internalSetup = setup;
106}