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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/IterateViaForeachTests.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
// IterateViaForeachTests.cs
 
3
//
 
4
// Author:
 
5
//       Simon Lindgren <simon.n.lindgren@gmail.com>
 
6
//
 
7
// Copyright (c) 2012 Simon Lindgren
 
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
using NUnit.Framework;
 
27
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
28
using System.Linq;
 
29
 
 
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
 
31
{
 
32
        [TestFixture]
 
33
        public class IterateViaForeachTests : ContextActionTestBase
 
34
        {
 
35
                [Test]
 
36
                public void HandlesNonGenericCase()
 
37
                {
 
38
                        Test<IterateViaForeachAction>(@"
 
39
using System.Collections;
 
40
class TestClass
 
41
{
 
42
        public void F()
 
43
        {
 
44
                var $list = new ArrayList ();
 
45
        }
 
46
}", @"
 
47
using System.Collections;
 
48
class TestClass
 
49
{
 
50
        public void F()
 
51
        {
 
52
                var list = new ArrayList ();
 
53
                foreach (var o in list) {
 
54
                }
 
55
        }
 
56
}");
 
57
                }
 
58
 
 
59
                [Test]
 
60
                public void HandlesExpressionStatement()
 
61
                {
 
62
                        Test<IterateViaForeachAction>(@"
 
63
using System.Collections.Generic;
 
64
class TestClass
 
65
{
 
66
        public IEnumerable<int> GetInts()
 
67
        {
 
68
                return new int[] { };
 
69
        }
 
70
 
 
71
        public void F()
 
72
        {
 
73
                GetInts$();
 
74
        }
 
75
}", @"
 
76
using System.Collections.Generic;
 
77
class TestClass
 
78
{
 
79
        public IEnumerable<int> GetInts()
 
80
        {
 
81
                return new int[] { };
 
82
        }
 
83
 
 
84
        public void F()
 
85
        {
 
86
                foreach (var i in GetInts ()) {
 
87
                }
 
88
        }
 
89
}");
 
90
                }
 
91
 
 
92
                [Test]
 
93
                public void HandlesAssignmentExpressionStatement()
 
94
                {
 
95
                        Test<IterateViaForeachAction>(@"
 
96
using System.Collections.Generic;
 
97
class TestClass
 
98
{
 
99
        public IEnumerable<int> GetInts ()
 
100
        {
 
101
                return new int[] { };
 
102
        }
 
103
 
 
104
        public void F()
 
105
        {
 
106
                IEnumerable<int> ints;
 
107
                ints = GetIn$ts ();
 
108
        }
 
109
}", @"
 
110
using System.Collections.Generic;
 
111
class TestClass
 
112
{
 
113
        public IEnumerable<int> GetInts ()
 
114
        {
 
115
                return new int[] { };
 
116
        }
 
117
 
 
118
        public void F()
 
119
        {
 
120
                IEnumerable<int> ints;
 
121
                ints = GetInts ();
 
122
                foreach (var i in ints) {
 
123
                }
 
124
        }
 
125
}");
 
126
                }
 
127
 
 
128
                [Test]
 
129
                public void HandlesAsExpressionStatement()
 
130
                {
 
131
                        Test<IterateViaForeachAction>(@"
 
132
using System.Collections.Generic;
 
133
class TestClass
 
134
{
 
135
        public void F()
 
136
        {
 
137
                object s = """";
 
138
                s as IEnumerable$<char>;
 
139
        }
 
140
}", @"
 
141
using System.Collections.Generic;
 
142
class TestClass
 
143
{
 
144
        public void F()
 
145
        {
 
146
                object s = """";
 
147
                foreach (var c in s as IEnumerable<char>) {
 
148
                }
 
149
        }
 
150
}", 0, true);
 
151
                }
 
152
 
 
153
                [Test]
 
154
                public void NonKnownTypeNamingTest()
 
155
                {
 
156
                        Test<IterateViaForeachAction>(@"
 
157
using System.Collections.Generic;
 
158
class TestClass
 
159
{
 
160
        public void F()
 
161
        {
 
162
                var items$ = new List<TestClass> ();
 
163
        }
 
164
}", @"
 
165
using System.Collections.Generic;
 
166
class TestClass
 
167
{
 
168
        public void F()
 
169
        {
 
170
                var items = new List<TestClass> ();
 
171
                foreach (var testClass in items) {
 
172
                }
 
173
        }
 
174
}");
 
175
                }
 
176
 
 
177
                [Test]
 
178
                public void HandlesAsExpression()
 
179
                {
 
180
                        Test<IterateViaForeachAction>(@"
 
181
using System.Collections.Generic;
 
182
class TestClass
 
183
{
 
184
        public void F()
 
185
        {
 
186
                object s = """";
 
187
                s as IEnumerable$<char>
 
188
        }
 
189
}", @"
 
190
using System.Collections.Generic;
 
191
class TestClass
 
192
{
 
193
        public void F()
 
194
        {
 
195
                object s = """";
 
196
                foreach (var c in s as IEnumerable<char>) {
 
197
                }
 
198
        }
 
199
}", 0, true);
 
200
                }
 
201
 
 
202
                [Test]
 
203
                public void HandlesLinqExpressionAssignment()
 
204
                {
 
205
                        Test<IterateViaForeachAction>(@"
 
206
using System.Collections.Generic;
 
207
using System.Linq;
 
208
class TestClass
 
209
{
 
210
        public IEnumerable<int> GetInts()
 
211
        {
 
212
                return new int[] { };
 
213
        }
 
214
 
 
215
        public void F()
 
216
        {
 
217
                var $filteredInts = from item in GetInts ()
 
218
                                                        where item > 0
 
219
                                                        select item;
 
220
        }
 
221
}", @"
 
222
using System.Collections.Generic;
 
223
using System.Linq;
 
224
class TestClass
 
225
{
 
226
        public IEnumerable<int> GetInts()
 
227
        {
 
228
                return new int[] { };
 
229
        }
 
230
 
 
231
        public void F()
 
232
        {
 
233
                var filteredInts = from item in GetInts ()
 
234
                                                        where item > 0
 
235
                                                        select item;
 
236
                foreach (var i in filteredInts) {
 
237
                }
 
238
        }
 
239
}");
 
240
                }
 
241
 
 
242
                [Test]
 
243
                public void IgnoresExpressionsInForeachStatement()
 
244
                {
 
245
                        TestWrongContext<IterateViaForeachAction>(@"
 
246
using System.Collections.Generic;
 
247
class TestClass
 
248
{
 
249
        public IEnumerable<int> GetInts()
 
250
        {
 
251
                return new int[] { };
 
252
        }
 
253
 
 
254
        public void F()
 
255
        {
 
256
                foreach (var i in $GetInts ()) {
 
257
                }
 
258
        }
 
259
}");
 
260
                }
 
261
 
 
262
                [Test]
 
263
                public void IgnoresInitializersInForStatement()
 
264
                {
 
265
                        TestWrongContext<IterateViaForeachAction>(@"
 
266
class TestClass
 
267
{
 
268
        public void F()
 
269
        {
 
270
                for (int[] i = new $int[] {} ;;) {
 
271
                }
 
272
        }
 
273
}");
 
274
                }
 
275
 
 
276
                [Test]
 
277
                public void AddsForToBodyOfUsingStatement()
 
278
                {
 
279
                        Test<IterateViaForeachAction>(@"
 
280
class TestClass
 
281
{
 
282
        public void F()
 
283
        {
 
284
                using (int[] i = new $int[] {}) {
 
285
                }
 
286
        }
 
287
}",@"
 
288
class TestClass
 
289
{
 
290
        public void F()
 
291
        {
 
292
                using (int[] i = new int[] {}) {
 
293
                        foreach (var j in i) {
 
294
                        }
 
295
                }
 
296
        }
 
297
}");
 
298
                }
 
299
 
 
300
                [Test]
 
301
                public void AddsBlockStatementToUsingStatement()
 
302
                {
 
303
                        Test<IterateViaForeachAction>(@"
 
304
class TestClass
 
305
{
 
306
        public void F()
 
307
        {
 
308
                using (int[] i = new $int[] {});
 
309
        }
 
310
}",@"
 
311
class TestClass
 
312
{
 
313
        public void F()
 
314
        {
 
315
                using (int[] i = new int[] {}) {
 
316
                        foreach (var j in i) {
 
317
                        }
 
318
                }
 
319
        }
 
320
}");
 
321
                }
 
322
                
 
323
                [Test]
 
324
                public void IgnoresFieldDeclarations()
 
325
                {
 
326
                        TestWrongContext<IterateViaForeachAction>(@"
 
327
using System.Collections.Generic;
 
328
class TestClass
 
329
{
 
330
        List<int> list = $new List<int>();
 
331
}");
 
332
                }
 
333
        }
 
334
}
 
335