We're in 2020. I wrote Java before you. Here is my first JUnit.
authorJBM <jbm@codingame.com>
Wed, 10 Jun 2020 15:13:53 +0000 (17:13 +0200)
committerJBM <jbm@codingame.com>
Wed, 10 Jun 2020 15:19:31 +0000 (17:19 +0200)
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.

pom.xml
src/test/java/TrollTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 675ba48..0e25cdd 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                 </compilerArgs>
               </configuration>
             </plugin>
                 </compilerArgs>
               </configuration>
             </plugin>
+            <plugin>
+              <groupId>org.apache.maven.plugins</groupId>
+              <artifactId>maven-surefire-plugin</artifactId>
+              <version>3.0.0-M4</version>
+            </plugin>
           </plugins>
         </build>
 
           </plugins>
         </build>
 
                        <artifactId>runner</artifactId>
                        <version>${gamengine.version}</version>
                </dependency>
                        <artifactId>runner</artifactId>
                        <version>${gamengine.version}</version>
                </dependency>
+
+                <dependency>
+                  <groupId>junit</groupId>
+                  <artifactId>junit</artifactId>
+                  <version>4.11</version>
+                  <scope>test</scope>
+                </dependency>
        </dependencies>
 </project>
        </dependencies>
 </project>
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) };
+    }
+}