Commit 2747b3d2e05e5be6acc633e4d8b2105dcf8fef3f

Authored by Remi
1 parent 178cc4eb

update tests

grid.data
No preview for this file type
src/ihm/TablooProto.java
... ... @@ -36,7 +36,7 @@ public class TablooProto extends JPanel {
36 36 private static Grid grid;
37 37  
38 38 // Fourni: ne rien changer.
39   - public TablooProto(Grid grid) throws ClassNotFoundException, IOException {
  39 + public TablooProto(Grid grid) {
40 40 super(new GridLayout(1, 0));
41 41  
42 42 // modele de donnees
... ... @@ -61,7 +61,6 @@ public class TablooProto extends JPanel {
61 61 TableColumn tm = table.getColumnModel().getColumn(0);
62 62 tm.setPreferredWidth(tm.getPreferredWidth() * 2 / 3);
63 63 tm.setCellRenderer(new PremiereColonneSpecificRenderer(Color.LIGHT_GRAY));
64   -
65 64 }
66 65  
67 66 // Inner class pour changer l'aspect de la premiere colonne consacree a la numerotation des lignes
... ...
src/kernel/Cell.java
1 1 package kernel;
2 2  
3 3 import kernel.exception.CreateCycleException;
4   -import kernel.exception.InvalidIntervalException;
5 4  
6 5 import java.io.Serializable;
7 6 import java.util.ArrayList;
... ... @@ -19,9 +18,6 @@ public class Cell implements Serializable {
19 18 private List<Cell> usedIn = new ArrayList<>();
20 19  
21 20 public Cell(String column, int line, double value) {
22   -
23   -
24   -
25 21 this.column = column;
26 22 this.line = line;
27 23 this.setValue(value);
... ... @@ -29,8 +25,6 @@ public class Cell implements Serializable {
29 25  
30 26 public Cell(String column, int line, Formula formula)
31 27 throws CreateCycleException {
32   -
33   -
34 28 this.column = column;
35 29 this.line = line;
36 30 this.setFormula(formula);
... ... @@ -100,5 +94,5 @@ public class Cell implements Serializable {
100 94 }
101 95 }
102 96  
103   -
  97 +
104 98 }
... ...
src/kernel/Grid.java
... ... @@ -19,7 +19,6 @@ public class Grid implements Serializable {
19 19 private Map<String, Cell> cells = new HashMap<>();
20 20 public static LanguageEnum language = LanguageEnum.FR;
21 21  
22   -
23 22 public void createCell(String column, int line, double value) throws InvalidIntervalException {
24 23 column = column.toUpperCase();
25 24  
... ...
src/kernel/test/CellTest.java
1 1 package kernel.test;
2 2  
3   -import static org.junit.Assert.*;
4   -
5   -import java.util.ArrayList;
6   -import java.util.List;
7   -
8 3 import kernel.Cell;
  4 +import kernel.Grid;
  5 +import kernel.LanguageEnum;
9 6 import kernel.exception.CreateCycleException;
10 7 import kernel.exception.InvalidIntervalException;
11 8 import kernel.function.Average;
12 9 import kernel.function.Sum;
13 10 import kernel.operation.Addition;
  11 +import org.junit.Before;
  12 +import org.junit.Test;
14 13  
  14 +import java.util.ArrayList;
  15 +import java.util.List;
15 16  
16   -public class CellTest {
  17 +import static org.junit.Assert.*;
17 18  
18   - @org.junit.Test
19   - public void CreateCellValueTest() throws InvalidIntervalException {
20   - Cell A1= new Cell("A",1,25.);
21   - assertEquals(A1.getValue(),25.,0);
22   - assertEquals(A1.containFormula(),false);
23   - assertEquals(A1.getUsedIn().size(),0);
24   - assertEquals(A1.getId(),"A1");
25   - }
26 19  
  20 +public class CellTest {
27 21  
28   - @org.junit.Test
29   - public void CreateCellBinaryOperationTest() throws CreateCycleException, InvalidIntervalException {
30   - Cell A1= new Cell("A",1,25.);
31   - Cell A2= new Cell("A",2,35.);
32   -
33   -
34   - Cell A3= new Cell("A",3,new Addition(A1,A2));
  22 + @Before
  23 + public void initData() {
  24 + Grid.language = LanguageEnum.EN;
  25 + }
  26 +
  27 + @Test
  28 + public void CreateCellValueTest() {
  29 + Cell A1 = new Cell("A", 1, 25.);
  30 + assertEquals(A1.getValue(), 25., 0);
  31 + assertFalse(A1.containFormula());
  32 + assertEquals(A1.getUsedIn().size(), 0);
  33 + assertEquals(A1.getId(), "A1");
  34 + }
  35 +
  36 + @Test
  37 + public void CreateCellBinaryOperationTest() throws CreateCycleException {
  38 + Cell A1 = new Cell("A", 1, 25.);
  39 + Cell A2 = new Cell("A", 2, 35.);
  40 + Cell A3 = new Cell("A", 3, new Addition(A1, A2));
35 41  
36   - assertEquals(A3.getValue(),60.,0);
37   - assertEquals(A3.containFormula(),true);
38   - assertEquals(A3.getUsedIn().size(),0);
39   - assertEquals(A3.getId(),"A3");
  42 + assertEquals(A3.getValue(), 60., 0);
  43 + assertTrue(A3.containFormula());
  44 + assertEquals(A3.getUsedIn().size(), 0);
  45 + assertEquals(A3.getId(), "A3");
40 46  
41   - assertEquals(A3.toString(),"A1+A2");
42   - assertEquals(A1.getUsedIn().size(),1);
43   - assertEquals(A2.getUsedIn().size(),1);
44   -
45   -
  47 + assertEquals(A3.toString(), "(A1+A2)");
  48 + assertEquals(A1.getUsedIn().size(), 1);
  49 + assertEquals(A2.getUsedIn().size(), 1);
46 50 }
47   -
48 51  
49   - @org.junit.Test
50   - public void CreateCellFunctionTest() throws CreateCycleException, InvalidIntervalException {
51   - Cell A1= new Cell("A",1,25.);
52   - Cell A2= new Cell("A",2,35.);
53   - Cell A3= new Cell("A",3,new Addition(A1,A2));
54   - Cell A5= new Cell("A",5,45.);
  52 + @Test
  53 + public void CreateCellFunctionTest() throws CreateCycleException {
  54 + Cell A1 = new Cell("A", 1, 25.);
  55 + Cell A2 = new Cell("A", 2, 35.);
  56 + Cell A3 = new Cell("A", 3, new Addition(A1, A2));
  57 + Cell A5 = new Cell("A", 5, 45.);
55 58 List<Cell> sumList = new ArrayList<>();
56   - sumList.add(A1);
57   - sumList.add(A2);
58   - sumList.add(A3);
59   - sumList.add(A5);
60   - Cell A4= new Cell("A",4,new Sum(sumList));
61   -
62   - assertEquals(A4.getValue(),165.,0);
63   - assertEquals(A4.containFormula(),true);
64   - assertEquals(A4.getUsedIn().size(),0);
  59 + sumList.add(A1);
  60 + sumList.add(A2);
  61 + sumList.add(A3);
  62 + sumList.add(A5);
  63 + Cell A4 = new Cell("A", 4, new Sum(sumList));
65 64  
  65 + assertEquals(A4.getValue(), 165., 0);
  66 + assertTrue(A4.containFormula());
  67 + assertEquals(A4.getUsedIn().size(), 0);
66 68  
67   - assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)");
68   - assertEquals(A4.getDevelopedFormula(),"SOMME(A1,A2,(A1+A2),A5)");
69   - assertEquals(A1.getUsedIn().size(),2);
70   - assertEquals(A2.getUsedIn().size(),2);
71   - assertEquals(A3.getUsedIn().size(),1);
72   - assertEquals(A5.getUsedIn().size(),1);
73   -
74   -
  69 + assertEquals(A4.toString(), "SUM(A1,A2,A3,A5)");
  70 + assertEquals(A4.getDevelopedFormula(), "SUM(A1,A2,(A1+A2),A5)");
  71 + assertEquals(A1.getUsedIn().size(), 2);
  72 + assertEquals(A2.getUsedIn().size(), 2);
  73 + assertEquals(A3.getUsedIn().size(), 1);
  74 + assertEquals(A5.getUsedIn().size(), 1);
75 75 }
76   -
77 76  
78   - @org.junit.Test
79   - public void ModifyCellValueTest() throws CreateCycleException, InvalidIntervalException {
80   - Cell A1= new Cell("A",1,25.);
81   - Cell A2= new Cell("A",2,35.);
82   - Cell A3= new Cell("A",3,new Addition(A1,A2));
83   - assertEquals(A1.getValue(),25.,0);
84   - assertEquals(A2.getValue(),35.,0);
85   - assertEquals(A3.getValue(),60.,0);
  77 + @Test
  78 + public void ModifyCellValueTest() throws CreateCycleException {
  79 + Cell A1 = new Cell("A", 1, 25.);
  80 + Cell A2 = new Cell("A", 2, 35.);
  81 + Cell A3 = new Cell("A", 3, new Addition(A1, A2));
  82 + assertEquals(A1.getValue(), 25., 0);
  83 + assertEquals(A2.getValue(), 35., 0);
  84 + assertEquals(A3.getValue(), 60., 0);
86 85 A1.setValue(45.);
87   - assertEquals(A1.getValue(),45.,0);
88   - assertEquals(A3.getValue(),80.,0);
89   -
  86 + assertEquals(A1.getValue(), 45., 0);
  87 + assertEquals(A3.getValue(), 80., 0);
90 88 }
91   -
92   - @org.junit.Test
93   - public void ModifyCellFormulaTest() throws CreateCycleException, InvalidIntervalException {
94   - Cell A1= new Cell("A",1,25.);
95   - Cell A2= new Cell("A",2,35.);
96   - Cell A3= new Cell("A",3,new Addition(A1,A2));
97   - Cell A5= new Cell("A",5,45.);
  89 +
  90 + @Test
  91 + public void ModifyCellFormulaTest() throws CreateCycleException {
  92 + Cell A1 = new Cell("A", 1, 25.);
  93 + Cell A2 = new Cell("A", 2, 35.);
  94 + Cell A3 = new Cell("A", 3, new Addition(A1, A2));
  95 + Cell A5 = new Cell("A", 5, 45.);
98 96 List<Cell> sumList = new ArrayList<>();
99   - sumList.add(A1);
100   - sumList.add(A2);
101   - sumList.add(A3);
102   - sumList.add(A5);
103   - Cell A4= new Cell("A",4,new Sum(sumList));
  97 + sumList.add(A1);
  98 + sumList.add(A2);
  99 + sumList.add(A3);
  100 + sumList.add(A5);
  101 + Cell A4 = new Cell("A", 4, new Sum(sumList));
104 102  
105   - assertEquals(A4.getValue(),165.,0);
106   - assertEquals(A4.containFormula(),true);
107   - assertEquals(A4.getUsedIn().size(),0);
  103 + assertEquals(A4.getValue(), 165., 0);
  104 + assertTrue(A4.containFormula());
  105 + assertEquals(A4.getUsedIn().size(), 0);
108 106  
109   - assertEquals(A1.getUsedIn().size(),2);
110   - assertEquals(A2.getUsedIn().size(),2);
111   - assertEquals(A3.getUsedIn().size(),1);
112   - assertEquals(A5.getUsedIn().size(),1);
  107 + assertEquals(A1.getUsedIn().size(), 2);
  108 + assertEquals(A2.getUsedIn().size(), 2);
  109 + assertEquals(A3.getUsedIn().size(), 1);
  110 + assertEquals(A5.getUsedIn().size(), 1);
113 111  
114   - assertEquals(A1.containFormula(),false);
115   - A1.setFormula(new Addition(A2,A5));
116   - assertEquals(A1.containFormula(),true);
  112 + assertFalse(A1.containFormula());
  113 + A1.setFormula(new Addition(A2, A5));
  114 + assertTrue(A1.containFormula());
117 115  
118   - assertEquals(A1.getValue(),80.,0);
119   - assertEquals(A3.getValue(),115.,0);
120   - assertEquals(A4.getValue(),275.,0);
121   -
122   -
  116 + assertEquals(A1.getValue(), 80., 0);
  117 + assertEquals(A3.getValue(), 115., 0);
  118 + assertEquals(A4.getValue(), 275., 0);
123 119  
  120 + assertEquals(A1.toString(), "(A2+A5)");
  121 + assertEquals(A1.getDevelopedFormula(), "(A2+A5)");
  122 + assertEquals(A4.toString(), "SUM(A1,A2,A3,A5)");
  123 + assertEquals(A4.getDevelopedFormula(), "SUM((A2+A5),A2,((A2+A5)+A2),A5)");
124 124  
125   - assertEquals(A1.toString(),"A2+A5");
126   - assertEquals(A1.getDevelopedFormula(),"(A2+A5)");
127   - assertEquals(A4.toString(),"SOMME(A1,A2,A3,A5)");
128   - assertEquals(A4.getDevelopedFormula(),"SOMME((A2+A5),A2,((A2+A5)+A2),A5)");
129   -
130   -
131   - assertEquals(A1.getUsedIn().size(),2);
132   - assertEquals(A2.getUsedIn().size(),3);
133   - assertEquals(A3.getUsedIn().size(),1);
134   - assertEquals(A5.getUsedIn().size(),2);
135   -
136   -
  125 + assertEquals(A1.getUsedIn().size(), 2);
  126 + assertEquals(A2.getUsedIn().size(), 3);
  127 + assertEquals(A3.getUsedIn().size(), 1);
  128 + assertEquals(A5.getUsedIn().size(), 2);
137 129 }
138 130  
139   - @org.junit.Test(expected=CreateCycleException.class)
140   - public void DirectCycleBynaryOperation() throws CreateCycleException, InvalidIntervalException {
141   - Cell A1= new Cell("A",1,25.);
142   - Cell A2= new Cell("A",2,35.);
143   - try{
144   - A1.setFormula(new Addition(A1,A2));
145   - }
146   - catch(CreateCycleException ex){
147   - throw ex;
148   - }
  131 + @Test(expected = CreateCycleException.class)
  132 + public void DirectCycleBynaryOperation() throws CreateCycleException {
  133 + Cell A1 = new Cell("A", 1, 25.);
  134 + Cell A2 = new Cell("A", 2, 35.);
  135 + A1.setFormula(new Addition(A1, A2));
149 136 }
150 137  
151 138  
152   - @org.junit.Test(expected=CreateCycleException.class)
153   - public void DirectCycleFunction() throws CreateCycleException, InvalidIntervalException {
154   - Cell A2= new Cell("A",2,25.);
155   - Cell B2= new Cell("B",2,35.);
156   - Cell A3= new Cell("A",3,0.5);
  139 + @Test(expected = CreateCycleException.class)
  140 + public void DirectCycleFunction() throws CreateCycleException {
  141 + Cell A2 = new Cell("A", 2, 25.);
  142 + Cell B2 = new Cell("B", 2, 35.);
  143 + Cell A3 = new Cell("A", 3, 0.5);
157 144 List<Cell> sumList = new ArrayList<>();
158   - sumList.add(A2);
159   - sumList.add(B2);
160   - sumList.add(A3);
161   -
162   - try{
163   - B2.setFormula(new Sum(sumList));
164   - }
165   - catch(CreateCycleException ex){
166   - throw ex;
167   - }
  145 + sumList.add(A2);
  146 + sumList.add(B2);
  147 + sumList.add(A3);
  148 +
  149 + B2.setFormula(new Sum(sumList));
168 150 }
169 151  
170   - @org.junit.Test(expected=CreateCycleException.class)
171   - public void IndirectCycle() throws CreateCycleException, InvalidIntervalException {
172   - Cell A1= new Cell("A",1,25.);
173   - Cell A2= new Cell("A",2,5.);
174   - Cell B4= new Cell("B",4,new Addition(A1,A2));
175   - Cell A4= new Cell("A",4,0.);
176   - Cell A3= new Cell("A",3,0.5);
177   - Cell B2= new Cell("B",2,12.);
  152 + @Test(expected = CreateCycleException.class)
  153 + public void IndirectCycle() throws CreateCycleException {
  154 + Cell A1 = new Cell("A", 1, 25.);
  155 + Cell A2 = new Cell("A", 2, 5.);
  156 + Cell B4 = new Cell("B", 4, new Addition(A1, A2));
  157 + Cell A4 = new Cell("A", 4, 0.);
  158 + Cell A3 = new Cell("A", 3, 0.5);
  159 + Cell B2 = new Cell("B", 2, 12.);
178 160  
179 161 List<Cell> sumList = new ArrayList<>();
180   - sumList.add(A2);
181   - sumList.add(B2);
182   - sumList.add(A3);
183   - Cell B6= new Cell("B",6,new Sum(sumList));
184   -
  162 + sumList.add(A2);
  163 + sumList.add(B2);
  164 + sumList.add(A3);
  165 + Cell B6 = new Cell("B", 6, new Sum(sumList));
  166 +
185 167 List<Cell> averageList = new ArrayList<>();
186 168 averageList.add(B6);
187 169 averageList.add(B4);
188 170 averageList.add(A4);
189   - Cell C6= new Cell("C",6,new Average(averageList));
190   -
191   - try{
192   - B2.setFormula(new Addition(A1,C6));
193   - }
194   - catch(CreateCycleException ex){
195   - throw ex;
196   - }
  171 + Cell C6 = new Cell("C", 6, new Average(averageList));
  172 +
  173 + B2.setFormula(new Addition(A1, C6));
197 174 }
198   -
199   -
200 175 }
... ...
src/kernel/test/FunctionTest.java
... ... @@ -3,7 +3,7 @@ package kernel.test;
3 3 import kernel.Cell;
4 4 import kernel.Grid;
5 5 import kernel.LanguageEnum;
6   -import kernel.exception.InvalidIntervalException;
  6 +import kernel.function.Average;
7 7 import kernel.function.Sum;
8 8 import org.junit.Before;
9 9 import org.junit.Test;
... ... @@ -11,12 +11,15 @@ import org.junit.Test;
11 11 import java.util.ArrayList;
12 12 import java.util.List;
13 13  
  14 +import static org.junit.Assert.assertEquals;
  15 +import static org.junit.Assert.assertNotNull;
  16 +
14 17 public class FunctionTest {
15 18  
16 19 private List<Cell> cells;
17 20  
18 21 @Before
19   - public void initData() throws InvalidIntervalException {
  22 + public void initData() {
20 23 Grid.language = LanguageEnum.EN;
21 24 this.cells = new ArrayList<>();
22 25  
... ... @@ -32,5 +35,18 @@ public class FunctionTest {
32 35 @Test
33 36 public void testSum() {
34 37 Sum sum = new Sum(this.cells);
  38 +
  39 + assertNotNull(sum);
  40 + assertEquals(30, sum.eval(), 0);
  41 + assertEquals("SUM(A1,A2,A3)", sum.toString());
  42 + }
  43 +
  44 + @Test
  45 + public void testAverage() {
  46 + Average average = new Average(this.cells);
  47 +
  48 + assertNotNull(average);
  49 + assertEquals(10, average.eval(), 0);
  50 + assertEquals("AVERAGE(A1,A2,A3)", average.toString());
35 51 }
36 52 }
... ...
src/kernel/test/GridTest.java
... ... @@ -3,6 +3,7 @@ package kernel.test;
3 3 import kernel.Cell;
4 4 import kernel.Grid;
5 5 import kernel.LanguageEnum;
  6 +import kernel.exception.CannotDeleteCellException;
6 7 import kernel.exception.CellNotFoundException;
7 8 import kernel.exception.CreateCycleException;
8 9 import kernel.exception.InvalidIntervalException;
... ... @@ -13,6 +14,7 @@ import kernel.operation.Multiplication;
13 14 import org.junit.Before;
14 15 import org.junit.Test;
15 16  
  17 +import java.io.IOException;
16 18 import java.util.ArrayList;
17 19 import java.util.List;
18 20  
... ... @@ -56,6 +58,13 @@ public class GridTest {
56 58 this.grid.createCell("AZ", 120, 0.);
57 59 }
58 60  
  61 + @Test(expected = CellNotFoundException.class)
  62 + public void testGetUnknownCell() throws CellNotFoundException, InvalidIntervalException {
  63 + this.createCellsWithValue();
  64 +
  65 + this.grid.getCell("B", 5);
  66 + }
  67 +
59 68 @Test
60 69 public void testGetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
61 70 this.createCellsWithFormula();
... ... @@ -199,6 +208,52 @@ public class GridTest {
199 208 assertEquals(3, this.grid.getCells().size());
200 209 }
201 210  
  211 + @Test
  212 + public void testSave() throws InvalidIntervalException, IOException, CellNotFoundException, ClassNotFoundException,
  213 + CreateCycleException {
  214 + this.createCellsWithFormula();
  215 +
  216 + this.grid.save();
  217 +
  218 + Grid g = Grid.load();
  219 +
  220 + Cell cell = g.getCell("A", 1);
  221 +
  222 + assertEquals("A1", cell.getId());
  223 + assertEquals(10, cell.getValue(), 0);
  224 +
  225 + cell = g.getCell("B", 1);
  226 +
  227 + assertEquals("B1", cell.getId());
  228 + assertTrue(cell.containFormula());
  229 + assertEquals(10, cell.getValue(), 0);
  230 + }
  231 +
  232 + @Test
  233 + public void testDeleteCell() throws CannotDeleteCellException, InvalidIntervalException {
  234 + this.createCellsWithValue();
  235 +
  236 + Cell cell = this.grid.getCell("A2");
  237 +
  238 + assertNotNull(cell);
  239 + assertEquals(0, cell.getValue(), 0);
  240 + assertFalse(cell.containFormula());
  241 +
  242 + this.grid.deleteCell("A", 2);
  243 +
  244 + cell = this.grid.getCell("A2");
  245 +
  246 + assertNull(cell);
  247 + }
  248 +
  249 + @Test(expected = CannotDeleteCellException.class)
  250 + public void testDeleteCellUsedInAnother() throws CannotDeleteCellException, InvalidIntervalException,
  251 + CellNotFoundException, CreateCycleException {
  252 + this.createCellsWithFormula();
  253 +
  254 + this.grid.deleteCell("A", 1);
  255 + }
  256 +
202 257 private void createCellsWithValue() throws InvalidIntervalException {
203 258 this.grid.createCell("A", 1, 10.);
204 259 this.grid.createCell("A", 2, 0.);
... ...