Home | History | Annotate | Download | only in writer
      1 /*
      2  * Copyright (C) 2014 Google, Inc.
      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 package dagger.internal.codegen.writer;
     17 
     18 import com.google.testing.compile.CompilationRule;
     19 import java.nio.charset.Charset;
     20 import java.util.Set;
     21 import javax.lang.model.element.TypeElement;
     22 import javax.lang.model.type.DeclaredType;
     23 import javax.lang.model.type.TypeKind;
     24 import javax.lang.model.type.TypeMirror;
     25 import org.junit.Rule;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 import org.junit.runners.JUnit4;
     29 
     30 import static com.google.common.truth.Truth.assertThat;
     31 
     32 @RunWith(JUnit4.class)
     33 public class TypeNamesTest {
     34   @Rule public final CompilationRule compilation = new CompilationRule();
     35 
     36   private TypeElement getElement(Class<?> clazz) {
     37     return compilation.getElements().getTypeElement(clazz.getCanonicalName());
     38   }
     39 
     40   private TypeMirror getType(Class<?> clazz) {
     41     return getElement(clazz).asType();
     42   }
     43 
     44   @Test
     45   public void forTypeMirror_basicTypes() {
     46     assertThat(TypeNames.forTypeMirror(getType(Object.class)))
     47         .isEqualTo(ClassName.fromClass(Object.class));
     48     assertThat(TypeNames.forTypeMirror(getType(Charset.class)))
     49         .isEqualTo(ClassName.fromClass(Charset.class));
     50     assertThat(TypeNames.forTypeMirror(getType(TypeNamesTest.class)))
     51         .isEqualTo(ClassName.fromClass(TypeNamesTest.class));
     52   }
     53 
     54   @Test
     55   public void forTypeMirror_parameterizedType() {
     56     DeclaredType setType =
     57         compilation.getTypes().getDeclaredType(getElement(Set.class), getType(Object.class));
     58     assertThat(TypeNames.forTypeMirror(setType))
     59         .isEqualTo(ParameterizedTypeName.create(Set.class, ClassName.fromClass(Object.class)));
     60   }
     61 
     62   @Test
     63   public void forTypeMirror_typeVariables() {
     64     TypeMirror setType = getType(Set.class);
     65     assertThat(TypeNames.forTypeMirror(setType))
     66         .isEqualTo(ParameterizedTypeName.create(Set.class, TypeVariableName.named("E")));
     67   }
     68 
     69   @Test
     70   public void forTypeMirror_primitive() {
     71     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.BOOLEAN)))
     72         .isEqualTo(PrimitiveName.BOOLEAN);
     73     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.BYTE)))
     74         .isEqualTo(PrimitiveName.BYTE);
     75     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.SHORT)))
     76         .isEqualTo(PrimitiveName.SHORT);
     77     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.INT)))
     78         .isEqualTo(PrimitiveName.INT);
     79     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.LONG)))
     80         .isEqualTo(PrimitiveName.LONG);
     81     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.CHAR)))
     82         .isEqualTo(PrimitiveName.CHAR);
     83     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.FLOAT)))
     84         .isEqualTo(PrimitiveName.FLOAT);
     85     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.DOUBLE)))
     86         .isEqualTo(PrimitiveName.DOUBLE);
     87   }
     88 
     89   @Test
     90   public void forTypeMirror_arrays() {
     91     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getArrayType(getType(Object.class))))
     92         .isEqualTo(new ArrayTypeName(ClassName.fromClass(Object.class)));
     93   }
     94 
     95   @Test
     96   public void forTypeMirror_void() {
     97     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getNoType(TypeKind.VOID)))
     98         .isEqualTo(VoidName.VOID);
     99   }
    100 
    101   @Test
    102   public void forTypeMirror_null() {
    103     assertThat(TypeNames.forTypeMirror(compilation.getTypes().getNullType()))
    104         .isEqualTo(NullName.NULL);
    105   }
    106 }
    107