Merge branch 'phelipealves-master'

* phelipealves-master:
  Increasing the code coverage of AdapterMethodsFactory
This commit is contained in:
jwilson
2015-11-04 19:56:13 -05:00
2 changed files with 58 additions and 3 deletions

View File

@@ -217,9 +217,9 @@ final class AdapterMethodsFactory implements JsonAdapter.Factory {
} else { } else {
throw new IllegalArgumentException("Unexpected signature for " + method + ".\n" throw new IllegalArgumentException("Unexpected signature for " + method + ".\n"
+ "@ToJson method signatures may have one of the following structures:\n" + "@FromJson method signatures may have one of the following structures:\n"
+ " <any access modifier> void toJson(JsonWriter writer, T value) throws <any>;\n" + " <any access modifier> void fromJson(JsonReader jsonReader) throws <any>;\n"
+ " <any access modifier> R toJson(T value) throws <any>;\n"); + " <any access modifier> R fromJson(T value) throws <any>;\n");
} }
} }

View File

@@ -183,6 +183,61 @@ public final class AdapterMethodsTest {
} }
} }
@Test public void emptyAdapters() throws Exception {
Moshi.Builder builder = new Moshi.Builder();
try {
builder.add(new EmptyJsonAdapter()).build();
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage(
"Expected at least one @ToJson or @FromJson method on "
+ "com.squareup.moshi.AdapterMethodsTest$EmptyJsonAdapter");
}
}
static class EmptyJsonAdapter {
}
@Test public void unexpectedSignatureToAdapters() throws Exception {
Moshi.Builder builder = new Moshi.Builder();
try {
builder.add(new UnexpectedSignatureToJsonAdapter()).build();
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Unexpected signature for void "
+ "com.squareup.moshi.AdapterMethodsTest$UnexpectedSignatureToJsonAdapter.pointToJson"
+ "(com.squareup.moshi.AdapterMethodsTest$Point).\n"
+ "@ToJson method signatures may have one of the following structures:\n"
+ " <any access modifier> void toJson(JsonWriter writer, T value) throws <any>;\n"
+ " <any access modifier> R toJson(T value) throws <any>;\n");
}
}
static class UnexpectedSignatureToJsonAdapter {
@ToJson void pointToJson(Point point) {
}
}
@Test public void unexpectedSignatureFromAdapters() throws Exception {
Moshi.Builder builder = new Moshi.Builder();
try {
builder.add(new UnexpectedSignatureFromJsonAdapter()).build();
fail();
} catch (IllegalArgumentException expected) {
assertThat(expected).hasMessage("Unexpected signature for void "
+ "com.squareup.moshi.AdapterMethodsTest$UnexpectedSignatureFromJsonAdapter.pointFromJson"
+ "(java.lang.String).\n"
+ "@FromJson method signatures may have one of the following structures:\n"
+ " <any access modifier> void fromJson(JsonReader jsonReader) throws <any>;\n"
+ " <any access modifier> R fromJson(T value) throws <any>;\n");
}
}
static class UnexpectedSignatureFromJsonAdapter {
@FromJson void pointFromJson(String point) {
}
}
/** /**
* Simple adapter methods are not invoked for null values unless they're annotated {@code * Simple adapter methods are not invoked for null values unless they're annotated {@code
* @Nullable}. (The specific annotation class doesn't matter; just its simple name.) * @Nullable}. (The specific annotation class doesn't matter; just its simple name.)