modernisation api et test unitaire

This commit is contained in:
2025-12-31 12:46:03 +01:00
parent c5772eef95
commit 9bcbc79706
9 changed files with 1328 additions and 28 deletions
@@ -0,0 +1,79 @@
package com.example.boidelov3.data;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Tests unitaires pour la classe Result.
*/
public class ResultTest {
@Test
public void testSuccess_createsSuccessfulResult() {
Result<String, Exception> result = Result.success("test data");
assertTrue("Result should be success", result.isSuccess());
assertFalse("Result should not be failure", result.isFailure());
assertEquals("Data should match", "test data", result.getData());
assertNull("Error should be null", result.getError());
}
@Test
public void testFailure_createsFailedResult() {
Exception error = new Exception("test error");
Result<String, Exception> result = Result.failure(error);
assertFalse("Result should not be success", result.isSuccess());
assertTrue("Result should be failure", result.isFailure());
assertNull("Data should be null", result.getData());
assertEquals("Error should match", error, result.getError());
}
@Test
public void testGetOrNull_returnsDataWhenSuccess() {
Result<String, Exception> result = Result.success("test data");
assertEquals("Should return data", "test data", result.getOrNull());
}
@Test
public void testGetOrNull_returnsNullWhenFailure() {
Result<String, Exception> result = Result.failure(new Exception("error"));
assertNull("Should return null", result.getOrNull());
}
@Test
public void testGetOrElse_returnsDataWhenSuccess() {
Result<String, Exception> result = Result.success("test data");
assertEquals("Should return data", "test data", result.getOrElse("default"));
}
@Test
public void testGetOrElse_returnsDefaultWhenFailure() {
Result<String, Exception> result = Result.failure(new Exception("error"));
assertEquals("Should return default", "default", result.getOrElse("default"));
}
@Test
public void testWithIntegerType() {
Result<Integer, Exception> result = Result.success(42);
assertTrue("Should be success", result.isSuccess());
assertEquals(Integer.valueOf(42), result.getData());
}
@Test
public void testWithCustomException() {
class CustomException extends Exception {
public CustomException(String message) { super(message); }
}
Result<String, CustomException> result = Result.failure(new CustomException("custom error"));
assertTrue("Should be failure", result.isFailure());
assertEquals("custom error", result.getError().getMessage());
}
}
@@ -0,0 +1,204 @@
package com.example.boidelov3.game;
import com.example.boidelov3.Question;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Tests unitaires pour la classe GameEngine.
*/
public class GameEngineTest {
private GameEngine gameEngine;
private List<String> players;
@Before
public void setUp() {
gameEngine = new GameEngine();
players = Arrays.asList("Alice", "Bob", "Charlie", "David");
}
@Test
public void testSelectRandomPlayers_returnsCorrectCount() {
List<String> selected = gameEngine.selectRandomPlayers(players, 2);
assertEquals("Should select 2 players", 2, selected.size());
assertTrue("Should contain players from original list", players.containsAll(selected));
}
@Test
public void testSelectRandomPlayers_returnsUniquePlayers() {
List<String> selected = gameEngine.selectRandomPlayers(players, 3);
// Vérifier l'unicité
assertEquals("Should have 3 unique players", 3, new ArrayList<>(selected).size());
}
@Test
public void testSelectRandomPlayers_handlesCountLargerThanList() {
List<String> selected = gameEngine.selectRandomPlayers(players, 10);
assertEquals("Should select max available players", 4, selected.size());
}
@Test
public void testProcessQuestion_replacesJ1Placeholder() {
Question question = createQuestion("<J1> doit boire 2 gorgées");
GameEngine.ProcessedQuestion processed = gameEngine.processQuestion(question, players, 0);
String text = processed.question.getQuestion();
assertFalse("Should not contain <J1> placeholder", text.contains("<J1>"));
assertTrue("Should contain a player name", containsAnyPlayer(text, players));
}
@Test
public void testProcessQuestion_replacesMultiplePlayerPlaceholders() {
Question question = createQuestion("<J1> et <J2> boivent ensemble");
GameEngine.ProcessedQuestion processed = gameEngine.processQuestion(question, players, 0);
String text = processed.question.getQuestion();
assertFalse("Should not contain <J1>", text.contains("<J1>"));
assertFalse("Should not contain <J2>", text.contains("<J2>"));
}
@Test
public void testProcessQuestion_replacesVariante() {
Question question = createQuestion("C'est une question <variante> !");
question.setVariante(Arrays.asList("simple", "compliquée", "drôle"));
GameEngine.ProcessedQuestion processed = gameEngine.processQuestion(question, players, 0);
String text = processed.question.getQuestion();
assertFalse("Should not contain <variante>", text.contains("<variante>"));
assertTrue("Should contain one of the variantes",
text.contains("simple") || text.contains("compliquée") || text.contains("drôle"));
}
@Test
public void testProcessQuestion_handlesDistribution() {
Question question = createQuestion("Question de test");
question.setDistribution(true);
question.setGorger(3);
GameEngine.ProcessedQuestion processed = gameEngine.processQuestion(question, players, 0);
String text = processed.question.getQuestion();
assertTrue("Should contain 'distribue'", text.contains("distribue"));
assertTrue("Should contain gorgée count", text.contains("3"));
}
@Test
public void testProcessQuestion_handlesRecois() {
Question question = createQuestion("Question de test");
question.setRecois(true);
question.setGorger(2);
GameEngine.ProcessedQuestion processed = gameEngine.processQuestion(question, players, 0);
String text = processed.question.getQuestion();
assertTrue("Should contain 'bois'", text.contains("bois"));
assertTrue("Should contain gorgée count", text.contains("2"));
}
@Test
public void testProcessQuestion_addsExtraGorgees() {
Question question = createQuestion("Question de test");
question.setDistribution(true);
question.setGorger(2);
GameEngine.ProcessedQuestion processed = gameEngine.processQuestion(question, players, 3);
String text = processed.question.getQuestion();
assertTrue("Should contain total gorgée count (2+3=5)", text.contains("5"));
}
@Test
public void testProcessQuestion_handlesManches() {
Question question = createQuestion("Défi à manches <manches> !");
question.setArret("Arrêtez maintenant !");
GameEngine.ProcessedQuestion processed = gameEngine.processQuestion(question, players, 0);
assertTrue("Should be marked as manche", processed.isManche);
assertTrue("Should have active manche", gameEngine.hasActiveManche());
assertNotNull("Manche should have end message", processed.question.getArretMessageManche());
}
@Test
public void testUpdateManches_decrementsMancheCount() {
// Créer une manche
Question question = createQuestion("Défi <manches>");
question.setArret("Fin !");
gameEngine.processQuestion(question, players, 0);
// Récupérer le nombre initial
GameEngine.MancheState initialState = gameEngine.updateManches();
int initialCount = initialState.activeManche.getManchesRestantes();
// Mettre à jour
GameEngine.MancheState updatedState = gameEngine.updateManches();
assertEquals("Manche count should decrease", initialCount - 1, updatedState.activeManche.getManchesRestantes());
}
@Test
public void testClearManches_removesAllManches() {
// Ajouter des manches
Question question = createQuestion("Défi <manches>");
question.setArret("Fin !");
gameEngine.processQuestion(question, players, 0);
gameEngine.clearManches();
assertFalse("Should have no active manches", gameEngine.hasActiveManche());
assertEquals("Should have 0 active manches", 0, gameEngine.getActiveManchesCount());
}
@Test
public void testProcessQuestion_handlesPluralGorgees() {
Question question = createQuestion("Test");
question.setDistribution(true);
question.setGorger(2);
GameEngine.ProcessedQuestion processed = gameEngine.processQuestion(question, players, 0);
String text = processed.question.getQuestion();
assertTrue("Should use plural 'gorgées'", text.contains("gorgées"));
}
@Test
public void testProcessQuestion_handlesSingularGorgee() {
Question question = createQuestion("Test");
question.setDistribution(true);
question.setGorger(1);
GameEngine.ProcessedQuestion processed = gameEngine.processQuestion(question, players, 0);
String text = processed.question.getQuestion();
assertTrue("Should use singular 'gorgée'", text.contains("gorgée"));
}
// Helper methods
private Question createQuestion(String text) {
Question q = new Question();
q.setId(1);
q.setQuestion(text);
return q;
}
private boolean containsAnyPlayer(String text, List<String> players) {
for (String player : players) {
if (text.contains(player)) {
return true;
}
}
return false;
}
}