GridTest.java 7.32 KB
package kernel.test;

import kernel.Cell;
import kernel.Grid;
import kernel.LanguageEnum;
import kernel.exception.CellNotFoundException;
import kernel.exception.CreateCycleException;
import kernel.exception.InvalidIntervalLineColumnEception;
import kernel.function.Average;
import kernel.function.Sum;
import kernel.operation.Addition;
import kernel.operation.Multiplication;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class GridTest {

    private Grid grid;

    @Before
    public void initData() {
        Grid.language = LanguageEnum.EN;
        this.grid = new Grid();
    }

    @Test
    public void testCreateCellWithValue() throws InvalidIntervalLineColumnEception {
        assertEquals(0, this.grid.getCells().size());

        this.grid.createCell("A", 1, 10.);

        assertEquals(1, this.grid.getCells().size());
    }

    @Test
    public void testCreateCellWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception {
        this.createCellsWithValue();
        assertEquals(3, this.grid.getCells().size());

        List<Cell> cellsFormula = new ArrayList<>();
        cellsFormula.add(this.grid.getCell("A", 1));
        cellsFormula.add(this.grid.getCell("A", 2));

        this.grid.createCell("B", 1, new Sum(cellsFormula));

        assertEquals(4, this.grid.getCells().size());
    }

    @Test
    public void testGetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception {
        this.createCellsWithFormula();

        assertEquals(10, this.grid.getValue("A", 1), 0);
        assertEquals(0, this.grid.getValue("A", 2), 0);
        assertEquals(2, this.grid.getValue("A", 3), 0);
        assertEquals(10, this.grid.getValue("B", 1), 0);
        assertEquals(12, this.grid.getValue("B", 2), 0);
        assertEquals(24, this.grid.getValue("B", 3), 0);
    }

    @Test
    public void testGetFormulaAsString() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception {
        Grid.language = LanguageEnum.EN;
        this.createCellsWithFormula();

        assertEquals("A1", this.grid.getFormulaAsString("A", 1));
        assertEquals("A2", this.grid.getFormulaAsString("A", 2));
        assertEquals("A3", this.grid.getFormulaAsString("A", 3));
        assertEquals("A1+A2", this.grid.getFormulaAsString("B", 1));
        assertEquals("SUM(A1,A2,A3)", this.grid.getFormulaAsString("B", 2));
        assertEquals("A3*B2", this.grid.getFormulaAsString("B", 3));
    }

    @Test
    public void testGetDevelopedFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception {
        this.createCellsWithFormula();

        assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3));
    }

    @Test
    public void testSetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception {
        this.createCellsWithFormula();

        assertEquals(10, this.grid.getValue("A", 1), 0);
        assertEquals(12, this.grid.getValue("B", 2), 0);
        assertEquals(24, this.grid.getValue("B", 3), 0);

        this.grid.setValue("A", 1, 15.);

        assertEquals(15, this.grid.getValue("A", 1), 0);
        assertEquals(17, this.grid.getValue("B", 2), 0);
        assertEquals(34, this.grid.getValue("B", 3), 0);
        assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3));

        this.grid.setValue("B", 2, 20.);

        assertEquals(40, this.grid.getValue("B", 3), 0);
        assertEquals("A3*B2", this.grid.getDevelopedFormula("B", 3));
    }

    @Test
    public void testSetFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception {
        this.createCellsWithFormula();

        assertEquals(12, this.grid.getValue("B", 2), 0);
        assertEquals(24, this.grid.getValue("B", 3), 0);
        assertEquals("SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 2));
        assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3));

        List<Cell> sumList = new ArrayList<>();
        sumList.add(this.grid.getCell("A", 1));
        sumList.add(this.grid.getCell("A", 3));
        sumList.add(this.grid.getCell("B", 1));

        this.grid.setFormula("B", 2, new Sum(sumList));

        // B2
        assertEquals(22, this.grid.getValue("B", 2), 0);
        assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2));
        assertEquals("SUM(A1,A3,A1+A2)", this.grid.getDevelopedFormula("B", 2));
        // B3
        assertEquals(44, this.grid.getValue("B", 3), 0);
        assertEquals("A3*B2", this.grid.getFormulaAsString("B", 3));
        assertEquals("A3*SUM(A1,A3,A1+A2)", this.grid.getDevelopedFormula("B", 3));

        this.grid.createCell("C", 1, 10.);
        this.grid.createCell("C", 2, 20.);

        List<Cell> averageList = new ArrayList<>();
        averageList.add(this.grid.getCell("C", 1));
        averageList.add(this.grid.getCell("C", 2));

        this.grid.setFormula("A", 1, new Average(averageList));

        // A1
        assertEquals(15, this.grid.getValue("A", 1), 0);
        assertEquals("AVERAGE(C1,C2)", this.grid.getDevelopedFormula("A", 1));
        // B1
        assertEquals(15, this.grid.getValue("B", 1), 0);
        assertEquals("A1+A2", this.grid.getFormulaAsString("B", 1));
        assertEquals("AVERAGE(C1,C2)+A2", this.grid.getDevelopedFormula("B", 1));
        // B2
        assertEquals(32, this.grid.getValue("B", 2), 0);
        assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2));
        assertEquals("SUM(AVERAGE(C1,C2),A3,AVERAGE(C1,C2)+A2)", this.grid.getDevelopedFormula("B", 2));
    }

    @Test
    public void testGetCell() throws CellNotFoundException, InvalidIntervalLineColumnEception {
        this.createCellsWithValue();

        Cell cell = this.grid.getCell("A", 1);

        assertEquals("A1", cell.getId());
        assertEquals(10, cell.getValue(), 0);
        assertFalse(cell.containFormula());
    }

    @Test(expected = CellNotFoundException.class)
    public void testGetCellNotFound() throws CellNotFoundException {
        Cell cell = this.grid.getCell("A", 1);
    }

    @Test
    public void testGetCells() throws InvalidIntervalLineColumnEception {
        assertEquals(0, this.grid.getCells().size());

        this.createCellsWithValue();

        assertEquals(3, this.grid.getCells().size());
    }

    private void createCellsWithValue() throws InvalidIntervalLineColumnEception {
        this.grid.createCell("A", 1, 10.);
        this.grid.createCell("A", 2, 0.);
        this.grid.createCell("A", 3, 2.);
    }

    private void createCellsWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalLineColumnEception {
        this.createCellsWithValue();

        Cell a1 = this.grid.getCell("A", 1);
        Cell a2 = this.grid.getCell("A", 2);
        Cell a3 = this.grid.getCell("A", 3);

        List<Cell> sumList = new ArrayList<>();
        sumList.add(a1);
        sumList.add(a2);
        sumList.add(a3);

        // A1+A2
        this.grid.createCell("B", 1, new Addition(a1, a2));
        // SUM(A1+A2+A3)
        this.grid.createCell("B", 2, new Sum(sumList));
        // A3*B2
        this.grid.createCell("B", 3, new Multiplication(a3, this.grid.getCell("B", 2)));
    }
}