Ajout assertion non null pour méthode, économie sur test unitaire
Clement Colmerauer

Clement Colmerauer commited on 2024-12-13 12:29:53
Showing 34 changed files, with 58 additions and 34 deletions.

... ...
@@ -41,7 +41,7 @@
41 41
 </td>
42 42
 <td>
43 43
 <div class="infoBox" id="duration">
44
-<div class="counter">0.169s</div>
44
+<div class="counter">0.162s</div>
45 45
 <p>duration</p>
46 46
 </div>
47 47
 </td>
... ...
@@ -76,7 +76,7 @@
76 76
 </thead>
77 77
 <tr>
78 78
 <td class="success">testAffichageBase()</td>
79
-<td class="success">0.169s</td>
79
+<td class="success">0.162s</td>
80 80
 <td class="success">passed</td>
81 81
 </tr>
82 82
 </table>
... ...
@@ -89,7 +89,7 @@
89 89
 <input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/>
90 90
 </label>
91 91
 </div>Generated by 
92
-<a href="http://www.gradle.org">Gradle 8.8</a> at 13 déc. 2024 à 12:05:19</p>
92
+<a href="http://www.gradle.org">Gradle 8.8</a> at 13 déc. 2024 à 12:29:27</p>
93 93
 </div>
94 94
 </div>
95 95
 </body>
... ...
@@ -41,7 +41,7 @@
41 41
 </td>
42 42
 <td>
43 43
 <div class="infoBox" id="duration">
44
-<div class="counter">0.036s</div>
44
+<div class="counter">0.069s</div>
45 45
 <p>duration</p>
46 46
 </div>
47 47
 </td>
... ...
@@ -84,19 +84,19 @@
84 84
 <tr>
85 85
 <td class="success">Item and player</td>
86 86
 <td class="success">testBuySell()</td>
87
-<td class="success">0.003s</td>
87
+<td class="success">0.002s</td>
88 88
 <td class="success">passed</td>
89 89
 </tr>
90 90
 <tr>
91 91
 <td class="success">Heal and hurt</td>
92 92
 <td class="success">testHealAndHurt()</td>
93
-<td class="success">0.001s</td>
93
+<td class="success">0.002s</td>
94 94
 <td class="success">passed</td>
95 95
 </tr>
96 96
 <tr>
97 97
 <td class="success">Item tests</td>
98 98
 <td class="success">testItem()</td>
99
-<td class="success">0.009s</td>
99
+<td class="success">0.035s</td>
100 100
 <td class="success">passed</td>
101 101
 </tr>
102 102
 <tr>
... ...
@@ -138,13 +138,13 @@
138 138
 <tr>
139 139
 <td class="success">removeMoney</td>
140 140
 <td class="success">testRemoveMoney()</td>
141
-<td class="success">0.008s</td>
141
+<td class="success">0.014s</td>
142 142
 <td class="success">passed</td>
143 143
 </tr>
144 144
 <tr>
145 145
 <td class="success">ex &amp; lvl</td>
146 146
 <td class="success">testRetrieveLevel()</td>
147
-<td class="success">0.003s</td>
147
+<td class="success">0.004s</td>
148 148
 <td class="success">passed</td>
149 149
 </tr>
150 150
 </table>
... ...
@@ -157,7 +157,7 @@
157 157
 <input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/>
158 158
 </label>
159 159
 </div>Generated by 
160
-<a href="http://www.gradle.org">Gradle 8.8</a> at 13 déc. 2024 à 12:05:19</p>
160
+<a href="http://www.gradle.org">Gradle 8.8</a> at 13 déc. 2024 à 12:29:27</p>
161 161
 </div>
162 162
 </div>
163 163
 </body>
... ...
@@ -38,7 +38,7 @@
38 38
 </td>
39 39
 <td>
40 40
 <div class="infoBox" id="duration">
41
-<div class="counter">0.205s</div>
41
+<div class="counter">0.231s</div>
42 42
 <p>duration</p>
43 43
 </div>
44 44
 </td>
... ...
@@ -85,7 +85,7 @@
85 85
 <td>13</td>
86 86
 <td>0</td>
87 87
 <td>0</td>
88
-<td>0.205s</td>
88
+<td>0.231s</td>
89 89
 <td class="success">100%</td>
90 90
 </tr>
91 91
 </tbody>
... ...
@@ -112,7 +112,7 @@
112 112
 <td>1</td>
113 113
 <td>0</td>
114 114
 <td>0</td>
