2
// ConvertForeachToForTests.cs
5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc.
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
27
using NUnit.Framework;
28
using ICSharpCode.NRefactory.CSharp.Refactoring;
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
33
public class ConvertForeachToForTests : ContextActionTestBase
36
public void TestArray ()
38
string result = RunContextAction (
39
new ConvertForeachToForAction (),
40
"using System;" + Environment.NewLine +
41
"class TestClass" + Environment.NewLine +
42
"{" + Environment.NewLine +
43
" void Test (string[] args)" + Environment.NewLine +
44
" {" + Environment.NewLine +
45
" $foreach (var v in args) {" + Environment.NewLine +
46
" Console.WriteLine (v);" + Environment.NewLine +
47
" }" + Environment.NewLine +
48
" }" + Environment.NewLine +
53
"using System;" + Environment.NewLine +
54
"class TestClass" + Environment.NewLine +
55
"{" + Environment.NewLine +
56
" void Test (string[] args)" + Environment.NewLine +
57
" {" + Environment.NewLine +
58
" for (int i = 0; i < args.Length; i++) {" + Environment.NewLine +
59
" var v = args [i];" + Environment.NewLine +
60
" Console.WriteLine (v);" + Environment.NewLine +
61
" }" + Environment.NewLine +
62
" }" + Environment.NewLine +
67
public void TestListOfT ()
69
string result = RunContextAction (
70
new ConvertForeachToForAction (),
71
"using System;" + Environment.NewLine +
72
"using System.Collections.Generic;" + Environment.NewLine +
73
"class TestClass" + Environment.NewLine +
74
"{" + Environment.NewLine +
75
" void Test (List<string> args)" + Environment.NewLine +
76
" {" + Environment.NewLine +
77
" $foreach (var v in args) {" + Environment.NewLine +
78
" Console.WriteLine (v);" + Environment.NewLine +
79
" }" + Environment.NewLine +
80
" }" + Environment.NewLine +
85
"using System;" + Environment.NewLine +
86
"using System.Collections.Generic;" + Environment.NewLine +
87
"class TestClass" + Environment.NewLine +
88
"{" + Environment.NewLine +
89
" void Test (List<string> args)" + Environment.NewLine +
90
" {" + Environment.NewLine +
91
" for (int i = 0; i < args.Count; i++) {" + Environment.NewLine +
92
" var v = args [i];" + Environment.NewLine +
93
" Console.WriteLine (v);" + Environment.NewLine +
94
" }" + Environment.NewLine +
95
" }" + Environment.NewLine +
100
public void TestEnumerableOfT ()
102
TestWrongContext<ConvertForeachToForAction> (
103
"using System;" + Environment.NewLine +
104
"using System.Collections.Generic;" + Environment.NewLine +
105
"class TestClass" + Environment.NewLine +
106
"{" + Environment.NewLine +
107
" void Test (IEnumerable<string> args)" + Environment.NewLine +
108
" {" + Environment.NewLine +
109
" $foreach (var v in args) {" + Environment.NewLine +
110
" Console.WriteLine (v);" + Environment.NewLine +
111
" }" + Environment.NewLine +
112
" }" + Environment.NewLine +
118
/// Bug 9876 - Convert to for loop created invalid code if iteration variable is called i
121
public void TestBug9876 ()
123
Test<ConvertForeachToForAction> (@"class TestClass
127
$foreach (var i in new[] { 1, 2, 3 }) {
128
Console.WriteLine (i);
131
}", @"class TestClass
140
for (int j = 0; j < list.Length; j++) {
142
Console.WriteLine (i);
b'\\ No newline at end of file'