1515 */
1616package org .destinationsol ;
1717
18+ import com .google .common .base .Enums ;
1819import org .junit .Before ;
1920import org .junit .Test ;
2021
@@ -48,37 +49,42 @@ public void initIniReader() {
4849 "intKey = 5\n " +
4950 "invalidIntKey = 3.4\n " +
5051 "anotherInvalidIntKey = two\n " +
52+ "blankIntKey = \n " +
5153 "floatKey = 6\n " +
5254 "anotherFloatKey = 7.3f\n " +
5355 "invalidFloatKey = 8,6\n " +
5456 "anotherInvalidFloatKey = hi\n " +
57+ "enumInvalid = 1\n " +
58+ "enumEmpty =\n " +
59+ "enumValid = KEYBOARD\n " +
5560 "UnicodeKey çáč🧝 = unicodevalue áśǵj́ḱĺóí⋄«»⋄⋄ǫő" ;
5661 iniReader = new IniReader (new BufferedReader (new StringReader (iniFileContents )));
5762 }
5863
5964 @ Test
6065 public void testInputHandling () {
61- assertEquals (iniReader .getString ("missingKey" , "correctValue" ), "correctValue" );
62- assertEquals (iniReader .getString ("terrible key name" , "wrongValue" ), "terrible key value" );
63- assertEquals (iniReader .getString ("partLineCommentKey" , "wrongValue" ), "correctValue1" );
64- assertEquals (iniReader .getString ("doubleRequestedKey" , "wrongValue" ), "validValue2" );
65- assertEquals (iniReader .getString ("doubleRequestedKey" , "wrongValue" ), "validValue2" );
66- assertEquals (iniReader .getString ("this shouldn't throw exception" , "correctValue" ), "correctValue" );
67- assertEquals (iniReader .getString ("UnicodeKey çáč🧝" , "wrongValue" ), "unicodevalue áśǵj́ḱĺóí⋄«»⋄⋄ǫő" );
66+ assertEquals ("correctValue" , iniReader .getString ("missingKey" , "correctValue" ));
67+ assertEquals ("terrible key value" , iniReader .getString ("terrible key name" , "wrongValue" ));
68+ assertEquals ("correctValue1" , iniReader .getString ("partLineCommentKey" , "wrongValue" ));
69+ assertEquals ("validValue2" , iniReader .getString ("doubleRequestedKey" , "wrongValue" ));
70+ assertEquals ("validValue2" , iniReader .getString ("doubleRequestedKey" , "wrongValue" ));
71+ assertEquals ("correctValue" , iniReader .getString ("this shouldn't throw exception" , "correctValue" ));
72+ assertEquals ("unicodevalue áśǵj́ḱĺóí⋄«»⋄⋄ǫő" , iniReader .getString ("UnicodeKey çáč🧝" , "wrongValue" ));
6873 }
6974
7075 @ Test
7176 public void testGetString () {
72- assertEquals (iniReader .getString ("asdfghjk" , "correctValue" ), "correctValue" );
73- assertEquals (iniReader .getString ("validStringKey" , "wrongValue" ), "validString" );
77+ assertEquals ("correctValue" , iniReader .getString ("asdfghjk" , "correctValue" ));
78+ assertEquals ("validString" , iniReader .getString ("validStringKey" , "wrongValue" ));
7479 }
7580
7681 @ Test
7782 public void testGetInt () {
78- assertEquals (iniReader .getInt ("asdfghjk" , 55 ), 55 );
79- assertEquals (iniReader .getInt ("intKey" , 0 ), 5 );
80- assertEquals (iniReader .getInt ("invalidIntKey" , 56 ), 56 );
81- assertEquals (iniReader .getInt ("anotherInvalidIntKey" , 57 ), 57 );
83+ assertEquals (55 , iniReader .getInt ("asdfghjk" , 55 ));
84+ assertEquals (5 , iniReader .getInt ("intKey" , 0 ));
85+ assertEquals (56 , iniReader .getInt ("invalidIntKey" , 56 ));
86+ assertEquals (57 , iniReader .getInt ("anotherInvalidIntKey" , 57 ));
87+ assertEquals (58 , iniReader .getInt ("blankIntKey" , 58 ));
8288 }
8389
8490 @ Test
@@ -102,5 +108,19 @@ public void testGetFloat() {
102108 assertTrue (Float .compare (iniReader .getFloat ("anotherInvalidFloatKey" , 7.8f ), 7.8f ) == 0 );
103109 }
104110
105- //TODO ADD MOAR TESTS
111+ @ Test
112+ public void testEnums () {
113+ // When no value exists in the file, use the defaultValue in getString.
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 ));
117+
118+ // When the value in the file isn't a valid enum, use the default value in getIfPresent
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 ));
121+
122+ // When the value is in the file, use that value regardless of the default values.
123+ assertEquals (GameOptions .ControlType .KEYBOARD , Enums .getIfPresent (GameOptions .ControlType .class , iniReader .getString ("enumValid" , "MIXED" )).or (GameOptions .ControlType .MIXED ));
124+ }
125+
106126}
0 commit comments