Add tests for enum constant toString() reading (#2080)
This commit is contained in:
parent
7ee3e2787f
commit
b5343ba96c
@ -37,6 +37,7 @@ import java.util.EnumSet;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Functional tests for Java 5.0 enums.
|
* Functional tests for Java 5.0 enums.
|
||||||
*
|
*
|
||||||
@ -174,7 +175,7 @@ public class EnumTest extends TestCase {
|
|||||||
assertEquals(expectedMap, actualMap);
|
assertEquals(expectedMap, actualMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Roshambo {
|
private enum Roshambo {
|
||||||
ROCK {
|
ROCK {
|
||||||
@Override Roshambo defeats() {
|
@Override Roshambo defeats() {
|
||||||
return SCISSORS;
|
return SCISSORS;
|
||||||
@ -206,7 +207,7 @@ public class EnumTest extends TestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Gender {
|
private enum Gender {
|
||||||
@SerializedName("boy")
|
@SerializedName("boy")
|
||||||
MALE,
|
MALE,
|
||||||
|
|
||||||
@ -217,9 +218,10 @@ public class EnumTest extends TestCase {
|
|||||||
public void testEnumClassWithFields() {
|
public void testEnumClassWithFields() {
|
||||||
assertEquals("\"RED\"", gson.toJson(Color.RED));
|
assertEquals("\"RED\"", gson.toJson(Color.RED));
|
||||||
assertEquals("red", gson.fromJson("RED", Color.class).value);
|
assertEquals("red", gson.fromJson("RED", Color.class).value);
|
||||||
|
assertEquals(2, gson.fromJson("BLUE", Color.class).index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Color {
|
private enum Color {
|
||||||
RED("red", 1), BLUE("blue", 2), GREEN("green", 3);
|
RED("red", 1), BLUE("blue", 2), GREEN("green", 3);
|
||||||
String value;
|
String value;
|
||||||
int index;
|
int index;
|
||||||
@ -228,4 +230,47 @@ public class EnumTest extends TestCase {
|
|||||||
this.index = index;
|
this.index = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testEnumToStringRead() {
|
||||||
|
// Should still be able to read constant name
|
||||||
|
assertEquals(CustomToString.A, gson.fromJson("\"A\"", CustomToString.class));
|
||||||
|
// Should be able to read toString() value
|
||||||
|
assertEquals(CustomToString.A, gson.fromJson("\"test\"", CustomToString.class));
|
||||||
|
|
||||||
|
assertNull(gson.fromJson("\"other\"", CustomToString.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum CustomToString {
|
||||||
|
A;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "test";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that enum constant names have higher precedence than {@code toString()}
|
||||||
|
* result.
|
||||||
|
*/
|
||||||
|
public void testEnumToStringReadInterchanged() {
|
||||||
|
assertEquals(InterchangedToString.A, gson.fromJson("\"A\"", InterchangedToString.class));
|
||||||
|
assertEquals(InterchangedToString.B, gson.fromJson("\"B\"", InterchangedToString.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum InterchangedToString {
|
||||||
|
A("B"),
|
||||||
|
B("A");
|
||||||
|
|
||||||
|
private final String toString;
|
||||||
|
|
||||||
|
InterchangedToString(String toString) {
|
||||||
|
this.toString = toString;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return toString;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user