public SuccessfulCompilationClause compilesWithoutError() { try { return tester.compilesWithoutError(); } catch (AssertionError e) { failWithoutActual(simpleFact(e.getMessage())); } return null; }
public SingleFileClause failsToCompile() { try { return new SingleFileClause(tester.failsToCompile(), source); } catch (AssertionError e) { failWithoutActual(simpleFact(e.getMessage())); } return null; }
@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(); }
.failsToCompile() .withErrorContaining("cannot find symbol"); "}" )) .compilesWithoutError() .withNoteContaining("@BindView field with unresolved type (GeneratedView)").and() .withNoteContaining("must elsewhere be generated as a View or interface").and()
.failsToCompile() .withErrorContaining("cannot find symbol"); "}" )) .compilesWithoutError() .withNoteContaining("@BindViews List or array with unresolved type (GeneratedView)").and() .withNoteContaining("must elsewhere be generated as a View or interface").and()
@Test public void unannotatedSource_shouldCompile() { assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forSourceString("HelloWorld", "final class HelloWorld {}"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .compilesWithoutError(); //.and().generatesNoSources(); Should add this assertion onces // it becomes available in compile-testing }
@Test public void generatedFile_shouldComplainAboutNonStaticInnerClasses() { assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/processing/shadows/ShadowOuterDummyWithErrs.java"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .failsToCompile() .withErrorContaining("inner shadow classes must be static"); }
@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 shouldGracefullyHandleUnrecognisedAnnotation() { assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/TestWithUnrecognizedAnnotation.java"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .compilesWithoutError(); }
@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(); }
.withCompilerOptions("-Xlint:-processing") .processedWith(new ButterKnifeProcessor()) .compilesWithoutWarnings() .and() .generatesSources(bindingSource);
@Test public void realObjectWithCorrectClassName_shouldCompile() { assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/processing/shadows/ShadowRealObjectWithCorrectClassName.java"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .compilesWithoutError(); }
@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); }
.withCompilerOptions("-Xlint:-processing") .processedWith(new ButterKnifeProcessor()) .compilesWithoutWarnings() .and() .generatesSources(bindingSource);
@Test public void generatedShadowProvider_canConfigureInstrumentingPackages() { Map<String, String> options = new HashMap<>(DEFAULT_OPTS); options.put(SHOULD_INSTRUMENT_PKG_OPT, "false"); assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/processing/shadows/ShadowDummy.java"))) .processedWith(new RobolectricProcessor(options)) .compilesWithoutError() .and() .generatesSources(forResource("org/robolectric/Robolectric_EmptyProvidedPackageNames.java")); } }
@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); }
.withCompilerOptions("-Xlint:-processing") .processedWith(new ButterKnifeProcessor()) .compilesWithoutWarnings() .and() .generatesSources(bindingSource);
@Test public void generatedFile_shouldNotGenerateShadowOfMethodsForExcludedClasses() { assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/processing/shadows/ShadowExcludedFromAndroidSdk.java"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .compilesWithoutError() .and() .generatesSources(forResource("org/robolectric/Robolectric_NoExcludedTypes.java")); }
@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); } }
.withCompilerOptions("-Xlint:-processing") .processedWith(new ButterKnifeProcessor()) .compilesWithoutWarnings() .and() .generatesSources(bindingSource);