115
-<td>0.169s</td>
115
+<td>0.162s</td>
116 116
 <td class="success">100%</td>
117 117
 </tr>
118 118
 <tr>
... ...
@@ -122,7 +122,7 @@
122 122
 <td>12</td>
123 123
 <td>0</td>
124 124
 <td>0</td>
125
-<td>0.036s</td>
125
+<td>0.069s</td>
126 126
 <td class="success">100%</td>
127 127
 </tr>
128 128
 </tbody>
... ...
@@ -136,7 +136,7 @@
136 136
 <input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/>
137 137
 </label>
138 138
 </div>Generated by 
139
-<a href="http://www.gradle.org">Gradle 8.8</a> at 13 déc. 2024 à 12:05:19</p>
139
+<a href="http://www.gradle.org">Gradle 8.8</a> at 13 déc. 2024 à 12:29:27</p>
140 140
 </div>
141 141
 </div>
142 142
 </body>
... ...
@@ -40,7 +40,7 @@
40 40
 </td>
41 41
 <td>
42 42
 <div class="infoBox" id="duration">
43
-<div class="counter">0.205s</div>
43
+<div class="counter">0.231s</div>
44 44
 <p>duration</p>
45 45
 </div>
46 46
 </td>
... ...
@@ -83,7 +83,7 @@
83 83
 <td>1</td>
84 84
 <td>0</td>
85 85
 <td>0</td>
86
-<td>0.169s</td>
86
+<td>0.162s</td>
87 87
 <td class="success">100%</td>
88 88
 </tr>
89 89
 <tr>
... ...
@@ -93,7 +93,7 @@
93 93
 <td>12</td>
94 94
 <td>0</td>
95 95
 <td>0</td>
96
-<td>0.036s</td>
96
+<td>0.069s</td>
97 97
 <td class="success">100%</td>
98 98
 </tr>
99 99
 </table>
... ...
@@ -106,7 +106,7 @@
106 106
 <input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/>
107 107
 </label>
108 108
 </div>Generated by 
109
-<a href="http://www.gradle.org">Gradle 8.8</a> at 13 déc. 2024 à 12:05:19</p>
109
+<a href="http://www.gradle.org">Gradle 8.8</a> at 13 déc. 2024 à 12:29:27</p>
110 110
 </div>
111 111
 </div>
112 112
 </body>
... ...
@@ -1,7 +1,7 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2
-<testsuite name="re.forestier.edu.GlobalTest" tests="1" skipped="0" failures="0" errors="0" timestamp="2024-12-13T11:05:19" hostname="clement-G551JM" time="0.169">
2
+<testsuite name="re.forestier.edu.GlobalTest" tests="1" skipped="0" failures="0" errors="0" timestamp="2024-12-13T11:29:27" hostname="clement-G551JM" time="0.162">
3 3
   <properties/>
4
-  <testcase name="testAffichageBase()" classname="re.forestier.edu.GlobalTest" time="0.169"/>
4
+  <testcase name="testAffichageBase()" classname="re.forestier.edu.GlobalTest" time="0.162"/>
5 5
   <system-out><![CDATA[]]></system-out>
6 6
   <system-err><![CDATA[]]></system-err>
7 7
 </testsuite>
... ...
@@ -1,13 +1,13 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2
-<testsuite name="re.forestier.edu.UnitTests" tests="12" skipped="0" failures="0" errors="0" timestamp="2024-12-13T11:05:19" hostname="clement-G551JM" time="0.049">
2
+<testsuite name="re.forestier.edu.UnitTests" tests="12" skipped="0" failures="0" errors="0" timestamp="2024-12-13T11:29:27" hostname="clement-G551JM" time="0.083">
3 3
   <properties/>
4
-  <testcase name="Item tests" classname="re.forestier.edu.UnitTests" time="0.009"/>
4
+  <testcase name="Item tests" classname="re.forestier.edu.UnitTests" time="0.035"/>
5 5
   <testcase name="Main" classname="re.forestier.edu.UnitTests" time="0.002"/>
6
-  <testcase name="removeMoney" classname="re.forestier.edu.UnitTests" time="0.008"/>
7
-  <testcase name="ex &amp; lvl" classname="re.forestier.edu.UnitTests" time="0.003"/>
6
+  <testcase name="removeMoney" classname="re.forestier.edu.UnitTests" time="0.014"/>
7
+  <testcase name="ex &amp; lvl" classname="re.forestier.edu.UnitTests" time="0.004"/>
8 8
   <testcase name="addMoney" classname="re.forestier.edu.UnitTests" time="0.001"/>
