From: JBM Date: Wed, 10 Jun 2020 15:13:53 +0000 (+0200) Subject: We're in 2020. I wrote Java before you. Here is my first JUnit. X-Git-Url: https://troll.desast.re/troll.git/commitdiff_plain/bda92e8fe2871496c68dc2de2d746f8e69c6fd96 We're in 2020. I wrote Java before you. Here is my first JUnit. I kind of lied. This is my first JUnit *that passes*. It's a close variation on my first JUnit, which is yesterday's version of the same. It took crazy too long to complete mostly because of the whacky interface the CG GameRunner has to run Java agents. I couldn't have done it without @dbdr. Not so much for the technical aspect of the end result so much as for the moral support while coming up with all sorts of crazy ways to pass the data around, and trying or proving them wrong. --- diff --git a/pom.xml b/pom.xml index 675ba48..0e25cdd 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,11 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M4 + @@ -52,5 +57,12 @@ runner ${gamengine.version} + + + junit + junit + 4.11 + test + diff --git a/src/test/java/TrollTest.java b/src/test/java/TrollTest.java new file mode 100644 index 0000000..abbf3c5 --- /dev/null +++ b/src/test/java/TrollTest.java @@ -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 void dumpList(PrintStream p, String tag, List 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 void dumpMap(PrintStream p, String tag, Map map) { + for (Map.Entry kv : map.entrySet()) { + p.print(tag + " for " + kv.getKey() + ": "); + dumpGeneric(p, kv.getValue()); + } + } + + static 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 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) }; + } +}