Skip to content

Commit 59a5846

Browse files
committed
Fix for #474, handle invalid enum in the ini file.
Also added tests.
1 parent 1c9eac5 commit 59a5846

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.badlogic.gdx.Gdx;
1919
import com.badlogic.gdx.Input;
20+
import com.google.common.base.Enums;
2021
import org.destinationsol.menu.Resolution;
2122
import org.destinationsol.menu.ResolutionProvider;
2223

@@ -185,7 +186,7 @@ public GameOptions(boolean mobile, SolFileReader solFileReader) {
185186
x = reader.getInt("x", 1366);
186187
y = reader.getInt("y", 768);
187188
fullscreen = reader.getBoolean("fullscreen", false);
188-
controlType = mobile ? ControlType.KEYBOARD : ControlType.valueOf(reader.getString("controlType", "MIXED"));
189+
controlType = mobile ? ControlType.KEYBOARD : Enums.getIfPresent(ControlType.class, reader.getString("controlType", "MIXED")).or(ControlType.MIXED);
189190
sfxVolume = Volume.valueOf(reader.getString("sfxVolume", "MAX"));
190191
musicVolume = Volume.valueOf(reader.getString("musicVolume", "MAX"));
191192
keyUpMouseName = reader.getString("keyUpMouse", DEFAULT_MOUSE_UP);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.destinationsol;
1717

18+
import com.google.common.base.Enums;
1819
import org.junit.Before;
1920
import org.junit.Test;
2021

@@ -52,6 +53,8 @@ public void initIniReader() {
5253
"anotherFloatKey = 7.3f\n" +
5354
"invalidFloatKey = 8,6\n" +
5455
"anotherInvalidFloatKey = hi\n" +
56+
"enumInvalid = 1\n" +
57+
"enumValid = KEYBOARD\n" +
5558
"UnicodeKey çáč🧝 = unicodevalue áśǵj́ḱĺóí⋄«»⋄⋄ǫő";
5659
iniReader = new IniReader(new BufferedReader(new StringReader(iniFileContents)));
5760
}
@@ -102,5 +105,19 @@ public void testGetFloat() {
102105
assertTrue(Float.compare(iniReader.getFloat("anotherInvalidFloatKey", 7.8f), 7.8f) == 0);
103106
}
104107

108+
@Test
109+
public void testEnums() {
110+
// 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);
113+
114+
// 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);
117+
118+
// 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);
120+
}
121+
105122
//TODO ADD MOAR TESTS
106123
}

0 commit comments

Comments
 (0)