Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MapTest class #7

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}

Loading