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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantElseIssueTests.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
// RedundantElseIssueTests.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.CodeIssues
 
31
{
 
32
        [TestFixture]
 
33
        public class RedundantElseIssueTests : InspectionActionTestBase
 
34
        {
 
35
 
 
36
                [Test]
 
37
                public void TestReturn ()
 
38
                {
 
39
                        var input = @"
 
40
class TestClass
 
41
{
 
42
        int TestMethod (int i)
 
43
        {
 
44
                if (i > 0)
 
45
                        return 1;
 
46
                else
 
47
                        return 0;
 
48
        }
 
49
}";
 
50
                        var output = @"
 
51
class TestClass
 
52
{
 
53
        int TestMethod (int i)
 
54
        {
 
55
                if (i > 0)
 
56
                        return 1;
 
57
                return 0;
 
58
        }
 
59
}";
 
60
                        Test<RedundantElseIssue> (input, 1, output);
 
61
                }
 
62
 
 
63
                [Test]
 
64
                public void TestBreakLoop ()
 
65
                {
 
66
                        var input = @"
 
67
class TestClass
 
68
{
 
69
        void TestMethod ()
 
70
        {
 
71
                int k = 0;
 
72
                for (int i = 0; i < 10; i++) {
 
73
                        if (i > 5)
 
74
                                break;
 
75
                        else
 
76
                                k++;
 
77
                }
 
78
        }
 
79
}";
 
80
                        var output = @"
 
81
class TestClass
 
82
{
 
83
        void TestMethod ()
 
84
        {
 
85
                int k = 0;
 
86
                for (int i = 0; i < 10; i++) {
 
87
                        if (i > 5)
 
88
                                break;
 
89
                        k++;
 
90
                }
 
91
        }
 
92
}";
 
93
                        Test<RedundantElseIssue> (input, 1, output);
 
94
                }
 
95
 
 
96
                [Test]
 
97
                public void TestContinueLoop ()
 
98
                {
 
99
                        var input = @"
 
100
class TestClass
 
101
{
 
102
        void TestMethod ()
 
103
        {
 
104
                int k = 0;
 
105
                for (int i = 0; i < 10; i++) {
 
106
                        if (i > 5)
 
107
                                continue;
 
108
                        else
 
109
                                k++;
 
110
                }
 
111
        }
 
112
}";
 
113
                        var output = @"
 
114
class TestClass
 
115
{
 
116
        void TestMethod ()
 
117
        {
 
118
                int k = 0;
 
119
                for (int i = 0; i < 10; i++) {
 
120
                        if (i > 5)
 
121
                                continue;
 
122
                        k++;
 
123
                }
 
124
        }
 
125
}";
 
126
                        Test<RedundantElseIssue> (input, 1, output);
 
127
                }
 
128
 
 
129
                [Test]
 
130
                public void TestBlockStatement()
 
131
                {
 
132
                        var input = @"
 
133
class TestClass
 
134
{
 
135
        int TestMethod (int i)
 
136
        {
 
137
                if (i > 0) {
 
138
                        return 1;
 
139
                } else {
 
140
                        return 0;
 
141
                }
 
142
        }
 
143
}";
 
144
                        var output = @"
 
145
class TestClass
 
146
{
 
147
        int TestMethod (int i)
 
148
        {
 
149
                if (i > 0) {
 
150
                        return 1;
 
151
                }
 
152
                return 0;
 
153
        }
 
154
}";
 
155
                        Test<RedundantElseIssue> (input, 1, output);
 
156
                }
 
157
 
 
158
                [Test]
 
159
                public void TestEmptyFalseBlock ()
 
160
                {
 
161
                        var input = @"
 
162
class TestClass
 
163
{
 
164
        void TestMethod (int i)
 
165
        {
 
166
                int a;
 
167
                if (i > 0)
 
168
                        a = 1;
 
169
                else { }
 
170
        }
 
171
}";
 
172
                        var output = @"
 
173
class TestClass
 
174
{
 
175
        void TestMethod (int i)
 
176
        {
 
177
                int a;
 
178
                if (i > 0)
 
179
                        a = 1;
 
180
        }
 
181
}";
 
182
                        Test<RedundantElseIssue> (input, 1, output);
 
183
                }
 
184
 
 
185
                [Test]
 
186
                public void TestNecessaryElse ()
 
187
                {
 
188
 
 
189
                        var input = @"
 
190
class TestClass
 
191
{
 
192
        void TestMethod (int i)
 
193
        {
 
194
                int a;
 
195
                if (i > 0)
 
196
                        a = 1;
 
197
                else
 
198
                        a = 0;
 
199
        }
 
200
}";
 
201
                        Test<RedundantElseIssue> (input, 0);
 
202
                }
 
203
 
 
204
                [Test]
 
205
                public void TestNecessaryElseCase2 ()
 
206
                {
 
207
 
 
208
                        var input = @"
 
209
class TestClass
 
210
{
 
211
        void TestMethod (int i)
 
212
        {
 
213
                int a;
 
214
                while (true) {
 
215
                        if (i > 0) {
 
216
                                a = 1;
 
217
                        } else if (i < 0) {
 
218
                                a = 0;
 
219
                                break;
 
220
                        } else {
 
221
                                break;
 
222
                        }
 
223
                }
 
224
        }
 
225
}";
 
226
                        Test<RedundantElseIssue> (input, 0);
 
227
                }
 
228
        }
 
229
}