From 9c7bedcd7cd3e7ff34e00f8bc76434d0533646dc Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Sat, 22 Jan 2022 17:03:02 +0100 Subject: [PATCH] changed interface Game to abstract class for better object orientation --- src/main/java/Game/Game.java | 21 +++++++++++-- src/main/java/Game/Tictactoe.java | 14 ++------- src/test/java/Game/GameTest.java | 43 +++++++++++++++++++++++++++ src/test/java/Game/TictactoeTest.java | 17 ----------- 4 files changed, 64 insertions(+), 31 deletions(-) create mode 100644 src/test/java/Game/GameTest.java diff --git a/src/main/java/Game/Game.java b/src/main/java/Game/Game.java index 9ddc0d2..540a3d3 100644 --- a/src/main/java/Game/Game.java +++ b/src/main/java/Game/Game.java @@ -1,7 +1,24 @@ package Game; -public interface Game { - public abstract void print(); +import java.util.ArrayList; + +public abstract class Game { + + protected ArrayList outputBuffer = new ArrayList<>(); + + public void print() { + for (String s : outputBuffer) { + System.out.println(s); + } + } + + protected void setOutputBuffer(ArrayList outputBuffer) { + this.outputBuffer = outputBuffer; + } + + public ArrayList getOutputBuffer() { + return this.outputBuffer; + } diff --git a/src/main/java/Game/Tictactoe.java b/src/main/java/Game/Tictactoe.java index 0988a12..371c54a 100644 --- a/src/main/java/Game/Tictactoe.java +++ b/src/main/java/Game/Tictactoe.java @@ -2,7 +2,7 @@ package Game; import java.util.ArrayList; -public class Tictactoe implements Game { +public class Tictactoe extends Game { enum State { @@ -10,7 +10,6 @@ public class Tictactoe implements Game { CROSS, EMPTY } - private ArrayList outputBuffer = new ArrayList(); private String input; private State[] currentBoard; @@ -22,12 +21,7 @@ public class Tictactoe implements Game { currentBoard = newBoard(); } - @Override - public void print() { - for (int i = 0; i < outputBuffer.size(); i++) { - System.out.println(outputBuffer.get(i)); - } - } + public char getStatedChar(State state) { switch (state) { @@ -50,10 +44,6 @@ public class Tictactoe implements Game { return board; } - void setOutputBuffer(ArrayList outputBuffer){ - this.outputBuffer = outputBuffer; - } - public State[] getCurrentBoard() { return currentBoard; } diff --git a/src/test/java/Game/GameTest.java b/src/test/java/Game/GameTest.java new file mode 100644 index 0000000..57c399e --- /dev/null +++ b/src/test/java/Game/GameTest.java @@ -0,0 +1,43 @@ +package Game; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; + +class GameTest { + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + Game game; + + @BeforeEach + void setUp() { + System.setOut(new PrintStream(outContent)); + game = mock(Game.class, Mockito.CALLS_REAL_METHODS); + } + + @AfterEach + void tearDown() { + System.setOut(originalOut); + } + + @Test + public void print() { + ArrayList testOB = new ArrayList<>(); + testOB.add("Hello"); + testOB.add("World"); + testOB.add("!!!"); + game.setOutputBuffer(testOB); + game.print(); + assertEquals("Hello\nWorld\n!!!\n", outContent.toString().replaceAll("\r", "")); + } +} \ No newline at end of file diff --git a/src/test/java/Game/TictactoeTest.java b/src/test/java/Game/TictactoeTest.java index 998accf..08e51ef 100644 --- a/src/test/java/Game/TictactoeTest.java +++ b/src/test/java/Game/TictactoeTest.java @@ -12,35 +12,18 @@ import static org.junit.jupiter.api.Assertions.*; class TictactoeTest { - private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - private final PrintStream originalOut = System.out; - Tictactoe ttt; - @BeforeEach void setUp() { ttt = new Tictactoe(); - System.setOut(new PrintStream(outContent)); } @AfterEach void tearDown() { - System.setOut(originalOut); - } - @Test - public void testPrint() { - ArrayList testOB = new ArrayList(); - testOB.add("Hello"); - testOB.add("World"); - testOB.add("!!!"); - ttt.setOutputBuffer(testOB); - ttt.print(); - assertEquals("Hello\nWorld\n!!!\n", outContent.toString().replaceAll("\r", "")); } - @Test void getStatedChar() { assertEquals(ttt.getStatedChar(Tictactoe.State.CIRCLE), 'O');