1
0

Initial Commit

This commit is contained in:
2025-07-16 18:06:02 +02:00
commit 2358585522
33 changed files with 1523 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
package com.qrouland.katatondeuse.controller;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import com.qrouland.katatondeuse.model.Pelouse;
import com.qrouland.katatondeuse.model.Tondeuse;
import com.qrouland.katatondeuse.view.PelouseView;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.io.*;
import java.util.List;
public class TontePelouseControllerTest {
private PelouseView pelouseViewMock;
@BeforeEach
public void setup() {
pelouseViewMock = mock(PelouseView.class);
}
@Test
public void testRun() throws IOException {
String fileContent = """
5 5
1 2 N
GAGAGAGAA
3 3 E
AADAADADDA
""";
File tempFile = File.createTempFile("testInstructions", ".txt");
tempFile.deleteOnExit();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
writer.write(fileContent);
}
TontePelouseController controller = new TontePelouseController(tempFile, pelouseViewMock);
controller.run();
verify(pelouseViewMock, atLeastOnce()).displayPelouseState(any(Pelouse.class));
verify(pelouseViewMock, atLeastOnce()).displayTondeuseAdd(any(Tondeuse.class));
ArgumentCaptor<Tondeuse> tondeuseCaptor = ArgumentCaptor.forClass(Tondeuse.class);
verify(pelouseViewMock, atLeastOnce()).displayTondeuseAdd(tondeuseCaptor.capture());
List<Tondeuse> addedTondeuses = tondeuseCaptor.getAllValues();
assertEquals(2, addedTondeuses.size());
assertEquals(1, addedTondeuses.get(0).getPosition().getX());
assertEquals(3, addedTondeuses.get(0).getPosition().getY());
assertEquals(com.qrouland.katatondeuse.model.Orientation.N, addedTondeuses.get(0).getPosition().getOrientation());
assertEquals(5, addedTondeuses.get(1).getPosition().getX());
assertEquals(1, addedTondeuses.get(1).getPosition().getY());
assertEquals(com.qrouland.katatondeuse.model.Orientation.E, addedTondeuses.get(1).getPosition().getOrientation());
}
}

View File

@@ -0,0 +1,41 @@
package com.qrouland.katatondeuse.controller;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import com.qrouland.katatondeuse.controller.movement.MovementStrategy;
import com.qrouland.katatondeuse.model.*;
import com.qrouland.katatondeuse.view.PelouseView;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
public class TonteTondeuseControllerTest {
private Pelouse pelouse;
private Tondeuse tondeuse;
private PelouseView pelouseView;
private TonteTondeuseController controller;
@BeforeEach
public void setup() {
pelouse = new Pelouse(5, 5);
Position initialPosition = new Position(1, 2, Orientation.N);
tondeuse = new Tondeuse(initialPosition);
pelouseView = mock(PelouseView.class);
controller = new TonteTondeuseController(tondeuse, pelouse, pelouseView);
}
@Test
public void testExecuteInstructions() {
List<Direction> instructions = List.of(Direction.A, Direction.D, Direction.G);
controller.executeInstructions(instructions);
verify(pelouseView, times(3))
.displayTondeuseMove(any(Position.class), any(Position.class), any(MovementStrategy.class));
Position finalPosition = tondeuse.getPosition();
assertEquals("1 3 N", finalPosition.toString());
}
}

View File

