Ajout toString player + passage de afficherJoueur en void
Clement COLMERAUER

Clement COLMERAUER commited on 2024-10-24 16:06:33
Showing 31 changed files, with 91 additions and 73 deletions.

... ...
@@ -41,7 +41,7 @@
41 41
 </td>
42 42
 <td>
43 43
 <div class="infoBox" id="duration">
44
-<div class="counter">0.178s</div>
44
+<div class="counter">0.160s</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.178s</td>
79
+<td class="success">0.160s</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 21 oct. 2024, 12:04:46</p>
92
+<a href="http://www.gradle.org">Gradle 8.8</a> at 24 oct. 2024, 16:05:59</p>
93 93
 </div>
94 94
 </div>
95 95
 </body>
... ...
@@ -23,7 +23,7 @@
23 23
 <tr>
24 24
 <td>
25 25
 <div class="infoBox" id="tests">
26
-<div class="counter">10</div>
26
+<div class="counter">9</div>
27 27
 <p>tests</p>
28 28
 </div>
29 29
 </td>
... ...
@@ -41,7 +41,7 @@
41 41
 </td>
42 42
 <td>
43 43
 <div class="infoBox" id="duration">
44
-<div class="counter">0.018s</div>
44
+<div class="counter">0.028s</div>
45 45
 <p>duration</p>
46 46
 </div>
47 47
 </td>
... ...
@@ -78,19 +78,13 @@
78 78
 <tr>
79 79
 <td class="success">addMoney</td>
80 80
 <td class="success">testAddMoney()</td>
81
-<td class="success">0.001s</td>
82
-<td class="success">passed</td>
83
-</tr>
84
-<tr>
85
-<td class="success">Affichage</td>
86
-<td class="success">testAffichage()</td>
87
-<td class="success">0.006s</td>
81
+<td class="success">0s</td>
88 82
 <td class="success">passed</td>
89 83
 </tr>
90 84
 <tr>
91 85
 <td class="success">Main</td>
92 86
 <td class="success">testMain()</td>
93
-<td class="success">0.001s</td>
87
+<td class="success">0.013s</td>
94 88
 <td class="success">passed</td>
95 89
 </tr>
96 90
 <tr>
... ...
@@ -108,25 +102,25 @@
108 102
 <tr>
109 103
 <td class="success">Constructor Player</td>
110 104
 <td class="success">testPlayerConstructor()</td>
111
-<td class="success">0.001s</td>
105
+<td class="success">0s</td>
112 106
 <td class="success">passed</td>
113 107
 </tr>
114 108
 <tr>
115 109
 <td class="success">Sample test</td>
116 110
 <td class="success">testPlayerName()</td>
117
-<td class="success">0s</td>
111
+<td class="success">0.001s</td>
118 112
 <td class="success">passed</td>
119 113
 </tr>
120 114
 <tr>
121 115
 <td class="success">removeMoney</td>
122 116
 <td class="success">testRemoveMoney()</td>
123
-<td class="success">0.004s</td>
117
+<td class="success">0.008s</td>
124 118
 <td class="success">passed</td>
125 119
 </tr>
126 120
 <tr>
127 121
 <td class="success">ex &amp; lvl</td>
128 122
 <td class="success">testRetrieveLevel()</td>
129
-<td class="success">0.002s</td>
123
+<td class="success">0.003s</td>
130 124
 <td class="success">passed</td>
131 125
 </tr>
132 126
 <tr>
... ...
@@ -145,7 +139,7 @@
145 139
 <input id="line-wrapping-toggle" type="checkbox" autocomplete="off"/>
146 140
 </label>
147 141
 </div>Generated by 
148
-<a href="http://www.gradle.org">Gradle 8.8</a> at 21 oct. 2024, 12:04:46</p>
142
+<a href="http://www.gradle.org">Gradle 8.8</a> at 24 oct. 2024, 16:05:59</p>
149 143
 </div>
150 144
 </div>
151 145
 </body>
... ...
@@ -20,7 +20,7 @@
20 20
 <tr>
21 21
 <td>
22 22
 <div class="infoBox" id="tests">
23
-<div class="counter">11</div>
23
+<div class="counter">10</div>
24 24
 <p>tests</p>
25 25
 </div>
