Skip to content

Commit

Permalink
Fix tests and complete exhaustiveness for java 21+
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Pine committed Apr 7, 2024
1 parent 2d426f4 commit 54630ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -851,19 +851,20 @@ private boolean checkExhaustive(CtAbstractSwitch<?> switchElement) {
return true;
}

Set<CtExpression<?>> caseExpressions = switchElement.getCases().stream().flatMap(cases -> cases.getCaseExpressions().stream()).collect(Collectors.toSet());
if (switchElement.getSelector().getType().isEnum()) {
Set<CtExpression<?>> caseExpressions = switchElement.getCases().stream().flatMap(cases -> cases.getCaseExpressions().stream()).collect(Collectors.toSet());
CtEnum<?> enumType = (CtEnum<?>) switchElement.getSelector().getType().getTypeDeclaration();
Set<String> enumConstantNames = enumType.getEnumValues().stream().map(CtEnumValue::getSimpleName).collect(Collectors.toSet());
Set<String> referencedEnumConstants = caseExpressions.stream().map(expression -> ((CtFieldRead<?>) expression).getVariable().getSimpleName()).collect(Collectors.toSet());

if (referencedEnumConstants.containsAll(enumConstantNames)) {
return true;
}
return referencedEnumConstants.containsAll(enumConstantNames);
}

// TODO: More complete exhaustive check
return false;
CtTypeReference<?> selectorTypeReference = switchElement.getSelector().getType();

boolean isEnhanced = !selectorTypeReference.isPrimitive() && !selectorTypeReference.equals(selectorTypeReference.getFactory().Type().createReference(String.class));

return isEnhanced;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ public void testEnhancedSwitchImplicitDefault() throws Exception {
assertEquals(2, paths.size());
}

@Test
public void testEnhancedSwitchImplicitDefaultString() throws Exception {
ControlFlowGraph graph = testMethod("enhancedSwitchImplicitDefaultString", true, null, null, null);
graph.simplify();
ControlFlowPathHelper pathHelper = new ControlFlowPathHelper();
ControlFlowNode entryNode = graph.findNodesOfKind(BEGIN).get(0);
List<List<ControlFlowNode>> paths = pathHelper.paths(entryNode);
assertEquals(2, paths.size());
}

@Test
public void testEnhancedSwitchMultipleExpressions() throws Exception {
ControlFlowGraph graph = testMethod("enhancedSwitchMultipleExpressions", true, null, null, null);
Expand Down Expand Up @@ -344,17 +354,7 @@ public void testComplexSealedHierarchyExhaustive() throws Exception {
ControlFlowPathHelper pathHelper = new ControlFlowPathHelper();
ControlFlowNode entryNode = graph.findNodesOfKind(BEGIN).get(0);
List<List<ControlFlowNode>> paths = pathHelper.paths(entryNode);
assertEquals(2, paths.size());
}

@Test
public void testEnhancedSwitchExhaustiveParametrization() throws Exception {
ControlFlowGraph graph = testMethod("enhancedSwitchExhaustiveParametrization", true, null, null, null);
graph.simplify();
ControlFlowPathHelper pathHelper = new ControlFlowPathHelper();
ControlFlowNode entryNode = graph.findNodesOfKind(BEGIN).get(0);
List<List<ControlFlowNode>> paths = pathHelper.paths(entryNode);
assertEquals(1, paths.size());
assertEquals(3, paths.size());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,16 @@ public void enhancedSwitchImplicitDefault() {
}
}

public void enhancedSwitchImplicitDefaultString() {
int a = 1;
int b = 0;
switch ("a") {
case "1" -> {
b = 1;
}
}
}

public void enhancedSwitchMultipleExpressions() {
switch (3) {
case 1, 2 -> {
Expand Down Expand Up @@ -503,11 +513,11 @@ final class B extends A {
final class C extends A {
}

static int enhancedSwitchSealedExhaustive(I i) {
static void enhancedSwitchSealedExhaustive(I i) {
switch (i) {
case A a -> {}
case R r -> {}
};
}
}

static int enhancedSwitchExhaustiveConstantTrueGuard(I i) {
Expand All @@ -525,15 +535,6 @@ static int enhancedSwitchMultilevelSealedExhaustive(I i) {
};
}

sealed interface J<X> permits D, E {}
final class D<Y> implements J<String> {}
final class E<X> implements J<X> {}

static int enhancedSwitchExhaustiveParametrization(J<Integer> ji) {
switch(ji) { // Exhaustive!
case E<Integer> e -> 42;
};
}

//All lines will be tested in this method
public int simple(int a) {
Expand Down

0 comments on commit 54630ae

Please sign in to comment.