aboutsummaryrefslogtreecommitdiff
path: root/value/src/test/java/com/google
diff options
context:
space:
mode:
authorÉamonn McManus <emcmanus@google.com>2021-03-16 21:28:38 -0700
committerGoogle Java Core Libraries <java-libraries-firehose+copybara@google.com>2021-03-16 21:29:10 -0700
commit5ac55fd3f85e4da6ee3e8d3aba52fa491b0c90cd (patch)
treea811ead8be2abcccf3603f45dfa4d0643e3d6c64 /value/src/test/java/com/google
parent8651aed278f9c1d2be50e4d6ad59614600de9b7b (diff)
downloadauto-5ac55fd3f85e4da6ee3e8d3aba52fa491b0c90cd.tar.gz
Add a simple ad-hoc indentation engine to the reformatting pass.
The background here is that AutoValue and its cousins produce code using Velocity templates, and then run a reformatter over the result to fix up various whitespace problems. That works fine except that it's very difficult to manage indentation in templates. For example, given something like this: ``` ${class}( #foreach ($p in $properties) $p.type $p.name #if ($foreach.hasNext) , #end #end ) { ``` Velocity deletes newlines after directives like `#foreach`, so we will get the spaces before `#foreach` at the start of the first parameter line and we'll get the spaces before `#end` at the start of the line beginning with `)`. This particular case can be fixed by adding a newline after the `#foreach` and after the `#end`. But it's very tricky to manage all these extra blank lines. What this change does is to replace whatever initial space comes out of the template with the correct indentation that we compute. That should allow us to make the templates a lot more readable by deleting the weird blank lines. The example above should work correctly without needing them. RELNOTES=n/a PiperOrigin-RevId: 363340310
Diffstat (limited to 'value/src/test/java/com/google')
-rw-r--r--value/src/test/java/com/google/auto/value/processor/ReformatterTest.java69
1 files changed, 61 insertions, 8 deletions
diff --git a/value/src/test/java/com/google/auto/value/processor/ReformatterTest.java b/value/src/test/java/com/google/auto/value/processor/ReformatterTest.java
index 98d31b48..48ecf5be 100644
--- a/value/src/test/java/com/google/auto/value/processor/ReformatterTest.java
+++ b/value/src/test/java/com/google/auto/value/processor/ReformatterTest.java
@@ -15,7 +15,7 @@
*/
package com.google.auto.value.processor;
-import static org.junit.Assert.assertEquals;
+import static com.google.common.truth.Truth.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -29,7 +29,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class ReformatterTest {
@Test
- public void testSimple() {
+ public void simple() {
String input =
"\n"
+ "package com.latin.declension; \n"
@@ -50,7 +50,7 @@ public class ReformatterTest {
+ "\n"
+ " Eodem ( Eadem eodem ) { }\n";
String output =
- "package com.latin.declension;\n"
+ "package com.latin.declension;\n"
+ "\n"
+ "public class Idem {\n"
+ "\n"
@@ -62,11 +62,11 @@ public class ReformatterTest {
+ " }\n"
+ "\n"
+ " Eodem (Eadem eodem) { }\n";
- assertEquals(output, Reformatter.fixup(input));
+ assertThat(Reformatter.fixup(input)).isEqualTo(output);
}
@Test
- public void testSpecialSpaces() {
+ public void specialSpaces() {
String input =
"\n"
+ "package com.example.whatever;\n"
@@ -79,7 +79,7 @@ public class ReformatterTest {
+ " static final char QUOTE2 = '\\\"' ;\n"
+ "}\n";
String output =
- "package com.example.whatever;\n"
+ "package com.example.whatever;\n"
+ "\n"
+ "public class SomeClass {\n"
+ " static final String STRING = \" hello world \\n\";\n"
@@ -88,13 +88,66 @@ public class ReformatterTest {
+ " static final char QUOTE = '\"';\n"
+ " static final char QUOTE2 = '\\\"';\n"
+ "}\n";
- assertEquals(output, Reformatter.fixup(input));
+ assertThat(Reformatter.fixup(input)).isEqualTo(output);
}
@Test
public void noTrailingNewline() {
String input = "package com.example.whatever;\n\npublic class SomeClass {}";
String output = input + "\n";
- assertEquals(output, Reformatter.fixup(input));
+ assertThat(Reformatter.fixup(input)).isEqualTo(output);
+ }
+
+ @Test
+ public void indent() {
+ String input =
+ " class Test {\n"
+ + "private final int field;\n"
+ + "\n"
+ + "Test(\n"
+ + "@Interesting Integer field,\n"
+ + "boolean ignored) {\n"
+ + "this.field = field;\n"
+ + "}\n"
+ + "\n"
+ + "@Override\n"
+ + "public boolean equals(Object x) {\n"
+ + "return x instanceof Test\n"
+ + "&& ((Test) x).field == field;\n"
+ + "// interesting\n"
+ + "}\n"
+ + "\n"
+ + "@Override\n"
+ + "public String toString() {\n"
+ + "return \"Test{\"\n"
+ + "+ \"field=\" + field\n"
+ + "+ \"}\";\n"
+ + "}\n"
+ + "}\n";
+ String output =
+ "class Test {\n"
+ + " private final int field;\n"
+ + "\n"
+ + " Test(\n"
+ + " @Interesting Integer field,\n"
+ + " boolean ignored) {\n"
+ + " this.field = field;\n"
+ + " }\n"
+ + "\n"
+ + " @Override\n"
+ + " public boolean equals(Object x) {\n"
+ + " return x instanceof Test\n"
+ + " && ((Test) x).field == field;\n"
+ + " // interesting\n"
+ + " }\n"
+ + "\n"
+ + " @Override\n"
+ + " public String toString() {\n"
+ + " return \"Test{\"\n"
+ + " + \"field=\" + field\n"
+ + " + \"}\";\n"
+ + " }\n"
+ + "}\n";
+ assertThat(Reformatter.fixup(input)).isEqualTo(output);
}
}