2
// ConvertIfToSwtichTests.cs
5
// Mansheng Yang <lightyang0@gmail.com>
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using ICSharpCode.NRefactory.CSharp.Refactoring;
28
using NUnit.Framework;
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
33
public class ConvertIfToSwtichTests : ContextActionTestBase
37
public void TestBreak ()
39
Test<ConvertIfToSwitchAction> (@"
42
void TestMethod (int a)
49
} else if (a == 2 || a == 3) {
58
void TestMethod (int a)
81
public void TestReturn ()
83
Test<ConvertIfToSwitchAction> (@"
86
int TestMethod (int a)
91
} else if (a == 2 || a == 3) {
100
int TestMethod (int a)
117
public void TestConstantExpression ()
119
Test<ConvertIfToSwitchAction> (@"
122
int TestMethod (int? a)
124
$if (a == (1 == 1 ? 11 : 12)) {
126
} else if (a == (2 * 3) + 1 || a == 6 / 2) {
128
} else if (a == null || a == (int)(10L + 2) || a == default(int) || a == sizeof(int)) {
137
int TestMethod (int? a)
140
case (1 == 1 ? 11 : 12):
157
Test<ConvertIfToSwitchAction> (@"
161
int TestMethod (int a)
166
} else if (a == b + c) {
176
int TestMethod (int a)
192
public void TestNestedOr ()
194
Test<ConvertIfToSwitchAction> (@"
197
int TestMethod (int a)
201
} else if ((a == 2 || a == 4) || (a == 3 || a == 5)) {
210
int TestMethod (int a)
228
public void TestComplexSwitchExpression ()
230
Test<ConvertIfToSwitchAction> (@"
233
int TestMethod (int a, int b)
237
} else if (1 == a + b) {
246
int TestMethod (int a, int b)
261
public void TestNonConstantExpression ()
263
TestWrongContext<ConvertIfToSwitchAction> (@"
266
void TestMethod (int a, int c)
273
} else if (a == 2 || a == 3) {
280
TestWrongContext<ConvertIfToSwitchAction> (@"
283
void TestMethod (int a, int c)
290
} else if (a == 2 || a == 3) {
297
TestWrongContext<ConvertIfToSwitchAction> (@"
300
void TestMethod (int a, int c)
307
} else if (a == 2 || a == c) {
317
public void TestNonEqualityComparison ()
319
TestWrongContext<ConvertIfToSwitchAction> (@"
322
void TestMethod (int a)
329
} else if (a == 2 || a == 3) {
339
public void TestValidType ()
342
Test<ConvertIfToSwitchAction> (@"
350
int TestMethod (TestEnum a)
352
$if (a == TestEnum.First) {
366
int TestMethod (TestEnum a)
377
// string, bool, char, integral, nullable
378
TestValidType ("string", "\"test\"");
379
TestValidType ("bool", "true");
380
TestValidType ("char", "'a'");
381
TestValidType ("byte", "0");
382
TestValidType ("sbyte", "0");
383
TestValidType ("short", "0");
384
TestValidType ("long", "0");
385
TestValidType ("ushort", "0");
386
TestValidType ("uint", "0");
387
TestValidType ("ulong", "0");
388
TestValidType ("bool?", "null");
391
void TestValidType (string type, string caseValue)
393
Test<ConvertIfToSwitchAction> (@"
396
int TestMethod (" + type + @" a)
398
$if (a == " + caseValue + @") {
407
int TestMethod (" + type + @" a)
410
case " + caseValue + @":
420
public void TestInvalidType ()
422
TestWrongContext<ConvertIfToSwitchAction> (@"
425
void TestMethod (double a)
438
public void TestNoElse()
440
Test<ConvertIfToSwitchAction> (@"
443
void TestMethod (int a)
455
void TestMethod (int a)