f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/test/java/io/protobase/f7/testutils/TestF7CodeExecutor.java
-rw-r--r--
3059
 1package io.protobase.f7.testutils;
 2
 3import io.protobase.f7.formulas.FormulaCaller;
 4import io.protobase.f7.models.A1Key;
 5import io.protobase.f7.models.CellQuery;
 6import io.protobase.f7.models.ColumnRowKey;
 7import io.protobase.f7.models.GridColumnRowKey;
 8import io.protobase.f7.spreadsheet.F7CodeExecutor;
 9import org.mockito.Mock;
10
11import java.util.HashMap;
12import java.util.function.BiFunction;
13import java.util.function.Function;
14
15import static org.mockito.Mockito.mock;
16
17public class TestF7CodeExecutor {
18  protected static final GridColumnRowKey A1 = new GridColumnRowKey("Alpha", A1Key.fromString("A1").toColumnRowKey());
19  protected static final ColumnRowKey M44 = A1Key.fromString("M44").toColumnRowKey();
20  protected static final CellQuery M44_RANGE = CellQuery.builder()
21      .grid("Alpha")
22      .columnsBetween(M44.getColumnIndex(), M44.getColumnIndex())
23      .rowsBetween(M44.getRowIndex(), M44.getRowIndex())
24      .build();
25  protected static final ColumnRowKey B1 = A1Key.fromString("B1").toColumnRowKey();
26  protected static final ColumnRowKey B4 = A1Key.fromString("B4").toColumnRowKey();
27  protected static final CellQuery B1_B4_RANGE = CellQuery.builder()
28      .grid("Alpha")
29      .columnsBetween(B1.getColumnIndex(), B4.getColumnIndex())
30      .rowsBetween(B1.getRowIndex(), B4.getRowIndex())
31      .build();
32
33  @Mock
34  protected Function<Object, Object> lookup = mock(Function.class);
35
36  @Mock
37  protected BiFunction<GridColumnRowKey, Object, Object> collateralLookup = mock(BiFunction.class);
38
39  /**
40   * Run a test at a given cell.
41   *
42   * @param originGrid - grid of origin cell.
43   * @param originKey  - key of origin cell
44   * @param code       - code to execute.
45   * @return result of executed code.
46   */
47  public Object run(String originGrid, String originKey, String code) {
48    F7CodeExecutor executor = new F7CodeExecutor(new HashMap<>(), lookup, (origin, value) -> value,
49        new FormulaCaller(lookup, (origin, value) -> value));
50    GridColumnRowKey origin = new GridColumnRowKey(originGrid, A1Key.fromString(originKey).toColumnRowKey());
51    return executor.execute(origin, code);
52  }
53
54  /**
55   * Run a test with the lookup instead of pass-through.
56   *
57   * @param code -  to execute.
58   * @return result of executed code.
59   */
60  public Object runWithLookup(String code) {
61    F7CodeExecutor executor = new F7CodeExecutor(new HashMap<>(), lookup, collateralLookup,
62        new FormulaCaller(lookup, collateralLookup));
63    GridColumnRowKey origin = new GridColumnRowKey("Alpha", A1Key.fromString("A1").toColumnRowKey());
64    return executor.execute(origin, code);
65  }
66
67  /**
68   * Run the code.
69   *
70   * @param code - to execute.
71   * @return
72   */
73  public Object run(String code) {
74    F7CodeExecutor executor = new F7CodeExecutor(new HashMap<>(), value -> value, (origin, value) -> value,
75        new FormulaCaller(value -> value, (origin, value) -> value));
76    GridColumnRowKey origin = new GridColumnRowKey("Alpha", A1Key.fromString("A1").toColumnRowKey());
77    return executor.execute(origin, code);
78  }
79}