Test unitaire affichage et updatePlayer
Clement Colmerauer

Clement Colmerauer commited on 2024-10-04 15:11:04
Showing 1 changed files, with 103 additions and 0 deletions.

... ...
@@ -2,15 +2,33 @@ package re.forestier.edu;
2 2
 
3 3
 import org.junit.jupiter.api.*;
4 4
 import re.forestier.edu.rpg.*;
5
+import java.io.PrintStream;
6
+import java.io.ByteArrayOutputStream;
5 7
 import static org.hamcrest.MatcherAssert.assertThat;
6 8
 import static org.hamcrest.Matchers.*;
7 9
 import static org.junit.jupiter.api.Assertions.fail;
8 10
 import static org.junit.jupiter.api.Assertions.assertThrows;
11
+import static org.junit.jupiter.api.Assertions.assertEquals;
12
+import static org.junit.jupiter.api.Assertions.assertNotNull;
13
+import org.junit.jupiter.api.BeforeEach;
14
+import org.junit.jupiter.api.AfterEach;
15
+
9 16
 
10 17
 import java.util.ArrayList;
11 18
 
12 19
 public class UnitTests {
13 20
 
21
+    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
22
+    private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
23
+    private final PrintStream originalOut = System.out;
24
+    private final PrintStream originalErr = System.err;
25
+
26
+    @BeforeEach
27
+    public void setUpStreams() {
28
+        System.setOut(new PrintStream(outContent));
29
+        System.setErr(new PrintStream(errContent));
30
+    }
31
+
14 32
     @Test
15 33
     @DisplayName("Sample test")
16 34
     void testPlayerName() {
... ...
@@ -41,6 +59,7 @@ public class UnitTests {
41 59
         assertThrows(IllegalArgumentException.class, () -> p.removeMoney(200));
42 60
         p.removeMoney(50);
43 61
         assertThat(p.money, is(50));
62
+        assertThat(p.money, not(150));
44 63
     }
45 64
 
46 65
     @Test
... ...
@@ -51,6 +70,13 @@ public class UnitTests {
51 70
         assertThat(p.money, is(150));
52 71
     }
53 72
 
73
+    @Test
74
+    @DisplayName("Construcor UpdatePlayer")
75
+    void testUpdatePlayer() {
76
+        UpdatePlayer p = new UpdatePlayer();
77
+        assertNotNull(p);
78
+    }
79
+
54 80
     @Test
55 81
     @DisplayName("ex & lvl")
56 82
     void testRetrieveLevel() {
... ...
@@ -69,4 +95,81 @@ public class UnitTests {
69 95
         assertThat(p.retrieveLevel(),is(5));
70 96
     }
71 97
 
98
+    @Test
99
+    @DisplayName("majDeFinDeTour")
100
+    void testMajFinTour() {
101
+        player p = new player("Florian", "Grognak le barbare", "ADVENTURER", 100, new ArrayList<>());
102
+        p.currenthealthpoints = 0;
103
+        UpdatePlayer.majFinDeTour(p);
104
+        assertEquals("Le joueur est KO !", outContent.toString().trim());
105
+        p.currenthealthpoints = 1;
106
+        p.healthpoints = 10;
107
+        UpdatePlayer.majFinDeTour(p);
108
+        assertThat(p.currenthealthpoints,is(2));
109
+        boolean b = UpdatePlayer.addXp(p,100);
110
+        UpdatePlayer.majFinDeTour(p);
111
+        p.currenthealthpoints = 9;
112
+        UpdatePlayer.majFinDeTour(p);
113
+        assertThat(p.currenthealthpoints,is(9));
114
+        p.currenthealthpoints = 11;
115
+        UpdatePlayer.majFinDeTour(p);
116
+        assertThat(p.currenthealthpoints,is(p.healthpoints));
117
+        
118
+        p = new player("Florian", "Grognak le barbare", "DWARF", 100, new ArrayList<>());
119
+        p.currenthealthpoints = 1;
120
+        p.healthpoints = 10;
121
+        UpdatePlayer.majFinDeTour(p);
122
+        assertThat(p.currenthealthpoints,is(2));
123
+
124
+        ArrayList<String> inv = new ArrayList<String>();
125
+        inv.add("Holy Elixir");
126
+        p = new player("Florian", "Grognak le barbare", "DWARF", 100, inv);
127
+        p.currenthealthpoints = 1;
128
+        p.healthpoints = 10;
129
+        UpdatePlayer.majFinDeTour(p);
130
+        assertThat(p.currenthealthpoints,is(3));
131
+
132
+        p = new player("Florian", "Grognak le barbare", "ARCHER", 100, new ArrayList<>());
133
+        p.currenthealthpoints = 1;
134
+        p.healthpoints = 10;
135
+        UpdatePlayer.majFinDeTour(p);
136
+        assertThat(p.currenthealthpoints,is(2));
137
+
138
+        inv.add("Magic Bow");
139
+        p = new player("Florian", "Grognak le barbare", "ARCHER", 100, inv);
140
+        p.currenthealthpoints = 1;
141
+        p.healthpoints = 10;
142
+        UpdatePlayer.majFinDeTour(p);
143
+        assertThat(p.currenthealthpoints,is(1));
144
+
145
+        p.currenthealthpoints = 16;
146
+        p.healthpoints = 40;
147
+        UpdatePlayer.majFinDeTour(p);
148
+        assertThat(p.currenthealthpoints,is(17 + 17/8-1));
149
+    }
150
+
151
+    @Test
152
+    @DisplayName("Affichage")
153
+    void testAfficage() {
154
+        Affichage a = new Affichage();
155
+        assertNotNull(a);
156
+
157
+        ArrayList<String> inv = new ArrayList<String>();
158
+        inv.add("Holy Elixir");
159
+        player p = new player("Florian", "Grognak le barbare", "ADVENTURER", 100, inv);
160
+        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));
161
+    }
162
+
163
+    @Test
164
+    @DisplayName("Main")
165
+    void testMain() {
166
+        Main m = new Main();
167
+        assertNotNull(m);
168
+    }
169
+
170
+    @AfterEach
171
+    public void restoreStreams() {
172
+        System.setOut(originalOut);
173
+        System.setErr(originalErr);
174
+    }
72 175
 }
73 176