2
// IterateViaForeachTests.cs
5
// Simon Lindgren <simon.n.lindgren@gmail.com>
7
// Copyright (c) 2012 Simon Lindgren
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
26
using NUnit.Framework;
27
using ICSharpCode.NRefactory.CSharp.Refactoring;
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
33
public class IterateViaForeachTests : ContextActionTestBase
36
public void HandlesNonGenericCase()
38
Test<IterateViaForeachAction>(@"
39
using System.Collections;
44
var $list = new ArrayList ();
47
using System.Collections;
52
var list = new ArrayList ();
53
foreach (var o in list) {
60
public void HandlesExpressionStatement()
62
Test<IterateViaForeachAction>(@"
63
using System.Collections.Generic;
66
public IEnumerable<int> GetInts()
76
using System.Collections.Generic;
79
public IEnumerable<int> GetInts()
86
foreach (var i in GetInts ()) {
93
public void HandlesAssignmentExpressionStatement()
95
Test<IterateViaForeachAction>(@"
96
using System.Collections.Generic;
99
public IEnumerable<int> GetInts ()
101
return new int[] { };
106
IEnumerable<int> ints;
110
using System.Collections.Generic;
113
public IEnumerable<int> GetInts ()
115
return new int[] { };
120
IEnumerable<int> ints;
122
foreach (var i in ints) {
129
public void HandlesAsExpressionStatement()
131
Test<IterateViaForeachAction>(@"
132
using System.Collections.Generic;
138
s as IEnumerable$<char>;
141
using System.Collections.Generic;
147
foreach (var c in s as IEnumerable<char>) {
154
public void NonKnownTypeNamingTest()
156
Test<IterateViaForeachAction>(@"
157
using System.Collections.Generic;
162
var items$ = new List<TestClass> ();
165
using System.Collections.Generic;
170
var items = new List<TestClass> ();
171
foreach (var testClass in items) {
178
public void HandlesAsExpression()
180
Test<IterateViaForeachAction>(@"
181
using System.Collections.Generic;
187
s as IEnumerable$<char>
190
using System.Collections.Generic;
196
foreach (var c in s as IEnumerable<char>) {
203
public void HandlesLinqExpressionAssignment()
205
Test<IterateViaForeachAction>(@"
206
using System.Collections.Generic;
210
public IEnumerable<int> GetInts()
212
return new int[] { };
217
var $filteredInts = from item in GetInts ()
222
using System.Collections.Generic;
226
public IEnumerable<int> GetInts()
228
return new int[] { };
233
var filteredInts = from item in GetInts ()
236
foreach (var i in filteredInts) {
243
public void IgnoresExpressionsInForeachStatement()
245
TestWrongContext<IterateViaForeachAction>(@"
246
using System.Collections.Generic;
249
public IEnumerable<int> GetInts()
251
return new int[] { };
256
foreach (var i in $GetInts ()) {
263
public void IgnoresInitializersInForStatement()
265
TestWrongContext<IterateViaForeachAction>(@"
270
for (int[] i = new $int[] {} ;;) {
277
public void AddsForToBodyOfUsingStatement()
279
Test<IterateViaForeachAction>(@"
284
using (int[] i = new $int[] {}) {
292
using (int[] i = new int[] {}) {
293
foreach (var j in i) {
301
public void AddsBlockStatementToUsingStatement()
303
Test<IterateViaForeachAction>(@"
308
using (int[] i = new $int[] {});
315
using (int[] i = new int[] {}) {
316
foreach (var j in i) {
324
public void IgnoresFieldDeclarations()
326
TestWrongContext<IterateViaForeachAction>(@"
327
using System.Collections.Generic;
330
List<int> list = $new List<int>();