From 948fcab51719ad92270ed619147123b54076da15 Mon Sep 17 00:00:00 2001 From: HliasMpGH Date: Fri, 7 Jun 2024 21:09:10 +0300 Subject: [PATCH] improve code coverage Improved the code coverage by testing untested components. --- .../com/squareup/javapoet/JavaFileTest.java | 2 +- .../squareup/javapoet/LineWrapperTest.java | 8 ++++ .../com/squareup/javapoet/MethodSpecTest.java | 46 +++++++++++++++++++ .../com/squareup/javapoet/TypeSpecTest.java | 10 ++-- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/squareup/javapoet/JavaFileTest.java b/src/test/java/com/squareup/javapoet/JavaFileTest.java index cdda641..6bd7adb 100644 --- a/src/test/java/com/squareup/javapoet/JavaFileTest.java +++ b/src/test/java/com/squareup/javapoet/JavaFileTest.java @@ -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()) diff --git a/src/test/java/com/squareup/javapoet/LineWrapperTest.java b/src/test/java/com/squareup/javapoet/LineWrapperTest.java index ba8472c..243d642 100644 --- a/src/test/java/com/squareup/javapoet/LineWrapperTest.java +++ b/src/test/java/com/squareup/javapoet/LineWrapperTest.java @@ -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"); + } } diff --git a/src/test/java/com/squareup/javapoet/MethodSpecTest.java b/src/test/java/com/squareup/javapoet/MethodSpecTest.java index 56cc3b7..87f82a4 100644 --- a/src/test/java/com/squareup/javapoet/MethodSpecTest.java +++ b/src/test/java/com/squareup/javapoet/MethodSpecTest.java @@ -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; @@ -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 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 { } @@ -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("" + + " void foo() {\n}\n" + ); + } + @Test public void ensureTrailingNewline() { MethodSpec methodSpec = MethodSpec.methodBuilder("method") .addCode("codeWithNoNewline();") diff --git a/src/test/java/com/squareup/javapoet/TypeSpecTest.java b/src/test/java/com/squareup/javapoet/TypeSpecTest.java index 57b29ae..02b5746 100644 --- a/src/test/java/com/squareup/javapoet/TypeSpecTest.java +++ b/src/test/java/com/squareup/javapoet/TypeSpecTest.java @@ -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() { @@ -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")