26 26
 </td>
... ...
@@ -38,7 +38,7 @@
38 38
 </td>
39 39
 <td>
40 40
 <div class="infoBox" id="duration">
41
-<div class="counter">0.196s</div>
41
+<div class="counter">0.188s</div>
42 42
 <p>duration</p>
43 43
 </div>
44 44
 </td>
... ...
@@ -82,10 +82,10 @@
82 82
 <td class="success">
83 83
 <a href="packages/re.forestier.edu.html">re.forestier.edu</a>
84 84
 </td>
85
-<td>11</td>
85
+<td>10</td>
86 86
 <td>0</td>
87 87
 <td>0</td>
88
-<td>0.196s</td>
88
+<td>0.188s</td>
89 89
 <td class="success">100%</td>
90 90
 </tr>
91 91
 </tbody>
... ...
@@ -112,17 +112,17 @@
112 112
 <td>1</td>
113 113
 <td>0</td>
114 114
 <td>0</td>
115
-<td>0.178s</td>
115
+<td>0.160s</td>
116 116
 <td class="success">100%</td>
117 117
 </tr>
118 118
 <tr>
119 119
 <td class="success">
120 120
 <a href="classes/re.forestier.edu.UnitTests.html">re.forestier.edu.UnitTests</a>
121 121
 </td>
122
-<td>10</td>
122
+<td>9</td>
123 123
 <td>0</td>
124 124
 <td>0</td>
125
-<td>0.018s</td>
125
+<td>0.028s</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 21 oct. 2024, 12:04:46</p>
139
+<a href="http://www.gradle.org">Gradle 8.8</a> at 24 oct. 2024, 16:05:59</p>
140 140
 </div>
141 141
 </div>
142 142
 </body>
... ...
@@ -22,7 +22,7 @@
22 22
 <tr>
23 23
 <td>
24 24
 <div class="infoBox" id="tests">
25
-<div class="counter">11</div>
25
+<div class="counter">10</div>
26 26
 <p>tests</p>
27 27
 </div>
28 28
 </td>
... ...
@@ -40,7 +40,7 @@
40 40
 </td>
41 41
 <td>
42 42
 <div class="infoBox" id="duration">
43
-<div class="counter">0.196s</div>
43
+<div class="counter">0.188s</div>
44 44
 <p>duration</p>
45 45
 </div>
46 46
 </td>
... ...
@@ -83,17 +83,17 @@
83 83
 <td>1</td>
84 84
 <td>0</td>
85 85
 <td>0</td>
86
-<td>0.178s</td>
86
+<td>0.160s</td>
87 87
 <td class="success">100%</td>
88 88
 </tr>
89 89
 <tr>
90 90
 <td class="success">
91 91
 <a href="../classes/re.forestier.edu.UnitTests.html">UnitTests</a>
92 92
 </td>
93
-<td>10</td>
93
+<td>9</td>
94 94
 <td>0</td>
95 95
 <td>0</td>
96
-<td>0.018s</td>
96
+<td>0.028s</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 21 oct. 2024, 12:04:46</p>
109
+<a href="http://www.gradle.org">Gradle 8.8</a> at 24 oct. 2024, 16:05:59</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-10-21T10:04:46" hostname="a206pc27L.local.isima.fr" time="0.178">
2
+<testsuite name="re.forestier.edu.GlobalTest" tests="1" skipped="0" failures="0" errors="0" timestamp="2024-10-24T14:05:59" hostname="a206pc27L.local.isima.fr" time="0.16">
3 3
   <properties/>
4
-  <testcase name="testAffichageBase()" classname="re.forestier.edu.GlobalTest" time="0.178"/>
4
+  <testcase name="testAffichageBase()" classname="re.forestier.edu.GlobalTest" time="0.16"/>
5 5
   <system-out><![CDATA[]]></system-out>
6 6
   <system-err><![CDATA[]]></system-err>
7 7
 </testsuite>
... ...
@@ -1,16 +1,15 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2
-<testsuite name="re.forestier.edu.UnitTests" tests="10" skipped="0" failures="0" errors="0" timestamp="2024-10-21T10:04:46" hostname="a206pc27L.local.isima.fr" time="0.024">
2
+<testsuite name="re.forestier.edu.UnitTests" tests="9" skipped="0" failures="0" errors="0" timestamp="2024-10-24T14:05:59" hostname="a206pc27L.local.isima.fr" time="0.034">
3 3
   <properties/>
