~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertIfToSwtichTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
ļ»æ// 
 
2
// ConvertIfToSwtichTests.cs
 
3
//  
 
4
// Author:
 
5
//       Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
28
using NUnit.Framework;
 
29
 
 
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
 
31
{
 
32
        [TestFixture]
 
33
        public class ConvertIfToSwtichTests : ContextActionTestBase
 
34
        {
 
35
 
 
36
                [Test]
 
37
                public void TestBreak ()
 
38
                {
 
39
                        Test<ConvertIfToSwitchAction> (@"
 
40
class TestClass
 
41
{
 
42
        void TestMethod (int a)
 
43
        {
 
44
                int b;
 
45
                $if (a == 0) {
 
46
                        b = 0;
 
47
                } else if (a == 1) {
 
48
                        b = 1;
 
49
                } else if (a == 2 || a == 3) {
 
50
                        b = 2;
 
51
                } else {
 
52
                        b = 3;
 
53
                }
 
54
        }
 
55
}", @"
 
56
class TestClass
 
57
{
 
58
        void TestMethod (int a)
 
59
        {
 
60
                int b;
 
61
                switch (a) {
 
62
                case 0:
 
63
                        b = 0;
 
64
                        break;
 
65
                case 1:
 
66
                        b = 1;
 
67
                        break;
 
68
                case 2:
 
69
                case 3:
 
70
                        b = 2;
 
71
                        break;
 
72
                default:
 
73
                        b = 3;
 
74
                        break;
 
75
                }
 
76
        }
 
77
}");
 
78
                }
 
79
 
 
80
                [Test]
 
81
                public void TestReturn ()
 
82
                {
 
83
                        Test<ConvertIfToSwitchAction> (@"
 
84
class TestClass
 
85
{
 
86
        int TestMethod (int a)
 
87
        {
 
88
                $if (a == 0) {
 
89
                        int b = 1;
 
90
                        return b + 1;
 
91
                } else if (a == 2 || a == 3) {
 
92
                        return 2;
 
93
                } else {
 
94
                        return -1;
 
95
                }
 
96
        }
 
97
}", @"
 
98
class TestClass
 
99
{
 
100
        int TestMethod (int a)
 
101
        {
 
102
                switch (a) {
 
103
                case 0:
 
104
                        int b = 1;
 
105
                        return b + 1;
 
106
                case 2:
 
107
                case 3:
 
108
                        return 2;
 
109
                default:
 
110
                        return -1;
 
111
                }
 
112
        }
 
113
}");
 
114
                }
 
115
                
 
116
                [Test]
 
117
                public void TestConstantExpression ()
 
118
                {
 
119
                        Test<ConvertIfToSwitchAction> (@"
 
120
class TestClass
 
121
{
 
122
        int TestMethod (int? a)
 
123
        {
 
124
                $if (a == (1 == 1 ? 11 : 12)) {
 
125
                        return 1;
 
126
                } else if (a == (2 * 3) + 1 || a == 6 / 2) {
 
127
                        return 2;
 
128
                } else if (a == null || a == (int)(10L + 2) || a == default(int) || a == sizeof(int)) {
 
129
                        return 3;               
 
130
                } else {
 
131
                        return -1;
 
132
                }
 
133
        }
 
134
}", @"
 
135
class TestClass
 
136
{
 
137
        int TestMethod (int? a)
 
138
        {
 
139
                switch (a) {
 
140
                case (1 == 1 ? 11 : 12):
 
141
                        return 1;
 
142
                case (2 * 3) + 1:
 
143
                case 6 / 2:
 
144
                        return 2;
 
145
                case null:
 
146
                case (int)(10L + 2):
 
147
                case default(int):
 
148
                case sizeof(int):
 
149
                        return 3;
 
150
                default:
 
151
                        return -1;
 
152
                }
 
153
        }
 
154
}");
 
155
 
 
156
 
 
157
                        Test<ConvertIfToSwitchAction> (@"
 
158
class TestClass
 
159
{
 
160
        const int b = 0;
 
161
        int TestMethod (int a)
 
162
        {
 
163
                const int c = 1;
 
164
                $if (a == b) {
 
165
                        return 1;
 
166
                } else if (a == b + c) {
 
167
                        return 0;
 
168
                } else {
 
169
                        return -1;
 
170
                }
 
171
        }
 
172
}", @"
 
173
class TestClass
 
174
{
 
175
        const int b = 0;
 
176
        int TestMethod (int a)
 
177
        {
 
178
                const int c = 1;
 
179
                switch (a) {
 
180
                case b:
 
181
                        return 1;
 
182
                case b + c:
 
183
                        return 0;
 
184
                default:
 
185
                        return -1;
 
186
                }
 
187
        }
 
188
}");
 
189
                }
 
190
 
 
191
                [Test]
 
192
                public void TestNestedOr ()
 
193
                {
 
194
                        Test<ConvertIfToSwitchAction> (@"
 
195
class TestClass
 
196
{
 
197
        int TestMethod (int a)
 
198
        {
 
199
                $if (a == 0) {
 
200
                        return 1;
 
201
                } else if ((a == 2 || a == 4) || (a == 3 || a == 5)) {
 
202
                        return 2;
 
203
                } else {
 
204
                        return -1;
 
205
                }
 
206
        }
 
207
}", @"
 
208
class TestClass
 
209
{
 
210
        int TestMethod (int a)
 
211
        {
 
212
                switch (a) {
 
213
                case 0:
 
214
                        return 1;
 
215
                case 2:
 
216
                case 4:
 
217
                case 3:
 
218
                case 5:
 
219
                        return 2;
 
220
                default:
 
221
                        return -1;
 
222
                }
 
223
        }
 
224
}");
 
225
                }
 
226
 
 
227
                [Test]
 
228
                public void TestComplexSwitchExpression ()
 
229
                {
 
230
                        Test<ConvertIfToSwitchAction> (@"
 
231
class TestClass
 
232
{
 
233
        int TestMethod (int a, int b)
 
234
        {
 
235
                $if (a + b == 0) {
 
236
                        return 1;
 
237
                } else if (1 == a + b) {
 
238
                        return 0;
 
239
                } else {
 
240
                        return -1;
 
241
                }
 
242
        }
 
243
}", @"
 
244
class TestClass
 
245
{
 
246
        int TestMethod (int a, int b)
 
247
        {
 
248
                switch (a + b) {
 
249
                case 0:
 
250
                        return 1;
 
251
                case 1:
 
252
                        return 0;
 
253
                default:
 
254
                        return -1;
 
255
                }
 
256
        }
 
257
}");
 
258
                }
 
259
 
 
260
                [Test]
 
261
                public void TestNonConstantExpression ()
 
262
                {
 
263
                        TestWrongContext<ConvertIfToSwitchAction> (@"
 
264
class TestClass
 
265
{
 
266
        void TestMethod (int a, int c)
 
267
        {
 
268
                int b;
 
269
                $if (a == 0) {
 
270
                        b = 0;
 
271
                } else if (a == c) {
 
272
                        b = 1;
 
273
                } else if (a == 2 || a == 3) {
 
274
                        b = 2;
 
275
                } else {
 
276
                        b = 3;
 
277
                }
 
278
        }
 
279
}");
 
280
                        TestWrongContext<ConvertIfToSwitchAction> (@"
 
281
class TestClass
 
282
{
 
283
        void TestMethod (int a, int c)
 
284
        {
 
285
                int b;
 
286
                $if (a == c) {
 
287
                        b = 0;
 
288
                } else if (a == 1) {
 
289
                        b = 1;
 
290
                } else if (a == 2 || a == 3) {
 
291
                        b = 2;
 
292
                } else {
 
293
                        b = 3;
 
294
                }
 
295
        }
 
296
}");
 
297
                        TestWrongContext<ConvertIfToSwitchAction> (@"
 
298
class TestClass
 
299
{
 
300
        void TestMethod (int a, int c)
 
301
        {
 
302
                int b;
 
303
                $if (a == 0) {
 
304
                        b = 0;
 
305
                } else if (a == 1) {
 
306
                        b = 1;
 
307
                } else if (a == 2 || a == c) {
 
308
                        b = 2;
 
309
                } else {
 
310
                        b = 3;
 
311
                }
 
312
        }
 
313
}");
 
314
                }
 
315
                
 
316
                [Test]
 
317
                public void TestNonEqualityComparison ()
 
318
                {
 
319
                        TestWrongContext<ConvertIfToSwitchAction> (@"
 
320
class TestClass
 
321
{
 
322
        void TestMethod (int a)
 
323
        {
 
324
                int b;
 
325
                $if (a == 0) {
 
326
                        b = 0;
 
327
                } else if (a > 4) {
 
328
                        b = 1;
 
329
                } else if (a == 2 || a == 3) {
 
330
                        b = 2;
 
331
                } else {
 
332
                        b = 3;
 
333
                }
 
334
        }
 
335
}");
 
336
                }
 
337
 
 
338
                [Test]
 
339
                public void TestValidType ()
 
340
                {
 
341
                        // enum
 
342
                        Test<ConvertIfToSwitchAction> (@"
 
343
enum TestEnum
 
344
{
 
345
        First,
 
346
        Second,
 
347
}
 
348
class TestClass
 
349
{
 
350
        int TestMethod (TestEnum a)
 
351
        {
 
352
                $if (a == TestEnum.First) {
 
353
                        return 1;
 
354
                } else {
 
355
                        return -1;
 
356
                }
 
357
        }
 
358
}", @"
 
359
enum TestEnum
 
360
{
 
361
        First,
 
362
        Second,
 
363
}
 
364
class TestClass
 
365
{
 
366
        int TestMethod (TestEnum a)
 
367
        {
 
368
                switch (a) {
 
369
                case TestEnum.First:
 
370
                        return 1;
 
371
                default:
 
372
                        return -1;
 
373
                }
 
374
        }
 
375
}");
 
376
 
 
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");
 
389
                }
 
390
 
 
391
                void TestValidType (string type, string caseValue)
 
392
                {
 
393
                        Test<ConvertIfToSwitchAction> (@"
 
394
class TestClass
 
395
{
 
396
        int TestMethod (" + type + @" a)
 
397
        {
 
398
                $if (a == " + caseValue + @") {
 
399
                        return 1;
 
400
                } else {
 
401
                        return -1;
 
402
                }
 
403
        }
 
404
}", @"
 
405
class TestClass
 
406
{
 
407
        int TestMethod (" + type + @" a)
 
408
        {
 
409
                switch (a) {
 
410
                case " + caseValue + @":
 
411
                        return 1;
 
412
                default:
 
413
                        return -1;
 
414
                }
 
415
        }
 
416
}");
 
417
                }
 
418
 
 
419
                [Test]
 
420
                public void TestInvalidType ()
 
421
                {
 
422
                        TestWrongContext<ConvertIfToSwitchAction> (@"
 
423
class TestClass
 
424
{
 
425
        void TestMethod (double a)
 
426
        {
 
427
                int b;
 
428
                $if (a == 0) {
 
429
                        b = 0;
 
430
                } else {
 
431
                        b = 3;
 
432
                }
 
433
        }
 
434
}");
 
435
                }
 
436
 
 
437
                [Test]
 
438
                public void TestNoElse()
 
439
                {
 
440
                        Test<ConvertIfToSwitchAction> (@"
 
441
class TestClass
 
442
{
 
443
        void TestMethod (int a)
 
444
        {
 
445
                int b;
 
446
                $if (a == 0) {
 
447
                        b = 0;
 
448
                } else if (a == 1) {
 
449
                        b = 1;
 
450
                }
 
451
        }
 
452
}", @"
 
453
class TestClass
 
454
{
 
455
        void TestMethod (int a)
 
456
        {
 
457
                int b;
 
458
                switch (a) {
 
459
                case 0:
 
460
                        b = 0;
 
461
                        break;
 
462
                case 1:
 
463
                        b = 1;
 
464
                        break;
 
465
                }
 
466
        }
 
467
}");
 
468
                }
 
469
        
 
470
        }
 
471
}