aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/squareup/javapoet/TypeSpecTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com/squareup/javapoet/TypeSpecTest.java')
-rw-r--r--src/test/java/com/squareup/javapoet/TypeSpecTest.java199
1 files changed, 197 insertions, 2 deletions
diff --git a/src/test/java/com/squareup/javapoet/TypeSpecTest.java b/src/test/java/com/squareup/javapoet/TypeSpecTest.java
index 9cd22c2..0f67c5c 100644
--- a/src/test/java/com/squareup/javapoet/TypeSpecTest.java
+++ b/src/test/java/com/squareup/javapoet/TypeSpecTest.java
@@ -17,6 +17,7 @@ package com.squareup.javapoet;
import com.google.common.collect.ImmutableMap;
import com.google.testing.compile.CompilationRule;
+import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
@@ -985,6 +986,74 @@ public final class TypeSpecTest {
+ "}\n");
}
+ @Test public void simpleNameConflictsWithTypeVariable() {
+ ClassName inPackage = ClassName.get("com.squareup.tacos", "InPackage");
+ ClassName otherType = ClassName.get("com.other", "OtherType");
+ ClassName methodInPackage = ClassName.get("com.squareup.tacos", "MethodInPackage");
+ ClassName methodOtherType = ClassName.get("com.other", "MethodOtherType");
+ TypeSpec gen = TypeSpec.classBuilder("Gen")
+ .addTypeVariable(TypeVariableName.get("InPackage"))
+ .addTypeVariable(TypeVariableName.get("OtherType"))
+ .addField(FieldSpec.builder(inPackage, "inPackage").build())
+ .addField(FieldSpec.builder(otherType, "otherType").build())
+ .addMethod(MethodSpec.methodBuilder("withTypeVariables")
+ .addTypeVariable(TypeVariableName.get("MethodInPackage"))
+ .addTypeVariable(TypeVariableName.get("MethodOtherType"))
+ .addStatement("$T inPackage = null", methodInPackage)
+ .addStatement("$T otherType = null", methodOtherType)
+ .build())
+ .addMethod(MethodSpec.methodBuilder("withoutTypeVariables")
+ .addStatement("$T inPackage = null", methodInPackage)
+ .addStatement("$T otherType = null", methodOtherType)
+ .build())
+ .addMethod(MethodSpec.methodBuilder("againWithTypeVariables")
+ .addTypeVariable(TypeVariableName.get("MethodInPackage"))
+ .addTypeVariable(TypeVariableName.get("MethodOtherType"))
+ .addStatement("$T inPackage = null", methodInPackage)
+ .addStatement("$T otherType = null", methodOtherType)
+ .build())
+ // https://github.com/square/javapoet/pull/657#discussion_r205514292
+ .addMethod(MethodSpec.methodBuilder("masksEnclosingTypeVariable")
+ .addTypeVariable(TypeVariableName.get("InPackage"))
+ .build())
+ .addMethod(MethodSpec.methodBuilder("hasSimpleNameThatWasPreviouslyMasked")
+ .addStatement("$T inPackage = null", inPackage)
+ .build())
+ .build();
+ assertThat(toString(gen)).isEqualTo(""
+ + "package com.squareup.tacos;\n"
+ + "\n"
+ + "import com.other.MethodOtherType;\n"
+ + "\n"
+ + "class Gen<InPackage, OtherType> {\n"
+ + " com.squareup.tacos.InPackage inPackage;\n"
+ + "\n"
+ + " com.other.OtherType otherType;\n"
+ + "\n"
+ + " <MethodInPackage, MethodOtherType> void withTypeVariables() {\n"
+ + " com.squareup.tacos.MethodInPackage inPackage = null;\n"
+ + " com.other.MethodOtherType otherType = null;\n"
+ + " }\n"
+ + "\n"
+ + " void withoutTypeVariables() {\n"
+ + " MethodInPackage inPackage = null;\n"
+ + " MethodOtherType otherType = null;\n"
+ + " }\n"
+ + "\n"
+ + " <MethodInPackage, MethodOtherType> void againWithTypeVariables() {\n"
+ + " com.squareup.tacos.MethodInPackage inPackage = null;\n"
+ + " com.other.MethodOtherType otherType = null;\n"
+ + " }\n"
+ + "\n"
+ + " <InPackage> void masksEnclosingTypeVariable() {\n"
+ + " }\n"
+ + "\n"
+ + " void hasSimpleNameThatWasPreviouslyMasked() {\n"
+ + " com.squareup.tacos.InPackage inPackage = null;\n"
+ + " }\n"
+ + "}\n");
+ }
+
@Test public void originatingElementsIncludesThoseOfNestedTypes() {
Element outerElement = Mockito.mock(Element.class);
Element innerElement = Mockito.mock(Element.class);
@@ -1780,7 +1849,8 @@ public final class TypeSpecTest {
+ " }\n"
+ "\n"
+ " /**\n"
- + " * chosen by fair dice roll ;) */\n"
+ + " * chosen by fair dice roll ;)\n"
+ + " */\n"
+ " public int getRandomQuantity() {\n"
+ " return 4;\n"
+ " }\n"
@@ -1839,7 +1909,7 @@ public final class TypeSpecTest {
@Test public void nullModifiersAddition() {
try {
- TypeSpec.classBuilder("Taco").addModifiers((Modifier) null);
+ TypeSpec.classBuilder("Taco").addModifiers((Modifier) null).build();
fail();
} catch(IllegalArgumentException expected) {
assertThat(expected.getMessage())
@@ -2196,6 +2266,7 @@ public final class TypeSpecTest {
@Test public void initializersToBuilder() {
// Tests if toBuilder() contains correct static and instance initializers
+ Element originatingElement = getElement(TypeSpecTest.class);
TypeSpec taco = TypeSpec.classBuilder("Taco")
.addField(String.class, "foo", Modifier.PRIVATE)
.addField(String.class, "FOO", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
@@ -2212,10 +2283,16 @@ public final class TypeSpecTest {
.addInitializerBlock(CodeBlock.builder()
.addStatement("foo = $S", "FOO")
.build())
+ .addOriginatingElement(originatingElement)
+ .alwaysQualify("com.example.AlwaysQualified")
.build();
TypeSpec recreatedTaco = taco.toBuilder().build();
assertThat(toString(taco)).isEqualTo(toString(recreatedTaco));
+ assertThat(taco.originatingElements)
+ .containsExactlyElementsIn(recreatedTaco.originatingElements);
+ assertThat(taco.alwaysQualifiedNames)
+ .containsExactlyElementsIn(recreatedTaco.alwaysQualifiedNames);
TypeSpec initializersAdded = taco.toBuilder()
.addInitializerBlock(CodeBlock.builder()
@@ -2356,4 +2433,122 @@ public final class TypeSpecTest {
assertThat(TypeSpec.enumBuilder(className).addEnumConstant("A").build().name).isEqualTo("Example");
assertThat(TypeSpec.annotationBuilder(className).build().name).isEqualTo("Example");
}
+
+ @Test
+ public void modifyAnnotations() {
+ TypeSpec.Builder builder =
+ TypeSpec.classBuilder("Taco")
+ .addAnnotation(Override.class)
+ .addAnnotation(SuppressWarnings.class);
+
+ builder.annotations.remove(1);
+ assertThat(builder.build().annotations).hasSize(1);
+ }
+
+ @Test
+ public void modifyModifiers() {
+ TypeSpec.Builder builder =
+ TypeSpec.classBuilder("Taco").addModifiers(Modifier.PUBLIC, Modifier.FINAL);
+
+ builder.modifiers.remove(1);
+ assertThat(builder.build().modifiers).containsExactly(Modifier.PUBLIC);
+ }
+
+ @Test
+ public void modifyFields() {
+ TypeSpec.Builder builder = TypeSpec.classBuilder("Taco")
+ .addField(int.class, "source");
+
+ builder.fieldSpecs.remove(0);
+ assertThat(builder.build().fieldSpecs).isEmpty();
+ }
+
+ @Test
+ public void modifyTypeVariables() {
+ TypeVariableName t = TypeVariableName.get("T");
+ TypeSpec.Builder builder =
+ TypeSpec.classBuilder("Taco")
+ .addTypeVariable(t)
+ .addTypeVariable(TypeVariableName.get("V"));
+
+ builder.typeVariables.remove(1);
+ assertThat(builder.build().typeVariables).containsExactly(t);
+ }
+
+ @Test
+ public void modifySuperinterfaces() {
+ TypeSpec.Builder builder = TypeSpec.classBuilder("Taco")
+ .addSuperinterface(File.class);
+
+ builder.superinterfaces.clear();
+ assertThat(builder.build().superinterfaces).isEmpty();
+ }
+
+ @Test
+ public void modifyMethods() {
+ TypeSpec.Builder builder = TypeSpec.classBuilder("Taco")
+ .addMethod(MethodSpec.methodBuilder("bell").build());
+
+ builder.methodSpecs.clear();
+ assertThat(builder.build().methodSpecs).isEmpty();
+ }
+
+ @Test
+ public void modifyTypes() {
+ TypeSpec.Builder builder = TypeSpec.classBuilder("Taco")
+ .addType(TypeSpec.classBuilder("Bell").build());
+
+ builder.typeSpecs.clear();
+ assertThat(builder.build().typeSpecs).isEmpty();
+ }
+
+ @Test
+ public void modifyEnumConstants() {
+ TypeSpec constantType = TypeSpec.anonymousClassBuilder("").build();
+ TypeSpec.Builder builder = TypeSpec.enumBuilder("Taco")
+ .addEnumConstant("BELL", constantType)
+ .addEnumConstant("WUT", TypeSpec.anonymousClassBuilder("").build());
+
+ builder.enumConstants.remove("WUT");
+ assertThat(builder.build().enumConstants).containsExactly("BELL", constantType);
+ }
+
+ @Test
+ public void modifyOriginatingElements() {
+ TypeSpec.Builder builder = TypeSpec.classBuilder("Taco")
+ .addOriginatingElement(Mockito.mock(Element.class));
+
+ builder.originatingElements.clear();
+ assertThat(builder.build().originatingElements).isEmpty();
+ }
+
+ @Test public void javadocWithTrailingLineDoesNotAddAnother() {
+ TypeSpec spec = TypeSpec.classBuilder("Taco")
+ .addJavadoc("Some doc with a newline\n")
+ .build();
+
+ assertThat(toString(spec)).isEqualTo(""
+ + "package com.squareup.tacos;\n"
+ + "\n"
+ + "/**\n"
+ + " * Some doc with a newline\n"
+ + " */\n"
+ + "class Taco {\n"
+ + "}\n");
+ }
+
+ @Test public void javadocEnsuresTrailingLine() {
+ TypeSpec spec = TypeSpec.classBuilder("Taco")
+ .addJavadoc("Some doc with a newline")
+ .build();
+
+ assertThat(toString(spec)).isEqualTo(""
+ + "package com.squareup.tacos;\n"
+ + "\n"
+ + "/**\n"
+ + " * Some doc with a newline\n"
+ + " */\n"
+ + "class Taco {\n"
+ + "}\n");
+ }
}