|
15 | 15 | */ |
16 | 16 | package org.destinationsol; |
17 | 17 |
|
| 18 | +import com.google.common.base.Enums; |
18 | 19 | import org.junit.Before; |
19 | 20 | import org.junit.Test; |
20 | 21 |
|
@@ -52,6 +53,8 @@ public void initIniReader() { |
52 | 53 | "anotherFloatKey = 7.3f\n" + |
53 | 54 | "invalidFloatKey = 8,6\n" + |
54 | 55 | "anotherInvalidFloatKey = hi\n" + |
| 56 | + "enumInvalid = 1\n" + |
| 57 | + "enumValid = KEYBOARD\n" + |
55 | 58 | "UnicodeKey çáč🧝 = unicodevalue áśǵj́ḱĺóí⋄«»⋄⋄ǫő"; |
56 | 59 | iniReader = new IniReader(new BufferedReader(new StringReader(iniFileContents))); |
57 | 60 | } |
@@ -102,5 +105,19 @@ public void testGetFloat() { |
102 | 105 | assertTrue(Float.compare(iniReader.getFloat("anotherInvalidFloatKey", 7.8f), 7.8f) == 0); |
103 | 106 | } |
104 | 107 |
|
| 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 | + |
105 | 122 | //TODO ADD MOAR TESTS |
106 | 123 | } |
0 commit comments