From 86c9c6cbfa77ce8f59e6b79395c8d73ba315f3a1 Mon Sep 17 00:00:00 2001 From: Phelipe Alves de Souza Date: Wed, 4 Nov 2015 22:12:00 -0200 Subject: [PATCH] Increasing the code coverage of AdapterMethodsFactory --- .../squareup/moshi/AdapterMethodsFactory.java | 6 +- .../squareup/moshi/AdapterMethodsTest.java | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/moshi/src/main/java/com/squareup/moshi/AdapterMethodsFactory.java b/moshi/src/main/java/com/squareup/moshi/AdapterMethodsFactory.java index ba1a0df..2025922 100644 --- a/moshi/src/main/java/com/squareup/moshi/AdapterMethodsFactory.java +++ b/moshi/src/main/java/com/squareup/moshi/AdapterMethodsFactory.java @@ -217,9 +217,9 @@ final class AdapterMethodsFactory implements JsonAdapter.Factory { } else { throw new IllegalArgumentException("Unexpected signature for " + method + ".\n" - + "@ToJson method signatures may have one of the following structures:\n" - + " void toJson(JsonWriter writer, T value) throws ;\n" - + " R toJson(T value) throws ;\n"); + + "@FromJson method signatures may have one of the following structures:\n" + + " void fromJson(JsonReader jsonReader) throws ;\n" + + " R fromJson(T value) throws ;\n"); } } diff --git a/moshi/src/test/java/com/squareup/moshi/AdapterMethodsTest.java b/moshi/src/test/java/com/squareup/moshi/AdapterMethodsTest.java index a0d32c5..6d3c87c 100644 --- a/moshi/src/test/java/com/squareup/moshi/AdapterMethodsTest.java +++ b/moshi/src/test/java/com/squareup/moshi/AdapterMethodsTest.java @@ -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" + + " void toJson(JsonWriter writer, T value) throws ;\n" + + " R toJson(T value) throws ;\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" + + " void fromJson(JsonReader jsonReader) throws ;\n" + + " R fromJson(T value) throws ;\n"); + } + } + + static class UnexpectedSignatureFromJsonAdapter { + @FromJson void pointFromJson(String point) { + } + } + /** * 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.)