@@ -0,0 +1,103 @@
package com.qrouland.katatondeuse.controller.movement;
import com.qrouland.katatondeuse.model.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class MovementAheadTest {
private MovementAhead movementAhead;
private Pelouse pelouse;
@BeforeEach
void setUp() {
movementAhead = new MovementAhead();
pelouse = new Pelouse(5, 5);
}
@Test
void moveNorth() {
Tondeuse tondeuse = new Tondeuse(new Position(2, 2, Orientation.N));
movementAhead.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(2, pos.getX());
assertEquals(3, pos.getY());
assertEquals(Orientation.N, pos.getOrientation());
}
@Test
void moveSouth() {
Tondeuse tondeuse = new Tondeuse(new Position(2, 2, Orientation.S));
movementAhead.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(2, pos.getX());
assertEquals(1, pos.getY());
assertEquals(Orientation.S, pos.getOrientation());
}
@Test
void moveEast() {
Tondeuse tondeuse = new Tondeuse(new Position(2, 2, Orientation.E));
movementAhead.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(3, pos.getX());
assertEquals(2, pos.getY());
assertEquals(Orientation.E, pos.getOrientation());
}
@Test
void moveWest() {
Tondeuse tondeuse = new Tondeuse(new Position(2, 2, Orientation.W));
movementAhead.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(1, pos.getX());
assertEquals(2, pos.getY());
assertEquals(Orientation.W, pos.getOrientation());
}
@Test
void move_OutOfBounds_MaxY() {
Tondeuse tondeuse = new Tondeuse(new Position(3, pelouse.getMaxY(), Orientation.N));
movementAhead.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(3, pos.getX());
assertEquals(pelouse.getMaxY(), pos.getY());
assertEquals(Orientation.N, pos.getOrientation());
}
@Test
void move_OutOfBounds_MaxX() {
Tondeuse tondeuse = new Tondeuse(new Position(pelouse.getMaxX(), 3, Orientation.E));
movementAhead.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(pelouse.getMaxX(), pos.getX());
assertEquals(3, pos.getY());
assertEquals(Orientation.E, pos.getOrientation());
}
@Test
void move_OutOfBounds_MinY() {
Tondeuse tondeuse = new Tondeuse(new Position(3, 0, Orientation.S));
movementAhead.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(3, pos.getX());
assertEquals(0, pos.getY());
assertEquals(Orientation.S, pos.getOrientation());
}
@Test
void mov_OutOfBounds_MinX() {
Tondeuse tondeuse = new Tondeuse(new Position(0, 3, Orientation.W));
movementAhead.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(0, pos.getX());
assertEquals(3, pos.getY());
assertEquals(Orientation.W, pos.getOrientation());
}
}

View File

@@ -0,0 +1,62 @@
package com.qrouland.katatondeuse.controller.movement;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.qrouland.katatondeuse.model.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class MovementLeftTest {
private MovementLeft movementLeft;
private Pelouse pelouse;
@BeforeEach
void setUp() {
movementLeft = new MovementLeft();
pelouse = new Pelouse(5, 5);
}
@Test
void testTurnLeftFromNorth() {
Tondeuse tondeuse = new Tondeuse(new Position(2, 3, Orientation.N));
movementLeft.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(2, pos.getX());
assertEquals(3, pos.getY());
assertEquals(Orientation.W, pos.getOrientation());
}
@Test
void testTurnLeftFromWest() {
Tondeuse tondeuse = new Tondeuse(new Position(2, 3, Orientation.W));
movementLeft.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(2, pos.getX());
assertEquals(3, pos.getY());
assertEquals(Orientation.S, pos.getOrientation());
}
@Test
void testTurnLeftFromSouth() {
Tondeuse tondeuse = new Tondeuse(new Position(2, 3, Orientation.S));
movementLeft.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(2, pos.getX());
assertEquals(3, pos.getY());
assertEquals(Orientation.E, pos.getOrientation());
}
@Test
void testTurnLeftFromEast() {
Tondeuse tondeuse = new Tondeuse(new Position(2, 3, Orientation.E));
movementLeft.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(2, pos.getX());
assertEquals(3, pos.getY());
assertEquals(Orientation.N, pos.getOrientation());
}
}

View File

@@ -0,0 +1,62 @@
package com.qrouland.katatondeuse.controller.movement;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.qrouland.katatondeuse.model.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class MovementRightTest {
private MovementRight movementRight;
private Pelouse pelouse;
@BeforeEach
void setUp() {
movementRight = new MovementRight();
pelouse = new Pelouse(5, 5);
}
@Test
void testTurnRightFromNorth() {
Tondeuse tondeuse = new Tondeuse(new Position(1, 1, Orientation.N));
movementRight.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(1, pos.getX());
assertEquals(1, pos.getY());
assertEquals(Orientation.E, pos.getOrientation());
}
@Test
void testTurnRightFromEast() {
Tondeuse tondeuse = new Tondeuse(new Position(1, 1, Orientation.E));
movementRight.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(1, pos.getX());
assertEquals(1, pos.getY());
assertEquals(Orientation.S, pos.getOrientation());
}
@Test
void testTurnRightFromSouth() {
Tondeuse tondeuse = new Tondeuse(new Position(1, 1, Orientation.S));
movementRight.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(1, pos.getX());
assertEquals(1, pos.getY());
assertEquals(Orientation.W, pos.getOrientation());
}
@Test
void testTurnRightFromWest() {
Tondeuse tondeuse = new Tondeuse(new Position(1, 1, Orientation.W));
movementRight.move(pelouse, tondeuse);
Position pos = tondeuse.getPosition();
assertEquals(1, pos.getX());
assertEquals(1, pos.getY());
assertEquals(Orientation.N, pos.getOrientation());
}
}

View File

