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

« back to all changes in this revision

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