// Copyright 2014 The Bazel Authors. All rights reserved.
//
// 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 com.google.devtools.common.options;

import static com.google.common.truth.Truth.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests {@link GenericTypeHelper}.
 */
@RunWith(JUnit4.class)
public class GenericTypeHelperTest {

  private static interface DoSomething<T> {
    T doIt();
  }

  private static class StringSomething implements DoSomething<String> {
    @Override
    public String doIt() {
      return null;
    }
  }

  private static class EnumSomething<T> implements DoSomething<T> {
    @Override
    public T doIt() {
      return null;
    }
  }

  private static class AlphabetSomething extends EnumSomething<String> {
  }

  private static class AlphabetTwoSomething extends AlphabetSomething {
  }

  private static void assertDoIt(Class<?> expected,
      Class<? extends DoSomething<?>> implementingClass) throws Exception {
    assertThat(
            GenericTypeHelper.getActualReturnType(
                implementingClass, implementingClass.getMethod("doIt")))
        .isEqualTo(expected);
  }

  @Test
  public void getConverterType() throws Exception {
    assertDoIt(String.class, StringSomething.class);
  }

  @Test
  public void getConverterTypeForGenericExtension() throws Exception {
    assertDoIt(String.class, AlphabetSomething.class);
  }

  @Test
  public void getConverterTypeForGenericExtensionSecondGrade() throws Exception {
    assertDoIt(String.class, AlphabetTwoSomething.class);
  }
}

@@ -711,16 +711,60 @@
 tThat;%0A%0A
+import com.google.common.reflect.TypeToken;%0A
 import o
@@ -2156,11 +2156,1186 @@
 ss);%0A  %7D
+%0A%0A  @Test%0A  public void getConverterTypeForParameterizedType() throws Exception %7B%0A    TypeToken%3CEnumSomething%3CString%3E%3E enSthTypeToken = new TypeToken%3C%3E() %7B%7D;%0A    EnumSomething%3CString%3E instance = new EnumSomething%3C%3E();%0A    assertThat(%0A            GenericTypeHelper.getActualReturnType(%0A                enSthTypeToken.getType(), instance.getClass().getMethod(%22doIt%22)))%0A        .isEqualTo(String.class);%0A  %7D%0A%0A  @Test%0A  public void assignableFromPrimitive() %7B%0A    assertThat(GenericTypeHelper.isAssignableFrom(Integer.TYPE, Integer.class)).isTrue();%0A    assertThat(GenericTypeHelper.isAssignableFrom(Integer.TYPE, Long.TYPE)).isFalse();%0A    assertThat(GenericTypeHelper.isAssignableFrom(Integer.TYPE, Integer.TYPE)).isFalse();%0A  %7D%0A%0A  @Test%0A  public void assignableFromSuper() %7B%0A    assertThat(GenericTypeHelper.isAssignableFrom(DoSomething.class, EnumSomething.class)).isTrue();%0A    assertThat(GenericTypeHelper.isAssignableFrom(EnumSomething.class, AlphabetSomething.class))%0A        .isTrue();%0A  %7D%0A%0A  @Test%0A  public void assignableFromSuperSecondGrade() %7B%0A    assertThat(GenericTypeHelper.isAssignableFrom(EnumSomething.class, AlphabetTwoSomething.class))%0A        .isTrue();%0A  %7D
 %0A%7D%0A

Add test cases for GenericTypeHelper class