Skip to content
This repository was archived by the owner on Aug 11, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/test/java/com/squareup/javapoet/JavaFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void recordTwoAnnotatedFields() {
AnnotationSpec annotationSpec = AnnotationSpec.builder(ClassName.get("com.squareup.tacos", "Spicy")).build();
String source = JavaFile
.builder("com.squareup.tacos",
TypeSpec.recordBuilder("Taco")
TypeSpec.recordBuilder(ClassName.get("tacos", "Taco")) // use recordBuilder(ClassName)
.addField(FieldSpec.builder(String.class, "name")
.addAnnotation(annotationSpec)
.build())
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/squareup/javapoet/LineWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,12 @@ public final class LineWrapperTest {
lineWrapper.close();
assertThat(out.toString()).isEqualTo("abcde fghi\nklmnopq\nrs\n tuvwxyz1");
}

@Test public void appendSubString() throws Exception {
StringBuffer out = new StringBuffer();
LineWrapper.RecordingAppendable appendable = new LineWrapper.RecordingAppendable(out);
String str = "append the second half of this string";
appendable.append(str, str.length() / 2, str.length());
assertThat(out.toString()).isEqualTo("half of this string");
}
}
46 changes: 46 additions & 0 deletions src/test/java/com/squareup/javapoet/MethodSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.lang.annotation.Target;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -100,6 +101,40 @@ private TypeElement getElement(Class<?> clazz) {
}
}

@Test public void exceptionsAsListAddition() {
ClassName ioException = ClassName.get(IOException.class);
ClassName timeoutException = ClassName.get(TimeoutException.class);
MethodSpec methodSpec = MethodSpec.methodBuilder("doSomething").addExceptions(List.of(
ioException,
timeoutException
)).build();
assertThat(methodSpec.exceptions).isEqualTo(Arrays.asList(ioException, timeoutException));
}

@Test public void commentAddition() {
MethodSpec methodSpec = MethodSpec.methodBuilder("doSomething")
.addComment("this is a comment").build();
assertThat(methodSpec.toString()).isEqualTo(""
+ "void doSomething() {\n"
+ " // this is a comment\n"
+ "}\n"
);
}

@Test public void namedCodeAddition() {
Map<String, Object> map = new LinkedHashMap<>();
map.put("food", "tacos");
map.put("count", 3);
MethodSpec methodSpec = MethodSpec.methodBuilder("doSomething")
.addNamedCode("I ate $count:L $food:L", map).build();

assertThat(methodSpec.toString()).isEqualTo(""
+ "void doSomething() {\n"
+ " I ate 3 tacos\n"
+ "}\n"
);
}

@Target(ElementType.PARAMETER)
@interface Nullable {
}
Expand Down Expand Up @@ -421,6 +456,17 @@ abstract static class AbstractClassWithPrivateAnnotation {
assertThat(builder.build().typeVariables).containsExactly(t);
}

@Test public void typeVariableAsListAddition() {
TypeVariableName t = TypeVariableName.get("T");
TypeVariableName v = TypeVariableName.get("V");
MethodSpec methodSpec = MethodSpec.methodBuilder("foo")
.addTypeVariables(List.of(t, v)).build();

assertThat(methodSpec.toString()).isEqualTo(""
+ "<T, V> void foo() {\n}\n"
);
}

@Test public void ensureTrailingNewline() {
MethodSpec methodSpec = MethodSpec.methodBuilder("method")
.addCode("codeWithNoNewline();")
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/com/squareup/javapoet/TypeSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,18 @@ private TypeElement getElement(Class<?> clazz) {
@Test public void sealedInterface() {
TypeSpec taco = TypeSpec.interfaceBuilder("Taco")
.addModifiers(Modifier.SEALED)
.addPermits(ClassName.get("bobo", "Baba"))
.addPermits(ClassName.get("bobo", "Baba")) // addPermits(TypeName)
.addPermits(List.of(ClassName.get("bobo", "Bibi"))) // addPermits(Iterable)
.build();
assertThat(toString(taco)).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "import bobo.Baba;\n"
+ "import bobo.Bibi;\n"
+ "\n"
+ "sealed interface Taco permits Baba {\n"
+ "sealed interface Taco permits Baba, Bibi {\n"
+ "}\n");
assertEquals(126258766, taco.hashCode()); // update expected number if source changes
assertEquals(691467292, taco.hashCode()); // update expected number if source changes
}

@Test public void disallowSealedMethods() {
Expand Down Expand Up @@ -2665,7 +2667,7 @@ public void modifyOriginatingElements() {
builder.originatingElements.clear();
assertThat(builder.build().originatingElements).isEmpty();
}

@Test public void javadocWithTrailingLineDoesNotAddAnother() {
TypeSpec spec = TypeSpec.classBuilder("Taco")
.addJavadoc("Some doc with a newline\n")
Expand Down