33import java .io .File ;
44import java .io .IOException ;
55import java .nio .charset .StandardCharsets ;
6- import java .util .ArrayList ;
76import java .util .Arrays ;
87import java .util .List ;
98
109import 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