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/formulas/logic/IFTest.java
-rw-r--r--
2828
 1package io.protobase.f7.formulas.logic;
 2
 3import io.protobase.f7.errors.DivException;
 4import io.protobase.f7.errors.NAException;
 5import io.protobase.f7.errors.NumException;
 6import io.protobase.f7.models.Grid;
 7import io.protobase.f7.testutils.TestFormula;
 8import org.junit.Test;
 9
10import static com.google.common.truth.Truth.assertThat;
11import static org.mockito.Mockito.verify;
12import static org.mockito.Mockito.verifyNoMoreInteractions;
13import static org.mockito.Mockito.when;
14
15public class IFTest extends TestFormula {
16  @Test
17  public void testApply_simple() {
18    assertThat(IF.SELF.apply(null, true, true, false)).isEqualTo(true);
19    assertThat(IF.SELF.apply(null, false, true, false)).isEqualTo(false);
20    assertThat(IF.SELF.apply(null, 10.0, "A", "B")).isEqualTo("A");
21    assertThat(IF.SELF.apply(null, -1716.1, "A", "B")).isEqualTo("A");
22    assertThat(IF.SELF.apply(null, "TRUE", "A", "B")).isEqualTo("A");
23    assertThat(IF.SELF.apply(null, "", "A", "B")).isEqualTo("B");
24  }
25
26  @Test
27  public void testApply_error() {
28    assertThat(IF.SELF.apply(null, true, 10.1, new DivException())).isEqualTo(10.1);
29    assertThat(IF.SELF.apply(null, true, new NumException(), new DivException())).isEqualTo(new NumException());
30    assertThat(IF.SELF.apply(null, false, new DivException(), 10.11)).isEqualTo(10.11);
31    assertThat(IF.SELF.apply(null, true, new DivException(), 10.11)).isEqualTo(new DivException());
32  }
33
34  @Test
35  public void test_lookup_first() {
36    IF F = new IF(lookup, collateralLookup);
37    when(collateralLookup.apply(A1, M22_RANGE)).thenReturn(true);
38    when(collateralLookup.apply(A1, G19_RANGE)).thenReturn(2.0);
39    assertThat(F.apply(A1, M22_RANGE, G19_RANGE, J6_RANGE)).isEqualTo(2.0);
40    verify(collateralLookup).apply(A1, M22_RANGE);
41    verify(collateralLookup).apply(A1, G19_RANGE);
42    verifyNoMoreInteractions(collateralLookup);
43  }
44
45  @Test
46  public void test_lookup_second() {
47    IF F = new IF(lookup, collateralLookup);
48    when(collateralLookup.apply(A1, M22_RANGE)).thenReturn(false);
49    when(collateralLookup.apply(A1, J6_RANGE)).thenReturn(100.0);
50    assertThat(F.apply(A1, M22_RANGE, G19_RANGE, J6_RANGE)).isEqualTo(100.0);
51    verify(collateralLookup).apply(A1, M22_RANGE);
52    verify(collateralLookup).apply(A1, J6_RANGE);
53    verifyNoMoreInteractions(collateralLookup);
54  }
55
56  @Test
57  public void testApply_grid() {
58    Grid<Object> grid = Grid.builder()
59        .add(0, 0, 0.0)
60        .add(0, 1, 1.0)
61        .add(0, 2, 2.0)
62        .add(0, 3, 3.0)
63        .build();
64    assertThat(IF.SELF.apply(null, true, grid, "Not me.")).isEqualTo(grid);
65  }
66
67  @Test
68  public void test_errorFromArgumentsMismatch() {
69    assertThat(IF.SELF.apply(null, 1.0, 2.0)).isEqualTo(new NAException());
70    assertThat(IF.SELF.apply(null, 1.0, 2.0, 3.0, 4.0)).isEqualTo(new NAException());
71  }
72}