Skip to content

Commit 7033526

Browse files
committed
F!! Storyboard to verify sequences
1 parent a6ca40f commit 7033526

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Initial:
2+
. . . . .
3+
. . . . .
4+
. x x x .
5+
. . . . .
6+
. . . . .
7+
8+
9+
Frame #1:
10+
. . . . .
11+
. . x . .
12+
. . x . .
13+
. . x . .
14+
. . . . .
15+
16+
17+
Frame #2:
18+
. . . . .
19+
. . . . .
20+
. x x x .
21+
. . . . .
22+
. . . . .
23+
24+
25+
Frame #3:
26+
. . . . .
27+
. . x . .
28+
. . x . .
29+
. . x . .
30+
. . . . .
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.approvaltests;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.lambda.functions.Function2;
5+
import org.lambda.query.Queryable;
6+
import org.lambda.utils.Grid;
7+
8+
public class StoryBoardTest
9+
{
10+
@Test
11+
void gameOfLife()
12+
{
13+
final GameOfLife gameOfLife = new GameOfLife((x, y) -> y == 2 && 1 <= x && x <= 3);
14+
final Storyboard storyboard = new Storyboard();
15+
storyboard.add(gameOfLife);
16+
storyboard.addFrames(3, gameOfLife::advance);
17+
Approvals.verify(storyboard);
18+
}
19+
private class GameOfLife
20+
{
21+
private Function2<Integer, Integer, Boolean> board;
22+
public GameOfLife(Function2<Integer, Integer, Boolean> board)
23+
{
24+
this.board = board;
25+
}
26+
public GameOfLife advance()
27+
{
28+
final Function2<Integer, Integer, Boolean> old = this.board;
29+
this.board = (x, y) -> {
30+
final int aliveNeighbours = Queryable.as(
31+
old.call(x - 1, y - 1),
32+
old.call(x - 0, y - 1),
33+
old.call(x + 1, y - 1),
34+
old.call(x - 1, y - 0),
35+
// old.call(x - 0, y - 0),
36+
old.call(x + 1, y - 0),
37+
old.call(x - 1, y + 1),
38+
old.call(x - 0, y + 1),
39+
old.call(x + 1, y + 1)
40+
).where(a -> a)
41+
.size();
42+
return aliveNeighbours == 3 || (aliveNeighbours == 2 && old.call(x, y));
43+
};
44+
return this;
45+
}
46+
@Override
47+
public String toString()
48+
{
49+
return Grid.print(5, 5, (x, y) -> board.call(x, y) ? "x " : ". ");
50+
}
51+
}
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.approvaltests;
2+
3+
import org.lambda.functions.Function0;
4+
5+
public class Storyboard
6+
{
7+
private StringBuffer stringBuffer = new StringBuffer();
8+
int index = 0;
9+
public void add(Object object)
10+
{
11+
String title = index == 0 ? "Initial:" : "\n\nFrame #" + index + ":";
12+
stringBuffer.append(String.format("%s\n%s", title, object));
13+
index++;
14+
}
15+
public void addFrames(int howMany, Function0<Object> getNextFrame)
16+
{
17+
for (int i = 0; i < howMany; i++) {
18+
add(getNextFrame.call());
19+
}
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return stringBuffer.toString();
25+
}
26+
}

0 commit comments

Comments
 (0)