Skip to content

Commit f9f275b

Browse files
committed
add fake replay test
1 parent 0eb01a7 commit f9f275b

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/test/java/bwapi/GameBuilder.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
5+
import java.nio.Buffer;
56
import java.nio.ByteBuffer;
67
import java.nio.file.Files;
78
import java.nio.file.Paths;
@@ -14,10 +15,12 @@ public static Game createGame() throws IOException {
1415
}
1516

1617
public static Game createGame(String mapName) throws IOException {
17-
final String location = "src/test/resources/" + mapName + "_frame0_buffer.bin";
18+
final ByteBuffer buffer = binToBuffer("src/test/resources/" + mapName + "_frame0_buffer.bin");
19+
return createGame(new Client(buffer));
20+
}
1821

19-
// load bytebuffer
20-
final byte[] compressedBytes = Files.readAllBytes(Paths.get(location));
22+
public static ByteBuffer binToBuffer(String binLocation) throws IOException {
23+
final byte[] compressedBytes = Files.readAllBytes(Paths.get(binLocation));
2124
final ByteArrayOutputStream out = new ByteArrayOutputStream();
2225
final InflaterOutputStream zin = new InflaterOutputStream(out);
2326
zin.write(compressedBytes);
@@ -26,8 +29,10 @@ public static Game createGame(String mapName) throws IOException {
2629
final byte[] bytes = out.toByteArray();
2730
final ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
2831
buffer.put(bytes);
32+
return buffer;
33+
}
2934

30-
final Client client = new Client(buffer);
35+
public static Game createGame(Client client) throws IOException {
3136
final Game game = new Game(client);
3237
game.init();
3338
return game;

src/test/java/bwapi/GameTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import static org.mockito.BDDMockito.given;
55
import static org.mockito.Mockito.mock;
66

7+
import java.io.IOException;
8+
import java.nio.ByteBuffer;
79
import java.util.ArrayList;
810
import java.util.List;
911
import org.junit.Before;
12+
import org.junit.Test;
1013
import org.junit.experimental.theories.DataPoints;
1114
import org.junit.experimental.theories.FromDataPoints;
1215
import org.junit.experimental.theories.Theories;
@@ -82,4 +85,21 @@ public void shouldNotFindNonOverlappingUnits(
8285
// THEN
8386
assertThat(unitsInRectangle).doesNotContain(dummy);
8487
}
88+
89+
@Test
90+
public void ifReplaySelfAndEnemyShouldBeNull() throws IOException {
91+
ByteBuffer buffer = GameBuilder.binToBuffer("src/test/resources/" + "(2)Benzene.scx" + "_frame0_buffer.bin");
92+
93+
Client client = new Client(buffer);
94+
// modify the buffer to fake a replay
95+
client.gameData().setIsReplay(true);
96+
97+
Game game = GameBuilder.createGame(client);
98+
99+
assertThat(game.isReplay());
100+
assertThat(game.self() == null);
101+
assertThat(game.enemy() == null);
102+
assertThat(game.enemies().isEmpty());
103+
assertThat(game.allies().isEmpty());
104+
}
85105
}

0 commit comments

Comments
 (0)