@@ -0,0 +1,28 @@
package com.qrouland.katatondeuse.model;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class DirectionTest {
@Test
void testFromChar() {
assertEquals(Direction.A, Direction.fromChar('A'));
assertEquals(Direction.G, Direction.fromChar('G'));
assertEquals(Direction.D, Direction.fromChar('D'));
}
@Test
void testFromChar_invalidCharacter() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> Direction.fromChar('X'));
assertEquals("Invalid direction character: X", exception.getMessage());
}
@Test
void testFromChar_lowercaseInput() {
assertThrows(IllegalArgumentException.class, () -> Direction.fromChar('a'));
assertThrows(IllegalArgumentException.class, () -> Direction.fromChar('g'));
assertThrows(IllegalArgumentException.class, () -> Direction.fromChar('d'));
}
}

View File

@@ -0,0 +1,27 @@
package com.qrouland.katatondeuse.model;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class OrientationTest {
@Test
void testFromChar() {
assertEquals(Orientation.N, Orientation.fromChar('N'));
assertEquals(Orientation.E, Orientation.fromChar('E'));
assertEquals(Orientation.S, Orientation.fromChar('S'));
assertEquals(Orientation.W, Orientation.fromChar('W'));
}
@Test
void testFromChar_invalidCharacter() {
Exception exception = assertThrows(IllegalArgumentException.class, () -> Orientation.fromChar('X'));
assertEquals("Invalid orientation character: X", exception.getMessage());
}
@Test
void testFromChar_lowercaseInput() {
assertThrows(IllegalArgumentException.class, () -> Orientation.fromChar('n'));
}
}

View File

@@ -0,0 +1,60 @@
package com.qrouland.katatondeuse.model;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class PelouseTest {
private Pelouse pelouse;
@BeforeEach
void setUp() {
pelouse = new Pelouse(5, 5);
}
@Test
void testAddTondeuseAndGetTondeuses() {
Tondeuse tondeuse = new Tondeuse(new Position(1, 2, Orientation.N));
pelouse.addTondeuse(tondeuse);
List<Tondeuse> tondeuses = pelouse.getTondeuses();
assertEquals(1, tondeuses.size());
assertSame(tondeuse, tondeuses.get(0));
}
@Test
void testGetTondeuses() {
assertTrue(pelouse.getTondeuses().isEmpty());
}
@Test
void testGetMaxX_returnsCorrectValue() {
Pelouse pelouse = new Pelouse(10, 7);
assertEquals(10, pelouse.getMaxX());
}
@Test
void testGetMaxY_returnsCorrectValue() {
Pelouse pelouse = new Pelouse(10, 7);
assertEquals(7, pelouse.getMaxY());
}
@Test
void testToString() {
Tondeuse tondeuse = new Tondeuse(new Position(3, 4, Orientation.E));
pelouse.addTondeuse(tondeuse);
String result = pelouse.toString();
assertTrue(result.contains("maxX=5"));
assertTrue(result.contains("maxY=5"));
assertTrue(result.contains("Tondeuse { position ="));
}
}

View File

@@ -0,0 +1,14 @@
package com.qrouland.katatondeuse.model;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PositionTest {
@Test
void testPositionToString() {
Position position = new Position(0, 0, Orientation.S);
String expected = "0 0 S";
assertEquals(expected, position.toString());
}
}

View File

@@ -0,0 +1,41 @@
package com.qrouland.katatondeuse.model;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class TondeuseTest {
@Test
void testPositionGetter() {
Position position = new Position(1, 2, Orientation.N);
Tondeuse tondeuse = new Tondeuse(position);
assertEquals(position, tondeuse.getPosition());
}
@Test
void testSetPosition() {
Position initialPosition = new Position(1, 2, Orientation.N);
Tondeuse tondeuse = new Tondeuse(initialPosition);
Position newPosition = new Position(3, 4, Orientation.E);
tondeuse.setPosition(newPosition);
assertEquals(newPosition, tondeuse.getPosition());
assertEquals(3, tondeuse.getPosition().getX());
assertEquals(4, tondeuse.getPosition().getY());
assertEquals(Orientation.E, tondeuse.getPosition().getOrientation());
}
@Test
void testTondeuseToString() {
Position position = new Position(3, 4, Orientation.E);
Tondeuse tondeuse = new Tondeuse(position);
String expected = "Tondeuse { position = 3 4 E }";
assertEquals(expected, tondeuse.toString());
}
}

View File

