@Test public void failsIfInInterface() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestNavigationModel", Joiner.on('\n') .join( "package test;", "import dart.BindExtra;", "public interface TestNavigationModel {", " @BindExtra(\"key\") String extra = null;", "}")); Compilation compilation = javac().withProcessors(extraBinderProcessorsWithoutParceler()).compile(source); assertThat(compilation) .hadErrorContaining( "@BindExtra field must not be private or static. (test.TestNavigationModel.extra)") .inFile(source) .onLine(4); }
/** * Returns the matching diagnostics found on a specific line of the file. Fails the test if none * are found. * * @param expectedLine the expected line number */ @CanIgnoreReturnValue private ImmutableList<Diagnostic<? extends JavaFileObject>> findMatchingDiagnosticsOnLine( long expectedLine) { ImmutableList<Diagnostic<? extends JavaFileObject>> diagnosticsOnLine = filterDiagnostics(diagnostic -> diagnostic.getLineNumber() == expectedLine); if (diagnosticsOnLine.isEmpty()) { failExpectingMatchingDiagnostic( " in %s on line:\n%s\nbut found it on line(s):\n%s", linesInFile.fileName(), linesInFile.listLine(expectedLine), mapDiagnostics(Diagnostic::getLineNumber).collect(linesInFile.toLineList())); } return diagnosticsOnLine; } }
/** * Returns the matching diagnostics found on a specific line of the file. Fails the test if none * are found. * * @param expectedLine the expected line number */ @CanIgnoreReturnValue private ImmutableList<Diagnostic<? extends JavaFileObject>> findMatchingDiagnosticsOnLine( long expectedLine) { ImmutableList<Diagnostic<? extends JavaFileObject>> diagnosticsOnLine = filterDiagnostics(diagnostic -> diagnostic.getLineNumber() == expectedLine); if (diagnosticsOnLine.isEmpty()) { failExpectingMatchingDiagnostic( " in %s on line:\n%s\nbut found it on line(s):\n%s", linesInFile.fileName(), linesInFile.listLine(expectedLine), mapDiagnostics(Diagnostic::getLineNumber).collect(linesInFile.toLineList())); } return diagnosticsOnLine; } }
/** Asserts that the note, warning, or error was found on a given line. */ @CanIgnoreReturnValue public DiagnosticAtColumn onLine(long expectedLine) { return new DiagnosticAtColumn( this, linesInFile, expectedLine, findMatchingDiagnosticsOnLine(expectedLine)); }
/** Asserts that the note, warning, or error was found in a given file. */ @CanIgnoreReturnValue public DiagnosticOnLine inFile(JavaFileObject expectedFile) { return new DiagnosticOnLine(this, expectedFile, findDiagnosticsInFile(expectedFile)); }
/** * Asserts that the note, warning, or error was found on the single line that contains a * substring. */ public void onLineContaining(String expectedLineSubstring) { findMatchingDiagnosticsOnLine(findLineContainingSubstring(expectedLineSubstring)); }
/** Asserts that the note, warning, or error was found on a given line. */ @CanIgnoreReturnValue public DiagnosticAtColumn onLine(long expectedLine) { return new DiagnosticAtColumn( this, linesInFile, expectedLine, findMatchingDiagnosticsOnLine(expectedLine)); }
/** Asserts that the note, warning, or error was found in a given file. */ @CanIgnoreReturnValue public DiagnosticOnLine inFile(JavaFileObject expectedFile) { return new DiagnosticOnLine(this, expectedFile, findDiagnosticsInFile(expectedFile)); }
/** * Asserts that the note, warning, or error was found on the single line that contains a * substring. */ public void onLineContaining(String expectedLineSubstring) { findMatchingDiagnosticsOnLine(findLineContainingSubstring(expectedLineSubstring)); }
.hadErrorContaining("Class must not be private") .inFile(javaFileObject) .onLine(20) .atColumn(26);
.hadErrorContaining("Class must not be private") .inFile(javaFileObject) .onLine(25) .atColumn(26);
@Test public void testPrivateProperty() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestPrivateProperty"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestPrivateProperty {\n" + " @State\n" + " private int test;\n" + "\n" + " private int getTest() {\n" + " return test;\n" + " }\n" + "\n" + " public void setTest(int test) {\n" + " this.test = test;\n" + " }\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Field test must be either non-private or provide a getter and setter method") .inFile(javaFileObject) .onLine(7) .atColumn(17); }
@Test public void testTypeNotSupported() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestTypeNotSupported"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestTypeNotSupported {\n" + " @State\n" + " public Other test;\n" + "\n" + " public static final class Other {}\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Don't know how to put com.evernote.android.state.test.TestTypeNotSupported.Other into a bundle") .inFile(javaFileObject) .onLine(7) .atColumn(18); } }
@Test public void testPrivateClass() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestPrivateClass"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestPrivateClass {\n" + " private static class Inner {\n" + " @State\n" + " public int test;\n" + " }\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Class must not be private") .inFile(javaFileObject) .onLine(6) .atColumn(20); }
@Test public void testFinal() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestFinal"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestFinal {\n" + " @State\n" + " public final int test = 5;\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Field must not be final") .inFile(javaFileObject) .onLine(7) .atColumn(22); }
@Test public void testPrivateField() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestPrivateField"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestPrivateField {\n" + " @State\n" + " private int test;\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Field test must be either non-private or provide a getter and setter method") .inFile(javaFileObject) .onLine(7) .atColumn(17); }
@Test public void testStatic() { JavaFileObject javaFileObject = JavaFileObjects.forSourceString(getName("TestStatic"), "" + "package com.evernote.android.state.test;\n" + "\n" + "import com.evernote.android.state.State;\n" + "\n" + "public class TestStatic {\n" + " @State\n" + " public static int test;\n" + "}\n"); Compilation compilation = Compiler.javac().withProcessors(new StateProcessor()).compile(javaFileObject); assertThat(compilation).failed(); assertThat(compilation) .hadErrorContaining("Field must not be static") .inFile(javaFileObject) .onLine(7) .atColumn(23); }
@Test public void failsIfStatic() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestNavigationModel", Joiner.on('\n') .join( "package test;", "import dart.BindExtra;", "public class TestNavigationModel {", " @BindExtra(\"key\") static String extra;", "}")); Compilation compilation = javac().withProcessors(extraBinderProcessorsWithoutParceler()).compile(source); assertThat(compilation) .hadErrorContaining( "@BindExtra field must not be private or static. (test.TestNavigationModel.extra)") .inFile(source) .onLine(4); }
@Test public void failsIfPrivate() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestNavigationModel", Joiner.on('\n') .join( "package test;", "import dart.BindExtra;", "public class TestNavigationModel {", " @BindExtra(\"key\") private String extra;", "}")); Compilation compilation = javac().withProcessors(extraBinderProcessorsWithoutParceler()).compile(source); assertThat(compilation) .hadErrorContaining( "@BindExtra field must not be private or static. (test.TestNavigationModel.extra)") .inFile(source) .onLine(4); }
@Test public void failsIfInPrivateClass() { JavaFileObject source = JavaFileObjects.forSourceString( "test.TestNavigationModel", Joiner.on('\n') .join( "package test;", "import dart.BindExtra;", "public class TestNavigationModel {", " private static class Inner {", " @BindExtra(\"key\") String extra;", " }", "}")); Compilation compilation = javac().withProcessors(extraBinderProcessorsWithoutParceler()).compile(source); assertThat(compilation) .hadErrorContaining("DartModel class Inner must not be private, static or abstract.") .inFile(source) .onLine(4); }