Skip to content

Commit 406ada5

Browse files
author
Arun Prasaad
committed
Add some Sokoban window tests
1 parent 97e43d1 commit 406ada5

File tree

4 files changed

+178
-1
lines changed

4 files changed

+178
-1
lines changed

src/main/java/logic/Controller.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package logic;
22

3-
import java.io.IOException;
43
import java.nio.file.FileSystems;
54
import java.util.Optional;
65
import java.util.regex.Matcher;
@@ -13,6 +12,15 @@ public class Controller {
1312

1413
private int level = customLevel;
1514
private final String pathToLevel;
15+
16+
public int getLines() {
17+
return warehouse.getLines();
18+
}
19+
20+
public int getColumns() {
21+
return warehouse.getColumns();
22+
}
23+
1624
public Warehouse warehouse;
1725
public Worker worker;
1826

src/main/java/logic/Warehouse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ private void parseLevel(Worker worker, List<String> linesFromFile) {
5959
}
6060
case "V" ->
6161
case_tableau.add(new Case(i, j, TileType.STORED_BOX, this));
62+
63+
default -> throw new RuntimeException("Invalid tile type: " + linesFromFile.get(i).charAt(j));
6264
}
6365

6466
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package ihm;
2+
3+
import logic.Controller;
4+
import logic.Warehouse;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import javax.swing.*;
9+
import java.awt.event.KeyEvent;
10+
import java.nio.file.Paths;
11+
12+
import static org.assertj.core.api.BDDAssertions.then;
13+
14+
class SokobanWindowTest {
15+
16+
private SokobanWindow window;
17+
private Controller controller;
18+
19+
@BeforeEach
20+
void setUp() {
21+
// Use a test level file
22+
String testLevelPath = Paths.get("src/test/resources/levels/test_level.txt").toAbsolutePath().toString();
23+
controller = new Controller(testLevelPath);
24+
window = new SokobanWindow(controller);
25+
}
26+
27+
@Test
28+
void window_has_correct_initial_state() {
29+
// then
30+
then(window.isVisible()).isTrue();
31+
then(window.isResizable()).isFalse();
32+
then(window.getDefaultCloseOperation()).isEqualTo(JFrame.EXIT_ON_CLOSE);
33+
34+
// Verify the window size is set based on the warehouse
35+
Warehouse warehouse = controller.warehouse;
36+
int columns = warehouse.getColumns();
37+
int lines = warehouse.getLines();
38+
int imageWidth = columns * SokobanWindow.IMAGE_SIZE;
39+
int imageHeight = lines * SokobanWindow.IMAGE_SIZE;
40+
41+
// Verify window is at least as large as the game content
42+
then(window.getSize().width).isGreaterThanOrEqualTo(imageWidth);
43+
then(window.getSize().height).isGreaterThanOrEqualTo(imageHeight);
44+
}
45+
46+
@Test
47+
void handles_right_direction_key_press() {
48+
// given - initial positions
49+
int initialLine = controller.worker.getLine();
50+
int initialColumn = controller.worker.getColumn();
51+
52+
// Move right - should push the box
53+
KeyEvent rightKey = new KeyEvent(window,
54+
KeyEvent.KEY_PRESSED,
55+
System.currentTimeMillis(),
56+
0,
57+
KeyEvent.VK_RIGHT,
58+
KeyEvent.CHAR_UNDEFINED);
59+
60+
// when
61+
window.keyPressed(rightKey);
62+
63+
// then - verify worker moved right (pushing the box)
64+
then(controller.worker.getLine()).isEqualTo(initialLine);
65+
then(controller.worker.getColumn()).isEqualTo(initialColumn + 1);
66+
}
67+
68+
@Test
69+
void handles_left_direction_key_press() {
70+
// given - initial positions
71+
int initialLine = controller.worker.getLine();
72+
int initialColumn = controller.worker.getColumn();
73+
74+
// Move left
75+
KeyEvent leftKey = new KeyEvent(window,
76+
KeyEvent.KEY_PRESSED,
77+
System.currentTimeMillis(),
78+
0,
79+
KeyEvent.VK_LEFT,
80+
KeyEvent.CHAR_UNDEFINED);
81+
82+
// when
83+
window.keyPressed(leftKey);
84+
85+
// then - verify worker moved left
86+
then(controller.worker.getLine()).isEqualTo(initialLine);
87+
then(controller.worker.getColumn()).isEqualTo(initialColumn - 1);
88+
}
89+
90+
@Test
91+
void handles_up_direction_key_press() {
92+
// given - initial positions
93+
int initialLine = controller.worker.getLine();
94+
int initialColumn = controller.worker.getColumn();
95+
96+
// Move up
97+
KeyEvent upKey = new KeyEvent(window,
98+
KeyEvent.KEY_PRESSED,
99+
System.currentTimeMillis(),
100+
0,
101+
KeyEvent.VK_UP,
102+
KeyEvent.CHAR_UNDEFINED);
103+
104+
// when
105+
window.keyPressed(upKey);
106+
107+
// then - verify worker moved up
108+
then(controller.worker.getLine()).isEqualTo(initialLine - 1);
109+
then(controller.worker.getColumn()).isEqualTo(initialColumn);
110+
}
111+
112+
@Test
113+
void handles_down_direction_key_press() {
114+
// given - initial positions
115+
int initialLine = controller.worker.getLine();
116+
int initialColumn = controller.worker.getColumn();
117+
118+
// Move down
119+
KeyEvent downKey = new KeyEvent(window,
120+
KeyEvent.KEY_PRESSED,
121+
System.currentTimeMillis(),
122+
0,
123+
KeyEvent.VK_DOWN,
124+
KeyEvent.CHAR_UNDEFINED);
125+
126+
// when
127+
window.keyPressed(downKey);
128+
129+
// then - verify worker moved down
130+
then(controller.worker.getLine()).isEqualTo(initialLine + 1);
131+
then(controller.worker.getColumn()).isEqualTo(initialColumn);
132+
}
133+
134+
@Test
135+
void handles_restart_action() {
136+
// given
137+
int initialColumn = controller.worker.getColumn();
138+
KeyEvent rightKey = new KeyEvent(window,
139+
KeyEvent.KEY_PRESSED,
140+
System.currentTimeMillis(),
141+
0,
142+
KeyEvent.VK_RIGHT,
143+
KeyEvent.CHAR_UNDEFINED);
144+
window.keyPressed(rightKey);
145+
int columnAfterMove = controller.worker.getColumn();
146+
147+
// when - press backspace to restart
148+
KeyEvent restartKey = new KeyEvent(window,
149+
KeyEvent.KEY_PRESSED,
150+
System.currentTimeMillis(),
151+
0,
152+
KeyEvent.VK_BACK_SPACE,
153+
KeyEvent.CHAR_UNDEFINED);
154+
window.keyPressed(restartKey);
155+
156+
// then - worker should be back at initial position
157+
then(controller.worker.getColumn()).isNotEqualTo(columnAfterMove);
158+
then(controller.worker.getColumn()).isEqualTo(initialColumn);
159+
}
160+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
MMMMMMM
2+
M#####M
3+
M#G##TM
4+
M##C##M
5+
M#####M
6+
M#####M
7+
MMMMMMM

0 commit comments

Comments
 (0)