PageRenderTime 244ms CodeModel.GetById 127ms app.highlight 5ms RepoModel.GetById 109ms app.codeStats 0ms

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

https://github.com/pombredanne/SharpDevelop
C# | 309 lines | 272 code | 12 blank | 25 comment | 0 complexity | b8b6c3fefb1e3713ffa8c6e6c95d987b MD5 | raw file
  1// 
  2// DontUseLinqWhenItsVerboseAndInefficientTests.cs
  3// 
  4// Author:
  5//      Luís Reis <luiscubal@gmail.com>
  6// 
  7// Copyright (c) 2013 Luís Reis
  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
 27using ICSharpCode.NRefactory.CSharp.CodeActions;
 28using ICSharpCode.NRefactory.CSharp.Refactoring;
 29using NUnit.Framework;
 30
 31namespace ICSharpCode.NRefactory.CSharp.CodeIssues
 32{
 33	[TestFixture]
 34	public class DontUseLinqWhenItsVerboseAndInefficientTests : InspectionActionTestBase
 35	{
 36		[Test]
 37		public void TestStringLength()
 38		{
 39			Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
 40using System.Linq;
 41class TestClass
 42{
 43	int TestMethod()
 44	{
 45		string x = ""Hello"";
 46		return x.Count ();
 47	}
 48}", @"
 49using System.Linq;
 50class TestClass
 51{
 52	int TestMethod()
 53	{
 54		string x = ""Hello"";
 55		return x.Length;
 56	}
 57}");
 58		}
 59
 60		[Test]
 61		public void TestArrayCount()
 62		{
 63			Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
 64using System.Linq;
 65class TestClass
 66{
 67	int TestMethod()
 68	{
 69		int[] x = { 1, 2, 3 };
 70		return x.Count ();
 71	}
 72}", @"
 73using System.Linq;
 74class TestClass
 75{
 76	int TestMethod()
 77	{
 78		int[] x = { 1, 2, 3 };
 79		return x.Length;
 80	}
 81}");
 82		}
 83
 84		[Test]
 85		public void TestStaticMethod()
 86		{
 87			TestWrongContext<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
 88using System.Linq;
 89class TestClass
 90{
 91	int TestMethod()
 92	{
 93		int[] x = { 1, 2, 3 };
 94		return Enumerable.Count<int> (x);
 95	}
 96}");
 97		}
 98
 99		[Test]
100		public void TestListCount()
101		{
102			Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
103using System.Linq;
104using System.Collections.Generic;
105class TestClass
106{
107	int TestMethod()
108	{
109		IList<int> x = new List<int> ();
110		return x.Count ();
111	}
112}", @"
113using System.Linq;
114using System.Collections.Generic;
115class TestClass
116{
117	int TestMethod()
118	{
119		IList<int> x = new List<int> ();
120		return x.Count;
121	}
122}");
123		}
124
125		[Test]
126		public void TestListFirst()
127		{
128			Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
129using System.Linq;
130using System.Collections.Generic;
131class TestClass
132{
133	int TestMethod()
134	{
135		IList<int> x = new List<int> ();
136		return x.First ();
137	}
138}", @"
139using System.Linq;
140using System.Collections.Generic;
141class TestClass
142{
143	int TestMethod()
144	{
145		IList<int> x = new List<int> ();
146		return x [0];
147	}
148}");
149		}
150
151		[Test]
152		public void TestListElementAt()
153		{
154			Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
155using System.Linq;
156using System.Collections.Generic;
157class TestClass
158{
159	int TestMethod()
160	{
161		IList<int> x = new List<int> ();
162		return x.ElementAt (1);
163	}
164}", @"
165using System.Linq;
166using System.Collections.Generic;
167class TestClass
168{
169	int TestMethod()
170	{
171		IList<int> x = new List<int> ();
172		return x [1];
173	}
174}");
175		}
176
177		[Test]
178		public void TestListLast()
179		{
180			TestWrongContext<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
181using System.Linq;
182using System.Collections.Generic;
183class TestClass
184{
185	int TestMethod()
186	{
187		IList<int> x = new List<int> ();
188		return x.Last ();
189	}
190}");
191		}
192
193		[Test]
194		public void TestListSkippedReversedLast()
195		{
196			Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
197using System.Linq;
198using System.Collections.Generic;
199class TestClass
200{
201	int TestMethod()
202	{
203		int[] x = new int [10];
204		return x.Skip (5).Reverse ().Last ();
205	}
206}", @"
207using System.Linq;
208using System.Collections.Generic;
209class TestClass
210{
211	int TestMethod()
212	{
213		int[] x = new int [10];
214		return x [5];
215	}
216}");
217		}
218
219		[Test]
220		public void TestListSkippedLast()
221		{
222			Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
223using System.Linq;
224using System.Collections.Generic;
225class TestClass
226{
227	int TestMethod()
228	{
229		int[] x = new int [10];
230		return x.Skip (5).Reverse ().Skip (5).Reverse ().Last ();
231	}
232}", @"
233using System.Linq;
234using System.Collections.Generic;
235class TestClass
236{
237	int TestMethod()
238	{
239		int[] x = new int [10];
240		return x [x.Length - 1 - (5)];
241	}
242}");
243		}
244
245		[Test]
246		public void TestListSkip()
247		{
248			Test<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
249using System.Linq;
250using System.Collections.Generic;
251class TestClass
252{
253	int TestMethod()
254	{
255		IList<int> x = new List<int> ();
256		int y = 5;
257		return x.Skip (y).First ();
258	}
259}", @"
260using System.Linq;
261using System.Collections.Generic;
262class TestClass
263{
264	int TestMethod()
265	{
266		IList<int> x = new List<int> ();
267		int y = 5;
268		return x [y];
269	}
270}");
271		}
272
273		[Test]
274		public void TestDisabledForNoCount()
275		{
276			TestWrongContext<DontUseLinqWhenItsVerboseAndInefficientIssue>(@"
277using System.Linq;
278using System.Collections;
279using System.Collections.Generic;
280class MyCollection : ICollection<int>
281{
282	int ICollection<int>.Count { get { return 0; } }
283	bool ICollection<int>.IsReadOnly { get { return true; } }
284
285	public IEnumerator<int> GetEnumerator()
286	{
287		return null;
288	}
289	IEnumerator IEnumerable.GetEnumerator()
290	{
291		return GetEnumerator();
292	}
293
294	public void Add(int item) { }
295	public bool Contains(int item) { return false; }
296	public void CopyTo(int[] array, int arrayIndex) {}
297	public bool Remove(int item) { return false; }
298}
299class TestClass
300{
301	int TestMethod()
302	{
303		MyCollection x = new MyCollection ();
304		return x.Count ();
305	}
306}");
307		}
308	}
309}