9
-  <testcase name="Heal and hurt" classname="re.forestier.edu.UnitTests" time="0.001"/>
10
-  <testcase name="Item and player" classname="re.forestier.edu.UnitTests" time="0.003"/>
9
+  <testcase name="Heal and hurt" classname="re.forestier.edu.UnitTests" time="0.002"/>
10
+  <testcase name="Item and player" classname="re.forestier.edu.UnitTests" time="0.002"/>
11 11
   <testcase name="Construcor Manager" classname="re.forestier.edu.UnitTests" time="0.001"/>
12 12
   <testcase name="majDeFinDeTour" classname="re.forestier.edu.UnitTests" time="0.002"/>
13 13
   <testcase name="Constructor Player" classname="re.forestier.edu.UnitTests" time="0.002"/>
... ...
@@ -12,6 +12,7 @@ public class Natural implements Comparable<Natural>, Cloneable
12 12
 
13 13
     private Natural(Integer i)
14 14
     {
15
+        assert i != null : "Argument must be non null";
15 16
         if(i == null)
16 17
             throw new IllegalArgumentException();
17 18
         if(i < 0)
... ...
@@ -38,6 +39,7 @@ public class Natural implements Comparable<Natural>, Cloneable
38 39
 
39 40
     public static Natural valueOf(Integer i)
40 41
     {
42
+        assert i != null : "Argument must be non null";
41 43
         if(i == null)
42 44
             throw new IllegalArgumentException();
43 45
         return new Natural(i);
... ...
@@ -50,6 +52,7 @@ public class Natural implements Comparable<Natural>, Cloneable
50 52
 
51 53
     public void add(Natural n)
52 54
     {
55
+        assert n != null : "Argument must be non null";
53 56
         if(n == null)
54 57
             throw new IllegalArgumentException();
55 58
         this.value += n.toInt();
... ...
@@ -58,6 +61,7 @@ public class Natural implements Comparable<Natural>, Cloneable
58 61
 
59 62
     public void substract(Natural n)
60 63
     {
64
+        assert n != null : "Argument must be non null";
61 65
         if(n == null || this.compareTo(n) == 1)
62 66
             throw new IllegalArgumentException();
63 67
         this.value -= n.toInt();
... ...
@@ -90,8 +94,7 @@ public class Natural implements Comparable<Natural>, Cloneable
90 94
     @Override
91 95
     public int compareTo(Natural n)
92 96
     {
93
-        if(n == null)
94
-            throw new IllegalArgumentException();
97
+        assert n != null : "Argument must be non null";
95 98
         if(n.value > this.value)
96 99
             return 1;
97 100
         else if(n.value == this.value)
... ...
@@ -9,11 +9,13 @@ public class Affichage {
9 9
 
10 10
     public static void afficherJoueur(Player player) 
11 11
     {
12
+        assert player != null : "Player must be non null";
12 13
         System.out.println(player.toString());
13 14
     }
14 15
 
15 16
     public static void playerToMarkDown(Player p)
16 17
     {
18
+        assert p != null : "Player must be non null";
17 19
         try 
18 20
         {
19 21
             File playerFile = new File("./"+p.getAvatarName()+".md");
... ...
@@ -29,6 +31,8 @@ public class Affichage {
29 31
 
30 32
     public static void playerToMarkDown(Player p, String path)
31 33
     {
34
+        assert p != null : "Player must be non null";
35
+        assert path != null : "Path must be non null";
32 36
         try 
33 37
         {
34 38
             File playerFile = new File(path+p.getAvatarName()+".md");
... ...
@@ -11,6 +11,11 @@ public class Item implements Cloneable
11 11
 
12 12
 	public Item(String name, String description, Natural weight, Natural value)
13 13
 	{
14
+		assert name != null : "Name must be non null";
15
+		assert description != null : "Description must be non null";
16
+		assert weight != null : "Weight must be non null";
17
+		assert value != null : "Value must be non null";
18
+
14 19
 		this.name = name;
15 20
 		this.description = description;
16 21
 		this.weight = weight;
... ...
@@ -19,6 +24,7 @@ public class Item implements Cloneable
19 24
 
20 25
 	Item(String name)
21 26
 	{
27
+		assert name != null : "Name must be non null";
22 28
 		this.name = name;
23 29
 		this.description = "Is used to search in list, if you see me, there is a problem";
24 30
 	}
... ...
@@ -19,7 +19,9 @@ public class Manager {
19 19
        //Since we don't have value for weight and value, we put neutral
20 20
 
21 21
     // majFinDeTour met à jour les points de vie
22
-    public static void majFinDeTour(Player player) {
22
+    public static void majFinDeTour(Player player) 
23
+    {
24
+        assert player != null : "Player must be non null";
23 25
         if(player.getCurrentHealthPoints() == 0) {
24 26
             System.out.println("Le joueur est KO !");
25 27
             return;
... ...
@@ -40,6 +40,11 @@ public class Player {
40 40
 
41 41
     public Player(String playerName, String avatar_name, Jobs avatarClass, int money, ArrayList<Item> inventory, int maxHp, int maxWeight)
42 42
     {
43
+        assert playerName != null : "Player name can't be null";
44
+        assert avatar_name != null : "Avatar name can't be null";
45
+        assert avatarClass != null : "Avatar class can't be null";
46
+        assert inventory != null : "Inventory can't be null";
47
+
43 48
         this.playerName = playerName;
44 49
         this.avatarName = avatar_name;
45 50
         this.avatarClass = avatarClass;
... ...
@@ -124,6 +129,7 @@ public class Player {
124 129
 
125 130
     public void addItem(Item i)
126 131
     {
132
+        assert i != null : "Argument must be non null";
127 133
         Natural w = (Natural)this.weight.clone();
128 134
         w.add(i.getWeight());
129 135
         if(w.compareTo(this.maxWeight) == -1)
... ...
@@ -138,6 +144,7 @@ public class Player {
138 144
 
139 145
     public void removeItem(Item i)
140 146
     {
147
+        assert i != null : "Argument must be non null";
141 148
         if(!this.inventory.contains(i))
142 149
         {
143 150
             throw new UnsupportedOperationException("Can't remove what player don't have");
... ...
@@ -148,6 +155,7 @@ public class Player {
148 155
 
149 156
     public void buy(Item i)
150 157
     {
158
+        assert i != null : "Argument must be non null";
151 159
         if(this.money.compareTo(i.getValue()) == 1)
152 160
         {
153 161
             throw new UnsupportedOperationException("Player doesn't have enough money.");
... ...
@@ -168,6 +176,7 @@ public class Player {
168 176
 
169 177
     public void sell(Item i)
170 178
     {
179
+        assert i != null : "Argument must be non null";
171 180
         if(!this.inventory.contains(i))
172 181
         {
173 182
             throw new UnsupportedOperationException("Player can't sell what they don't own.");
... ...
@@ -245,16 +245,16 @@ public class UnitTests {
245 245
         assertEquals(n.toInt(),2);
246 246
         assertThrows(IllegalArgumentException.class, () -> Natural.valueOf(-2));
247 247
         assertTrue(Integer.valueOf(2).equals(n.toInteger()));
248
-        assertThrows(IllegalArgumentException.class, () -> Natural.valueOf((Integer)null));
248
+        assertThrows(AssertionError.class, () -> Natural.valueOf((Integer)null));
249 249
 
250 250
         n = Natural.valueOf(Integer.valueOf(2));
251 251
         assertEquals(n.toInt(),2);
252 252
         assertThrows(IllegalArgumentException.class, () -> Natural.valueOf(Integer.valueOf(-2)));
253
-        assertThrows(IllegalArgumentException.class, () -> Natural.valueOf(null));
253
+        assertThrows(AssertionError.class, () -> Natural.valueOf(null));
254 254
 
255 255
         n.add(Natural.valueOf(2));
256 256
         assertEquals(n,Natural.valueOf(4));
257
-        assertThrows(IllegalArgumentException.class, () -> Natural.valueOf(0).add(null));
257
+        assertThrows(AssertionError.class, () -> Natural.valueOf(0).add(null));
258 258
         n.substract(Natural.valueOf(4));
259 259
         assertEquals(n,Natural.valueOf(0));
260 260
         assertThrows(IllegalArgumentException.class, () -> Natural.valueOf(0).substract(Natural.valueOf(4)));
... ...
@@ -270,7 +270,7 @@ public class UnitTests {
270 270
         assertEquals(n.compareTo(Natural.valueOf(2)),0);
271 271
         assertEquals(n.compareTo(Natural.valueOf(3)),1);
272 272
         assertEquals(n.compareTo(Natural.valueOf(1)),-1);
273
-        assertThrows(IllegalArgumentException.class, () -> Natural.valueOf(0).compareTo(null));
273
+        assertThrows(AssertionError.class, () -> Natural.valueOf(0).compareTo(null));
274 274
 
275 275
 
276 276
     }
277 277