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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertForeachToForTests.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
// ConvertForeachToForTests.cs
 
3
//  
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin Inc.
 
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 System;
 
27
using NUnit.Framework;
 
28
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
29
 
 
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
 
31
{
 
32
        [TestFixture]
 
33
        public class ConvertForeachToForTests : ContextActionTestBase
 
34
        {
 
35
                [Test()]
 
36
                public void TestArray ()
 
37
                {
 
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 +
 
49
                                "}"
 
50
                        );
 
51
                        
 
52
                        Assert.AreEqual (
 
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 +
 
63
                                "}", result);
 
64
                }
 
65
                
 
66
                [Test()]
 
67
                public void TestListOfT ()
 
68
                {
 
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 +
 
81
                                "}"
 
82
                        );
 
83
                        
 
84
                        Assert.AreEqual (
 
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 +
 
96
                                "}", result);
 
97
                }
 
98
                
 
99
                [Test()]
 
100
                public void TestEnumerableOfT ()
 
101
                {
 
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 +
 
113
                                "}"
 
114
                        );
 
115
                }
 
116
 
 
117
                /// <summary>
 
118
                /// Bug 9876 - Convert to for loop created invalid code if iteration variable is called i
 
119
                /// </summary>
 
120
                [Test]
 
121
                public void TestBug9876 ()
 
122
                {
 
123
                        Test<ConvertForeachToForAction> (@"class TestClass
 
124
{
 
125
        void TestMethod ()
 
126
        {
 
127
                $foreach (var i in new[] { 1, 2, 3 }) {
 
128
                        Console.WriteLine (i);
 
129
                }
 
130
        }
 
131
}", @"class TestClass
 
132
{
 
133
        void TestMethod ()
 
134
        {
 
135
                var list = new[] {
 
136
                        1,
 
137
                        2,
 
138
                        3
 
139
                };
 
140
                for (int j = 0; j < list.Length; j++) {
 
141
                        var i = list [j];
 
142
                        Console.WriteLine (i);
 
143
                }
 
144
        }
 
145
}");
 
146
                }
 
147
        }
 
148
}
 
 
b'\\ No newline at end of file'