@@ -0,0 +1,107 @@
package com.qrouland.katatondeuse;
import com.qrouland.katatondeuse.model.Direction;
import com.qrouland.katatondeuse.model.Orientation;
import com.qrouland.katatondeuse.model.Pelouse;
import com.qrouland.katatondeuse.model.Tondeuse;
import com.qrouland.katatondeuse.parser.FileInputParser;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class FileInputParserTest {
@Test
void testParseTondeuseInstructions_validInput() {
String position = "1 2 N";
String instructions = "AGAD";
ImmutablePair<Tondeuse, List<Direction>> result = FileInputParser.parseTondeuseInstructions(position, instructions);
assertNotNull(result);
assertEquals(1, result.left.getPosition().getX());
assertEquals(2, result.left.getPosition().getY());
assertEquals(Orientation.N, result.left.getPosition().getOrientation());
assertEquals(List.of(Direction.A, Direction.G, Direction.A, Direction.D), result.right);
}
@Test
void testParseTondeuseInstructions_invalidPositionFormat() {
String invalidPosition = "1 N 2";
String instructions = "AGAD";
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () ->
FileInputParser.parseTondeuseInstructions(invalidPosition, instructions));
assertEquals("Invalid tondeuse position format : " + invalidPosition, exception.getMessage());
}
@Test
void testParseTondeuseInstructions_invalidInstructionsFormat() {
String position = "1 2 N";
String invalidInstructions = "AGZRA";
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () ->
FileInputParser.parseTondeuseInstructions(position, invalidInstructions));
assertEquals("Invalid tondeuse instruction format : " + invalidInstructions, exception.getMessage());
}
@Test
void testParsePelouseDefinition_ValidInput() {
Pelouse pelouse = FileInputParser.parsePelouseDefinition("5 5");
assertNotNull(pelouse);
assertEquals(5, pelouse.getMaxX());
assertEquals(5, pelouse.getMaxY());
}
@Test
void testParsePelouseDefinition_InvalidInput() {
String invalidLine = "5x5";
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () ->
FileInputParser.parsePelouseDefinition(invalidLine));
assertTrue(exception.getMessage().contains("Invalid Pelouse size format"));
}
@Test
void testInputParserFile_ValidInputFile() throws IOException {
File tempFile = File.createTempFile("test", ".txt");
tempFile.deleteOnExit();
String[] args = { tempFile.getAbsolutePath() };
File result = FileInputParser.parseInputFile(args);
assertNotNull(result);
assertTrue(result.exists());
assertEquals(tempFile.getAbsolutePath(), result.getAbsolutePath());
}
@Test
void testFileInputParser_MissingArgument() {
String[] args = {};
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
FileInputParser.parseInputFile(args);
});
assertEquals("Missing input file argument.", exception.getMessage());
}
@Test
void testFileInputParser_NonExistentFile() {
String[] args = { "nonexistentfile.txt" };
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
FileInputParser.parseInputFile(args);
});
assertEquals("File does not exist: nonexistentfile.txt", exception.getMessage());
}
}

View File

@@ -0,0 +1,81 @@
package com.qrouland.katatondeuse.view;
import com.qrouland.katatondeuse.controller.movement.MovementAhead;
import com.qrouland.katatondeuse.model.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.jupiter.api.Assertions.*;
class ConsolePelouseViewTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private final PrintStream originalErr = System.err;
private final ConsolePelouseView view = new ConsolePelouseView();
@BeforeEach
void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@AfterEach
void restoreStreams() {
System.setOut(originalOut);
System.setErr(originalErr);
}
@Test
void testDisplayPelouseState() {
Pelouse pelouse = new Pelouse(3, 3);
pelouse.addTondeuse(new Tondeuse(new Position(0, 0, Orientation.S)));
pelouse.addTondeuse(new Tondeuse(new Position(2, 2, Orientation.E)));
view.displayPelouseState(pelouse);
String output = outContent.toString().trim();
assertTrue(output.contains("Pelouse 3 x 3"));
assertTrue(output.contains("0 0 S | 2 2 E"));
}
@Test
void testDisplayTondeuseAdd() {
Tondeuse tondeuse = new Tondeuse(new Position(2, 3, Orientation.E));
view.displayTondeuseAdd(tondeuse);
String output = outContent.toString().trim();
assertTrue(output.contains("Added"));
assertTrue(output.contains("2 3 E"));
}
@Test
void testDisplayTondeuseMove() {
Position origin = new Position(1, 1, Orientation.N);
Position destination = new Position(1, 2, Orientation.N);
MovementAhead movement = new MovementAhead();
view.displayTondeuseMove(origin, destination, movement);
String output = outContent.toString().trim();
assertTrue(output.contains("Moved"));
assertTrue(output.contains("1 1 N"));
assertTrue(output.contains("1 2 N"));
assertTrue(output.contains("MovementAhead"));
}
@Test
void testDisplayError() {
view.displayError("Test error message");
String output = errContent.toString().trim();
assertEquals("Test error message", output);
}
}