123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- describe("Basic functions", () => {
- const workbook = createWorkbook();
- const sheet = createSheet(workbook, "Sheet 1");
- sheet.makeCurrent();
- sheet.setCell("A", 0, "0");
- sheet.setCell("A", 1, "1");
- sheet.setCell("A", 2, "2");
- test("select", () => {
- expect(select).toBeDefined();
- expect(select(true, 1, 2)).toBe(1);
- expect(select(false, 1, 2)).toBe(2);
- });
- test("choose", () => {
- expect(choose).toBeDefined();
- expect(choose(0, 1, 2, 3)).toBe(1);
- expect(choose(1, 1, 2, 3)).toBe(2);
- expect(choose(3, 1, 2, 3)).toBeUndefined();
- expect(choose(-1, 1, 2, 3)).toBeUndefined();
- });
- test("now", () => {
- expect(now).toBeDefined();
- expect(now()).toBeInstanceOf(Date);
- });
- test("randRange", () => {
- expect(randRange).toBeDefined();
- });
- test("integer", () => {
- expect(integer).toBeDefined();
- expect(integer("0")).toEqual(0);
- expect(integer("32")).toEqual(32);
- });
- test("sheet", () => {
- expect(globalThis.sheet).toBeDefined();
- expect(globalThis.sheet("Sheet 1")).toBe(sheet);
- expect(globalThis.sheet("Not a sheet")).toBeUndefined();
- });
- test("reduce", () => {
- expect(reduce).toBeDefined();
- expect(reduce(acc => acc + 1, 0, [1, 2, 3, 4])).toEqual(4);
- expect(reduce(acc => acc + 1, 0, [])).toEqual(0);
- expect(reduce((acc, x) => acc + "|" + x.toString(), 0, R`A0:A2`)).toEqual("0|0|1|2");
- expect(reduce((acc, x) => acc + "|" + x.toString(), 0, R`A0:A0`)).toEqual("0|0");
- });
- test("numericReduce", () => {
- expect(numericReduce).toBeDefined();
- expect(numericReduce(acc => acc + 1, 0, [1, 2, 3, 4])).toEqual(4);
- expect(numericReduce(acc => acc + 1, 0, [])).toEqual(0);
- expect(numericReduce((acc, x) => acc + x, 19, R`A0:A2`)).toEqual(22);
- expect(numericReduce(acc => acc + 1, 3, R`A0:A0`)).toEqual(4);
- });
- test("numericResolve", () => {
- expect(numericResolve).toBeDefined();
- expect(numericResolve(["0", "1", "2"])).toEqual([0, 1, 2]);
- expect(numericResolve([])).toEqual([]);
- expect(numericResolve(R`A0:A2`)).toEqual([0, 1, 2]);
- expect(numericResolve(R`A0:A0`)).toEqual([0]);
- });
- test("resolve", () => {
- expect(resolve).toBeDefined();
- expect(resolve(["A", "B", "C"])).toEqual(["A", "B", "C"]);
- expect(resolve([])).toEqual([]);
- expect(resolve(R`A0:A2`)).toEqual(["0", "1", "2"]);
- expect(resolve(R`A0:A0`)).toEqual(["0"]);
- });
- });
- describe("Statistics", () => {
- const workbook = createWorkbook();
- const sheet = createSheet(workbook, "Sheet 1");
- sheet.makeCurrent();
- for (let i = 0; i < 10; ++i) sheet.setCell("A", i, `${i}`);
- for (let i = 0; i < 10; ++i) sheet.setCell("B", i, `${i * i}`);
- test("sum", () => {
- expect(sum).toBeDefined();
- expect(sum(R`A0:A9`)).toEqual(45);
- });
- test("sumIf", () => {
- expect(sumIf).toBeDefined();
- expect(sumIf(x => !Number.isNaN(x), R`A0:A10`)).toEqual(45);
- });
- test("count", () => {
- expect(count).toBeDefined();
- expect(count(R`A0:A9`)).toEqual(10);
- });
- test("countIf", () => {
- expect(countIf).toBeDefined();
- expect(countIf(x => x, R`A0:A10`)).toEqual(10);
- });
- test("average", () => {
- expect(average).toBeDefined();
- expect(average(R`A0:A9`)).toEqual(4.5);
- });
- test("averageIf", () => {
- expect(averageIf).toBeDefined();
- expect(averageIf(x => !Number.isNaN(x), R`A0:A10`)).toEqual(4.5);
- });
- test("minIf", () => {
- expect(minIf).toBeDefined();
- expect(minIf(x => x > 25, R`B0:B9`)).toEqual(36);
- });
- test("min", () => {
- expect(min).toBeDefined();
- expect(min(R`B0:B9`)).toEqual(0);
- });
- test("maxIf", () => {
- expect(maxIf).toBeDefined();
- expect(maxIf(x => x > 25, R`B0:B9`)).toEqual(81);
- });
- test("max", () => {
- expect(max).toBeDefined();
- expect(max(R`B0:B9`)).toEqual(81);
- });
- test("sumProductIf", () => {
- expect(sumProductIf).toBeDefined();
- expect(sumProductIf((a, b) => b > 25, R`A0:A9`, R`B0:B9`)).toEqual(1800);
- });
- test("sumProduct", () => {
- expect(sumProduct).toBeDefined();
- expect(sumProduct(R`A0:A9`, R`B0:B9`)).toEqual(2025);
- });
- test("median", () => {
- expect(median).toBeDefined();
- expect(median(R`A0:A9`)).toEqual(4.5);
- expect(median(R`A0:A2`)).toEqual(1);
- });
- test("variance", () => {
- expect(variance).toBeDefined();
- expect(variance(R`A0:A0`)).toEqual(0);
- expect(variance(R`A0:A9`)).toEqual(82.5);
- });
- test("mode", () => {
- expect(mode).toBeDefined();
- expect(mode(R`A0:A0`.union(R`A0:A0`).union(R`A1:A9`))).toEqual(0);
- });
- test("stddev", () => {
- expect(stddev).toBeDefined();
- expect(stddev(R`A0:A0`)).toEqual(0);
- expect(stddev(R`A0:A9`)).toEqual(Math.sqrt(82.5));
- });
- });
- describe("Lookup", () => {
- const workbook = createWorkbook();
- const sheet = createSheet(workbook, "Sheet 1");
- sheet.makeCurrent();
- for (let i = 0; i < 10; ++i) {
- sheet.setCell("A", i, `${i}`);
- sheet.setCell("B", i, `B${i}`);
- }
- sheet.focusCell("A", 0);
- test("row", () => {
- expect(row()).toEqual(0);
- });
- test("column", () => {
- expect(column()).toEqual("A");
- });
- test("lookup", () => {
- expect(lookup).toBeDefined();
- // Note: String ordering.
- expect(lookup("2", R`A0:A9`, R`B0:B9`)).toEqual("B2");
- expect(lookup("20", R`A0:A9`, R`B0:B9`)).toBeUndefined();
- expect(lookup("80", R`A0:A9`, R`B0:B9`, undefined, "nextlargest")).toEqual("B9");
- });
- test("reflookup", () => {
- expect(reflookup).toBeDefined();
- // Note: String ordering.
- expect(reflookup("2", R`A0:A9`, R`B0:B9`).name).toEqual("B2");
- expect(reflookup("20", R`A0:A9`, R`B0:B9`)).toEqual(here());
- expect(reflookup("80", R`A0:A9`, R`B0:B9`, undefined, "nextlargest").name).toEqual("B9");
- });
- });
|