Skip to content

Commit 184fbd2

Browse files
committed
F!! fluent syntax and factory method
1 parent 7033526 commit 184fbd2

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

approvaltests-tests/src/test/java/org/approvaltests/StoryBoardTest.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ public class StoryBoardTest
1010
@Test
1111
void gameOfLife()
1212
{
13-
final GameOfLife gameOfLife = new GameOfLife((x, y) -> y == 2 && 1 <= x && x <= 3);
14-
final Storyboard storyboard = new Storyboard();
13+
GameOfLife gameOfLife = new GameOfLife((x, y) -> y == 2 && 1 <= x && x <= 3);
14+
StoryBoard storyboard = new StoryBoard();
1515
storyboard.add(gameOfLife);
1616
storyboard.addFrames(3, gameOfLife::advance);
1717
Approvals.verify(storyboard);
18+
gameOfLife = new GameOfLife((x, y) -> y == 2 && 1 <= x && x <= 3);
19+
Approvals.verify(StoryBoard.createSequence(gameOfLife, 3, gameOfLife::advance));
20+
gameOfLife = new GameOfLife((x, y) -> y == 2 && 1 <= x && x <= 3);
21+
Approvals.verify(new StoryBoard().add(gameOfLife).addFrames(3, gameOfLife::advance));
1822
}
1923
private class GameOfLife
2024
{
@@ -27,18 +31,11 @@ public GameOfLife advance()
2731
{
2832
final Function2<Integer, Integer, Boolean> old = this.board;
2933
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();
34+
final int aliveNeighbours = Queryable
35+
.as(old.call(x - 1, y - 1), old.call(x - 0, y - 1), old.call(x + 1, y - 1), old.call(x - 1, y - 0),
36+
// old.call(x - 0, y - 0),
37+
old.call(x + 1, y - 0), old.call(x - 1, y + 1), old.call(x - 0, y + 1), old.call(x + 1, y + 1))
38+
.where(a -> a).size();
4239
return aliveNeighbours == 3 || (aliveNeighbours == 2 && old.call(x, y));
4340
};
4441
return this;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 static <T> StoryBoard createSequence(T initial, int additionalFrames, Function0<T> getNextFrame)
10+
{
11+
return new StoryBoard().add(initial).addFrames(additionalFrames, getNextFrame);
12+
}
13+
public <T> StoryBoard add(T object)
14+
{
15+
String title = index == 0 ? "Initial:" : "\n\nFrame #" + index + ":";
16+
stringBuffer.append(String.format("%s\n%s", title, object));
17+
index++;
18+
return this;
19+
}
20+
public <T> StoryBoard addFrames(int howMany, Function0<T> getNextFrame)
21+
{
22+
for (int i = 0; i < howMany; i++)
23+
{
24+
add(getNextFrame.call());
25+
}
26+
return this;
27+
}
28+
@Override
29+
public String toString()
30+
{
31+
return stringBuffer.toString();
32+
}
33+
}

approvaltests/src/main/java/org/approvaltests/Storyboard.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)