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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/SimplifyAnonymousMethodToDelegateIssueTests.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
// SimplifyAnonymousMethodToDelegateIssueTests.cs
 
3
//
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@xamarin.com>
 
6
//
 
7
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.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
using System;
 
27
using NUnit.Framework;
 
28
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
29
using ICSharpCode.NRefactory.CSharp.CodeActions;
 
30
 
 
31
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
 
32
{
 
33
        [TestFixture]
 
34
        public class SimplifyAnonymousMethodToDelegateIssueTests : InspectionActionTestBase
 
35
        {
 
36
                [Test]
 
37
                public void TestSimpleLambda ()
 
38
                {
 
39
                        var input = @"class Foo
 
40
{
 
41
        void Bar (string str)
 
42
        {
 
43
                var action = $(foo, bar) => MyMethod (foo, bar);
 
44
        }
 
45
}";
 
46
                        
 
47
                        TestRefactoringContext context;
 
48
                        var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
 
49
                        Assert.AreEqual (1, issues.Count);
 
50
                        CheckFix (context, issues, @"class Foo
 
51
{
 
52
        void Bar (string str)
 
53
        {
 
54
                var action = MyMethod;
 
55
        }
 
56
}");
 
57
                }
 
58
 
 
59
                [Test]
 
60
                public void TestLambdaWithBody ()
 
61
                {
 
62
                        var input = @"class Foo
 
63
{
 
64
        void Bar (string str)
 
65
        {
 
66
                var action = $(foo, bar) => { return MyMethod (foo, bar); };
 
67
        }
 
68
}";
 
69
                        
 
70
                        TestRefactoringContext context;
 
71
                        var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
 
72
                        Assert.AreEqual (1, issues.Count);
 
73
                        CheckFix (context, issues, @"class Foo
 
74
{
 
75
        void Bar (string str)
 
76
        {
 
77
                var action = MyMethod;
 
78
        }
 
79
}");
 
80
                }
 
81
 
 
82
                [Test]
 
83
                public void TestSimpleInvalidLambda ()
 
84
                {
 
85
                        var input = @"class Foo
 
86
{
 
87
        void Bar (string str)
 
88
        {
 
89
                var action = $(foo, bar) => MyMethod (bar, foo);
 
90
        }
 
91
}";
 
92
                        
 
93
                        TestRefactoringContext context;
 
94
                        var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
 
95
                        Assert.AreEqual (0, issues.Count);
 
96
                }
 
97
 
 
98
                [Test]
 
99
                public void TestSimpleAnonymousMethod ()
 
100
                {
 
101
                        var input = @"class Foo
 
102
{
 
103
        int MyMethod (int x, int y) { return x * y; }
 
104
 
 
105
        void Bar (string str)
 
106
        {
 
107
                var action = $delegate(int foo, int bar) { return MyMethod (foo, bar); };
 
108
        }
 
109
}";
 
110
                        
 
111
                        TestRefactoringContext context;
 
112
                        var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
 
113
                        Assert.AreEqual (1, issues.Count);
 
114
                        CheckFix (context, issues, @"class Foo
 
115
{
 
116
        int MyMethod (int x, int y) { return x * y; }
 
117
 
 
118
        void Bar (string str)
 
119
        {
 
120
                var action = MyMethod;
 
121
        }
 
122
}");
 
123
                }
 
124
 
 
125
                [Test]
 
126
                public void TestSkipComplexCase ()
 
127
                {
 
128
                        var input = @"using System;
 
129
using System.Linq;
 
130
 
 
131
class Foo
 
132
{
 
133
        int MyMethod (int x, int y) { return x * y; }
 
134
 
 
135
        void Bar (string str)
 
136
        {
 
137
                var action = $() => str.Where (c => c != 'a').ToArray ();
 
138
        }
 
139
}";
 
140
                        
 
141
                        TestRefactoringContext context;
 
142
                        var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
 
143
                        Assert.AreEqual (0, issues.Count);
 
144
                }
 
145
 
 
146
                [Test]
 
147
                public void TestComplexCase ()
 
148
                {
 
149
                        var input = @"class Foo
 
150
{
 
151
        int MyMethod (int x, int y) { return x * y; }
 
152
 
 
153
        void Bar (string str)
 
154
        {
 
155
                var action = $delegate(int foo, int bar) { return MyMethod (bar, foo); };
 
156
        }
 
157
}";
 
158
                        
 
159
                        TestRefactoringContext context;
 
160
                        var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
 
161
                        Assert.AreEqual (0, issues.Count);
 
162
                }
 
163
        }
 
164
}
 
165