Skip to content

Commit

Permalink
Rewrite lightcycle-processor test (#67)
Browse files Browse the repository at this point in the history
Fix the lightcycle-processor test to run in the IDE.

- Remove unnecessary header
- Inline Java source string constants

#64
  • Loading branch information
NikoYuwono authored and glung committed Nov 10, 2016
1 parent ff018b5 commit 101a747
Show file tree
Hide file tree
Showing 27 changed files with 882 additions and 767 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package com.soundcloud.lightcycle;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

import org.junit.Test;

import javax.tools.JavaFileObject;

import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaFileObjects.forSourceString;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;

public class ActivityInjectionTest {

@Test
public void shouldGenerateInjectorForActivity() {
JavaFileObject validTestActivity = forSourceString("com/test/ValidTestActivity", Joiner.on("\n").join(
"package com.test;",
"",
"import com.soundcloud.lightcycle.ActivityLightCycle;",
"import com.soundcloud.lightcycle.DefaultActivityLightCycle;",
"import com.soundcloud.lightcycle.LightCycle;",
"import com.soundcloud.lightcycle.LightCycleDispatcher;",
"",
"import android.app.Activity;",
"",
"public class ValidTestActivity ",
" extends Activity ",
" implements LightCycleDispatcher<ActivityLightCycle<ValidTestActivity>> {",
"",
" @LightCycle LightCycle1 lightCycle1;",
" @LightCycle LightCycle2 lightCycle2;",
"",
" @Override",
" public void bind(ActivityLightCycle<ValidTestActivity> lightCycle) {",
" // nop",
" }",
"",
"}",
"",
"class LightCycle1 extends DefaultActivityLightCycle<ValidTestActivity> {",
"}",
"",
"class LightCycle2 extends DefaultActivityLightCycle<ValidTestActivity> {",
"}"));

JavaFileObject expectedSource = forSourceString("com.test.ValidTestActivity$LightCycleBinder", Joiner.on("\n").join(
"package com.test;",
"",
"public final class ValidTestActivity$LightCycleBinder {",
"",
" public static void bind(ValidTestActivity target) {",
" final com.soundcloud.lightcycle.ActivityLightCycle<com.test.ValidTestActivity> lightCycle1$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle1);",
" target.bind(lightCycle1$Lifted);",
" final com.soundcloud.lightcycle.ActivityLightCycle<com.test.ValidTestActivity> lightCycle2$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle2);",
" target.bind(lightCycle2$Lifted);",
" }",
"}"));

assertAbout(javaSource())
.that(validTestActivity)
.processedWith(new LightCycleProcessor())
.compilesWithoutError()
.and().generatesSources(expectedSource);
}

@Test
public void shouldGenerateInjectorForActivityHierarchy() {
JavaFileObject validTestHierarchyActivity = forSourceString("com/test/ValidTestHierarchyActivity", Joiner.on("\n").join(
"package com.test;",
"",
"import com.soundcloud.lightcycle.ActivityLightCycle;",
"import com.soundcloud.lightcycle.DefaultActivityLightCycle;",
"import com.soundcloud.lightcycle.LightCycle;",
"import com.soundcloud.lightcycle.LightCycleDispatcher;",
"",
"import android.app.Activity;",
"",
"public class ValidTestHierarchyActivity extends BaseActivity {",
" @LightCycle LightCycle2 lightCycle2;",
"",
"}",
"",
"class BaseActivity ",
" extends Activity ",
" implements LightCycleDispatcher<ActivityLightCycle<BaseActivity>> {",
"",
" @LightCycle LightCycle1 lightCycle1;",
"",
" @Override",
" public void bind(ActivityLightCycle<BaseActivity> lightCycle) {",
" // nop",
" }",
"",
"}",
"",
"class LightCycle1 extends DefaultActivityLightCycle<BaseActivity> {",
"}",
"",
"class LightCycle2 extends DefaultActivityLightCycle<BaseActivity> {",
"}"));

JavaFileObject expectedSource = forSourceString("com.test.ValidTestHierarchyActivity$LightCycleBinder", Joiner.on("\n").join(
"package com.test;",
"",
"public final class ValidTestHierarchyActivity$LightCycleBinder {",
"",
" public static void bind(ValidTestHierarchyActivity target) {",
" com.test.BaseActivity$LightCycleBinder.bind(target);",
" final com.soundcloud.lightcycle.ActivityLightCycle<com.test.BaseActivity> lightCycle2$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle2);",
" target.bind(lightCycle2$Lifted);",
" }",
"}"));

assertAbout(javaSource())
.that(validTestHierarchyActivity)
.processedWith(new LightCycleProcessor())
.compilesWithoutError()
.and().generatesSources(expectedSource);
}

@Test
public void shouldGenerateInjectorForLightCycleAppCompatActivity() {
JavaFileObject validTestLightCycleAppCompatActivity = forSourceString("com/test/ValidTestLightCycleAppCompatActivity", Joiner.on("\n").join(
"package com.test;",
"",
"import com.soundcloud.lightcycle.ActivityLightCycle;",
"import com.soundcloud.lightcycle.DefaultActivityLightCycle;",
"import com.soundcloud.lightcycle.LightCycle;",
"import com.soundcloud.lightcycle.LightCycleDispatcher;",
"import com.soundcloud.lightcycle.LightCycleAppCompatActivity;",
"",
"import android.app.Activity;",
"",
"public class ValidTestLightCycleAppCompatActivity ",
" extends LightCycleAppCompatActivity<ValidTestLightCycleAppCompatActivity> {",
" @LightCycle LightCycle1 lightCycle1;",
" @LightCycle LightCycle2 lightCycle2;",
"",
"}",
"",
"class LightCycle1 extends DefaultActivityLightCycle<ValidTestLightCycleAppCompatActivity> {",
"}",
"",
"class LightCycle2 extends DefaultActivityLightCycle<ValidTestLightCycleAppCompatActivity> {",
"}"));

// Because neither the processor or the lib (java libraries) can depend on the api (Android library),
// we have to create a fake `LightCycleAppCompatActivity` here for testing purpose.
JavaFileObject fakeLightCycleAppCompatActivity = forSourceString("com/soundcloud/lightcycle/LightCycleAppCompatActivity", Joiner.on("\n").join(
"package com.soundcloud.lightcycle;",
"",
"import android.app.Activity;",
"",
"public abstract class LightCycleAppCompatActivity<T extends Activity>",
" extends Activity",
" implements LightCycleDispatcher<ActivityLightCycle<T>> {",
"",
" @Override",
" public void bind(ActivityLightCycle<T> lightCycle) { }",
"",
"}"));

JavaFileObject expectedSource = forSourceString("com.test.ValidTestLightCycleAppCompatActivity$LightCycleBinder", Joiner.on("\n").join(
"package com.test;",
"",
"public final class ValidTestLightCycleAppCompatActivity$LightCycleBinder {",
"",
" public static void bind(ValidTestLightCycleAppCompatActivity target) {",
" final com.soundcloud.lightcycle.ActivityLightCycle<com.test.ValidTestLightCycleAppCompatActivity> lightCycle1$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle1);",
" target.bind(lightCycle1$Lifted);",
" final com.soundcloud.lightcycle.ActivityLightCycle<com.test.ValidTestLightCycleAppCompatActivity> lightCycle2$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle2);",
" target.bind(lightCycle2$Lifted);",
" }",
"}"));

assertAbout(javaSources())
.that(ImmutableList.of(validTestLightCycleAppCompatActivity, fakeLightCycleAppCompatActivity))
.processedWith(new LightCycleProcessor())
.compilesWithoutError()
.and().generatesSources(expectedSource);
}

@Test
public void shouldGenerateInjectorForLightCycleActionBarActivity() {
JavaFileObject validTestLightCycleActionBarActivity = forSourceString("com/test/ValidTestLightCycleActionBarActivity", Joiner.on("\n").join(
"package com.test;",
"",
"import com.soundcloud.lightcycle.ActivityLightCycle;",
"import com.soundcloud.lightcycle.DefaultActivityLightCycle;",
"import com.soundcloud.lightcycle.LightCycle;",
"import com.soundcloud.lightcycle.LightCycleDispatcher;",
"import com.soundcloud.lightcycle.LightCycleActionBarActivity;",
"",
"import android.app.Activity;",
"",
"public class ValidTestLightCycleActionBarActivity ",
" extends LightCycleActionBarActivity<ValidTestLightCycleActionBarActivity> {",
" @LightCycle LightCycle1 lightCycle1;",
" @LightCycle LightCycle2 lightCycle2;",
"",
"}",
"",
"class LightCycle1 extends DefaultActivityLightCycle<ValidTestLightCycleActionBarActivity> {",
"}",
"",
"class LightCycle2 extends DefaultActivityLightCycle<ValidTestLightCycleActionBarActivity> {",
"}"));

// Because neither the processor or the lib (java libraries) can depend on the api (Android library),
// we have to create a fake `LightCycleActionBarActivity` here for testing purpose.
JavaFileObject fakeLightCycleActionBarActivity = forSourceString("com/soundcloud/lightcycle/LightCycleActionBarActivity", Joiner.on("\n").join(
"package com.soundcloud.lightcycle;",
"",
"import android.app.Activity;",
"",
"public abstract class LightCycleActionBarActivity<T extends Activity>",
" extends Activity",
" implements LightCycleDispatcher<ActivityLightCycle<T>> {",
"",
" @Override",
" public void bind(ActivityLightCycle<T> lightCycle) { }",
"",
"}"));

JavaFileObject expectedSource = forSourceString("com.test.ValidTestLightCycleActionBarActivity$LightCycleBinder", Joiner.on("\n").join(
"package com.test;",
"",
"public final class ValidTestLightCycleActionBarActivity$LightCycleBinder {",
"",
" public static void bind(ValidTestLightCycleActionBarActivity target) {",
" final com.soundcloud.lightcycle.ActivityLightCycle<com.test.ValidTestLightCycleActionBarActivity> lightCycle1$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle1);",
" target.bind(lightCycle1$Lifted);",
" final com.soundcloud.lightcycle.ActivityLightCycle<com.test.ValidTestLightCycleActionBarActivity> lightCycle2$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle2);",
" target.bind(lightCycle2$Lifted);",
" }",
"}"));

assertAbout(javaSources())
.that(ImmutableList.of(validTestLightCycleActionBarActivity, fakeLightCycleActionBarActivity))
.processedWith(new LightCycleProcessor())
.compilesWithoutError()
.and().generatesSources(expectedSource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.soundcloud.lightcycle;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;

import org.junit.Test;

import javax.tools.JavaFileObject;

import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaFileObjects.forSourceString;
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource;
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;

public class FragmentInjectionTest {

@Test
public void shouldGenerateInjectorForFragment() {
JavaFileObject validTestFragment = forSourceString("com/test/ValidTestFragment", Joiner.on("\n").join(
"package com.test;",
"",
"import com.soundcloud.lightcycle.FragmentLightCycle;",
"import com.soundcloud.lightcycle.LightCycle;",
"import com.soundcloud.lightcycle.LightCycleDispatcher;",
"import com.soundcloud.lightcycle.DefaultFragmentLightCycle;",
"",
"import android.app.Fragment;",
"",
"public class ValidTestFragment extends Fragment ",
" implements LightCycleDispatcher<FragmentLightCycle<ValidTestFragment>> {",
"",
" @LightCycle LightCycle1 lightCycle1;",
" @LightCycle LightCycle2 lightCycle2;",
"",
" @Override",
" public void bind(FragmentLightCycle<ValidTestFragment> lightCycle) {",
" // nop",
" }",
"",
"}",
"",
"class LightCycle1 extends DefaultFragmentLightCycle<Fragment> {",
"}",
"",
"class LightCycle2 extends DefaultFragmentLightCycle<Fragment> {",
"}"));

JavaFileObject expectedSource = forSourceString("com.test.ValidTestFragment$LightCycleBinder", Joiner.on("\n").join(
"package com.test;",
"",
"public final class ValidTestFragment$LightCycleBinder {",
"",
" public static void bind(ValidTestFragment target) {",
" final com.soundcloud.lightcycle.FragmentLightCycle<com.test.ValidTestFragment> lightCycle1$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle1);",
" target.bind(lightCycle1$Lifted);",
" final com.soundcloud.lightcycle.FragmentLightCycle<com.test.ValidTestFragment> lightCycle2$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle2);",
" target.bind(lightCycle2$Lifted);",
" }",
"}"));

assertAbout(javaSource())
.that(validTestFragment)
.processedWith(new LightCycleProcessor())
.compilesWithoutError()
.and().generatesSources(expectedSource);
}

@Test
public void shouldGenerateInjectorForLightCycleFragment() {
JavaFileObject validTestLightCycleFragment = forSourceString("com/test/ValidTestLightCycleFragment", Joiner.on("\n").join(
"package com.test;",
"",
"import com.soundcloud.lightcycle.DefaultFragmentLightCycle;",
"import com.soundcloud.lightcycle.LightCycle;",
"import com.soundcloud.lightcycle.LightCycleDispatcher;",
"import com.soundcloud.lightcycle.LightCycleFragment;",
"",
"import android.app.Activity;",
"",
"public class ValidTestLightCycleFragment ",
" extends LightCycleFragment<ValidTestLightCycleFragment> {",
" @LightCycle LightCycle1 lightCycle1;",
" @LightCycle LightCycle2 lightCycle2;",
"",
"}",
"",
"class LightCycle1 extends DefaultFragmentLightCycle<ValidTestLightCycleFragment> {",
"}",
"",
"class LightCycle2 extends DefaultFragmentLightCycle<ValidTestLightCycleFragment> {",
"}"));

// Because neither the processor or the lib (java libraries) can depend on the api (Android library),
// we have to create a fake `LightCycleFragment` here for testing purpose.
JavaFileObject fakeLightCycleFragment = forSourceString("com/soundcloud/lightcycle/LightCycleFragment", Joiner.on("\n").join(
"package com.soundcloud.lightcycle;",
"",
"import android.app.Fragment;",
"",
"public abstract class LightCycleFragment<FragmentType extends Fragment>",
" extends Fragment",
" implements LightCycleDispatcher<FragmentLightCycle<FragmentType>> {",
"",
" @Override",
" public void bind(FragmentLightCycle<FragmentType> lifeCycleComponent) { }",
"",
"}"));

JavaFileObject expectedSource = forSourceString("com.test.ValidTestLightCycleFragment$LightCycleBinder", Joiner.on("\n").join(
"package com.test;",
"",
"public final class ValidTestLightCycleFragment$LightCycleBinder {",
"",
" public static void bind(ValidTestLightCycleFragment target) {",
" final com.soundcloud.lightcycle.FragmentLightCycle<com.test.ValidTestLightCycleFragment> lightCycle1$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle1);",
" target.bind(lightCycle1$Lifted);",
" final com.soundcloud.lightcycle.FragmentLightCycle<com.test.ValidTestLightCycleFragment> lightCycle2$Lifted = ",
" com.soundcloud.lightcycle.LightCycles.lift(target.lightCycle2);",
" target.bind(lightCycle2$Lifted);",
" }",
"}"));

assertAbout(javaSources())
.that(ImmutableList.of(validTestLightCycleFragment, fakeLightCycleFragment))
.processedWith(new LightCycleProcessor())
.compilesWithoutError()
.and().generatesSources(expectedSource);
}
}
Loading

0 comments on commit 101a747

Please sign in to comment.