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