@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_shouldHandleNonPublicClasses() { assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/processing/shadows/ShadowPrivate.java"), forResource("org/robolectric/annotation/processing/shadows/ShadowOuterDummy2.java"), forResource("org/robolectric/annotation/processing/shadows/ShadowDummy.java"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .compilesWithoutError() .and() .generatesSources(forResource("org/robolectric/Robolectric_HiddenClasses.java")); }
private JavaFileObject applyDiff( JavaFileObject sourceFileObject, Context context, JCCompilationUnit tree) throws IOException { ImportOrganizer importOrganizer = ImportOrderParser.getImportOrganizer(importOrder); final DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree, importOrganizer); transformer(refactoringBugChecker) .apply( new TreePath(tree), context, new DescriptionListener() { @Override public void onDescribed(Description description) { if (!description.fixes.isEmpty()) { diff.handleFix(fixChooser.choose(description.fixes)); } } }); SourceFile sourceFile = SourceFile.create(sourceFileObject); diff.applyDifferences(sourceFile); JavaFileObject transformed = JavaFileObjects.forSourceString(getFullyQualifiedName(tree), sourceFile.getSourceText()); return transformed; }
@Test public void generatedFile_shouldHandleInnerClassCollisions() { // Because the Generated annotation has a retention of "source", it can't // be tested by a unit test - must run a source-level test. assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/processing/shadows/ShadowDummy.java"), forResource("org/robolectric/annotation/processing/shadows/ShadowOuterDummy.java"), forResource("org/robolectric/annotation/processing/shadows/ShadowUniqueDummy.java"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .compilesWithoutError() .and() .generatesSources(forResource("org/robolectric/Robolectric_InnerClassCollision.java")); }
@Test public void testProcessorWithEmptyCustomPrefixFail() { JavaFileObject emptyPrefixLink = JavaFileObjects .forSourceString("MyDeepLink", "package com.example;\n" + "import com.airbnb.deeplinkdispatch.DeepLinkSpec;\n" + "@DeepLinkSpec(prefix = { \"http://\", \"\" })\n" + "public @interface MyDeepLink {\n" + " String[] value();\n" + "}"); assertAbout(javaSources()) .that(Arrays.asList(emptyPrefixLink, SIMPLE_DEEPLINK_ACTIVITY, SIMPLE_DEEPLINK_MODULE)) .processedWith(new DeepLinkProcessor()) .failsToCompile() .withErrorContaining("Prefix property cannot have null or empty strings"); }
@Test public void testCommandWithMixin() { Compilation compilation = compareCommandDump(slurp("/picocli/examples/mixin/CommandWithMixin.yaml"), JavaFileObjects.forResource("picocli/examples/mixin/CommandWithMixin.java"), JavaFileObjects.forResource("picocli/examples/mixin/CommonOption.java")); assertOnlySourceVersionWarning(compilation); }
@Test public void testProcessorWithEmptyDeepLinkSpecPrefixesFail() { JavaFileObject emptyPrefixArrayLink = JavaFileObjects .forSourceString("MyDeepLink", "package com.example;\n" + "import com.airbnb.deeplinkdispatch.DeepLinkSpec;\n" + "@DeepLinkSpec(prefix = { })\n" + "public @interface MyDeepLink {\n" + " String[] value();\n" + "}"); assertAbout(javaSources()) .that(Arrays.asList(emptyPrefixArrayLink, SIMPLE_DEEPLINK_ACTIVITY, SIMPLE_DEEPLINK_MODULE)) .processedWith(new DeepLinkProcessor()) .failsToCompile() .withErrorContaining("Prefix property cannot be empty"); }
@Test public void shouldGenerateMetaInfServicesFile() { assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/processing/shadows/ShadowClassNameOnly.java"), forResource("org/robolectric/annotation/processing/shadows/ShadowDummy.java"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .compilesWithoutError() .and() .generatesFiles(forResource("META-INF/services/org.robolectric.internal.ShadowProvider")); }
@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 generatedFile_shouldHandleClassNameOnlyShadows() { assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/processing/shadows/ShadowClassNameOnly.java"), forResource("org/robolectric/annotation/processing/shadows/ShadowDummy.java"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .compilesWithoutError() .and() .generatesSources(forResource("org/robolectric/Robolectric_ClassNameOnly.java")); }
@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 shouldGenerateGenericShadowOf() { assertAbout(javaSources()) .that(ImmutableList.of( SHADOW_PROVIDER_SOURCE, SHADOW_EXTRACTOR_SOURCE, forResource("org/robolectric/annotation/processing/shadows/ShadowDummy.java"), forResource("org/robolectric/annotation/processing/shadows/ShadowParameterizedDummy.java"))) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)) .compilesWithoutError() .and() .generatesSources(forResource("org/robolectric/Robolectric_Parameterized.java")); }
@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 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 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 twoMethodShortcutActivities() { JavaFileObject source1 = JavaFileObjects.forResource("MethodShortcutActivity.java"); JavaFileObject source2 = JavaFileObjects.forResource("MethodShortcutActivity2.java"); JavaFileObject generated = JavaFileObjects.forResource("TwoMethodShortcutActivitiesGenerated.java"); assertAbout(javaSources()).that(Arrays.asList(source1, source2)) .processedWith(new ShortcutProcessor()) .compilesWithoutError() .and() .generatesSources(generated); } }
@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 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); } }
public SingleClassSubject(FailureMetadata failureMetadata, String subject) { super(failureMetadata, subject); source = JavaFileObjects.forResource(Utils.toResourcePath(subject)); tester = assertAbout(javaSources()) .that(ImmutableList.of(source, Utils.SHADOW_EXTRACTOR_SOURCE)) .processedWith(new RobolectricProcessor(DEFAULT_OPTS)); }