1 /* 2 * Copyright (C) 2019 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.hilt.processor.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.squareup.javapoet.ClassName; 22 import com.squareup.javapoet.MethodSpec; 23 import com.squareup.javapoet.ParameterizedTypeName; 24 import com.squareup.javapoet.TypeVariableName; 25 import java.util.List; 26 import javax.lang.model.element.Modifier; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 @RunWith(JUnit4.class) 32 public class ProcessorsTest { 33 34 @Test testCopyMethodSpecWithoutBodyForMethod()35 public void testCopyMethodSpecWithoutBodyForMethod() throws Exception { 36 TypeVariableName t = TypeVariableName.get("T", Number.class); 37 ClassName list = ClassName.get(List.class); 38 MethodSpec.Builder builder = MethodSpec.methodBuilder("myMethod") 39 .addJavadoc("This is a test line 1 in the java doc \n" 40 + "This is a test line 2 in the java doc \n" 41 + "<p>Test Use of links {@link $T} \n", list) 42 .addAnnotation(Override.class) 43 .addModifiers(Modifier.PUBLIC) 44 .addParameter(ParameterizedTypeName.get(list, t), "list", Modifier.FINAL) 45 .returns(ParameterizedTypeName.get(list, t)) 46 .addException(IllegalArgumentException.class) 47 .addTypeVariable(t); 48 49 // Test that method copy is the same as original 50 MethodSpec method = builder.build(); 51 MethodSpec methodCopy = Processors.copyMethodSpecWithoutBody(method).build(); 52 assertThat(method.toString()).contains(methodCopy.toString()); 53 54 // Test that method copy removes the body, compare with the old copy 55 MethodSpec methodWithBody = builder.addStatement("return list").build(); 56 MethodSpec methodCopyWithBody = Processors.copyMethodSpecWithoutBody(methodWithBody).build(); 57 assertThat(methodCopyWithBody.toString()).isEqualTo(methodCopy.toString()); 58 59 } 60 61 @Test testCopyMethodSpecWithoutBodyForConstructor()62 public void testCopyMethodSpecWithoutBodyForConstructor() throws Exception { 63 TypeVariableName t = TypeVariableName.get("T", Number.class); 64 ClassName list = ClassName.get(List.class); 65 MethodSpec.Builder builder = MethodSpec.constructorBuilder() 66 .addJavadoc("This is a test line 1 in the java doc \n" 67 + "This is a test line 2 in the java doc \n" 68 + "<p>Test Use of links {@link $T} \n", list) 69 .addAnnotation(Override.class) 70 .addModifiers(Modifier.PUBLIC) 71 .addParameter(ParameterizedTypeName.get(list, t), "list", Modifier.FINAL) 72 .addException(IllegalArgumentException.class) 73 .addTypeVariable(t); 74 75 // Test that method copy is the same as original 76 MethodSpec method = builder.build(); 77 MethodSpec methodCopy = Processors.copyMethodSpecWithoutBody(method).build(); 78 assertThat(method.toString()).contains(methodCopy.toString()); 79 80 // Test that method copy removes the body, compare with the old copy 81 MethodSpec methodWithBody = builder.addStatement("this.list = list").build(); 82 MethodSpec methodCopyWithBody = Processors.copyMethodSpecWithoutBody(methodWithBody).build(); 83 assertThat(methodCopyWithBody.toString()).isEqualTo(methodCopy.toString()); 84 } 85 } 86