2
// SimplifyAnonymousMethodToDelegateIssueTests.cs
5
// Mike Krüger <mkrueger@xamarin.com>
7
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
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;
29
using ICSharpCode.NRefactory.CSharp.CodeActions;
31
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
34
public class SimplifyAnonymousMethodToDelegateIssueTests : InspectionActionTestBase
37
public void TestSimpleLambda ()
39
var input = @"class Foo
43
var action = $(foo, bar) => MyMethod (foo, bar);
47
TestRefactoringContext context;
48
var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
49
Assert.AreEqual (1, issues.Count);
50
CheckFix (context, issues, @"class Foo
54
var action = MyMethod;
60
public void TestLambdaWithBody ()
62
var input = @"class Foo
66
var action = $(foo, bar) => { return MyMethod (foo, bar); };
70
TestRefactoringContext context;
71
var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
72
Assert.AreEqual (1, issues.Count);
73
CheckFix (context, issues, @"class Foo
77
var action = MyMethod;
83
public void TestSimpleInvalidLambda ()
85
var input = @"class Foo
89
var action = $(foo, bar) => MyMethod (bar, foo);
93
TestRefactoringContext context;
94
var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
95
Assert.AreEqual (0, issues.Count);
99
public void TestSimpleAnonymousMethod ()
101
var input = @"class Foo
103
int MyMethod (int x, int y) { return x * y; }
105
void Bar (string str)
107
var action = $delegate(int foo, int bar) { return MyMethod (foo, bar); };
111
TestRefactoringContext context;
112
var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
113
Assert.AreEqual (1, issues.Count);
114
CheckFix (context, issues, @"class Foo
116
int MyMethod (int x, int y) { return x * y; }
118
void Bar (string str)
120
var action = MyMethod;
126
public void TestSkipComplexCase ()
128
var input = @"using System;
133
int MyMethod (int x, int y) { return x * y; }
135
void Bar (string str)
137
var action = $() => str.Where (c => c != 'a').ToArray ();
141
TestRefactoringContext context;
142
var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
143
Assert.AreEqual (0, issues.Count);
147
public void TestComplexCase ()
149
var input = @"class Foo
151
int MyMethod (int x, int y) { return x * y; }
153
void Bar (string str)
155
var action = $delegate(int foo, int bar) { return MyMethod (bar, foo); };
159
TestRefactoringContext context;
160
var issues = GetIssues (new SimplifyAnonymousMethodToDelegateIssue (), input, out context);
161
Assert.AreEqual (0, issues.Count);