JDK 13 RFR of JDK-8222430: Add tests for ElementKind predicates

Joe Darcy joe.darcy at oracle.com
Fri Apr 12 21:35:24 UTC 2019


Hello,

Ahead of some amber work, please review the addition of a test for the 
ElementKind primitives:

     JDK-8222430: Add tests for ElementKind predicates
     http://cr.openjdk.java.net/~darcy/8222430.0/

New test sans standard license below.

Thanks,

-Joe

// license omitted here for brevity

/*
  * @test
  * @bug 8222430
  * @summary Test various predicates of ElementKind.
  */

import java.util.Set;
import java.util.function.Predicate;
import javax.lang.model.element.ElementKind;

/**
  * Test the isClass, isField, and isInterface predicates of ElementKind.
  */
public class TestElementKindPredicates {
     public static void main(String... args) {
         Set<ElementKind> ALL_KINDS = Set.of(ElementKind.values());

         // isClass: Returns true if this is a kind of class: either 
CLASS or ENUM.
         test(ALL_KINDS,
              (ElementKind k) -> Set.of(ElementKind.CLASS,
ElementKind.ENUM).contains(k),
              (ElementKind k) -> k.isClass(), "isClass");

         // isField: Returns true if this is a kind of field: either 
FIELD or ENUM_CONSTANT.
         test(ALL_KINDS,
              (ElementKind k) -> Set.of(ElementKind.FIELD,
ElementKind.ENUM_CONSTANT).contains(k),
              (ElementKind k) -> k.isField(), "isField");

         // isInterface: Returns true if this is a kind of interface: 
either INTERFACE or ANNOTATION_TYPE.
         test(ALL_KINDS,
              (ElementKind k) -> Set.of(ElementKind.INTERFACE,
ElementKind.ANNOTATION_TYPE).contains(k),
              (ElementKind k) -> k.isInterface(), "isInterface");
     }

     private static void test(Set<ElementKind> kinds,
                              Predicate<ElementKind> expectedPred,
                              Predicate<ElementKind> actualPred,
                              String errorMessage) {
         for(ElementKind kind : kinds) {
             boolean expected = expectedPred.test(kind);
             boolean actual = actualPred.test(kind);

             if (expected != actual) {
                 throw new RuntimeException("Error testing ElementKind." 
+ errorMessage + "(" +  kind +
                                            "):\texpected " + expected + 
"\tgot " + actual);
             }
         }
     }
}



More information about the compiler-dev mailing list