Trailing comma tests

This commit is contained in:
Jesse Wilson 2011-07-01 21:36:05 +00:00
parent 62675b7f46
commit 937019651a

View File

@ -16,8 +16,15 @@
package com.google.gson.functional;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import com.google.gson.Gson;
@ -43,7 +50,7 @@ public class JsonParserTest extends TestCase {
super.setUp();
gson = new Gson();
}
public void testDeserializingCustomTree() {
JsonObject obj = new JsonObject();
obj.addProperty("stringValue", "foo");
@ -52,7 +59,7 @@ public class JsonParserTest extends TestCase {
assertEquals(11, target.intValue);
assertEquals("foo", target.stringValue);
}
public void testBadTypeForDeserializingCustomTree() {
JsonObject obj = new JsonObject();
obj.addProperty("stringValue", "foo");
@ -64,7 +71,7 @@ public class JsonParserTest extends TestCase {
fail("BagOfPrimitives is not an array");
} catch (JsonParseException expected) { }
}
public void testBadFieldTypeForCustomDeserializerCustomTree() {
JsonArray array = new JsonArray();
array.add(new JsonPrimitive("blah"));
@ -89,7 +96,7 @@ public class JsonParserTest extends TestCase {
JsonObject obj = new JsonObject();
obj.add("primitive1", primitive1);
obj.add("primitive2", array);
try {
gson.fromJson(obj, Nested.class);
fail("Nested has field BagOfPrimitives which is not an array");
@ -97,7 +104,7 @@ public class JsonParserTest extends TestCase {
}
public void testChangingCustomTreeAndDeserializing() {
StringReader json =
StringReader json =
new StringReader("{'stringValue':'no message','intValue':10,'longValue':20}");
JsonObject obj = (JsonObject) new JsonParser().parse(json);
obj.remove("stringValue");
@ -107,4 +114,24 @@ public class JsonParserTest extends TestCase {
assertEquals(20, target.longValue);
assertEquals("fooBar", target.stringValue);
}
public void testExtraCommasInArrays() {
Type type = new TypeToken<List<String>>() {}.getType();
assertEquals(list("a", null, "b", null, null), gson.fromJson("[a,,b,,]", type));
assertEquals(list(null, null), gson.fromJson("[,]", type));
assertEquals(list("a", null), gson.fromJson("[a,]", type));
}
public void testExtraCommasInMaps() {
Type type = new TypeToken<Map<String, String>>() {}.getType();
try {
gson.fromJson("{a:b,}", type);
fail();
} catch (JsonSyntaxException expected) {
}
}
private <T> List<T> list(T... elements) {
return Arrays.asList(elements);
}
}