From e5c097f9e99b710431afd8fef923575476c6b7d8 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 13 Jan 2025 10:13:13 -0800 Subject: [PATCH] Add `lines()` and `stripped()` helpers to GwtIncompatibleStripperTest Hopefully this will make the tests a bit easier to read by hiding some of the tricks from the test cases themselves. PiperOrigin-RevId: 715016726 --- .../GwtIncompatibleStripperTest.java | 358 +++++++++--------- 1 file changed, 173 insertions(+), 185 deletions(-) diff --git a/tools/javatests/com/google/j2cl/tools/gwtincompatible/GwtIncompatibleStripperTest.java b/tools/javatests/com/google/j2cl/tools/gwtincompatible/GwtIncompatibleStripperTest.java index b4d3b644a4..ffb1428b7f 100644 --- a/tools/javatests/com/google/j2cl/tools/gwtincompatible/GwtIncompatibleStripperTest.java +++ b/tools/javatests/com/google/j2cl/tools/gwtincompatible/GwtIncompatibleStripperTest.java @@ -15,15 +15,14 @@ */ package com.google.j2cl.tools.gwtincompatible; +import static com.google.common.base.Preconditions.checkArgument; import static org.junit.Assert.assertEquals; import com.google.common.base.Joiner; -import com.google.common.base.Strings; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Tests for {@link JavaPreprocessor}. */ @RunWith(JUnit4.class) public class GwtIncompatibleStripperTest { @@ -48,259 +47,248 @@ public void testNoProcessString() { @Test public void testProcessClass() { String before = - Joiner.on("\n") - .join( - "import a.b.X;", - "@GwtIncompatible", - "public class Foo {", - " public X m() {return null;}", - "}"); + lines( + "import a.b.X;", + "@GwtIncompatible", + "public class Foo {", + " public X m() {return null;}", + "}"); String after = - Joiner.on("\n") - .join( - Strings.repeat(" ", "import a.b.X;".length()), - Strings.repeat(" ", "@GwtIncompatible".length()), - Strings.repeat(" ", "public class Foo {".length()), - Strings.repeat(" ", " public X m() {return null;}".length()), - Strings.repeat(" ", "}".length())); + lines( + stripped("import a.b.X;"), + stripped("@GwtIncompatible"), + stripped("public class Foo {"), + stripped(" public X m() {return null;}"), + stripped("}")); assertEquals(after, GwtIncompatibleStripper.strip(before, "GwtIncompatible")); } @Test public void testProcessJ2ktIncompatibleClass() { String before = - Joiner.on("\n") - .join( - "import a.b.X;", - "@J2ktIncompatible", - "public class Foo {", - " public X m() {return null;}", - "}"); + lines( + "import a.b.X;", + "@J2ktIncompatible", + "public class Foo {", + " public X m() {return null;}", + "}"); String after = - Joiner.on("\n") - .join( - Strings.repeat(" ", "import a.b.X;".length()), - Strings.repeat(" ", "@J2ktIncompatible".length()), - Strings.repeat(" ", "public class Foo {".length()), - Strings.repeat(" ", " public X m() {return null;}".length()), - Strings.repeat(" ", "}".length())); + lines( + stripped("import a.b.X;"), + stripped("@J2ktIncompatible"), + stripped("public class Foo {"), + stripped(" public X m() {return null;}"), + stripped("}")); assertEquals(after, GwtIncompatibleStripper.strip(before, "J2ktIncompatible")); } @Test public void testProcessMultipleAnnotatons() { String before = - Joiner.on("\n") - .join( - "import a.b.X;", - "@GwtIncompatible", - "@J2ktIncompatible", - "public class Foo {", - " public X m() {return null;}", - "}"); + lines( + "import a.b.X;", + "@GwtIncompatible", + "@J2ktIncompatible", + "public class Foo {", + " public X m() {return null;}", + "}"); String after = - Joiner.on("\n") - .join( - Strings.repeat(" ", "import a.b.X;".length()), - Strings.repeat(" ", "@GwtIncompatible".length()), - Strings.repeat(" ", "@J2ktIncompatible".length()), - Strings.repeat(" ", "public class Foo {".length()), - Strings.repeat(" ", " public X m() {return null;}".length()), - Strings.repeat(" ", "}".length())); + lines( + stripped("import a.b.X;"), + stripped("@GwtIncompatible"), + stripped("@J2ktIncompatible"), + stripped("public class Foo {"), + stripped(" public X m() {return null;}"), + stripped("}")); assertEquals(after, GwtIncompatibleStripper.strip(before, "J2ktIncompatible")); } @Test public void testProcessMethod() { String before = - Joiner.on("\n") - .join( - "import a.b.C;", - "import a.b.D;", - "public class Foo {", - " public C m() {}", - " @Nullable", - " @GwtIncompatible", - " public D n() {}", - "}"); + lines( + "import a.b.C;", + "import a.b.D;", + "public class Foo {", + " public C m() {}", + " @Nullable", + " @GwtIncompatible", + " public D n() {}", + "}"); String after = - Joiner.on("\n") - .join( - "import a.b.C;", - Strings.repeat(" ", "import a.b.D;".length()), - "public class Foo {", - " public C m() {}", - Strings.repeat(" ", " @Nullable".length()), - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " public D n() {}".length()), - "}"); + lines( + "import a.b.C;", + stripped("import a.b.D;"), + "public class Foo {", + " public C m() {}", + stripped(" @Nullable"), + stripped(" @GwtIncompatible"), + stripped(" public D n() {}"), + "}"); assertEquals(after, GwtIncompatibleStripper.strip(before, "GwtIncompatible")); } @Test public void testProcessField() { String before = - Joiner.on("\n") - .join( - "public class Foo {", - " public String a;", - " @GwtIncompatible", - " public String b;", - " public String c;", - "}"); + lines( + "public class Foo {", + " public String a;", + " @GwtIncompatible", + " public String b;", + " public String c;", + "}"); String after = - Joiner.on("\n") - .join( - "public class Foo {", - " public String a;", - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " public String b;".length()), - " public String c;", - "}"); + lines( + "public class Foo {", + " public String a;", + stripped(" @GwtIncompatible"), + stripped(" public String b;"), + " public String c;", + "}"); assertEquals(after, GwtIncompatibleStripper.strip(before, "GwtIncompatible")); } @Test public void testProcessEnumConstant() { - String before = - Joiner.on("\n") - .join("public enum Foo {", " A,", " @GwtIncompatible", " B,", " C;", "}"); + String before = lines("public enum Foo {", " A,", " @GwtIncompatible", " B,", " C;", "}"); String after = - Joiner.on("\n") - .join( - "public enum Foo {", - " A,", - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " B,".length()), - " C;", - "}"); + lines( + "public enum Foo {", + " A,", + stripped(" @GwtIncompatible"), + stripped(" B,"), + " C;", + "}"); assertEquals(after, GwtIncompatibleStripper.strip(before, "GwtIncompatible")); } @Test public void testProcessAnnotationMember() { String before = - Joiner.on("\n") - .join( - "public @interface Foo {", - " public String m();", - " @GwtIncompatible", - " public String n();", - "}"); + lines( + "public @interface Foo {", + " public String m();", + " @GwtIncompatible", + " public String n();", + "}"); String after = - Joiner.on("\n") - .join( - "public @interface Foo {", - " public String m();", - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " public String n();".length()), - "}"); + lines( + "public @interface Foo {", + " public String m();", + stripped(" @GwtIncompatible"), + stripped(" public String n();"), + "}"); assertEquals(after, GwtIncompatibleStripper.strip(before, "GwtIncompatible")); } @Test public void testProcessMultiple() { String before = - Joiner.on("\n") - .join( - "public class Foo {", - " @GwtIncompatible", - " public void m();", - " public String c;", - " @GwtIncompatible", - " public void n();", - " public void o();", - " @GwtIncompatible", - " private static class X {", - " void q() {}", - " @GwtIncompatible", - " void r() {}", - " }", - " String s;", - "}"); + lines( + "public class Foo {", + " @GwtIncompatible", + " public void m();", + " public String c;", + " @GwtIncompatible", + " public void n();", + " public void o();", + " @GwtIncompatible", + " private static class X {", + " void q() {}", + " @GwtIncompatible", + " void r() {}", + " }", + " String s;", + "}"); String after = - Joiner.on("\n") - .join( - "public class Foo {", - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " public void m();".length()), - " public String c;", - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " public void n();".length()), - " public void o();", - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " private static class X {".length()), - Strings.repeat(" ", " void q() {}".length()), - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " void r() {}".length()), - Strings.repeat(" ", " }".length()), - " String s;", - "}"); + lines( + "public class Foo {", + stripped(" @GwtIncompatible"), + stripped(" public void m();"), + " public String c;", + stripped(" @GwtIncompatible"), + stripped(" public void n();"), + " public void o();", + stripped(" @GwtIncompatible"), + stripped(" private static class X {"), + stripped(" void q() {}"), + stripped(" @GwtIncompatible"), + stripped(" void r() {}"), + stripped(" }"), + " String s;", + "}"); assertEquals(after, GwtIncompatibleStripper.strip(before, "GwtIncompatible")); } @Test public void testNestedComment() { String before = - Joiner.on("\n") - .join( - "public class Foo {", - " public void m() {}", - " @GwtIncompatible", - " public void n() {foo(x /* the value of x */);}", - "}"); + lines( + "public class Foo {", + " public void m() {}", + " @GwtIncompatible", + " public void n() {foo(x /* the value of x */);}", + "}"); String after = - Joiner.on("\n") - .join( - "public class Foo {", - " public void m() {}", - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " public void n() {foo(x /* the value of x */);}".length()), - "}"); + lines( + "public class Foo {", + " public void m() {}", + stripped(" @GwtIncompatible"), + stripped(" public void n() {foo(x /* the value of x */);}"), + "}"); assertEquals(after, GwtIncompatibleStripper.strip(before, "GwtIncompatible")); } @Test public void testTabs() { String before = - Joiner.on("\n") - .join( - "public class Foo {", - " public A m() {}", - " @GwtIncompatible", - " \tpublic B n() {}", - "}"); + lines( + "public class Foo {", + " public A m() {}", + " @GwtIncompatible", + " \tpublic B n() {}", + "}"); String after = - Joiner.on("\n") - .join( - "public class Foo {", - " public A m() {}", - Strings.repeat(" ", " @GwtIncompatible".length()), - " \t" + Strings.repeat(" ", "public B n() {}".length()), - "}"); + lines( + "public class Foo {", + " public A m() {}", + stripped(" @GwtIncompatible"), + " \t" + stripped("public B n() {}"), + "}"); assertEquals(after, GwtIncompatibleStripper.strip(before, "GwtIncompatible")); } @Test public void testMultibyteChars() { String before = - Joiner.on("\n") - .join( - "public class Foo {", - " public A m() {}", - " @GwtIncompatible", - " //மெ.பை.", - " public B n() {}", - "}"); + lines( + "public class Foo {", + " public A m() {}", + " @GwtIncompatible", + " //மெ.பை.", + " public B n() {}", + "}"); String after = - Joiner.on("\n") - .join( - "public class Foo {", - " public A m() {}", - Strings.repeat(" ", " @GwtIncompatible".length()), - Strings.repeat(" ", " //மெ.பை.".length()), - Strings.repeat(" ", " public B n() {}".length()), - "}"); + lines( + "public class Foo {", + " public A m() {}", + stripped(" @GwtIncompatible"), + stripped(" //மெ.பை."), + stripped(" public B n() {}"), + "}"); assertEquals(after, GwtIncompatibleStripper.strip(before, "GwtIncompatible")); } + + private static String lines(String... lines) { + return LINE_JOINER.join(lines); + } + + private static String stripped(String lineContent) { + checkArgument( + !lineContent.contains("\n"), "stripped() should only be called with one line at a time."); + return " ".repeat(lineContent.length()); + } + + private static final Joiner LINE_JOINER = Joiner.on('\n'); }