4
-  <testcase name="Affichage" classname="re.forestier.edu.UnitTests" time="0.006"/>
5
-  <testcase name="Main" classname="re.forestier.edu.UnitTests" time="0.001"/>
6
-  <testcase name="removeMoney" classname="re.forestier.edu.UnitTests" time="0.004"/>
7
-  <testcase name="ex &amp; lvl" classname="re.forestier.edu.UnitTests" time="0.002"/>
8
-  <testcase name="addMoney" classname="re.forestier.edu.UnitTests" time="0.001"/>
4
+  <testcase name="Main" classname="re.forestier.edu.UnitTests" time="0.013"/>
5
+  <testcase name="removeMoney" classname="re.forestier.edu.UnitTests" time="0.008"/>
6
+  <testcase name="ex &amp; lvl" classname="re.forestier.edu.UnitTests" time="0.003"/>
7
+  <testcase name="addMoney" classname="re.forestier.edu.UnitTests" time="0.0"/>
9 8
   <testcase name="Construcor UpdatePlayer" classname="re.forestier.edu.UnitTests" time="0.0"/>
10 9
   <testcase name="majDeFinDeTour" classname="re.forestier.edu.UnitTests" time="0.001"/>
11
-  <testcase name="Constructor Player" classname="re.forestier.edu.UnitTests" time="0.001"/>
10
+  <testcase name="Constructor Player" classname="re.forestier.edu.UnitTests" time="0.0"/>
12 11
   <testcase name="Natural Tests" classname="re.forestier.edu.UnitTests" time="0.002"/>
13
-  <testcase name="Sample test" classname="re.forestier.edu.UnitTests" time="0.0"/>
12
+  <testcase name="Sample test" classname="re.forestier.edu.UnitTests" time="0.001"/>
14 13
   <system-out><![CDATA[]]></system-out>
15 14
   <system-err><![CDATA[]]></system-err>
16 15
 </testsuite>
... ...
@@ -11,9 +11,9 @@ public class Main {
11 11
         firstPlayer.addMoney(400);
12 12
 
13 13
         firstPlayer.addXp(15);
14
-        System.out.println(Affichage.afficherJoueur(firstPlayer));
14
+        Affichage.afficherJoueur(firstPlayer);
15 15
         System.out.println("------------------");
16 16
         firstPlayer.addXp(20);
17
-        System.out.println(Affichage.afficherJoueur(firstPlayer));
17
+        Affichage.afficherJoueur(firstPlayer);
18 18
     }
19 19
 }
20 20
\ No newline at end of file
... ...
@@ -2,18 +2,7 @@ package re.forestier.edu.rpg;
2 2
 
