PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/main/tests/MonoDevelop.CSharpBinding.Tests/Features/CodeCompletion/NR5/CodeCompletionCSharp3Tests.cs

http://github.com/mono/monodevelop
C# | 475 lines | 394 code | 17 blank | 64 comment | 6 complexity | 72f6c4f7037a7268084ac08fcd7a380f MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, CC-BY-SA-3.0, MIT, LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. //
  2. // CodeCompletionCSharp3Tests.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mkrueger@novell.com>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using NUnit.Framework;
  30. using ICSharpCode.NRefactory6.CSharp.Completion;
  31. namespace ICSharpCode.NRefactory6.CSharp.CodeCompletion
  32. {
  33. [TestFixture()]
  34. public class CodeCompletionCSharp3Tests
  35. {
  36. /* Currently fails but works in monodevelop. Seems to be a bug in the unit test somewhere.
  37. [Test()]
  38. public void TestExtensionMethods ()
  39. {
  40. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  41. @"using System;
  42. public static class EMClass
  43. {
  44. public static int ToInt32Ext (this Program s)
  45. {
  46. return Int32.Parse (s);
  47. }
  48. }
  49. class Program
  50. {
  51. static void Main (string[] args)
  52. {
  53. Program s;
  54. int i = s.$
  55. }
  56. }
  57. ");
  58. Assert.IsNotNull (provider, "provider == null");
  59. Assert.IsNotNull (provider.Find ("ToInt32Ext"), "extension method 'ToInt32Ext' not found.");
  60. }
  61. */
  62. [Test()]
  63. public void TestVarLocalVariables ()
  64. {
  65. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  66. @"using System;
  67. class Test
  68. {
  69. public void TestMethod ()
  70. {
  71. }
  72. }
  73. class Program
  74. {
  75. static void Main (string[] args)
  76. {
  77. var t = new Test ();
  78. $t.$
  79. }
  80. }
  81. ");
  82. Assert.IsNotNull (provider, "provider == null");
  83. Assert.IsNotNull (provider.Find ("TestMethod"), "method 'TestMethod' not found.");
  84. }
  85. [Test()]
  86. public void TestVarLoopVariable ()
  87. {
  88. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  89. @"using System;
  90. class Test
  91. {
  92. public void TestMethod ()
  93. {
  94. }
  95. }
  96. class Program
  97. {
  98. static void Main (string[] args)
  99. {
  100. var t = new Test[] {};
  101. foreach (var loopVar in t) {
  102. $loopVar.$
  103. }
  104. }
  105. }
  106. ");
  107. Assert.IsNotNull (provider, "provider == null");
  108. Assert.IsNotNull (provider.Find ("TestMethod"), "method 'TestMethod' not found.");
  109. }
  110. [Test()]
  111. public void TestAnonymousType ()
  112. {
  113. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  114. @"
  115. class Program
  116. {
  117. static void Main (string[] args)
  118. {
  119. var t = new { TestInt = 6, TestChar='e', TestString =""Test""};
  120. $t.$
  121. }
  122. }
  123. ");
  124. Assert.IsNotNull (provider, "provider == null");
  125. Assert.IsNotNull (provider.Find ("TestInt"), "property 'TestInt' not found.");
  126. Assert.IsNotNull (provider.Find ("TestChar"), "property 'TestChar' not found.");
  127. Assert.IsNotNull (provider.Find ("TestString"), "property 'TestString' not found.");
  128. }
  129. [Test()]
  130. public void TestQueryExpression ()
  131. {
  132. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  133. @"
  134. using System;
  135. using System.Collections.Generic;
  136. static class Linq
  137. {
  138. public static IEnumerable<T> Select<S, T> (this IEnumerable<S> collection, Func<S, T> func)
  139. {
  140. }
  141. }
  142. class Program
  143. {
  144. public void TestMethod ()
  145. {
  146. }
  147. static void Main (string[] args)
  148. {
  149. Program[] numbers;
  150. foreach (var x in from n in numbers select n) {
  151. $x.$
  152. }
  153. }
  154. }
  155. ");
  156. Assert.IsNotNull (provider, "provider == null");
  157. Assert.IsNotNull (provider.Find ("TestMethod"), "method 'TestMethod' not found.");
  158. }
  159. [Test()]
  160. public void TestLambdaExpressionCase1 ()
  161. {
  162. CompletionResult provider = CodeCompletionBugTests.CreateCtrlSpaceProvider (
  163. @"
  164. using System;
  165. class Test
  166. {
  167. public void Foo ()
  168. {
  169. $Func<Test,int> x = s => s.$
  170. }
  171. }
  172. ");
  173. Assert.IsNotNull (provider, "provider == null");
  174. Assert.IsNotNull (provider.Find ("Foo"), "method 'Foo' not found.");
  175. }
  176. [Test()]
  177. public void TestLambdaExpressionCase2 ()
  178. {
  179. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  180. @"
  181. namespace System {
  182. public class Array {
  183. public Test this[int i] {
  184. get {
  185. }
  186. set {
  187. }
  188. }
  189. }
  190. }
  191. static class ExtMethods
  192. {
  193. public static T Where<T>(this T[] t, Func<T, bool> pred)
  194. {
  195. return t;
  196. }
  197. }
  198. class Test
  199. {
  200. public void TestMethod ()
  201. {
  202. Test[] en = new Test[0];
  203. var x = en.Where (t => t != null);
  204. $x.$
  205. }
  206. }
  207. ");
  208. Assert.IsNotNull (provider, "provider == null");
  209. Assert.IsNotNull (provider.Find ("TestMethod"), "method 'TestMethod' not found.");
  210. }
  211. /// <summary>
  212. /// Bug 487237 - Broken lambda intellisense
  213. /// </summary>
  214. [Test()]
  215. public void TestBug487237 ()
  216. {
  217. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  218. @"
  219. public interface IHelper
  220. {
  221. void DoIt ();
  222. }
  223. public class Program
  224. {
  225. delegate T MyDelegate <T> (T t);
  226. static int Main ()
  227. {
  228. $MyDelegate<IHelper> e = helper => helper.$
  229. return 0;
  230. }
  231. }
  232. ");
  233. Assert.IsNotNull (provider, "provider == null");
  234. Assert.IsNotNull (provider.Find ("DoIt"), "method 'DoIt' not found.");
  235. }
  236. /// <summary>
  237. /// Bug 491016 - No intellisense for lambdas inside linq query
  238. /// </summary>
  239. [Test()]
  240. public void TestBug491016 ()
  241. {
  242. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  243. @"
  244. using System;
  245. using System.Collections.Generic;
  246. namespace System.Collections.Generic {
  247. public interface IEnumerable<T>
  248. {
  249. }
  250. }
  251. namespace Foo
  252. {
  253. class Data
  254. {
  255. public int Value = 5;
  256. }
  257. static class Ex
  258. {
  259. public static System.Collections.Generic.IEnumerable<TR> Foo<T, TR> (this System.Collections.Generic.IEnumerable<T> t, Func<T, TR> f)
  260. {
  261. yield return f (t.First ());
  262. }
  263. }
  264. public class C
  265. {
  266. public static void Main ()
  267. {
  268. System.Collections.Generic.IEnumerable<Data> i = new Data [0];
  269. $var prods = from pe in i.Foo (p2 => p2.$
  270. }
  271. }
  272. }
  273. ");
  274. Assert.IsNotNull (provider, "provider == null");
  275. Assert.IsNotNull (provider.Find ("Value"), "field 'Value' not found.");
  276. }
  277. /// <summary>
  278. /// Bug 491017 - No intellisense for static LINQ queries
  279. /// </summary>
  280. [Test()]
  281. public void TestBug491017 ()
  282. {
  283. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  284. @"
  285. using System.Linq;
  286. using System.Linq.Expressions;
  287. class Test
  288. {
  289. $object e = from entity in ""olololcolc"" select entity.$
  290. }
  291. ");
  292. Assert.IsNotNull (provider, "provider == null");
  293. Assert.IsNotNull (provider.Find ("ToString"), "method 'ToString' not found.");
  294. Assert.IsNull (provider.Find ("Length"), "property 'Length' found, but shouldn't (indicates wrong return type).");
  295. }
  296. [Test()]
  297. public void TestDefaultParameterBug ()
  298. {
  299. CompletionResult provider = CodeCompletionBugTests.CreateProvider (
  300. @"
  301. namespace Foo
  302. {
  303. class Data
  304. {
  305. public int Value = 5;
  306. }
  307. public class C
  308. {
  309. public void Foo (bool aBool = false)
  310. {
  311. Data data;
  312. $data.$
  313. }
  314. }
  315. }
  316. ");
  317. Assert.IsNotNull (provider, "provider == null");
  318. Assert.IsNotNull (provider.Find ("Value"), "field 'Value' not found.");
  319. }
  320. [Test()]
  321. public void TestLinqWhere() {
  322. CompletionResult provider = CodeCompletionBugTests.CreateProvider(
  323. @"
  324. using System.Collections.Generic;
  325. using System.Linq;
  326. class A
  327. {
  328. public static void Method1()
  329. {
  330. int[] enumerable = new int[]{1,2,3};
  331. $IEnumerable<int> q = from i in enumerable where i.$
  332. }
  333. }
  334. ");
  335. Assert.IsNotNull(provider); // <--- here 0 item in the completion list
  336. Assert.IsNotNull(provider.Find("ToString"));
  337. }
  338. [Test()]
  339. public void TestLinqSelectContext ()
  340. {
  341. var provider = CodeCompletionBugTests.CreateProvider(
  342. @"
  343. using System.Collections.Generic;
  344. using System.Linq;
  345. class A
  346. {
  347. public static void Main (string[] args)
  348. {
  349. $from a in args select n$
  350. }
  351. }
  352. ");
  353. Assert.IsNotNull(provider); // <--- here 0 item in the completion list
  354. Assert.IsNotNull(provider.Find("new"), "'new' not found");
  355. Assert.IsNotNull(provider.Find("args"), "'args' not found");
  356. Assert.IsNotNull(provider.Find("a"), "'a' not found");
  357. }
  358. [Test()]
  359. public void TestLinqAnonymousTypeContext ()
  360. {
  361. var provider = CodeCompletionBugTests.CreateProvider(
  362. @"
  363. using System.Collections.Generic;
  364. using System.Linq;
  365. class A
  366. {
  367. public static void Main (string[] args)
  368. {
  369. Test($from a in args select new { t$);
  370. }
  371. }
  372. ");
  373. Assert.IsNotNull(provider);
  374. Assert.IsFalse (provider.AutoSelect );
  375. }
  376. [Test()]
  377. public void TestLinqAnonymousTypeContextCase2 ()
  378. {
  379. var provider = CodeCompletionBugTests.CreateProvider(
  380. @"
  381. using System.Collections.Generic;
  382. using System.Linq;
  383. class A
  384. {
  385. public static void Main (string[] args)
  386. {
  387. $from a in args select new { test = a$
  388. }
  389. }
  390. ");
  391. Assert.IsNotNull(provider); // <--- here 0 item in the completion list
  392. Assert.IsTrue (provider.AutoSelect );
  393. Assert.IsNotNull(provider.Find("a"), "'a' not found");
  394. Assert.IsNotNull(provider.Find("new"), "'new' not found");
  395. Assert.IsNotNull(provider.Find("args"), "'args' not found");
  396. }
  397. [Test()]
  398. public void TestLinqAnonymousTypeContextCase3 ()
  399. {
  400. var provider = CodeCompletionBugTests.CreateProvider(
  401. @"
  402. using System.Collections.Generic;
  403. using System.Linq;
  404. class A
  405. {
  406. public static void Main (string[] args)
  407. {
  408. $from a in args select new { test = a }$
  409. }
  410. }
  411. ");
  412. Assert.IsTrue(provider == null || provider.Count == 0); // <--- here 0 item in the completion list
  413. }
  414. [Test()]
  415. public void TestLinqExpressionContext ()
  416. {
  417. var provider = CodeCompletionBugTests.CreateProvider(
  418. @"
  419. using System.Collections.Generic;
  420. using System.Linq;
  421. class A
  422. {
  423. public static void Main (string[] args)
  424. {
  425. $from a in args where a !$
  426. }
  427. }
  428. ");
  429. Assert.IsTrue(provider == null || provider.Count == 0); // <--- here 0 item in the completion list
  430. }
  431. }
  432. }