Skip to content

Commit b15447d

Browse files
author
Valentin Vetter
committed
feat(runner): allow YAML testcase files
1 parent 9b50b9c commit b15447d

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

runner/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

@@ -45,6 +46,11 @@
4546
<artifactId>commons-lang3</artifactId>
4647
<version>3.5</version>
4748
</dependency>
49+
<dependency>
50+
<groupId>org.yaml</groupId>
51+
<artifactId>snakeyaml</artifactId>
52+
<version>1.24</version>
53+
</dependency>
4854
</dependencies>
4955

5056
</project>

runner/src/main/java/com/codingame/gameengine/runner/ConfigHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void setLeaguesDetected(boolean leaguesDetected) {
6767
}
6868
}
6969

70-
class TestCase {
70+
public static class TestCase {
7171
private Map<Integer, String> title;
7272
private String testIn;
7373
private Boolean isTest;

runner/src/main/java/com/codingame/gameengine/runner/SoloGameRunner.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.charset.StandardCharsets;
6-
import java.util.ArrayList;
76
import java.util.Arrays;
87
import java.util.List;
98

109
import org.apache.commons.io.FileUtils;
10+
import org.apache.commons.io.FilenameUtils;
11+
import org.yaml.snakeyaml.Yaml;
1112

12-
import com.google.gson.JsonObject;
13-
import com.google.gson.JsonParser;
13+
import com.codingame.gameengine.runner.ConfigHelper.TestCase;
14+
import com.google.gson.Gson;
15+
import com.google.gson.JsonSyntaxException;
1416

1517
/**
1618
* The class to use to run local games and display the replay in a webpage on a temporary local server.
@@ -27,18 +29,33 @@ public SoloGameRunner() {
2729
}
2830

2931
private List<String> getLinesFromTestCaseFile(File file) {
30-
List<String> lines = new ArrayList<>();
32+
TestCase testCase;
3133
try {
32-
JsonObject testCaseJson = new JsonParser().parse(FileUtils.readFileToString(file, StandardCharsets.UTF_8)).getAsJsonObject();
33-
lines.addAll(Arrays.asList(testCaseJson.get("testIn").getAsString().split("\\n")));
34+
testCase = parseTestCaseFile(file);
3435
} catch (IOException e) {
35-
throw new RuntimeException("Cannot read file", e);
36-
} catch (NullPointerException e) {
37-
throw new RuntimeException("Cannot find \"testIn\" property");
36+
throw new RuntimeException("Cannot read file " + file.getName(), e);
3837
} catch (Exception e) {
39-
throw new RuntimeException("Cannot parse file", e);
38+
throw new RuntimeException("Cannot parse file " + file.getName(), e);
39+
}
40+
41+
if (testCase.getTestIn() == null) {
42+
throw new RuntimeException("Cannot find \"testIn\" property");
43+
}
44+
45+
return Arrays.asList(testCase.getTestIn().split("\\n"));
46+
}
47+
48+
private TestCase parseTestCaseFile(File file) throws JsonSyntaxException, IOException {
49+
String extension = FilenameUtils.getExtension(file.getName());
50+
51+
switch (extension.toLowerCase()) {
52+
case "yaml":
53+
case "yml":
54+
return new Yaml().loadAs(FileUtils.readFileToString(file, StandardCharsets.UTF_8), TestCase.class);
55+
case "json":
56+
default:
57+
return new Gson().fromJson(FileUtils.readFileToString(file, StandardCharsets.UTF_8), TestCase.class);
4058
}
41-
return lines;
4259
}
4360

4461
/**
@@ -66,10 +83,10 @@ public void setTestCase(File testCaseFile) {
6683
if (!testCaseFile.isFile()) {
6784
throw new RuntimeException("Given test case is not a file " + testCaseFile.getAbsolutePath());
6885
}
69-
86+
7087
setTestCaseInput(getLinesFromTestCaseFile(testCaseFile));
7188
}
72-
89+
7390
/**
7491
* Sets a list of <code>String</code> as a test case input that will be sent to the Game Manager.
7592
*
@@ -79,7 +96,7 @@ public void setTestCase(File testCaseFile) {
7996
public void setTestCaseInput(List<String> testCaseInput) {
8097
this.testCaseInput = testCaseInput;
8198
}
82-
99+
83100
/**
84101
* Sets a <code>String</code> as a test case input that will be sent to the Game Manager.
85102
* <p>

0 commit comments

Comments
 (0)