Skip to content

Commit

Permalink
Merge pull request #7 from B1G-SAM/JUnit-Test
Browse files Browse the repository at this point in the history
Add MapTest class
  • Loading branch information
B1G-SAM committed Mar 14, 2024
2 parents f469445 + b8be087 commit 65b609b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/map/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ public void movePlayerRightOne(){
public void nextMapBasedOnCommand(Command userCommand){
userCommand.execute();
}

public ArrayList<ArrayList<Character>> getMap(){
return storedMap;
}
}
65 changes: 65 additions & 0 deletions src/test/java/map/MapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;

class MapTest {

private FirstMap map;

@BeforeEach
void setUp() {
map = new FirstMap();
map.initMap(5, 5);
map.initPlayerLocation(2, 2);
}

@Test
void mapShouldBeCorrectlyInitialized() {
ArrayList<ArrayList<Character>> expectedMap = new ArrayList<>();
for (int i = 0; i < 5; i++) {
ArrayList<Character> row = new ArrayList<>();
for (int j = 0; j < 5; j++) {
if (i == 2 && j == 2) {
row.add('P');
} else {
row.add('.');
}
}
expectedMap.add(row);
}

assertEquals(expectedMap, map.getMap(), "Map should be initialized correctly with player at (2,2)");
}

@Test
void playerShouldMoveUpCorrectly() {
map.movePlayerUpOne();
assertEquals('P', map.getStoredMap().get(1).get(2), "Player should move up 1 place");
assertEquals('.', map.getStoredMap().get(2).get(2), "Original player position should be empty after moving up");
}

@Test
void playerShouldMoveDownCorrectly() {
map.movePlayerDownOne();
assertEquals('P', map.getStoredMap().get(3).get(2), "Player should move down 1 place");
assertEquals('.', map.getStoredMap().get(2).get(2), "Original player position should be empty after moving down");
}

@Test
void playerShouldMoveLeftCorrectly() {
map.movePlayerLeftOne();
assertEquals('P', map.getStoredMap().get(2).get(1), "Player should move left 1 place");
assertEquals('.', map.getStoredMap().get(2).get(2), "Original player position should be empty after moving left");
}

@Test
void playerShouldMoveRightCorrectly() {
map.movePlayerRightOne();
assertEquals('P', map.getStoredMap().get(2).get(3), "Player moves right 1 place");
assertEquals('.', map.getStoredMap().get(2).get(2), "Original player position should be empty after moving right");
}
}

0 comments on commit 65b609b

Please sign in to comment.