|
| 1 | +/* |
| 2 | + * Copyright 2025 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Moderne Source Available License (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 | + * <p> |
| 8 | + * https://docs.moderne.io/licensing/moderne-source-available-license |
| 9 | + * <p> |
| 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 | +package org.openrewrite.java.migrate.lang; |
| 17 | + |
| 18 | +import org.intellij.lang.annotations.Language; |
| 19 | +import org.junit.jupiter.api.Disabled; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.openrewrite.ExecutionContext; |
| 22 | +import org.openrewrite.InMemoryExecutionContext; |
| 23 | +import org.openrewrite.TreeVisitor; |
| 24 | +import org.openrewrite.java.JavaParser; |
| 25 | +import org.openrewrite.java.tree.J; |
| 26 | + |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | + |
| 32 | +class SwitchUtilsTest { |
| 33 | + private static J.Switch getSwitchElement(@Language("java") String code) { |
| 34 | + JavaParser parser = JavaParser.fromJavaVersion().build(); |
| 35 | + J.CompilationUnit cu = (J.CompilationUnit) parser.parse(code).findFirst().get(); |
| 36 | + |
| 37 | + AtomicReference<J.Switch> foundSwitch = new AtomicReference<>(); |
| 38 | + new TreeVisitor<J, ExecutionContext>() { |
| 39 | + @Override |
| 40 | + public J preVisit(J tree, ExecutionContext executionContext) { |
| 41 | + if (foundSwitch.get() != null) { |
| 42 | + return tree; |
| 43 | + } |
| 44 | + if (tree instanceof J.Switch sw) { |
| 45 | + foundSwitch.set(sw); |
| 46 | + } |
| 47 | + return super.preVisit(tree, executionContext); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + }.visit(cu, new InMemoryExecutionContext()); |
| 52 | + |
| 53 | + return foundSwitch.get(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void coversAllCasesAllEnums() { |
| 58 | + assertTrue(SwitchUtils.coversAllPossibleValues(getSwitchElement(""" |
| 59 | + public class Test { |
| 60 | + void method(TrafficLight light) { |
| 61 | + switch (light) { |
| 62 | + case RED -> System.out.println("stop"); |
| 63 | + case YELLOW -> System.out.println("caution"); |
| 64 | + case GREEN -> System.out.println("go"); |
| 65 | + } |
| 66 | + } |
| 67 | + enum TrafficLight { RED, YELLOW, GREEN } |
| 68 | + } |
| 69 | + """))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void coversAllCasesMissingEnums() { |
| 74 | + assertFalse(SwitchUtils.coversAllPossibleValues(getSwitchElement(""" |
| 75 | + public class Test { |
| 76 | + void method(TrafficLight light) { |
| 77 | + switch (light) { |
| 78 | + case RED -> System.out.println("stop"); |
| 79 | + case YELLOW -> System.out.println("caution"); |
| 80 | + } |
| 81 | + } |
| 82 | + enum TrafficLight { RED, YELLOW, GREEN } |
| 83 | + } |
| 84 | + """))); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void coversAllCasesMissingEnumsWithDefault() { |
| 89 | + assertTrue(SwitchUtils.coversAllPossibleValues(getSwitchElement(""" |
| 90 | + public class Test { |
| 91 | + void method(TrafficLight light) { |
| 92 | + switch (light) { |
| 93 | + case RED -> System.out.println("stop"); |
| 94 | + case YELLOW -> System.out.println("caution"); |
| 95 | + default -> System.out.println("unknown"); |
| 96 | + } |
| 97 | + } |
| 98 | + enum TrafficLight { RED, YELLOW, GREEN } |
| 99 | + } |
| 100 | + """))); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void coversAllCasesEnumOnlyDefault() { |
| 105 | + assertTrue(SwitchUtils.coversAllPossibleValues(getSwitchElement(""" |
| 106 | + public class Test { |
| 107 | + void method(TrafficLight light) { |
| 108 | + switch (light) { |
| 109 | + default -> System.out.println("unknown"); |
| 110 | + } |
| 111 | + } |
| 112 | + enum TrafficLight { RED, YELLOW, GREEN } |
| 113 | + } |
| 114 | + """))); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void coversAllCasesObjectOnlyDefault() { |
| 119 | + assertTrue(SwitchUtils.coversAllPossibleValues(getSwitchElement(""" |
| 120 | + public class Test { |
| 121 | + void method(Object obj) { |
| 122 | + switch (obj) { |
| 123 | + default -> System.out.println("default"); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + """))); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + @Disabled("Unsupported yet") |
| 132 | + void coversAllCasesAllSealedClasses() { |
| 133 | + assertTrue(SwitchUtils.coversAllPossibleValues(getSwitchElement(""" |
| 134 | + public class Test { |
| 135 | + sealed abstract class Shape permits Circle, Square, Rectangle {} |
| 136 | + void method(Shape shape) { |
| 137 | + switch (shape) { |
| 138 | + case Circle c -> System.out.println("circle"); |
| 139 | + case Square s -> System.out.println("square"); |
| 140 | + case Rectangle r -> System.out.println("rectangle"); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + """))); |
| 145 | + } |
| 146 | +} |
0 commit comments