1 /* 2 * Copyright (C) 2017 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; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static dagger.internal.codegen.SourceFiles.simpleVariableName; 21 22 import com.google.testing.compile.CompilationRule; 23 import java.util.List; 24 import javax.lang.model.element.TypeElement; 25 import org.junit.Rule; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 /** Tests for {@link SourceFiles}. */ 31 @RunWith(JUnit4.class) 32 public final class SourceFilesTest { 33 @Rule public CompilationRule compilation = new CompilationRule(); 34 typeElementFor(Class<?> clazz)35 private TypeElement typeElementFor(Class<?> clazz) { 36 return compilation.getElements().getTypeElement(clazz.getCanonicalName()); 37 } 38 39 private static final class Int {} 40 41 @Test testSimpleVariableName_typeCollisions()42 public void testSimpleVariableName_typeCollisions() { 43 // a handful of boxed types 44 assertThat(simpleVariableName(typeElementFor(Long.class))).isEqualTo("l"); 45 assertThat(simpleVariableName(typeElementFor(Double.class))).isEqualTo("d"); 46 // not a boxed type type, but a custom type might collide 47 assertThat(simpleVariableName(typeElementFor(Int.class))).isEqualTo("i"); 48 // void is the weird pseudo-boxed type 49 assertThat(simpleVariableName(typeElementFor(Void.class))).isEqualTo("v"); 50 // reflective types 51 assertThat(simpleVariableName(typeElementFor(Class.class))).isEqualTo("clazz"); 52 assertThat(simpleVariableName(typeElementFor(Package.class))).isEqualTo("pkg"); 53 } 54 55 private static final class For {} 56 57 private static final class Goto {} 58 59 @Test testSimpleVariableName_randomKeywords()60 public void testSimpleVariableName_randomKeywords() { 61 assertThat(simpleVariableName(typeElementFor(For.class))).isEqualTo("for_"); 62 assertThat(simpleVariableName(typeElementFor(Goto.class))).isEqualTo("goto_"); 63 } 64 65 @Test testSimpleVariableName()66 public void testSimpleVariableName() { 67 assertThat(simpleVariableName(typeElementFor(Object.class))).isEqualTo("object"); 68 assertThat(simpleVariableName(typeElementFor(List.class))).isEqualTo("list"); 69 } 70 } 71