xref: /aosp_15_r20/external/dagger2/javatests/dagger/internal/codegen/ExpressionTest.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.xprocessing.XProcessingEnvs.getPrimitiveIntType;
21 
22 import androidx.room.compiler.processing.XProcessingEnv;
23 import androidx.room.compiler.processing.XType;
24 import com.google.testing.compile.CompilationRule;
25 import com.squareup.javapoet.ClassName;
26 import dagger.Component;
27 import dagger.internal.codegen.javac.JavacPluginModule;
28 import dagger.internal.codegen.javapoet.Expression;
29 import javax.inject.Inject;
30 import javax.inject.Singleton;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 @RunWith(JUnit4.class)
38 public class ExpressionTest {
39   @Rule public CompilationRule compilationRule = new CompilationRule();
40 
41   @Inject XProcessingEnv processingEnv;
42 
43   interface Supertype {}
44 
45   interface Subtype extends Supertype {}
46 
47   @Before
setUp()48   public void setUp() {
49     DaggerExpressionTest_TestComponent.builder()
50         .javacPluginModule(
51             new JavacPluginModule(compilationRule.getElements(), compilationRule.getTypes()))
52         .build()
53         .inject(this);
54   }
55 
56   @Test
castTo()57   public void castTo() {
58     XType subtype = type(Subtype.class);
59     XType supertype = type(Supertype.class);
60     Expression expression = Expression.create(subtype, "new $T() {}", subtype.getTypeName());
61 
62     Expression castTo = expression.castTo(supertype);
63 
64     assertThat(castTo.type().getTypeName()).isEqualTo(supertype.getTypeName());
65     assertThat(castTo.codeBlock().toString())
66         .isEqualTo(
67             "(dagger.internal.codegen.ExpressionTest.Supertype) "
68                 + "new dagger.internal.codegen.ExpressionTest.Subtype() {}");
69   }
70 
71   @Test
box()72   public void box() {
73     XType primitiveInt = getPrimitiveIntType(processingEnv);
74 
75     Expression primitiveExpression = Expression.create(primitiveInt, "5");
76     Expression boxedExpression = primitiveExpression.box();
77 
78     assertThat(boxedExpression.codeBlock().toString()).isEqualTo("(java.lang.Integer) 5");
79     assertThat(boxedExpression.type().getTypeName()).isEqualTo(type(Integer.class).getTypeName());
80   }
81 
type(Class<?> clazz)82   private XType type(Class<?> clazz) {
83     return processingEnv.requireType(ClassName.get(clazz));
84   }
85 
86   @Singleton
87   @Component(modules = JavacPluginModule.class)
88   interface TestComponent {
inject(ExpressionTest test)89     void inject(ExpressionTest test);
90   }
91 }
92