Skip to content

Commit a311d7a

Browse files
committed
Handle invalid ini values for Volum.
Also fixed up tests and added a couple more.
1 parent 59a5846 commit a311d7a

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

engine/src/main/java/org/destinationsol/GameOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ public GameOptions(boolean mobile, SolFileReader solFileReader) {
187187
y = reader.getInt("y", 768);
188188
fullscreen = reader.getBoolean("fullscreen", false);
189189
controlType = mobile ? ControlType.KEYBOARD : Enums.getIfPresent(ControlType.class, reader.getString("controlType", "MIXED")).or(ControlType.MIXED);
190-
sfxVolume = Volume.valueOf(reader.getString("sfxVolume", "MAX"));
191-
musicVolume = Volume.valueOf(reader.getString("musicVolume", "MAX"));
190+
sfxVolume = Enums.getIfPresent(Volume.class, reader.getString("sfxVolume", "MAX")).or(Volume.MAX);
191+
musicVolume = Enums.getIfPresent(Volume.class, reader.getString("musicVolume", "MAX")).or(Volume.MAX);
192192
keyUpMouseName = reader.getString("keyUpMouse", DEFAULT_MOUSE_UP);
193193
keyDownMouseName = reader.getString("keyDownMouse", DEFAULT_MOUSE_DOWN);
194194
keyUpName = reader.getString("keyUp", DEFAULT_UP);

engine/src/test/java/org/destinationsol/IniReaderTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ public void initIniReader() {
4949
"intKey = 5\n" +
5050
"invalidIntKey = 3.4\n" +
5151
"anotherInvalidIntKey = two\n" +
52+
"blankIntKey = \n" +
5253
"floatKey = 6\n" +
5354
"anotherFloatKey = 7.3f\n" +
5455
"invalidFloatKey = 8,6\n" +
5556
"anotherInvalidFloatKey = hi\n" +
5657
"enumInvalid = 1\n" +
58+
"enumEmpty =\n" +
5759
"enumValid = KEYBOARD\n" +
5860
"UnicodeKey çáč🧝 = unicodevalue áśǵj́ḱĺóí⋄«»⋄⋄ǫő";
5961
iniReader = new IniReader(new BufferedReader(new StringReader(iniFileContents)));
@@ -82,6 +84,7 @@ public void testGetInt() {
8284
assertEquals(iniReader.getInt("intKey", 0), 5);
8385
assertEquals(iniReader.getInt("invalidIntKey", 56), 56);
8486
assertEquals(iniReader.getInt("anotherInvalidIntKey", 57), 57);
87+
assertEquals(iniReader.getInt("blankIntKey", 58), 58);
8588
}
8689

8790
@Test
@@ -108,16 +111,16 @@ public void testGetFloat() {
108111
@Test
109112
public void testEnums() {
110113
// When no value exists in the file, use the defaultValue in getString.
111-
assertEquals(Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumDefault", "MIXED")).or(GameOptions.ControlType.KEYBOARD), GameOptions.ControlType.MIXED);
112-
assertEquals(Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumDefault", "MIXED")).or(GameOptions.ControlType.MIXED), GameOptions.ControlType.MIXED);
114+
assertEquals(GameOptions.ControlType.MIXED, Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumDefault", "MIXED")).or(GameOptions.ControlType.KEYBOARD));
115+
assertEquals(GameOptions.ControlType.MIXED, Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumDefault", "MIXED")).or(GameOptions.ControlType.MIXED));
116+
assertEquals(GameOptions.ControlType.KEYBOARD, Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumEmpty", "KEYBOARD")).or(GameOptions.ControlType.MIXED));
113117

114118
// When the value in the file isn't a valid enum, use the default value in getIfPresent
115-
assertEquals(Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumInvalid", "KEYBOARD")).or(GameOptions.ControlType.MIXED), GameOptions.ControlType.MIXED);
116-
assertEquals(Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumInvalid", "MIXED")).or(GameOptions.ControlType.MIXED), GameOptions.ControlType.MIXED);
119+
assertEquals(GameOptions.ControlType.MIXED, Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumInvalid", "KEYBOARD")).or(GameOptions.ControlType.MIXED));
120+
assertEquals(GameOptions.ControlType.MIXED, Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumInvalid", "MIXED")).or(GameOptions.ControlType.MIXED));
117121

118122
// When the value is in the file, use that value regardless of the default values.
119-
assertEquals(Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumValid", "MIXED")).or(GameOptions.ControlType.MIXED), GameOptions.ControlType.KEYBOARD);
123+
assertEquals(GameOptions.ControlType.KEYBOARD, Enums.getIfPresent(GameOptions.ControlType.class, iniReader.getString("enumValid", "MIXED")).or(GameOptions.ControlType.MIXED));
120124
}
121125

122-
//TODO ADD MOAR TESTS
123126
}

0 commit comments

Comments
 (0)