We're in 2020. I wrote Java before you. Here is my first JUnit.
[troll.git] / src / test / java / TrollTest.java
diff --git a/src/test/java/TrollTest.java b/src/test/java/TrollTest.java
new file mode 100644 (file)
index 0000000..abbf3c5
--- /dev/null
@@ -0,0 +1,97 @@
+import java.util.Properties;
+import java.util.List;
+import java.util.Map;
+import java.io.PrintStream;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+import com.codingame.gameengine.runner.MultiplayerGameRunner;
+import com.codingame.gameengine.runner.dto.*;
+
+public class TrollTest {
+    @Test
+    public void test() {
+        MultiplayerGameRunner gameRunner = new MultiplayerGameRunner();
+        Properties gameParameters = new Properties();
+        gameParameters.setProperty("roadLength", "6");
+        gameParameters.setProperty("initialStones", "15");
+        gameRunner.setGameParameters(gameParameters);
+        gameRunner.setSeed(0l);
+        gameRunner.setLeagueLevel(1);
+
+        gameRunner.addAgent(agentOne);
+        gameRunner.addAgent(agentOne);
+
+        assertIsDraw(gameRunner.simulate());
+    }
+
+    // great thanks to @dbdr for the intense moral support leading to
+    // the following:
+    static String agentOne = "yes 1";
+    static String agentTwo = "yes 2";
+
+    static void dumpGameResult(PrintStream p, GameResult gameResult) {
+        for (AgentDto agent : gameResult.agents) dumpAgent(p, agent);
+        dumpMap(p, "errors", gameResult.errors);
+        dumpString(p, "failCause", gameResult.failCause);
+        dumpMap(p, "ids", gameResult.ids);
+        dumpString(p, "metadata", gameResult.metadata);
+        dumpMap(p, "outputs", gameResult.outputs);
+        dumpList(p, "summaries", gameResult.summaries);
+        dumpList(p, "tooltips", gameResult.tooltips);
+        // dumpList(p, "views", gameResult.views);
+    }
+
+    static <V> void dumpList(PrintStream p, String tag, List<V> list) {
+        V[] a = (V[]) list.toArray();
+        for (int i = 0; i < a.length; i++) {
+            if (tag != null) p.print(tag + " ");
+            p.print(i + ": ");
+            dumpGeneric(p, a[i]);
+        }
+    }
+
+    static <K,V> void dumpMap(PrintStream p, String tag, Map<K,V> map) {
+        for (Map.Entry kv : map.entrySet()) {
+            p.print(tag + " for " + kv.getKey() + ": ");
+            dumpGeneric(p, kv.getValue());
+        }
+    }
+
+    static <V> void dumpGeneric(PrintStream p, V v) {
+        if (v instanceof List) {
+            dumpList(p, null, (List) v);
+        }
+        else {
+            p.println(v);
+        }
+    }
+
+    static void dumpString(PrintStream p, String tag, String msg) {
+        p.println(tag + ": " + msg);
+    }
+
+    static void dumpAgent(PrintStream p, AgentDto agent) {
+        p.println("[agent] " + agent.agentId + ": " + agent.avatar + " " + agent.index + " " + agent.name);
+    }
+
+    static void assertIsDraw(GameResult gameResult) {
+        int[] scores = assertTwoScores(gameResult);
+        if (scores == null) return;
+
+        int s1 = scores[0], s2 = scores[1];
+        assertEquals("Player scores are equal", s1, s2);
+        dumpGameResult(System.err, gameResult);
+        assertTrue("Player scores are non-negative", s1 >= 0);
+    }
+
+    static int[] assertTwoScores(GameResult gameResult) {
+        Map<Integer,Integer> scores = gameResult.scores;
+        assertEquals("Two scores are reported", scores.size(), 2);
+        assertTrue("Player 0 has a score", scores.containsKey(0));
+        assertTrue("Player 1 has a score", scores.containsKey(1));
+        return new int[] { scores.get(0), scores.get(1) };
+    }
+}