• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Google Inc. All Rights Reserved.
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 
17 package com.google.turbine.processing;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 import static com.google.common.truth.TruthJUnit.assume;
21 import static org.junit.Assert.fail;
22 
23 import com.google.common.base.Joiner;
24 import com.google.common.collect.ImmutableSet;
25 import javax.lang.model.type.PrimitiveType;
26 import javax.lang.model.type.TypeKind;
27 import javax.lang.model.type.TypeMirror;
28 import javax.lang.model.util.Types;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.Parameterized;
32 import org.junit.runners.Parameterized.Parameters;
33 
34 @RunWith(Parameterized.class)
35 public class TurbineTypesUnaryTest extends AbstractTurbineTypesTest {
36 
37   @Parameters(name = "{index}: {0}")
parameters()38   public static Iterable<Object[]> parameters() throws Exception {
39     return unaryParameters();
40   }
41 
42   final String testDescription;
43   final Types javacTypes;
44   final TypeMirror javacA;
45   final Types turbineTypes;
46   final TypeMirror turbineA;
47 
TurbineTypesUnaryTest( String testDescription, Types javacTypes, TypeMirror javacA, Types turbineTypes, TypeMirror turbineA)48   public TurbineTypesUnaryTest(
49       String testDescription,
50       Types javacTypes,
51       TypeMirror javacA,
52       Types turbineTypes,
53       TypeMirror turbineA) {
54     this.testDescription = testDescription;
55     this.javacTypes = javacTypes;
56     this.javacA = javacA;
57     this.turbineTypes = turbineTypes;
58     this.turbineA = turbineA;
59   }
60 
61   @Test
unboxedType()62   public void unboxedType() {
63     IllegalArgumentException thrown = null;
64     String expectedType = null;
65     try {
66       expectedType = javacTypes.unboxedType(javacA).toString();
67     } catch (IllegalArgumentException e) {
68       thrown = e;
69     }
70     if (thrown != null) {
71       try {
72         turbineTypes.unboxedType(turbineA).toString();
73         fail(String.format("expected unboxedType(`%s`) to throw", turbineA));
74       } catch (IllegalArgumentException expected) {
75         // expected
76       }
77     } else {
78       String actual = turbineTypes.unboxedType(turbineA).toString();
79       assertWithMessage("unboxedClass(`%s`) = unboxedClass(`%s`)", javacA, turbineA)
80           .that(actual)
81           .isEqualTo(expectedType);
82     }
83   }
84 
85   @Test
boxedClass()86   public void boxedClass() {
87     assume().that(javacA).isInstanceOf(PrimitiveType.class);
88     assume().that(turbineA).isInstanceOf(PrimitiveType.class);
89 
90     String expected = javacTypes.boxedClass((PrimitiveType) javacA).toString();
91     String actual = turbineTypes.boxedClass((PrimitiveType) turbineA).toString();
92     assertWithMessage("boxedClass(`%s`) = boxedClass(`%s`)", javacA, turbineA)
93         .that(actual)
94         .isEqualTo(expected);
95   }
96 
97   @Test
erasure()98   public void erasure() {
99     String expected = javacTypes.erasure(javacA).toString();
100     String actual = turbineTypes.erasure(turbineA).toString();
101     assertWithMessage("erasure(`%s`) = erasure(`%s`)", javacA, turbineA)
102         .that(actual)
103         .isEqualTo(expected);
104   }
105 
106   private static final ImmutableSet<TypeKind> UNSUPPORTED_BY_DIRECT_SUPERTYPES =
107       ImmutableSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE);
108 
109   @Test
directSupertypes()110   public void directSupertypes() {
111     assume().that(UNSUPPORTED_BY_DIRECT_SUPERTYPES).doesNotContain(javacA.getKind());
112 
113     String expected = Joiner.on(", ").join(javacTypes.directSupertypes(javacA));
114     String actual = Joiner.on(", ").join(turbineTypes.directSupertypes(turbineA));
115     assertWithMessage("directSupertypes(`%s`) = directSupertypes(`%s`)", javacA, turbineA)
116         .that(actual)
117         .isEqualTo(expected);
118   }
119 
120   @Test
directSupertypesThrows()121   public void directSupertypesThrows() {
122     assume().that(UNSUPPORTED_BY_DIRECT_SUPERTYPES).contains(javacA.getKind());
123 
124     try {
125       javacTypes.directSupertypes(turbineA);
126       fail();
127     } catch (IllegalArgumentException expected) {
128     }
129     try {
130       turbineTypes.directSupertypes(turbineA);
131       fail();
132     } catch (IllegalArgumentException expected) {
133     }
134   }
135 
136   @Test
asElement()137   public void asElement() {
138     // TODO(cushon): this looks like a javac bug
139     assume().that(javacA.getKind()).isNotEqualTo(TypeKind.INTERSECTION);
140 
141     String expected = String.valueOf(javacTypes.asElement(javacA));
142     String actual = String.valueOf(turbineTypes.asElement(turbineA));
143     assertWithMessage("asElement(`%s`) = asElement(`%s`)", javacA, turbineA)
144         .that(actual)
145         .isEqualTo(expected);
146   }
147 }
148