/*
 * Copyright (C) 2014 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dagger.internal.codegen.writer;

import dagger.internal.codegen.writer.ClassNameTest.OuterClass.InnerClass;

import com.google.common.collect.ImmutableList;
import com.google.testing.compile.CompilationRule;
import dagger.internal.codegen.writer.ClassName;
import java.util.Map;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static com.google.common.truth.Truth.assert_;
import static org.junit.Assert.fail;

@RunWith(JUnit4.class)
public class ClassNameTest {
  @Rule public CompilationRule compilationRule = new CompilationRule();

  @Test public void bestGuessForString_simpleClass() {
    assert_().that(ClassName.bestGuessFromString(String.class.getName()))
        .isEqualTo(ClassName.create("java.lang", "String"));
  }

  static class OuterClass {
    static class InnerClass {}
  }

  @Test public void bestGuessForString_nestedClass() {
    assert_().that(ClassName.bestGuessFromString(Map.Entry.class.getCanonicalName()))
        .isEqualTo(ClassName.create("java.util", ImmutableList.of("Map"), "Entry"));
    assert_().that(ClassName.bestGuessFromString(OuterClass.InnerClass.class.getCanonicalName()))
        .isEqualTo(
            ClassName.create("dagger.internal.codegen",
                ImmutableList.of("ClassNameTest", "OuterClass"), "InnerClass"));
  }

  @Test public void bestGuessForString_defaultPackage() {
    assert_().that(ClassName.bestGuessFromString("SomeClass"))
        .isEqualTo(ClassName.create("", "SomeClass"));
    assert_().that(ClassName.bestGuessFromString("SomeClass.Nested"))
        .isEqualTo(ClassName.create("", ImmutableList.of("SomeClass"), "Nested"));
    assert_().that(ClassName.bestGuessFromString("SomeClass.Nested.EvenMore"))
        .isEqualTo(ClassName.create("", ImmutableList.of("SomeClass", "Nested"), "EvenMore"));
  }

  @Test public void bestGuessForString_confusingInput() {
    try {
      ClassName.bestGuessFromString("com.test.$");
      fail();
    } catch (IllegalArgumentException expected) {}
    try {
      ClassName.bestGuessFromString("com.test.LooksLikeAClass.pkg");
      fail();
    } catch (IllegalArgumentException expected) {}
    try {
      ClassName.bestGuessFromString("!@#$gibberish%^&*");
      fail();
    } catch (IllegalArgumentException expected) {}
  }

  @Test public void classNameFromTypeElement() {
    Elements elements = compilationRule.getElements();
    TypeElement element = elements.getTypeElement(Object.class.getCanonicalName());
    assert_().that(ClassName.fromTypeElement(element).canonicalName())
        .isEqualTo("java.lang.Object");
  }

  @Test public void peerNamed_topLevelClass() {
    Elements elements = compilationRule.getElements();
    TypeElement element = elements.getTypeElement(ClassNameTest.class.getCanonicalName());
    ClassName className = ClassName.fromTypeElement(element);
    ClassName peerName = className.peerNamed("Foo");
    assert_().that(peerName.canonicalName())
        .isEqualTo("dagger.internal.codegen.Foo");
  }

  @Test public void peerNamed_nestedClass() {
    Elements elements = compilationRule.getElements();
    TypeElement element = elements.getTypeElement(OuterClass.class.getCanonicalName());
    ClassName className = ClassName.fromTypeElement(element);
    ClassName peerName = className.peerNamed("Foo");
    assert_().that(peerName.canonicalName())
        .isEqualTo("dagger.internal.codegen.ClassNameTest.Foo");
  }

  @Test public void peerNamed_deeplyNestedClass() {
    Elements elements = compilationRule.getElements();
    TypeElement element = elements.getTypeElement(InnerClass.class.getCanonicalName());
    ClassName className = ClassName.fromTypeElement(element);
    ClassName peerName = className.peerNamed("Foo");
    assert_().that(peerName.canonicalName())
        .isEqualTo("dagger.internal.codegen.ClassNameTest.OuterClass.Foo");
  }
}

@@ -1944,16 +1944,23 @@
 .codegen
+.writer
 %22,%0A     
@@ -3716,16 +3716,23 @@
 codegen.
+writer.
 Foo%22);%0A 
@@ -4120,32 +4120,39 @@
 nternal.codegen.
+writer.
 ClassNameTest.Fo
@@ -4560,16 +4560,23 @@
 codegen.
+writer.
 ClassNam

Fix up a stray test whose expected text changed when it was moved, and make another test less brittle.