vlsi commented on a change in pull request #2488:
URL: https://github.com/apache/calcite/pull/2488#discussion_r690106000



##########
File path: linq4j/src/test/java/org/apache/calcite/linq4j/tree/TypeTest.java
##########
@@ -62,4 +83,236 @@
     assertEquals(Object.class, Types.gcd(String.class, int.class));
     java.io.Serializable o = true ? "x" : 1;
   }
+
+  private static void assertFields(List<Field> expected, List<Field> computed) 
{
+    assertEquals(expected, computed, "Expected field(s) ["
+        + expected + "] but found [" + computed + "]");
+  }
+
+  @Test void testExcludedStaticFieldsAndViaAnnotation() throws 
NoSuchFieldException {
+    List<Field> fieldNames = 
Collections.singletonList(A.class.getField("strField"));
+
+    assertFields(fieldNames, Types.getClassFields(A.class));
+  }
+
+  @Test void testAlphabeticalAndHierarchicalOrder() throws 
NoSuchFieldException {
+    List<Field> fieldNames = Arrays.asList(AB.class.getField("aField"),
+        A.class.getField("strField"),
+        AB.class.getField("zField"));
+
+    assertFields(
+        fieldNames, Types.getClassFields(AB.class, true,
+        Types.FieldsOrdering.ALPHABETICAL, null));
+  }
+
+  @Test void testOrderViaFieldNamesAndClassHierarchy() throws 
NoSuchFieldException {
+    List<Field> fieldNames = Arrays.asList(A.class.getField("strField"),
+        AB.class.getField("aField"),
+        AB.class.getField("zField"));
+
+    assertFields(
+        fieldNames, Types.getClassFields(AB.class, true,
+        Types.FieldsOrdering.ALPHABETICAL_AND_HIERARCHY, null));
+  }
+
+  @Test void testNoHierarchyConstructorAlphabetical() throws 
NoSuchFieldException {
+    List<Field> fieldNames = Arrays.asList(AB.class.getField("aField"),
+        AB.class.getField("zField"));
+
+    assertFields(
+        fieldNames, Types.getClassFields(AB.class, false,
+        Types.FieldsOrdering.CONSTRUCTOR, null));
+  }
+
+  @TestIfParameterNames(clazz = AC.class)
+  void testNoHierarchyConstructorInDeclarationOrder() throws 
NoSuchFieldException {
+    List<Field> fieldNames = Arrays.asList(AC.class.getField("zField"),
+        AC.class.getField("aField"));
+
+    assertFields(
+        fieldNames, Types.getClassFields(AC.class, false,
+        Types.FieldsOrdering.CONSTRUCTOR, null));
+  }
+
+
+  @TestIfParameterNames(clazz = AB.class)
+  void testHierarchyConstructorAlphabetical() throws NoSuchFieldException {
+    List<Field> fieldNames = Arrays.asList(AB.class.getField("aField"),
+        AB.class.getField("zField"),
+        A.class.getField("strField"));
+
+    assertFields(
+        fieldNames, Types.getClassFields(AB.class, true,
+        Types.FieldsOrdering.CONSTRUCTOR, null));
+  }
+
+  @TestIfParameterNames(clazz = AC.class)
+  void testHierarchyConstructorInDeclarationOrder() throws 
NoSuchFieldException {
+    List<Field> fieldNames = Arrays.asList(A.class.getField("strField"),
+        AC.class.getField("zField"),
+        AC.class.getField("aField"));
+
+    assertFields(
+        fieldNames, Types.getClassFields(AC.class, true,
+            Types.FieldsOrdering.CONSTRUCTOR, null));
+  }
+
+  @TestIfParameterNames(clazz = AD.class)
+  void testIncompleteConstructors() throws NoSuchFieldException {
+    List<Field> fieldNames = Arrays.asList(A.class.getField("strField"),
+        AD.class.getField("zField"),
+        AD.class.getField("aField"));
+
+    assertFields(
+        fieldNames, Types.getClassFields(AD.class, true,
+            Types.FieldsOrdering.CONSTRUCTOR, null));
+  }
+
+  @Test void testExplicitFieldNames() throws NoSuchFieldException {
+    Map<Class, List<Field>> classFieldsMap = new HashMap<>();
+    List<Field> fieldNames = Arrays.asList(AC.class.getField("zField"),
+        A.class.getField("strField"),
+        AC.class.getField("aField"));
+    classFieldsMap.put(AC.class, fieldNames);
+
+    assertFields(
+        fieldNames, Types.getClassFields(AC.class, true,
+            Types.FieldsOrdering.EXPLICIT, classFieldsMap));
+  }
+
+  @Test void testExplicitFieldNamesIncompleteKO() throws NoSuchFieldException {
+    Map<Class, List<Field>> classFieldsMap = new HashMap<>();
+    List<Field> fieldNames = Arrays.asList(AC.class.getField("zField"),
+        A.class.getField("strField"));
+    classFieldsMap.put(AC.class, fieldNames);
+
+    Types.FieldsOrdering explicitFieldsOrdering = 
Types.FieldsOrdering.EXPLICIT;
+
+    IllegalArgumentException thrown = assertThrows(
+        IllegalArgumentException.class,
+        () -> Types.getClassFields(AC.class, true, explicitFieldsOrdering, 
classFieldsMap),
+        "Expected 'Types.getClassFields' to throw on incomplete field list"
+    );
+
+    String expectedErrorPrefix = "Incomplete list of fields is not compatible 
with \""
+        + explicitFieldsOrdering + "\"";
+    assertTrue(thrown.getMessage().contains(expectedErrorPrefix));

Review comment:
       Please refrain from `assertTrue(...complexExpression...)` since the 
failure would look like `expected true got false` which often is puzzling for 
maintainers :-/
   
   There are several ways to approach it:
   
   a) `assertThat(actual, contains(substring))` from Hamcreset. I don't really 
like "hamcreset all the way", however, trivial comparisons like `contains` are 
more-or-less readable and they produce reasonable messages
   b) `if (!thrown.getMessage().contains(...)) { throw new 
AssertionException("Types.getClassFields(AC, true, " + explicitFieldsOrdering + 
", " + classFieldsMap + ") should throw incomplete list of fields exception 
since field ... was not listed in parameter ...", thrown); }`
   c) `assertTrue(expression, () -> "clarifying message"...)`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to