|
| 1 | +namespace JavaToCSharp.Tests; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Tests for the Java 21 switch pattern matching labels (JEP 441) that record patterns did not |
| 5 | +/// already cover, namely the null label and its combined `case null, default` form. |
| 6 | +/// </summary> |
| 7 | +public class ConvertSwitchPatternTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void Switch_Expression_Null_Label_Is_Converted_To_A_Null_Pattern() |
| 11 | + { |
| 12 | + const string javaCode = """ |
| 13 | + package com.example; |
| 14 | + public class Shapes { |
| 15 | + public String test(Object obj) { |
| 16 | + return switch (obj) { |
| 17 | + case null -> "null"; |
| 18 | + case String s -> s; |
| 19 | + default -> "other"; |
| 20 | + }; |
| 21 | + } |
| 22 | + } |
| 23 | + """; |
| 24 | + |
| 25 | + var parsed = Convert(javaCode); |
| 26 | + |
| 27 | + Assert.Contains("null => \"null\"", parsed); |
| 28 | + Assert.Contains("_ => \"other\"", parsed); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Java models `case null, default` as a null label carrying the default flag. Dropping the |
| 33 | + /// default half would leave the arm matching only null, so a non-null value that matched no |
| 34 | + /// other arm would throw at runtime instead of taking this arm. |
| 35 | + /// </summary> |
| 36 | + [Fact] |
| 37 | + public void Switch_Expression_Null_Default_Label_Is_Converted_To_A_Discard() |
| 38 | + { |
| 39 | + const string javaCode = """ |
| 40 | + package com.example; |
| 41 | + public class Shapes { |
| 42 | + public String test(Object obj) { |
| 43 | + return switch (obj) { |
| 44 | + case Integer i -> "int"; |
| 45 | + case null, default -> "fallback"; |
| 46 | + }; |
| 47 | + } |
| 48 | + } |
| 49 | + """; |
| 50 | + |
| 51 | + var parsed = Convert(javaCode); |
| 52 | + |
| 53 | + Assert.Contains("_ => \"fallback\"", parsed); |
| 54 | + Assert.DoesNotContain("null => \"fallback\"", parsed); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Switch_Statement_Null_Default_Label_Is_Converted_To_A_Default_Section() |
| 59 | + { |
| 60 | + const string javaCode = """ |
| 61 | + package com.example; |
| 62 | + public class Shapes { |
| 63 | + public String test(Object obj) { |
| 64 | + switch (obj) { |
| 65 | + case Integer i -> { return "int"; } |
| 66 | + case null, default -> { return "fallback"; } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + """; |
| 71 | + |
| 72 | + var parsed = Convert(javaCode); |
| 73 | + |
| 74 | + Assert.Contains("default:", parsed); |
| 75 | + Assert.DoesNotContain("case null:", parsed); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void Switch_Expression_Over_Unrelated_Types_Uses_Type_Patterns() |
| 80 | + { |
| 81 | + const string javaCode = """ |
| 82 | + package com.example; |
| 83 | + public class Shapes { |
| 84 | + public String test(Object obj) { |
| 85 | + return switch (obj) { |
| 86 | + case Integer i -> "int " + i; |
| 87 | + case String s -> "string " + s; |
| 88 | + default -> "other"; |
| 89 | + }; |
| 90 | + } |
| 91 | + } |
| 92 | + """; |
| 93 | + |
| 94 | + var parsed = Convert(javaCode); |
| 95 | + |
| 96 | + Assert.Contains("int i =>", parsed); |
| 97 | + Assert.Contains("string s =>", parsed); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// C# has no exhaustiveness concept to carry over, so an exhaustive Java switch simply converts |
| 102 | + /// its arms and gains no default. |
| 103 | + /// </summary> |
| 104 | + [Fact] |
| 105 | + public void Exhaustive_Switch_Over_Sealed_Types_Does_Not_Gain_A_Default_Arm() |
| 106 | + { |
| 107 | + const string javaCode = """ |
| 108 | + package com.example; |
| 109 | + public class Shapes { |
| 110 | + sealed interface Shape permits Circle, Square {} |
| 111 | + record Circle(int r) implements Shape {} |
| 112 | + record Square(int s) implements Shape {} |
| 113 | + public String test(Shape shape) { |
| 114 | + return switch (shape) { |
| 115 | + case Circle(int r) -> "circle"; |
| 116 | + case Square(int s) -> "square"; |
| 117 | + }; |
| 118 | + } |
| 119 | + } |
| 120 | + """; |
| 121 | + |
| 122 | + var parsed = Convert(javaCode, allowWarnings: true); |
| 123 | + |
| 124 | + Assert.Contains("Circle (int r) =>", parsed); |
| 125 | + Assert.Contains("Square (int s) =>", parsed); |
| 126 | + Assert.DoesNotContain("_ =>", parsed); |
| 127 | + } |
| 128 | + |
| 129 | + private static string Convert(string javaCode, bool allowWarnings = false) |
| 130 | + { |
| 131 | + var options = new JavaConversionOptions { IncludeComments = false }; |
| 132 | + |
| 133 | + options.WarningEncountered += (_, eventArgs) => |
| 134 | + { |
| 135 | + if (!allowWarnings) |
| 136 | + { |
| 137 | + throw new InvalidOperationException($"Encountered a warning in conversion: {eventArgs.Message}"); |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + return JavaToCSharpConverter.ConvertText(javaCode, options) ?? ""; |
| 142 | + } |
| 143 | +} |
0 commit comments