mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 13:55:08 +00:00
206 lines
6.3 KiB
JavaScript
206 lines
6.3 KiB
JavaScript
describe("Position", () => {
|
|
test("here", () => {
|
|
const workbook = createWorkbook();
|
|
const sheet = createSheet(workbook, "Sheet 1");
|
|
sheet.makeCurrent();
|
|
|
|
sheet.setCell("A", 0, "0");
|
|
sheet.focusCell("A", 0);
|
|
|
|
expect(here).toBeDefined();
|
|
let position = here();
|
|
expect(position).toBeDefined();
|
|
|
|
expect(position.column).toEqual("A");
|
|
expect(position.name).toEqual("A0");
|
|
expect(position.row).toEqual(0);
|
|
expect(position.sheet).toBe(sheet);
|
|
|
|
expect(position.contents).toEqual("0");
|
|
expect(position.value()).toEqual("0");
|
|
expect(position.toString()).toEqual("<Cell at A0>");
|
|
|
|
position.contents = "=1 + 1";
|
|
expect(position.contents).toEqual("=1 + 1");
|
|
expect(position.value()).toEqual(2);
|
|
|
|
expect(position.up().row).toEqual(0);
|
|
expect(position.down().row).toEqual(1);
|
|
expect(position.right().row).toEqual(0);
|
|
expect(position.left().row).toEqual(0);
|
|
|
|
sheet.addColumn("B");
|
|
expect(position.up().column).toEqual("A");
|
|
expect(position.down().column).toEqual("A");
|
|
expect(position.right().column).toEqual("B");
|
|
expect(position.left().column).toEqual("A");
|
|
});
|
|
|
|
test("Position.from_name", () => {
|
|
const workbook = createWorkbook();
|
|
const sheet = createSheet(workbook, "Sheet 1");
|
|
sheet.makeCurrent();
|
|
|
|
sheet.setCell("A", 0, "0");
|
|
sheet.focusCell("A", 0);
|
|
|
|
expect(Position.from_name).toBeDefined();
|
|
let position = Position.from_name("A0");
|
|
expect(position).toBeInstanceOf(Position);
|
|
|
|
position = Position.from_name("A123");
|
|
expect(position).toBeInstanceOf(Position);
|
|
});
|
|
});
|
|
|
|
describe("Range", () => {
|
|
test("simple", () => {
|
|
const workbook = createWorkbook();
|
|
const sheet = createSheet(workbook, "Sheet 1");
|
|
sheet.makeCurrent();
|
|
|
|
sheet.setCell("A", 0, "0");
|
|
sheet.setCell("A", 10, "0");
|
|
sheet.setCell("B", 1, "0");
|
|
sheet.focusCell("A", 0);
|
|
|
|
expect(R).toBeDefined();
|
|
let cellsVisited = 0;
|
|
R`A0:A10`.forEach(name => {
|
|
++cellsVisited;
|
|
});
|
|
expect(cellsVisited).toEqual(11);
|
|
|
|
cellsVisited = 0;
|
|
R`A0:A10:1:2`.forEach(name => {
|
|
++cellsVisited;
|
|
});
|
|
expect(cellsVisited).toEqual(6);
|
|
});
|
|
|
|
test("Ranges", () => {
|
|
const workbook = createWorkbook();
|
|
const sheet = createSheet(workbook, "Sheet 1");
|
|
sheet.makeCurrent();
|
|
|
|
sheet.setCell("A", 0, "0");
|
|
sheet.setCell("A", 10, "0");
|
|
sheet.setCell("B", 1, "0");
|
|
sheet.focusCell("A", 0);
|
|
|
|
let cellsVisited = 0;
|
|
R`A0:A5`.union(R`A6:A10`).forEach(name => {
|
|
++cellsVisited;
|
|
});
|
|
expect(cellsVisited).toEqual(11);
|
|
});
|
|
|
|
test("Range#first", () => {
|
|
const workbook = createWorkbook();
|
|
const sheet = createSheet(workbook, "Sheet 1");
|
|
sheet.makeCurrent();
|
|
|
|
sheet.setCell("A", 0, "0");
|
|
sheet.setCell("A", 1, "0");
|
|
sheet.setCell("A", 2, "0");
|
|
sheet.focusCell("A", 0);
|
|
expect(R`A0:A`.first().name).toEqual("A0");
|
|
expect(R`A0:A25`.first().name).toEqual("A0");
|
|
expect(R`A2:A25`.first().name).toEqual("A2");
|
|
});
|
|
|
|
test("Range#at", () => {
|
|
const workbook = createWorkbook();
|
|
const sheet = createSheet(workbook, "Sheet 1");
|
|
sheet.makeCurrent();
|
|
let i = 0;
|
|
for (const col of ["A", "B"]) {
|
|
for (const row of [0, 1, 2]) {
|
|
sheet.setCell(col, row, Math.pow(i++, 2));
|
|
}
|
|
}
|
|
|
|
sheet.focusCell("A", 0);
|
|
expect(R`A0:A2`.at(2).name).toEqual("A2");
|
|
expect(Ranges.from(R`A0:A2`, R`B0:B2`).at(5).name).toEqual("B2");
|
|
});
|
|
|
|
test("Range(s)#toArray", () => {
|
|
const workbook = createWorkbook();
|
|
const sheet = createSheet(workbook, "Sheet 1");
|
|
sheet.makeCurrent();
|
|
let i = 0;
|
|
for (const col of ["A", "B"]) {
|
|
for (const row of [0, 1, 2]) {
|
|
sheet.setCell(col, row, Math.pow(i++, 2));
|
|
}
|
|
}
|
|
|
|
sheet.focusCell("A", 0);
|
|
expect(R`A0:A2`.toArray().toString()).toEqual("<Cell at A0>,<Cell at A1>,<Cell at A2>");
|
|
expect(
|
|
Ranges.from(R`A0:A2`, R`B0:B2`)
|
|
.toArray()
|
|
.toString()
|
|
).toEqual("<Cell at A0>,<Cell at A1>,<Cell at A2>,<Cell at B0>,<Cell at B1>,<Cell at B2>");
|
|
});
|
|
});
|
|
|
|
describe("R function", () => {
|
|
const workbook = createWorkbook();
|
|
const sheet = createSheet(workbook, "Sheet 1");
|
|
sheet.makeCurrent();
|
|
/*
|
|
A B
|
|
+++
|
|
0+1 1
|
|
1+2 4
|
|
2+3 9
|
|
*/
|
|
for (const row of ["A", "B"]) {
|
|
for (let col of [0, 1, 2]) {
|
|
sheet.setCell(row, col++, row === "A" ? col : Math.pow(col, 2));
|
|
}
|
|
}
|
|
sheet.focusCell("A", 0);
|
|
|
|
test("Check for correctness: R`A0:A`", () => {
|
|
const range = R`A0:A`;
|
|
expect(range.toString()).toEqual("R`A0:A`");
|
|
expect(count(range)).toEqual(3);
|
|
expect(sum(range)).toEqual(6);
|
|
});
|
|
test("Check for correctness: R`A`", () => {
|
|
const range = R`A`;
|
|
expect(range.toString()).toEqual("R`A0:A`");
|
|
expect(count(range)).toEqual(3);
|
|
expect(sum(range)).toEqual(6);
|
|
});
|
|
test("Check for correctness: R`A0:B`", () => {
|
|
const range = R`A0:B`;
|
|
expect(range.toString()).toEqual("R`A0:B`");
|
|
expect(count(range)).toEqual(6);
|
|
expect(sum(range)).toEqual(20);
|
|
});
|
|
test("Check for correctness: R`A0:B0`", () => {
|
|
const range = R`A0:B0`;
|
|
expect(range.toString()).toEqual("R`A0:B0`");
|
|
expect(count(range)).toEqual(2);
|
|
expect(sum(range)).toEqual(2);
|
|
});
|
|
test("Check for correctness: R`A0:B:2:1`", () => {
|
|
const range = R`A0:B2:1:2`;
|
|
expect(range.toString()).toEqual("R`A0:B2:1:2`");
|
|
expect(range.toArray().toString()).toEqual(
|
|
"<Cell at A0>,<Cell at A2>,<Cell at B0>,<Cell at B2>"
|
|
);
|
|
expect(count(range)).toEqual(4);
|
|
expect(sum(range)).toEqual(14);
|
|
});
|
|
test("R`B`", () => {
|
|
const range = R`B`;
|
|
expect(range.toString()).toEqual("R`B0:B`");
|
|
expect(count(range)).toEqual(3);
|
|
expect(sum(range)).toEqual(14);
|
|
});
|
|
});
|