1 /* 2 * Copyright (C) 2016 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.internal.codegen.javapoet; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static dagger.internal.codegen.javapoet.CodeBlocks.javadocLinkTo; 21 import static dagger.internal.codegen.javapoet.CodeBlocks.toParametersCodeBlock; 22 import static javax.lang.model.element.ElementKind.METHOD; 23 24 import com.google.testing.compile.CompilationRule; 25 import com.squareup.javapoet.CodeBlock; 26 import java.util.stream.Stream; 27 import javax.lang.model.element.ExecutableElement; 28 import javax.lang.model.util.Elements; 29 import org.junit.Before; 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.junit.runners.JUnit4; 34 35 /** Tests for {@link CodeBlocks}. */ 36 @RunWith(JUnit4.class) 37 public final class CodeBlocksTest { 38 private static final CodeBlock objectO = CodeBlock.of("$T o", Object.class); 39 private static final CodeBlock stringS = CodeBlock.of("$T s", String.class); 40 private static final CodeBlock intI = CodeBlock.of("$T i", int.class); 41 42 @Rule public CompilationRule compilationRule = new CompilationRule(); 43 private Elements elements; 44 45 @Before setUp()46 public void setUp() { 47 this.elements = compilationRule.getElements(); 48 } 49 50 @Test testToParametersCodeBlock()51 public void testToParametersCodeBlock() { 52 assertThat(Stream.of(objectO, stringS, intI).collect(toParametersCodeBlock())) 53 .isEqualTo(CodeBlock.of("$T o, $T s, $T i", Object.class, String.class, int.class)); 54 } 55 56 @Test testToParametersCodeBlock_empty()57 public void testToParametersCodeBlock_empty() { 58 assertThat(Stream.<CodeBlock>of().collect(toParametersCodeBlock())).isEqualTo(CodeBlock.of("")); 59 } 60 61 @Test testToParametersCodeBlock_oneElement()62 public void testToParametersCodeBlock_oneElement() { 63 assertThat(Stream.of(objectO).collect(toParametersCodeBlock())).isEqualTo(objectO); 64 } 65 66 @Test testJavadocLinkTo()67 public void testJavadocLinkTo() { 68 ExecutableElement equals = 69 elements 70 .getTypeElement(Object.class.getCanonicalName()) 71 .getEnclosedElements() 72 .stream() 73 .filter(element -> element.getKind().equals(METHOD)) 74 .map(ExecutableElement.class::cast) 75 .filter(method -> method.getSimpleName().contentEquals("equals")) 76 .findFirst() 77 .get(); 78 assertThat(javadocLinkTo(equals)) 79 .isEqualTo(CodeBlock.of("{@link $T#equals($T)}", Object.class, Object.class)); 80 } 81 } 82