From 9a6e8f1af4da1ca296056c1d37b7b56c59b3f1e7 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 19 Jan 2022 10:42:44 +0100 Subject: [PATCH] Added Gameinterface with TTT class and printTest --- src/main/java/Game/Game.java | 8 +++++ src/main/java/Game/Tictactoe.java | 18 +++++++++++ src/test/java/Game/TictactoeTest.java | 44 +++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/main/java/Game/Game.java create mode 100644 src/main/java/Game/Tictactoe.java create mode 100644 src/test/java/Game/TictactoeTest.java diff --git a/src/main/java/Game/Game.java b/src/main/java/Game/Game.java new file mode 100644 index 0000000..9ddc0d2 --- /dev/null +++ b/src/main/java/Game/Game.java @@ -0,0 +1,8 @@ +package Game; + +public interface Game { + public abstract void print(); + + + +} diff --git a/src/main/java/Game/Tictactoe.java b/src/main/java/Game/Tictactoe.java new file mode 100644 index 0000000..87da6c4 --- /dev/null +++ b/src/main/java/Game/Tictactoe.java @@ -0,0 +1,18 @@ +package Game; + +import java.util.ArrayList; + +public class Tictactoe implements Game { + private ArrayList outputBuffer = new ArrayList(); + + @Override + public void print() { + for (int i = 0; i < outputBuffer.size(); i++) { + System.out.println(outputBuffer.get(i)); + } + } + + void setOutputBuffer(ArrayList outputBuffer){ + this.outputBuffer = outputBuffer; + } +} diff --git a/src/test/java/Game/TictactoeTest.java b/src/test/java/Game/TictactoeTest.java new file mode 100644 index 0000000..55d7874 --- /dev/null +++ b/src/test/java/Game/TictactoeTest.java @@ -0,0 +1,44 @@ +package Game; + +import org.junit.Rule; +import org.junit.contrib.java.lang.system.SystemOutRule; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; + +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", "")); + } +} \ No newline at end of file