32
32
/** Tests for {@link StatementSwitchToExpressionSwitch}. */
33
33
@ RunWith (JUnit4 .class )
34
34
public final class StatementSwitchToExpressionSwitchTest {
35
- private static final String SIDE =
36
- """
37
- enum Side {
38
- OBVERSE,
39
- REVERSE
40
- };
41
- """ ;
42
35
private static final String SUIT =
43
36
"""
44
37
enum Suit {
@@ -50,21 +43,16 @@ enum Suit {
50
43
""" ;
51
44
private final CompilationTestHelper helper =
52
45
CompilationTestHelper .newInstance (StatementSwitchToExpressionSwitch .class , getClass ())
53
- .addSourceLines ("Suit.java" , SUIT )
54
- .addSourceLines ("Side.java" , SIDE );
46
+ .addSourceLines ("Suit.java" , SUIT );
55
47
private final BugCheckerRefactoringTestHelper refactoringHelper =
56
48
BugCheckerRefactoringTestHelper .newInstance (
57
49
StatementSwitchToExpressionSwitch .class , getClass ())
58
50
.addInputLines ("Suit.java" , SUIT )
59
- .expectUnchanged ()
60
- .addInputLines ("Side.java" , SIDE )
61
51
.expectUnchanged ();
62
52
private final BugCheckerRefactoringTestHelper refactoringHelper2 =
63
53
BugCheckerRefactoringTestHelper .newInstance (
64
54
StatementSwitchToExpressionSwitch .class , getClass ())
65
55
.addInputLines ("Suit.java" , SUIT )
66
- .expectUnchanged ()
67
- .addInputLines ("Side.java" , SIDE )
68
56
.expectUnchanged ();
69
57
70
58
@ Test
@@ -74,19 +62,21 @@ public void switchByEnum_removesRedundantBreak_error() {
74
62
"Test.java" ,
75
63
"""
76
64
class Test {
77
- public void foo(Side side ) {
78
- switch (side ) {
79
- case OBVERSE /* left comment */ /* and there is more: */ // to end of line
65
+ public void foo(Suit suit ) {
66
+ switch (suit ) {
67
+ case HEART /* left comment */ /* and there is more: */ // to end of line
80
68
:
81
69
// Explanatory comment
82
- System.out.println("the front is called the");
70
+ System.out.println("the best suit is called the");
83
71
// Middle comment
84
- System.out.println("obverse ");
72
+ System.out.println("heart ");
85
73
// Break comment
86
74
break;
87
75
// End comment
88
- case REVERSE:
89
- System.out.println("reverse");
76
+ case SPADE:
77
+ case CLUB:
78
+ case DIAMOND:
79
+ System.out.println("non-heart");
90
80
}
91
81
}
92
82
}
@@ -95,20 +85,20 @@ public void foo(Side side) {
95
85
"Test.java" ,
96
86
"""
97
87
class Test {
98
- public void foo(Side side ) {
99
- switch (side ) {
100
- case OBVERSE -> {
88
+ public void foo(Suit suit ) {
89
+ switch (suit ) {
90
+ case HEART -> {
101
91
/* left comment */
102
92
/* and there is more: */
103
93
// to end of line
104
94
// Explanatory comment
105
- System.out.println("the front is called the");
95
+ System.out.println("the best suit is called the");
106
96
// Middle comment
107
- System.out.println("obverse ");
97
+ System.out.println("heart ");
108
98
// Break comment
109
99
// End comment
110
100
}
111
- case REVERSE -> System.out.println("reverse ");
101
+ case SPADE, CLUB, DIAMOND -> System.out.println("non-heart ");
112
102
}
113
103
}
114
104
}
@@ -125,17 +115,19 @@ public void switchByEnumWithCompletionAnalysis_removesRedundantBreak_error() {
125
115
"Test.java" ,
126
116
"""
127
117
class Test {
128
- public void foo(Side side ) {
129
- switch (side ) {
118
+ public void foo(Suit suit ) {
119
+ switch (suit ) {
130
120
// Comment before first case
131
- case OBVERSE :
121
+ case HEART :
132
122
// Explanatory comment
133
123
System.out.println("this block cannot complete normally");
134
124
{
135
125
throw new NullPointerException();
136
126
}
137
- case REVERSE:
138
- System.out.println("reverse");
127
+ case CLUB:
128
+ case DIAMOND:
129
+ case SPADE:
130
+ System.out.println("non-heart");
139
131
}
140
132
}
141
133
}
@@ -144,17 +136,17 @@ public void foo(Side side) {
144
136
"Test.java" ,
145
137
"""
146
138
class Test {
147
- public void foo(Side side ) {
148
- switch (side ) {
149
- case OBVERSE -> {
139
+ public void foo(Suit suit ) {
140
+ switch (suit ) {
141
+ case HEART -> {
150
142
// Comment before first case
151
143
// Explanatory comment
152
144
System.out.println("this block cannot complete normally");
153
145
{
154
146
throw new NullPointerException();
155
147
}
156
148
}
157
- case REVERSE -> System.out.println("reverse ");
149
+ case CLUB, DIAMOND, SPADE -> System.out.println("non-heart ");
158
150
}
159
151
}
160
152
}
@@ -173,17 +165,19 @@ public void switchByEnumExhaustiveWithoutDefault_removesDefault_error() {
173
165
"Test.java" ,
174
166
"""
175
167
class Test {
176
- public void foo(Side side ) {
177
- switch (side ) {
168
+ public void foo(Suit suit ) {
169
+ switch (suit ) {
178
170
// Comment before first case
179
- case OBVERSE :
171
+ case HEART :
180
172
// Explanatory comment
181
173
System.out.println("this block cannot complete normally");
182
174
{
183
175
throw new NullPointerException();
184
176
}
185
- case REVERSE:
186
- System.out.println("reverse");
177
+ case CLUB:
178
+ case SPADE:
179
+ case DIAMOND:
180
+ System.out.println("non-heart");
187
181
break;
188
182
default:
189
183
System.out.println("default");
@@ -195,17 +189,17 @@ public void foo(Side side) {
195
189
"Test.java" ,
196
190
"""
197
191
class Test {
198
- public void foo(Side side ) {
199
- switch (side ) {
200
- case OBVERSE -> {
192
+ public void foo(Suit suit ) {
193
+ switch (suit ) {
194
+ case HEART -> {
201
195
// Comment before first case
202
196
// Explanatory comment
203
197
System.out.println("this block cannot complete normally");
204
198
{
205
199
throw new NullPointerException();
206
200
}
207
201
}
208
- case REVERSE -> System.out.println("reverse ");
202
+ case CLUB, SPADE, DIAMOND -> System.out.println("non-heart ");
209
203
default -> System.out.println("default");
210
204
}
211
205
}
@@ -220,17 +214,19 @@ public void foo(Side side) {
220
214
"Test.java" ,
221
215
"""
222
216
class Test {
223
- public void foo(Side side ) {
224
- switch (side ) {
217
+ public void foo(Suit suit ) {
218
+ switch (suit ) {
225
219
// Comment before first case
226
- case OBVERSE :
220
+ case HEART :
227
221
// Explanatory comment
228
222
System.out.println("this block cannot complete normally");
229
223
{
230
224
throw new NullPointerException();
231
225
}
232
- case REVERSE:
233
- System.out.println("reverse");
226
+ case CLUB:
227
+ case SPADE:
228
+ case DIAMOND:
229
+ System.out.println("non-heart");
234
230
break;
235
231
default:
236
232
System.out.println("default");
@@ -242,17 +238,17 @@ public void foo(Side side) {
242
238
"Test.java" ,
243
239
"""
244
240
class Test {
245
- public void foo(Side side ) {
246
- switch (side ) {
247
- case OBVERSE -> {
241
+ public void foo(Suit suit ) {
242
+ switch (suit ) {
243
+ case HEART -> {
248
244
// Comment before first case
249
245
// Explanatory comment
250
246
System.out.println("this block cannot complete normally");
251
247
{
252
248
throw new NullPointerException();
253
249
}
254
250
}
255
- case REVERSE -> System.out.println("reverse ");
251
+ case CLUB, SPADE, DIAMOND -> System.out.println("non-heart ");
256
252
}
257
253
}
258
254
}
@@ -1034,16 +1030,16 @@ private void bar() {}
1034
1030
1035
1031
@ Test
1036
1032
public void switchByEnum_surroundingBracesCannotRemove_error () {
1037
- // Can't remove braces around OBVERSE because break statements are not a member of
1033
+ // Can't remove braces around HEART because break statements are not a member of
1038
1034
// KINDS_CONVERTIBLE_WITHOUT_BRACES
1039
1035
refactoringHelper
1040
1036
.addInputLines (
1041
1037
"Test.java" ,
1042
1038
"""
1043
1039
class Test {
1044
- public void foo(Side side ) {
1045
- switch (side ) {
1046
- case OBVERSE :
1040
+ public void foo(Suit suit ) {
1041
+ switch (suit ) {
1042
+ case HEART :
1047
1043
{
1048
1044
// The quick brown fox, jumps over the lazy dog, etc.
1049
1045
break;
@@ -1061,9 +1057,9 @@ public void foo(Side side) {
1061
1057
"Test.java" ,
1062
1058
"""
1063
1059
class Test {
1064
- public void foo(Side side ) {
1065
- switch (side ) {
1066
- case OBVERSE -> {
1060
+ public void foo(Suit suit ) {
1061
+ switch (suit ) {
1062
+ case HEART -> {
1067
1063
// The quick brown fox, jumps over the lazy dog, etc.
1068
1064
}
1069
1065
default -> throw new RuntimeException("Invalid type.");
@@ -1078,16 +1074,16 @@ public void foo(Side side) {
1078
1074
1079
1075
@ Test
1080
1076
public void switchByEnum_surroundingBracesEmpty_error () {
1081
- // Test handling of cases with surrounding braces that are empty. The braces around OBVERSE
1077
+ // Test handling of cases with surrounding braces that are empty. The braces around HEART
1082
1078
// can be removed because throw is a member of KINDS_CONVERTIBLE_WITHOUT_BRACES.
1083
1079
refactoringHelper
1084
1080
.addInputLines (
1085
1081
"Test.java" ,
1086
1082
"""
1087
1083
class Test {
1088
- public void foo(Side side ) {
1089
- switch (side ) {
1090
- case OBVERSE :
1084
+ public void foo(Suit suit ) {
1085
+ switch (suit ) {
1086
+ case HEART :
1091
1087
{
1092
1088
// The quick brown fox, jumps over the lazy dog, etc.
1093
1089
throw new RuntimeException("Invalid.");
@@ -1104,9 +1100,9 @@ public void foo(Side side) {
1104
1100
"Test.java" ,
1105
1101
"""
1106
1102
class Test {
1107
- public void foo(Side side ) {
1108
- switch (side ) {
1109
- case OBVERSE ->
1103
+ public void foo(Suit suit ) {
1104
+ switch (suit ) {
1105
+ case HEART ->
1110
1106
// The quick brown fox, jumps over the lazy dog, etc.
1111
1107
throw new RuntimeException("Invalid.");
1112
1108
default -> {}
0 commit comments