3 3
 public class Affichage {
4 4
 
5
-    public static String afficherJoueur(Player player) {
6
-        final String[] finalString = {"Joueur " + player.getAvatarName() + " joué par " + player.getPlayerName()};
7
-        finalString[0] += "\nNiveau : " + player.getLevel() + " (XP totale : " + player.getXp() + ")";
8
-        finalString[0] += "\n\nCapacités :";
9
-        player.abilities.forEach((name, level) -> {
10
-            finalString[0] += "\n   " + name + " : " + level;
11
-        });
12
-        finalString[0] += "\n\nInventaire :";
13
-        player.inventory.forEach(item -> {
14
-            finalString[0] += "\n   " + item;
15
-        });
16
-
17
-        return finalString[0];
5
+    public static void afficherJoueur(Player player) {
6
+        System.out.println(player.toString());
18 7
     }
19 8
 }
... ...
@@ -162,4 +162,26 @@ public class Player {
162 162
     public void addMoney(int amount) { 
163 163
         money = money + amount;
164 164
     }
165
+
166
+    @Override 
167
+    public String toString()
168
+    {
169
+        StringBuilder sb = new StringBuilder("Joueur ");
170
+        sb.append(this.Avatar_name);
171
+        sb.append(" joué par ");
172
+        sb.append(this.playerName);
173
+        sb.append("\nNiveau : ");
174
+        sb.append(this.level.toInt());
175
+        sb.append(" (XP totale : ");
176
+        sb.append(this.xp);
177
+        sb.append(")\n\nCapacités :");
178
+        this.abilities.forEach((name, level) -> {
179
+            sb.append("\n   " + name + " : " + level);
180
+        });
181
+        sb.append("\n\nInventaire :");
182
+        this.inventory.forEach(item -> {
183
+            sb.append("\n   " + item);
184
+        });
185
+        return sb.toString();
186
+    }
165 187
 }
166 188
\ No newline at end of file
... ...
@@ -123,7 +123,6 @@ public class UpdatePlayer {
123 123
                 case "ARCHER" :
124 124
                     player.heal(1);
125 125
                     if(player.inventory.contains("Magic Bow")) {
126
-                        //player.setCurrentHealthPoints(player.getCurrentHealthPoints()+player.getCurrentHealthPoints()/8-1);
127 126
                         int potentialHeal = player.getCurrentHealthPoints()/8-1;
128 127
                         player.heal(potentialHeal < 0 ? 0 : potentialHeal);
129 128
                     }
... ...
@@ -9,18 +9,45 @@ import re.forestier.edu.rpg.Player;
9 9
 import java.util.ArrayList;
10 10
 
11 11
 import static org.approvaltests.Approvals.verify;
12
+import java.io.ByteArrayOutputStream;
12 13
 import static org.hamcrest.MatcherAssert.assertThat;
13
-import static org.hamcrest.Matchers.is;
14
+import static org.hamcrest.Matchers.*;
14 15
 import static org.junit.jupiter.api.Assertions.fail;
16
+import static org.junit.jupiter.api.Assertions.assertThrows;
17
+import static org.junit.jupiter.api.Assertions.assertTrue;
18
+import static org.junit.jupiter.api.Assertions.assertFalse;
19
+import static org.junit.jupiter.api.Assertions.assertEquals;
20
+import static org.junit.jupiter.api.Assertions.assertNotNull;
21
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
22
+import org.junit.jupiter.api.BeforeEach;
23
+import org.junit.jupiter.api.AfterEach;
24
+import java.io.PrintStream;
15 25
 
16 26
 public class GlobalTest {
17 27
 
28
+    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
29
+    private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
30
+    private final PrintStream originalOut = System.out;
31
+    private final PrintStream originalErr = System.err;
32
+
33
+     @BeforeEach
34
+    public void setUpStreams() {
35
+        System.setOut(new PrintStream(outContent));
36
+        System.setErr(new PrintStream(errContent));
37
+    }
38
+
39
+        @AfterEach
40
+    public void restoreStreams() {
41
+        System.setOut(originalOut);
42
+        System.setErr(originalErr);
43
+    }
44
+
18 45
     @Test
19 46
     void testAffichageBase() {
20 47
         Player player = new Player("Florian", "Gnognak le Barbare", "ADVENTURER", 200, new ArrayList<>());
21 48
         player.addXp(20);
22 49
         player.inventory = new ArrayList<>();
23
-
24
-        verify(Affichage.afficherJoueur(player));
50
+        Affichage.afficherJoueur(player);
51
+        verify(outContent );
25 52
     }
26 53
 }
... ...
@@ -179,18 +179,6 @@ public class UnitTests {
179 179
         assertEquals(p.getCurrentHealthPoints(),20);
180 180
     }
181 181
 
182
-    @Test
183
-    @DisplayName("Affichage")
184
-    void testAffichage() {
185
-        Affichage a = new Affichage();
186
-        assertNotNull(a);
187
-
188
-        ArrayList<String> inv = new ArrayList<String>();
189
-        inv.add("Holy Elixir");
190
-        Player p = new Player("Florian", "Grognak le barbare", "ADVENTURER", 100, inv);
191
-        assertEquals("Joueur Grognak le barbare joué par Florian\nNiveau : 1 (XP totale : 0)\n\nCapacités :\n   DEF : 1\n   ATK : 3\n   CHA : 3\n   INT : 2\n\nInventaire :\n   Holy Elixir",  Affichage.afficherJoueur(p));
192
-    }
193
-
194 182
     @Test
195 183
     @DisplayName("Main")
196 184
     void testMain() {
197 185