/
EasyMockExample_RemovingFullRowsTest.java
executable file
·143 lines (117 loc) · 4.01 KB
/
EasyMockExample_RemovingFullRowsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright (c) 2008-2009 Esko Luontola, www.orfjackal.net
*
* You may use and modify this source code freely for personal non-commercial use.
* This source code may NOT be used as course material without prior written agreement.
*/
package tetris.easymock;
import net.orfjackal.nestedjunit.NestedJUnit4;
import static org.easymock.EasyMock.*;
import org.junit.*;
import org.junit.runner.RunWith;
import tetris.*;
/**
* @author Esko Luontola
*/
@RunWith(NestedJUnit4.class)
public class EasyMockExample_RemovingFullRowsTest extends Assert {
private static final RotatableGrid PIECE = new RotatablePiece("" +
".X.\n" +
".X.\n" +
".X.\n"
);
private Board board;
private final RowRemovalListener listener = createMock(RowRemovalListener.class);
@After
public void verifyMocks() {
verify(listener);
}
private void dropAndFallToBottom(RotatableGrid piece) {
board.drop(piece);
while (board.hasFalling()) {
board.tick();
}
}
public class When_one_row_becomes_full {
@Before
public void dropPiece() {
board = new Board("" +
"........\n" +
"........\n" +
"AA.A.AAA\n" +
"BBBB.BBB\n" +
"CCCC.CC.\n");
board.addRowRemovalListener(listener);
the_row_removal_listener_is_notified_about_the_removed_row(listener);
replay(listener);
dropAndFallToBottom(PIECE);
}
@Test
public void that_row_is_removed() {
String s = board.toString();
assertFalse("Should not contain 'B':\n" + s, s.contains("B"));
}
@Test
public void the_other_rows_move_down_to_fill_the_empty_space() {
assertEquals("" +
"........\n" +
"........\n" +
"........\n" +
"AA.AXAAA\n" +
"CCCCXCC.\n", board.toString());
}
private void the_row_removal_listener_is_notified_about_the_removed_row(RowRemovalListener listener) {
listener.onRowsRemoved(1);
}
}
public class When_many_rows_become_full_at_the_same_time {
@Before
public void dropPiece() {
board = new Board("" +
"........\n" +
"........\n" +
"AAAA.AAA\n" +
"BBBB..BB\n" +
"CCCC.CCC\n");
board.addRowRemovalListener(listener);
the_row_removal_listener_is_notified_about_the_removed_rows(listener);
replay(listener);
dropAndFallToBottom(PIECE);
}
@Test
public void all_of_those_rows_are_removed() {
String s = board.toString();
assertFalse("Should not contain 'A':\n" + s, s.contains("A"));
assertFalse("Should not contain 'C':\n" + s, s.contains("C"));
}
@Test
public void the_other_rows_fall_down_to_fill_the_empty_space() {
assertEquals("" +
"........\n" +
"........\n" +
"........\n" +
"........\n" +
"BBBBX.BB\n", board.toString());
}
private void the_row_removal_listener_is_notified_about_the_removed_rows(RowRemovalListener listener) {
listener.onRowsRemoved(2);
}
}
public class When_no_rows_become_full {
@Before
public void initBoard() {
board = new Board("" +
"........\n" +
"........\n" +
"AAA..AAA\n" +
"BBBB..BB\n" +
"CCC..CCC\n");
board.addRowRemovalListener(listener);
}
@Test
public void the_row_removal_listener_is_not_notified() {
replay(listener); // zero expectations
dropAndFallToBottom(PIECE);
}
}
}