xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/SourceFilesTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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.binding.SourceFiles.simpleVariableName;
21 
22 import com.squareup.javapoet.ClassName;
23 import dagger.internal.codegen.binding.SourceFiles;
24 import java.util.List;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 /** Tests for {@link SourceFiles}. */
30 @RunWith(JUnit4.class)
31 public final class SourceFilesTest {
32   private static final class Int {}
33 
34   @Test
testSimpleVariableName_typeCollisions()35   public void testSimpleVariableName_typeCollisions() {
36     // a handful of boxed types
37     assertThat(simpleVariableName(ClassName.get(Long.class))).isEqualTo("l");
38     assertThat(simpleVariableName(ClassName.get(Double.class))).isEqualTo("d");
39     // not a boxed type type, but a custom type might collide
40     assertThat(simpleVariableName(ClassName.get(Int.class))).isEqualTo("i");
41     // void is the weird pseudo-boxed type
42     assertThat(simpleVariableName(ClassName.get(Void.class))).isEqualTo("v");
43     // reflective types
44     assertThat(simpleVariableName(ClassName.get(Class.class))).isEqualTo("clazz");
45     assertThat(simpleVariableName(ClassName.get(Package.class))).isEqualTo("pkg");
46   }
47 
48   private static final class For {}
49 
50   private static final class Goto {}
51 
52   @Test
testSimpleVariableName_randomKeywords()53   public void testSimpleVariableName_randomKeywords() {
54     assertThat(simpleVariableName(ClassName.get(For.class))).isEqualTo("for_");
55     assertThat(simpleVariableName(ClassName.get(Goto.class))).isEqualTo("goto_");
56   }
57 
58   @Test
testSimpleVariableName()59   public void testSimpleVariableName() {
60     assertThat(simpleVariableName(ClassName.get(Object.class))).isEqualTo("object");
61     assertThat(simpleVariableName(ClassName.get(List.class))).isEqualTo("list");
62   }
63 }
64