|
28 | 28 | import com.google.common.collect.ImmutableSet;
|
29 | 29 | import com.google.common.collect.Iterables;
|
30 | 30 | import com.google.common.testing.EquivalenceTester;
|
| 31 | +import com.google.common.truth.Expect; |
31 | 32 | import com.google.testing.compile.CompilationRule;
|
32 | 33 | import java.lang.annotation.Annotation;
|
33 | 34 | import java.util.List;
|
|
55 | 56 |
|
56 | 57 | @RunWith(JUnit4.class)
|
57 | 58 | public class MoreTypesTest {
|
58 |
| - @Rule public CompilationRule compilationRule = new CompilationRule(); |
| 59 | + @Rule public final CompilationRule compilationRule = new CompilationRule(); |
| 60 | + @Rule public final Expect expect = Expect.create(); |
59 | 61 |
|
60 | 62 | @Test
|
61 | 63 | public void equivalence() {
|
@@ -442,4 +444,58 @@ public List<? extends AnnotationMirror> getAnnotationMirrors() {
|
442 | 444 | return null;
|
443 | 445 | }
|
444 | 446 | };
|
| 447 | + |
| 448 | + @Test |
| 449 | + public void testIsConversionFromObjectUnchecked_yes() { |
| 450 | + Elements elements = compilationRule.getElements(); |
| 451 | + TypeElement unchecked = elements.getTypeElement(Unchecked.class.getCanonicalName()); |
| 452 | + for (VariableElement field : ElementFilter.fieldsIn(unchecked.getEnclosedElements())) { |
| 453 | + TypeMirror type = field.asType(); |
| 454 | + expect |
| 455 | + .withMessage("Casting to %s is unchecked", type) |
| 456 | + .that(MoreTypes.isConversionFromObjectUnchecked(type)) |
| 457 | + .isTrue(); |
| 458 | + } |
| 459 | + } |
| 460 | + |
| 461 | + @Test |
| 462 | + public void testIsConversionFromObjectUnchecked_no() { |
| 463 | + Elements elements = compilationRule.getElements(); |
| 464 | + TypeElement notUnchecked = elements.getTypeElement(NotUnchecked.class.getCanonicalName()); |
| 465 | + for (VariableElement field : ElementFilter.fieldsIn(notUnchecked.getEnclosedElements())) { |
| 466 | + TypeMirror type = field.asType(); |
| 467 | + expect |
| 468 | + .withMessage("Casting to %s is not unchecked", type) |
| 469 | + .that(MoreTypes.isConversionFromObjectUnchecked(type)) |
| 470 | + .isFalse(); |
| 471 | + } |
| 472 | + } |
| 473 | + |
| 474 | + // The type of every field here is such that casting to it provokes an "unchecked" warning. |
| 475 | + @SuppressWarnings("unused") |
| 476 | + private static class Unchecked<T> { |
| 477 | + private List<String> listOfString; |
| 478 | + private List<? extends CharSequence> listOfExtendsCharSequence; |
| 479 | + private List<? super CharSequence> listOfSuperCharSequence; |
| 480 | + private List<T> listOfT; |
| 481 | + private List<T[]> listOfArrayOfT; |
| 482 | + private T t; |
| 483 | + private T[] arrayOfT; |
| 484 | + private List<T>[] arrayOfListOfT; |
| 485 | + private Map<?, String> mapWildcardToString; |
| 486 | + private Map<String, ?> mapStringToWildcard; |
| 487 | + } |
| 488 | + |
| 489 | + // The type of every field here is such that casting to it doesn't provoke an "unchecked" warning. |
| 490 | + @SuppressWarnings("unused") |
| 491 | + private static class NotUnchecked { |
| 492 | + private String string; |
| 493 | + private int integer; |
| 494 | + private String[] arrayOfString; |
| 495 | + private int[] arrayOfInt; |
| 496 | + private Thread.State threadStateEnum; |
| 497 | + private List<?> listOfWildcard; |
| 498 | + private List<? extends Object> listOfWildcardExtendsObject; |
| 499 | + private Map<?, ?> mapWildcardToWildcard; |
| 500 | + } |
445 | 501 | }
|
0 commit comments