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 */
16package dagger.internal.codegen;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Iterables;
20import com.google.testing.compile.CompilationRule;
21import dagger.internal.codegen.MethodSignatureFormatterTest.OuterClass.InnerClass;
22import javax.inject.Singleton;
23import javax.lang.model.element.ExecutableElement;
24import javax.lang.model.element.TypeElement;
25import javax.lang.model.util.Elements;
26import org.junit.Rule;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.JUnit4;
30
31import static com.google.common.truth.Truth.assertThat;
32import static javax.lang.model.util.ElementFilter.methodsIn;
33
34@RunWith(JUnit4.class)
35public class MethodSignatureFormatterTest {
36  @Rule public CompilationRule compilationRule = new CompilationRule();
37
38  static class OuterClass {
39    @interface Foo {
40       Class<?> bar();
41    }
42
43    static class InnerClass {
44      @Foo(bar = String.class)
45      @Singleton
46      String foo(@SuppressWarnings("unused") int a, ImmutableList<Boolean> blah) { return "foo"; }
47    }
48  }
49
50  @Test public void methodSignatureTest() {
51    Elements elements = compilationRule.getElements();
52    TypeElement inner = elements.getTypeElement(InnerClass.class.getCanonicalName());
53    ExecutableElement method = Iterables.getOnlyElement(methodsIn(inner.getEnclosedElements()));
54    String formatted = new MethodSignatureFormatter(compilationRule.getTypes()).format(method);
55    // This is gross, but it turns out that annotation order is not guaranteed when getting
56    // all the AnnotationMirrors from an Element, so I have to test this chopped-up to make it
57    // less brittle.
58    assertThat(formatted).contains("@Singleton");
59    assertThat(formatted).doesNotContain("@javax.inject.Singleton"); // maybe more importantly
60    assertThat(formatted)
61        .contains("@dagger.internal.codegen.MethodSignatureFormatterTest.OuterClass.Foo"
62            + "(bar=String.class)");
63    assertThat(formatted).contains(" String "); // return type compressed
64    assertThat(formatted).contains("int, ImmutableList<Boolean>)"); // parameters compressed.
65  }
66}
67