PageRenderTime 329ms CodeModel.GetById 171ms app.highlight 3ms RepoModel.GetById 152ms app.codeStats 0ms

/src/Libraries/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/ReplaceWithOfTypeIssueTests.cs

https://github.com/pombredanne/SharpDevelop
C# | 221 lines | 181 code | 15 blank | 25 comment | 0 complexity | 8f8395ffbdeeed9ec1e68e1f4aa6305a MD5 | raw file
  1//
  2// ReplaceWithOfTypeIssueTests.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.
 26using System;
 27using NUnit.Framework;
 28using ICSharpCode.NRefactory.CSharp.Refactoring;
 29using ICSharpCode.NRefactory.CSharp.CodeActions;
 30using System.Linq;
 31
 32namespace ICSharpCode.NRefactory.CSharp.CodeIssues
 33{
 34	[TestFixture]
 35	public class ReplaceWithOfTypeIssueTests : InspectionActionTestBase
 36	{
 37		[Test]
 38		public void TestCaseSelectNotNull ()
 39		{
 40			Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
 41class Test
 42{
 43	public void Foo(object[] obj)
 44	{
 45		obj.SelectNotNull((object o) => o as Test);
 46	}
 47}", @"using System.Linq;
 48class Test
 49{
 50	public void Foo(object[] obj)
 51	{
 52		obj.OfType<Test> ();
 53	}
 54}");
 55			Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
 56class Test
 57{
 58	public void Foo(object[] obj)
 59	{
 60		obj.SelectNotNull(o => o as Test);
 61	}
 62}", @"using System.Linq;
 63class Test
 64{
 65	public void Foo(object[] obj)
 66	{
 67		obj.OfType<Test> ();
 68	}
 69}");
 70		}
 71
 72		[Test]
 73		public void TestCaseSelectNotNullWithParentheses ()
 74		{
 75			Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
 76class Test
 77{
 78	public void Foo(object[] obj)
 79	{
 80		obj.SelectNotNull(o => ((o as Test)));
 81	}
 82}", @"using System.Linq;
 83class Test
 84{
 85	public void Foo(object[] obj)
 86	{
 87		obj.OfType<Test> ();
 88	}
 89}");
 90			Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
 91class Test
 92{
 93	public void Foo(object[] obj)
 94	{
 95		obj.SelectNotNull(o => o as Test);
 96	}
 97}", @"using System.Linq;
 98class Test
 99{
100	public void Foo(object[] obj)
101	{
102		obj.OfType<Test> ();
103	}
104}");
105		}
106
107
108		[Test]
109		public void TestCaseSelectWhereCase1 ()
110		{
111			Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
112class Test
113{
114	public void Foo(object[] obj)
115	{
116		obj.Where(o => o is Test).Select (o => o as Test);
117	}
118}", @"using System.Linq;
119class Test
120{
121	public void Foo(object[] obj)
122	{
123		obj.OfType<Test> ();
124	}
125}");
126			Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
127class Test
128{
129	public void Foo(object[] obj)
130	{
131		obj.SelectNotNull(o => o as Test);
132	}
133}",@"using System.Linq;
134class Test
135{
136	public void Foo(object[] obj)
137	{
138		obj.OfType<Test> ();
139	}
140}");
141		}
142
143		[Test]
144		public void TestCaseSelectWhereGabage ()
145		{
146			TestWrongContext<ReplaceWithOfTypeIssue>(@"using System.Linq;
147class Test
148{
149	public void Foo(object[] obj)
150	{
151		obj.Where(o => o is Test).Select (x => o as Test);
152	}
153}");
154			TestWrongContext<ReplaceWithOfTypeIssue>(@"using System.Linq;
155class Test
156{
157	public void Foo(object[] obj)
158	{
159		obj.SelectNotNull(o => null as Test);
160	}
161}");
162		}
163
164
165		[Test]
166		public void TestCaseSelectWhereCase2WithParens ()
167		{
168			Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
169class Test
170{
171	public void Foo(object[] obj)
172	{
173		obj.Where(o => (o is Test)).Select (o => ((Test)(o)));
174	}
175}", @"using System.Linq;
176class Test
177{
178	public void Foo(object[] obj)
179	{
180		obj.OfType<Test> ();
181	}
182}");
183			Test<ReplaceWithOfTypeIssue>(@"using System.Linq;
184class Test
185{
186	public void Foo(object[] obj)
187	{
188		obj.SelectNotNull(o => o as Test);
189	}
190}", @"using System.Linq;
191class Test
192{
193	public void Foo(object[] obj)
194	{
195		obj.OfType<Test> ();
196	}
197}");
198		}
199
200
201
202		[Test]
203		public void TestDisable ()
204		{
205			TestWrongContext<ReplaceWithOfTypeIssue>(@"using System.Linq;
206class Test
207{
208	public void Foo(object[] obj)
209	{
210		// ReSharper disable once ReplaceWithOfType
211		obj.SelectNotNull((object o) => o as Test);
212	}
213}");
214		}
215
216
217
218	}
219	
220}
221