@Override void verifyMatch(JavaFileObject refactoredSource, JavaFileObject expectedSource) { assertAbout(javaSource()).that(refactoredSource).parsesAs(expectedSource); } };
@Test public void textChangedWithWrongParameter() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.OnTextChanged;\n" + "public class Test {\n" + " @OnTextChanged(1) void doStuff(String p0, int p1, int p2, int p3) {}\n" + "}" ); assertAbout(javaSource()).that(source) .withCompilerOptions("-Xlint:-processing") .processedWith(new ButterKnifeProcessor()) .failsToCompile(); }
@Test public void fieldVisibility() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import android.view.View;\n" + "import butterknife.BindViews;\n" + "import java.util.List;\n" + "public class Test {\n" + " @BindViews(1) public List<View> thing1;\n" + " @BindViews(2) List<View> thing2;\n" + " @BindViews(3) protected List<View> thing3;\n" + "}" ); assertAbout(javaSource()).that(source) .withCompilerOptions("-Xlint:-processing") .processedWith(new ButterKnifeProcessor()) .compilesWithoutWarnings(); }
@Test public void fieldVisibility() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import android.view.View;\n" + "import butterknife.BindView;\n" + "public class Test {\n" + " @BindView(1) public View thing1;\n" + " @BindView(2) View thing2;\n" + " @BindView(3) protected View thing3;\n" + "}" ); assertAbout(javaSource()).that(source) .withCompilerOptions("-Xlint:-processing") .processedWith(new ButterKnifeProcessor()) .compilesWithoutWarnings(); }
@Test public void failsToCompileWithWrongContext() throws IOException { final JavaFileObject javaFileObject = JavaFileObjects.forResource( Resources.getResource( getClass(), RES_PREFIX + "IncorrectOnCreateLayoutArgsComponentSpec.java")); Truth.assertAbout(JavaSourceSubjectFactory.javaSource()) .that(javaFileObject) .processedWith(new ComponentsProcessor()) .failsToCompile() .withErrorCount(1) .withErrorContaining( "Parameter in position 0 of a method annotated with interface " + "com.facebook.litho.annotations.OnCreateLayout should be of type " + "com.facebook.litho.ComponentContext") .in(javaFileObject) .onLine(28); }
@Test public void fieldWithoutObject() { ASSERT.about(javaSource()) .that(JavaFileObjects.forResource("model/bad/FieldWithoutObjectModel.java")) .processedWith(new JsonAnnotationProcessor()) .failsToCompile() .withErrorContaining("@JsonField fields can only be in classes annotated with @JsonObject."); }
@Test public void privateField() { ASSERT.about(javaSource()) .that(JavaFileObjects.forResource("model/bad/PrivateFieldModelWithoutAccessors.java")) .processedWith(new JsonAnnotationProcessor()) .failsToCompile() .withErrorContaining("@JsonField annotation can only be used on private fields if both getter and setter are present."); }
@Test public void invalidTypeConverter() { ASSERT.about(javaSource()) .that(JavaFileObjects.forResource("model/bad/InvalidTypeConverterModel.java")) .processedWith(new JsonAnnotationProcessor()) .failsToCompile() .withErrorContaining("TypeConverter elements must implement the TypeConverter interface or extend from one of the convenience helpers"); }
@Test public void methodWithArgs() { ASSERT.about(javaSource()) .that(JavaFileObjects.forResource("model/bad/MethodWithArgsModel.java")) .processedWith(new JsonAnnotationProcessor()) .failsToCompile() .withErrorContaining("@OnJsonParseComplete methods must not take any parameters."); }
@Test public void multipleMethods() { ASSERT.about(javaSource()) .that(JavaFileObjects.forResource("model/bad/MultipleMethodsModel.java")) .processedWith(new JsonAnnotationProcessor()) .failsToCompile() .withErrorContaining("There can only be one @OnJsonParseComplete method per class"); } }
@Test public void methodObject() { ASSERT.about(javaSource()) .that(JavaFileObjects.forResource("model/bad/MethodWithoutObjectModel.java")) .processedWith(new JsonAnnotationProcessor()) .failsToCompile() .withErrorContaining("@OnJsonParseComplete methods can only be in classes annotated with @JsonObject."); }
@Test public void typeMustBeTypeface() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.BindFont;\n" + "public class Test {\n" + " @BindFont(1) String one;\n" + "}" ); assertAbout(javaSource()).that(source) .processedWith(new ButterKnifeProcessor()) .failsToCompile() .withErrorContaining("@BindFont field type must be 'Typeface'. (test.Test.one)") .in(source).onLine(4); }
@Test public void failsWithInvalidId() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.OnItemClick;\n" + "public class Test {\n" + " @OnItemClick({1, -1}) void doStuff() {}\n" + "}" ); assertAbout(javaSource()).that(source) .processedWith(new ButterKnifeProcessor()) .failsToCompile() .withErrorContaining("@OnItemClick annotation contains invalid ID -1. (test.Test.doStuff)") .in(source).onLine(4); }
@Test public void typeMustBeBitmap() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.BindBitmap;\n" + "public class Test {\n" + " @BindBitmap(1) String one;\n" + "}" ); assertAbout(javaSource()).that(source) .processedWith(new ButterKnifeProcessor()) .failsToCompile() .withErrorContaining("@BindBitmap field type must be 'Bitmap'. (test.Test.one)") .in(source).onLine(4); } }
@Test public void failsIfArrayNotView() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.BindViews;\n" + "public class Test {\n" + " @BindViews(1) String[] thing;\n" + "}" ); assertAbout(javaSource()).that(source) .processedWith(new ButterKnifeProcessor()) .failsToCompile() .withErrorContaining( "@BindViews List or array type must extend from View or be an interface. (test.Test.thing)") .in(source).onLine(4); }
@Test public void typeMustBeBoolean() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.BindBool;\n" + "public class Test {\n" + " @BindBool(1) String one;\n" + "}" ); assertAbout(javaSource()).that(source) .processedWith(new ButterKnifeProcessor()) .failsToCompile() .withErrorContaining("@BindBool field type must be 'boolean'. (test.Test.one)") .in(source).onLine(4); } }
@Test public void typeMustBeAnimation() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.BindAnim;\n" + "public class Test {\n" + " @BindAnim(1) String one;\n" + "}" ); assertAbout(javaSource()).that(source) .processedWith(new ButterKnifeProcessor()) .failsToCompile() .withErrorContaining("@BindAnim field type must be 'Animation'. (test.Test.one)") .in(source).onLine(4); } }
@Test public void typeMustBeDrawable() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.BindDrawable;\n" + "public class Test {\n" + " @BindDrawable(1) String one;\n" + "}" ); assertAbout(javaSource()).that(source) .processedWith(new ButterKnifeProcessor()) .failsToCompile() .withErrorContaining("@BindDrawable field type must be 'Drawable'. (test.Test.one)") .in(source).onLine(4); } }
@Test public void failsInAndroidPackage() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package android.test;\n" + "import butterknife.OnClick;\n" + "public class Test {\n" + " @OnClick(1) void doStuff() {}\n" + "}" ); assertAbout(javaSource()).that(source) .processedWith(new ButterKnifeProcessor()) .failsToCompile() .withErrorContaining( "@OnClick-annotated class incorrectly in Android framework package. (android.test.Test)") .in(source).onLine(4); }
@Test public void typeMustBeIntOrFloat() { JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.BindDimen;\n" + "public class Test {\n" + " @BindDimen(1) String one;\n" + "}" ); assertAbout(javaSource()).that(source) .processedWith(new ButterKnifeProcessor()) .failsToCompile() .withErrorContaining("@BindDimen field type must be 'int' or 'float'. (test.Test.one)") .in(source).onLine(4); } }