/** * Behaves exactly like {@link #forSourceString}, but joins lines so that multi-line source * strings may omit the newline characters. For example: <pre> {@code * * JavaFileObjects.forSourceLines("example.HelloWorld", * "package example;", * "", * "final class HelloWorld {", * " void sayHello() {", * " System.out.println(\"hello!\");", * " }", * "}"); * }</pre> */ public static JavaFileObject forSourceLines(String fullyQualifiedName, String... lines) { return forSourceLines(fullyQualifiedName, Arrays.asList(lines)); }
/** Parses the source given and produces a {@link ParseResult}. */ static ParseResult parseLines(Iterable<String> source) { return Parser.parse(ImmutableList.of(JavaFileObjects.forSourceLines("", source))); }
/** Parses the source given and produces a {@link ParseResult}. */ static ParseResult parseLines(Iterable<String> source) { return Parser.parse(ImmutableList.of(JavaFileObjects.forSourceLines("", source))); }
/** Parses the source given into a {@link CompilationUnitTree}. */ static CompilationUnitTree parseLinesToTree(Iterable<String> source) { Iterable<? extends CompilationUnitTree> parseResults = Parser.parse(ImmutableList.of(JavaFileObjects.forSourceLines("", source))) .compilationUnits(); return Iterables.getOnlyElement(parseResults); }
/** Parses the source given into a {@link CompilationUnitTree}. */ static CompilationUnitTree parseLinesToTree(Iterable<String> source) { Iterable<? extends CompilationUnitTree> parseResults = Parser.parse(ImmutableList.of(JavaFileObjects.forSourceLines("", source))) .compilationUnits(); return Iterables.getOnlyElement(parseResults); }
@Test public void combinedReducerAndBindableReducer() { assertJavaSource( forSourceLines("RootStore", Source.CombinedReducerAndBindableReducer.TARGET), forSourceLines("DroiduxRootStore_CounterStoreImpl", Source.StoreImpl.COUNTER), forSourceLines("DroiduxRootStore_TodoListStoreImpl", Source.StoreImpl.TODO_LIST), forSourceLines("DroiduxRootStore", Source.CombinedReducerAndBindableReducer.GENERATED) ); }
@Test public void combinedTwoReducers() { assertJavaSource( forSourceLines("RootStore", Source.CombinedTwoReducers.TARGET), forSourceLines("DroiduxRootStore_CounterStoreImpl", Source.StoreImpl.COUNTER), forSourceLines("DroiduxRootStore_TodoListStoreImpl", Source.StoreImpl.TODO_LIST), forSourceLines("DroiduxRootStore", Source.CombinedTwoReducers.GENERATED) ); }
@Test public void singleReducerBindable() { assertJavaSource( forSourceLines("RootStore", Source.BindableCounter.TARGET), forSourceLines("DroiduxRootStore_CounterStoreImpl", Source.StoreImpl.COUNTER), forSourceLines("DroiduxRootStore", Source.BindableCounter.GENERATED_STORE) ); }
@Test public void singleReducer() { assertJavaSource( forSourceLines("RootStore", Source.Counter.TARGET), forSourceLines("DroiduxRootStore_CounterStoreImpl", Source.StoreImpl.COUNTER), forSourceLines("DroiduxRootStore", Source.Counter.GENERATED_STORE) ); }
@Test public void singleReducer_with_BackpressureStrategySpecification() { assertJavaSource( forSourceLines("RootStore", Source.CounterWithBackpressureStrategy.TARGET), forSourceLines("DroiduxRootStore_CounterStoreImpl", Source.StoreImpl.COUNTER), forSourceLines("DroiduxRootStore", Source.CounterWithBackpressureStrategy.GENERATED_STORE) ); }
@Test public void reducerWithoutSuffix() { expectedException.expect(RuntimeException.class); expectedException.expectMessage( "@Reducer class name must end with \"Reducer\". \"CounterReduce\" has invalid class name." ); assertJavaSource( forSourceLines("CounterStore", Source.ReducerWithoutSuffix.TARGET), forSourceLines("DroiduxCounterStore_CounterStoreImpl", Source.EMPTY), forSourceLines("DroiduxCounterStore", Source.EMPTY) ); }
@Test public void dispatchableMethodReturnsWrongType() { expectedException.expect(RuntimeException.class); expectedException.expectMessage( "@Dispatchable method can have arguments only state or action. " + "CounterReducer#increment() has more than one invalid argument." ); assertJavaSource( forSourceLines("CounterStore", Source.DispatchableTakesExtraArguments.TARGET), forSourceLines("DroiduxCounterStore_CounterStoreImpl", Source.EMPTY), forSourceLines("DroiduxCounterStore", Source.EMPTY) ); }
@Test public void dispatchableMethodTakesWrongStateType() { expectedException.expect(RuntimeException.class); expectedException.expectMessage( "@Dispatchable method can have arguments only state or action. " + "CounterReducer#increment() has more than one invalid argument." ); assertJavaSource( forSourceLines("CounterStore", Source.DispatchableTakesWrongStateType.TARGET), forSourceLines("DroiduxCounterStore_CounterStoreImpl", Source.EMPTY), forSourceLines("DroiduxCounterStore", Source.EMPTY) ); }
@Test public void dispatchableMethodTakesWrongActionType() { expectedException.expect(RuntimeException.class); expectedException.expectMessage( "@Dispatchable method can have arguments only state or action. " + "TodoListReducer#addItem() has more than one invalid argument." ); assertJavaSource( forSourceLines("TodoListStore", Source.DispatchableTakesWrongActionType.TARGET), forSourceLines("DroiduxTodoListStore_TodoListStoreImpl", Source.EMPTY), forSourceLines("DroiduxTodoListStore", Source.EMPTY) ); }
@Test public void storeNotExtendBaseStore() { expectedException.expect(RuntimeException.class); expectedException.expectMessage( "The interface that is annotated @Store must extend \"BaseStore\"." ); assertJavaSource( forSourceLines("CounterStore", Source.StoreNotExtendsBaseStore.TARGET), forSourceLines("DroiduxCounterStore_CounterStoreImpl", Source.EMPTY), forSourceLines("DroiduxCounterStore", Source.EMPTY) ); } }
@Test public void dispatchableMethodShouldReturnState() { expectedException.expect(RuntimeException.class); expectedException.expectMessage( "@Dispatchable method must return new state. " + "But CounterReducer#increment() returns invalid type." ); assertJavaSource( forSourceLines("CounterStore", Source.DispatchableMethosReturnsWrongType.TARGET), forSourceLines("DroiduxCounterStore_CounterStoreImpl", Source.EMPTY), forSourceLines("DroiduxCounterStore", Source.EMPTY) ); }
@Test public void undoableReducerWithoutUndoableState() { expectedException.expect(RuntimeException.class); expectedException.expectMessage( "@Reducer class annotated with @Undoable must have the state implements \"UndoableState<T>\". " + "Counter state of CounterReducer does not implement it." ); assertJavaSource( forSourceLines("CounterStore", Source.UndoableReducerWithoutUndoableState.TARGET), forSourceLines("DroiduxCounterStore_CounterStoreImpl", Source.EMPTY), forSourceLines("DroiduxCounterStore", Source.EMPTY) ); }
@Test public void storeHasClassThatIsNotAnnotatedWithReducer() { expectedException.expect(RuntimeException.class); expectedException.expectMessage( "Values of @Store annotation must have only classes annotated with \"@Reducer\"." + "But CounterStore has invalid value." ); assertJavaSource( forSourceLines("CounterStore", Source.StoreHasInvalidValue.TARGET), forSourceLines("DroiduxCounterStore_CounterStoreImpl", Source.EMPTY), forSourceLines("DroiduxCounterStore", Source.EMPTY) ); }
private void assertCompileFail(String fullQualifiedName, String module) { assert_().about(javaSource()) .that(JavaFileObjects.forSourceLines(fullQualifiedName, module)) .processedWith(new ModuleProcessor()) .failsToCompile(); }
public void testAnnotationOnInterface() throws Exception { JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( "foo.bar.Baz", "package foo.bar;", "", "import retrofacebook.RetroFacebook;", "", "@RetroFacebook", "public interface Baz {}"); assertAbout(javaSource()) .that(javaFileObject) .processedWith(new RetroFacebookProcessor()) .failsToCompile() .withErrorContaining("RetroFacebook only applies to classes") .in(javaFileObject).onLine(6); }