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.javapoet;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.auto.common.MoreTypes;
22 import com.google.testing.compile.CompilationRule;
23 import dagger.internal.codegen.langmodel.DaggerElements;
24 import dagger.internal.codegen.langmodel.DaggerTypes;
25 import javax.lang.model.type.PrimitiveType;
26 import javax.lang.model.type.TypeKind;
27 import javax.lang.model.type.TypeMirror;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 @RunWith(JUnit4.class)
35 public class ExpressionTest {
36   @Rule public CompilationRule compilationRule = new CompilationRule();
37   private DaggerElements elements;
38   private DaggerTypes types;
39 
40   interface Supertype {}
41 
42   interface Subtype extends Supertype {}
43 
44   @Before
setUp()45   public void setUp() {
46     elements = new DaggerElements(compilationRule.getElements(), compilationRule.getTypes());
47     types = new DaggerTypes(compilationRule.getTypes(), elements);
48   }
49 
50   @Test
castTo()51   public void castTo() {
52     TypeMirror subtype = type(Subtype.class);
53     TypeMirror supertype = type(Supertype.class);
54     Expression expression = Expression.create(subtype, "new $T() {}", subtype);
55 
56     Expression castTo = expression.castTo(supertype);
57 
58     assertThat(castTo.type()).isSameInstanceAs(supertype);
59     assertThat(castTo.codeBlock().toString())
60         .isEqualTo(
61             "(dagger.internal.codegen.javapoet.ExpressionTest.Supertype) "
62                 + "new dagger.internal.codegen.javapoet.ExpressionTest.Subtype() {}");
63   }
64 
65   @Test
box()66   public void box() {
67     PrimitiveType primitiveInt = types.getPrimitiveType(TypeKind.INT);
68 
69     Expression primitiveExpression = Expression.create(primitiveInt, "5");
70     Expression boxedExpression = primitiveExpression.box(types);
71 
72     assertThat(boxedExpression.codeBlock().toString()).isEqualTo("(java.lang.Integer) 5");
73     assertThat(MoreTypes.equivalence().equivalent(boxedExpression.type(), type(Integer.class)))
74         .isTrue();
75   }
76 
type(Class<?> clazz)77   private TypeMirror type(Class<?> clazz) {
78     return elements.getTypeElement(clazz).asType();
79   }
80 }
81