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.common.collect.ImmutableList;
     19 import com.google.testing.compile.CompilationRule;
     20 import dagger.internal.codegen.writer.ClassNameTest.OuterClass.InnerClass;
     21 import java.util.Map;
     22 import javax.lang.model.element.TypeElement;
     23 import javax.lang.model.util.Elements;
     24 import org.junit.Rule;
     25 import org.junit.Test;
     26 import org.junit.runner.RunWith;
     27 import org.junit.runners.JUnit4;
     28 
     29 import static com.google.common.truth.Truth.assertThat;
     30 import static org.junit.Assert.fail;
     31 
     32 @RunWith(JUnit4.class)
     33 public class ClassNameTest {
     34   @Rule public CompilationRule compilationRule = new CompilationRule();
     35 
     36   @Test public void bestGuessForString_simpleClass() {
     37     assertThat(ClassName.bestGuessFromString(String.class.getName()))
     38         .isEqualTo(ClassName.create("java.lang", "String"));
     39   }
     40 
     41   static class OuterClass {
     42     static class InnerClass {}
     43   }
     44 
     45   @Test public void bestGuessForString_nestedClass() {
     46     assertThat(ClassName.bestGuessFromString(Map.Entry.class.getCanonicalName()))
     47         .isEqualTo(ClassName.create("java.util", ImmutableList.of("Map"), "Entry"));
     48     assertThat(ClassName.bestGuessFromString(OuterClass.InnerClass.class.getCanonicalName()))
     49         .isEqualTo(
     50             ClassName.create("dagger.internal.codegen.writer",
     51                 ImmutableList.of("ClassNameTest", "OuterClass"), "InnerClass"));
     52   }
     53 
     54   @Test public void bestGuessForString_defaultPackage() {
     55     assertThat(ClassName.bestGuessFromString("SomeClass"))
     56         .isEqualTo(ClassName.create("", "SomeClass"));
     57     assertThat(ClassName.bestGuessFromString("SomeClass.Nested"))
     58         .isEqualTo(ClassName.create("", ImmutableList.of("SomeClass"), "Nested"));
     59     assertThat(ClassName.bestGuessFromString("SomeClass.Nested.EvenMore"))
     60         .isEqualTo(ClassName.create("", ImmutableList.of("SomeClass", "Nested"), "EvenMore"));
     61   }
     62 
     63   @Test public void bestGuessForString_confusingInput() {
     64     try {
     65       ClassName.bestGuessFromString("com.test.$");
     66       fail();
     67     } catch (IllegalArgumentException expected) {}
     68     try {
     69       ClassName.bestGuessFromString("com.test.LooksLikeAClass.pkg");
     70       fail();
     71     } catch (IllegalArgumentException expected) {}
     72     try {
     73       ClassName.bestGuessFromString("!@#$gibberish%^&*");
     74       fail();
     75     } catch (IllegalArgumentException expected) {}
     76   }
     77 
     78   @Test public void classNameFromTypeElement() {
     79     Elements elements = compilationRule.getElements();
     80     TypeElement element = elements.getTypeElement(Object.class.getCanonicalName());
     81     assertThat(ClassName.fromTypeElement(element).canonicalName())
     82         .isEqualTo("java.lang.Object");
     83   }
     84 
     85   @Test public void peerNamed_topLevelClass() {
     86     Elements elements = compilationRule.getElements();
     87     TypeElement element = elements.getTypeElement(ClassNameTest.class.getCanonicalName());
     88     ClassName className = ClassName.fromTypeElement(element);
     89     ClassName peerName = className.peerNamed("Foo");
     90     assertThat(peerName.canonicalName())
     91         .isEqualTo("dagger.internal.codegen.writer.Foo");
     92   }
     93 
     94   @Test public void peerNamed_nestedClass() {
     95     Elements elements = compilationRule.getElements();
     96     TypeElement element = elements.getTypeElement(OuterClass.class.getCanonicalName());
     97     ClassName className = ClassName.fromTypeElement(element);
     98     ClassName peerName = className.peerNamed("Foo");
     99     assertThat(peerName.canonicalName())
    100         .isEqualTo("dagger.internal.codegen.writer.ClassNameTest.Foo");
    101   }
    102 
    103   @Test public void peerNamed_deeplyNestedClass() {
    104     Elements elements = compilationRule.getElements();
    105     TypeElement element = elements.getTypeElement(InnerClass.class.getCanonicalName());
    106     ClassName className = ClassName.fromTypeElement(element);
    107     ClassName peerName = className.peerNamed("Foo");
    108     assertThat(peerName.canonicalName())
    109         .isEqualTo("dagger.internal.codegen.writer.ClassNameTest.OuterClass.Foo");
    110   }
    111 
    112   @Test public void fromClass_NonNestedClass() {
    113     ClassName className = ClassName.fromClass(ClassNameTest.class);
    114     assertThat(className.canonicalName()).isEqualTo(
    115         "dagger.internal.codegen.writer.ClassNameTest");
    116   }
    117 
    118   @Test public void fromClass_NestedClass() {
    119     ClassName className = ClassName.fromClass(InnerClass.class);
    120     assertThat(className.canonicalName()).isEqualTo(
    121         "dagger.internal.codegen.writer.ClassNameTest.OuterClass.InnerClass");
    122   }
    123 
    124   @Test public void fromClass_classFileName() {
    125     ClassName className = ClassName.fromClass(InnerClass.class);
    126     assertThat(className.classFileName('_')).isEqualTo("ClassNameTest_OuterClass_InnerClass");
    127   }
    128 }
    129