PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/Plugins/Ycm/Plugin/third_party/ycmd/third_party/OmniSharpServer/NRefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateMethodDeclarationTests.cs

https://bitbucket.org/WscriChy/vim-configuration
C# | 743 lines | 669 code | 34 blank | 40 comment | 0 complexity | 0c12b021e0ae3aa30d7d15a22ff1bc18 MD5 | raw file
  1. //
  2. // CreateMethodDeclarationTests.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mkrueger@xamarin.com>
  6. //
  7. // Copyright (c) 2012 Xamarin <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 System.Text;
  30. namespace ICSharpCode.NRefactory.CSharp.CodeActions
  31. {
  32. [TestFixture]
  33. public class CreateMethodDeclarationTests : ContextActionTestBase
  34. {
  35. [Test()]
  36. public void TestPrivateSimpleCreateMethod ()
  37. {
  38. Test<CreateMethodDeclarationAction> (@"class TestClass
  39. {
  40. int member = 5;
  41. string Test { get; set; }
  42. void TestMethod ()
  43. {
  44. $NonExistantMethod (member, Test, 5);
  45. }
  46. }", @"class TestClass
  47. {
  48. int member = 5;
  49. string Test { get; set; }
  50. void NonExistantMethod (int member, string test, int i)
  51. {
  52. throw new System.NotImplementedException ();
  53. }
  54. void TestMethod ()
  55. {
  56. NonExistantMethod (member, Test, 5);
  57. }
  58. }");
  59. }
  60. [Test()]
  61. public void TestStaticSimpleCreateMethod ()
  62. {
  63. Test<CreateMethodDeclarationAction> (@"class TestClass
  64. {
  65. public static void TestMethod ()
  66. {
  67. int testLocalVar;
  68. $NonExistantMethod (testLocalVar);
  69. }
  70. }", @"class TestClass
  71. {
  72. static void NonExistantMethod (int testLocalVar)
  73. {
  74. throw new System.NotImplementedException ();
  75. }
  76. public static void TestMethod ()
  77. {
  78. int testLocalVar;
  79. NonExistantMethod (testLocalVar);
  80. }
  81. }");
  82. }
  83. [Test()]
  84. public void TestGuessAssignmentReturnType ()
  85. {
  86. Test<CreateMethodDeclarationAction> (@"class TestClass
  87. {
  88. static void TestMethod ()
  89. {
  90. int testLocalVar = $NonExistantMethod ();
  91. }
  92. }", @"class TestClass
  93. {
  94. static int NonExistantMethod ()
  95. {
  96. throw new System.NotImplementedException ();
  97. }
  98. static void TestMethod ()
  99. {
  100. int testLocalVar = NonExistantMethod ();
  101. }
  102. }");
  103. }
  104. [Test()]
  105. public void TestGuessAssignmentReturnTypeCase2 ()
  106. {
  107. Test<CreateMethodDeclarationAction> (@"class TestClass
  108. {
  109. static void TestMethod ()
  110. {
  111. int testLocalVar;
  112. testLocalVar = $NonExistantMethod ();
  113. }
  114. }", @"class TestClass
  115. {
  116. static int NonExistantMethod ()
  117. {
  118. throw new System.NotImplementedException ();
  119. }
  120. static void TestMethod ()
  121. {
  122. int testLocalVar;
  123. testLocalVar = NonExistantMethod ();
  124. }
  125. }");
  126. }
  127. [Test()]
  128. public void TestGuessAssignmentReturnTypeCase3 ()
  129. {
  130. Test<CreateMethodDeclarationAction> (@"class TestClass
  131. {
  132. static void TestMethod ()
  133. {
  134. var testLocalVar = (string)$NonExistantMethod ();
  135. }
  136. }", @"class TestClass
  137. {
  138. static string NonExistantMethod ()
  139. {
  140. throw new System.NotImplementedException ();
  141. }
  142. static void TestMethod ()
  143. {
  144. var testLocalVar = (string)NonExistantMethod ();
  145. }
  146. }");
  147. }
  148. [Test()]
  149. public void TestGuessParameterType ()
  150. {
  151. Test<CreateMethodDeclarationAction> (@"class TestClass
  152. {
  153. void TestMethod ()
  154. {
  155. Test ($NonExistantMethod ());
  156. }
  157. void Test (int a) {}
  158. }", @"class TestClass
  159. {
  160. int NonExistantMethod ()
  161. {
  162. throw new System.NotImplementedException ();
  163. }
  164. void TestMethod ()
  165. {
  166. Test (NonExistantMethod ());
  167. }
  168. void Test (int a) {}
  169. }");
  170. }
  171. [Test()]
  172. public void TestCreateDelegateDeclarationIdentifierCase ()
  173. {
  174. Test<CreateMethodDeclarationAction> (@"class TestClass
  175. {
  176. public event MyDelegate MyEvent;
  177. void TestMethod ()
  178. {
  179. MyEvent += $NonExistantMethod;
  180. }
  181. }
  182. public delegate string MyDelegate (int a, object b);
  183. ", @"class TestClass
  184. {
  185. public event MyDelegate MyEvent;
  186. string NonExistantMethod (int a, object b)
  187. {
  188. throw new System.NotImplementedException ();
  189. }
  190. void TestMethod ()
  191. {
  192. MyEvent += NonExistantMethod;
  193. }
  194. }
  195. public delegate string MyDelegate (int a, object b);
  196. ");
  197. }
  198. [Test()]
  199. public void TestCreateDelegateDeclarationMemberReferenceCase ()
  200. {
  201. Test<CreateMethodDeclarationAction> (@"class TestClass
  202. {
  203. public event MyDelegate MyEvent;
  204. void TestMethod ()
  205. {
  206. MyEvent += $this.NonExistantMethod;
  207. }
  208. }
  209. public delegate string MyDelegate (int a, object b);
  210. ", @"class TestClass
  211. {
  212. public event MyDelegate MyEvent;
  213. string NonExistantMethod (int a, object b)
  214. {
  215. throw new System.NotImplementedException ();
  216. }
  217. void TestMethod ()
  218. {
  219. MyEvent += this.NonExistantMethod;
  220. }
  221. }
  222. public delegate string MyDelegate (int a, object b);
  223. ");
  224. }
  225. [Test()]
  226. public void TestCreateDelegateDeclarationInOtherClassMemberReferenceCase ()
  227. {
  228. Test<CreateMethodDeclarationAction> (@"class Foo {
  229. }
  230. class TestClass
  231. {
  232. public event MyDelegate MyEvent;
  233. void TestMethod ()
  234. {
  235. MyEvent += $new Foo ().NonExistantMethod;
  236. }
  237. }
  238. public delegate string MyDelegate (int a, object b);
  239. ", @"class Foo {
  240. public string NonExistantMethod (int a, object b)
  241. {
  242. throw new System.NotImplementedException ();
  243. }
  244. }
  245. class TestClass
  246. {
  247. public event MyDelegate MyEvent;
  248. void TestMethod ()
  249. {
  250. MyEvent += new Foo ().NonExistantMethod;
  251. }
  252. }
  253. public delegate string MyDelegate (int a, object b);
  254. ");
  255. }
  256. [Test()]
  257. public void TestRefOutCreateMethod ()
  258. {
  259. Test<CreateMethodDeclarationAction> (@"class TestClass
  260. {
  261. void TestMethod ()
  262. {
  263. int a, b;
  264. $NonExistantMethod (ref a, out b);
  265. }
  266. }", @"class TestClass
  267. {
  268. void NonExistantMethod (ref int a, out int b)
  269. {
  270. throw new System.NotImplementedException ();
  271. }
  272. void TestMethod ()
  273. {
  274. int a, b;
  275. NonExistantMethod (ref a, out b);
  276. }
  277. }");
  278. }
  279. [Test()]
  280. public void TestExternMethod ()
  281. {
  282. Test<CreateMethodDeclarationAction> (
  283. @"
  284. class FooBar
  285. {
  286. }
  287. class TestClass
  288. {
  289. void TestMethod ()
  290. {
  291. var fb = new FooBar ();
  292. fb.$NonExistantMethod ();
  293. }
  294. }
  295. ", @"
  296. class FooBar
  297. {
  298. public void NonExistantMethod ()
  299. {
  300. throw new System.NotImplementedException ();
  301. }
  302. }
  303. class TestClass
  304. {
  305. void TestMethod ()
  306. {
  307. var fb = new FooBar ();
  308. fb.NonExistantMethod ();
  309. }
  310. }
  311. ");
  312. }
  313. [Test()]
  314. public void TestCreateInterfaceMethod ()
  315. {
  316. Test<CreateMethodDeclarationAction> (
  317. @"
  318. interface FooBar
  319. {
  320. }
  321. class TestClass
  322. {
  323. void TestMethod ()
  324. {
  325. FooBar fb;
  326. fb.$NonExistantMethod ();
  327. }
  328. }
  329. ", @"
  330. interface FooBar
  331. {
  332. void NonExistantMethod ();
  333. }
  334. class TestClass
  335. {
  336. void TestMethod ()
  337. {
  338. FooBar fb;
  339. fb.NonExistantMethod ();
  340. }
  341. }
  342. ");
  343. }
  344. [Test()]
  345. public void TestCreateInStaticClassMethod ()
  346. {
  347. Test<CreateMethodDeclarationAction> (
  348. @"
  349. static class FooBar
  350. {
  351. }
  352. class TestClass
  353. {
  354. void TestMethod ()
  355. {
  356. FooBar.$NonExistantMethod ();
  357. }
  358. }
  359. ", @"
  360. static class FooBar
  361. {
  362. public static void NonExistantMethod ()
  363. {
  364. throw new System.NotImplementedException ();
  365. }
  366. }
  367. class TestClass
  368. {
  369. void TestMethod ()
  370. {
  371. FooBar.NonExistantMethod ();
  372. }
  373. }
  374. ");
  375. }
  376. /// <summary>
  377. /// Bug 677522 - "Create Method" creates at wrong indent level
  378. /// </summary>
  379. [Test()]
  380. public void TestBug677522 ()
  381. {
  382. Test<CreateMethodDeclarationAction> (
  383. @"namespace Test {
  384. class TestClass
  385. {
  386. void TestMethod ()
  387. {
  388. $NonExistantMethod ();
  389. }
  390. }
  391. }
  392. ", @"namespace Test {
  393. class TestClass
  394. {
  395. void NonExistantMethod ()
  396. {
  397. throw new System.NotImplementedException ();
  398. }
  399. void TestMethod ()
  400. {
  401. NonExistantMethod ();
  402. }
  403. }
  404. }
  405. ");
  406. }
  407. /// <summary>
  408. /// Bug 677527 - "Create Method" uses fully qualified namespace when "using" statement exists
  409. /// </summary>
  410. [Test()]
  411. public void TestBug677527 ()
  412. {
  413. Test<CreateMethodDeclarationAction> (
  414. @"using System.Text;
  415. namespace Test {
  416. class TestClass
  417. {
  418. void TestMethod ()
  419. {
  420. StringBuilder sb = new StringBuilder ();
  421. $NonExistantMethod (sb);
  422. }
  423. }
  424. }
  425. ", @"using System.Text;
  426. namespace Test {
  427. class TestClass
  428. {
  429. void NonExistantMethod (StringBuilder sb)
  430. {
  431. throw new System.NotImplementedException ();
  432. }
  433. void TestMethod ()
  434. {
  435. StringBuilder sb = new StringBuilder ();
  436. NonExistantMethod (sb);
  437. }
  438. }
  439. }
  440. ");
  441. }
  442. /// <summary>
  443. /// Bug 693949 - Create method uses the wrong type for param
  444. /// </summary>
  445. [Ignore("TODO")]
  446. [Test()]
  447. public void TestBug693949 ()
  448. {
  449. // the c# code isn't 100% correct since test isn't accessible in Main (can't call non static method from static member)
  450. Test<CreateMethodDeclarationAction> (
  451. @"using System.Text;
  452. namespace Test {
  453. class TestClass
  454. {
  455. string test(string a)
  456. {
  457. }
  458. public static void Main(string[] args)
  459. {
  460. Type a = $M(test(""a""));
  461. }
  462. }
  463. }
  464. ", @"using System.Text;
  465. namespace Test {
  466. class TestClass
  467. {
  468. string test(string a)
  469. {
  470. }
  471. static Type M (string par1)
  472. {
  473. throw new System.NotImplementedException ();
  474. }
  475. public static void Main(string[] args)
  476. {
  477. Type a = $M(test(""a""));
  478. }
  479. }
  480. }
  481. ");
  482. }
  483. /// <summary>
  484. /// Bug 469 - CreateMethod created a method incorrectly
  485. /// </summary>
  486. [Test()]
  487. public void TestBug469 ()
  488. {
  489. Test<CreateMethodDeclarationAction> (
  490. @"class Test
  491. {
  492. public override string ToString ()
  493. {
  494. $BeginDownloadingImage (this);
  495. }
  496. }
  497. ", @"class Test
  498. {
  499. void BeginDownloadingImage (Test test)
  500. {
  501. throw new System.NotImplementedException ();
  502. }
  503. public override string ToString ()
  504. {
  505. BeginDownloadingImage (this);
  506. }
  507. }
  508. ");
  509. }
  510. [Test()]
  511. public void TestTestGuessReturnReturnType ()
  512. {
  513. Test<CreateMethodDeclarationAction> (
  514. @"class Test
  515. {
  516. public override string ToString ()
  517. {
  518. return $BeginDownloadingImage (this);
  519. }
  520. }
  521. ", @"class Test
  522. {
  523. string BeginDownloadingImage (Test test)
  524. {
  525. throw new System.NotImplementedException ();
  526. }
  527. public override string ToString ()
  528. {
  529. return BeginDownloadingImage (this);
  530. }
  531. }
  532. ");
  533. }
  534. [Test()]
  535. public void TestStringParameterNameGuessing ()
  536. {
  537. Test<CreateMethodDeclarationAction> (@"class TestClass
  538. {
  539. static void TestMethod ()
  540. {
  541. $NonExistantMethod (""Hello World!"");
  542. }
  543. }", @"class TestClass
  544. {
  545. static void NonExistantMethod (string helloWorld)
  546. {
  547. throw new System.NotImplementedException ();
  548. }
  549. static void TestMethod ()
  550. {
  551. NonExistantMethod (""Hello World!"");
  552. }
  553. }");
  554. }
  555. [Test()]
  556. public void TestMethodInFrameworkClass ()
  557. {
  558. TestWrongContext<CreateMethodDeclarationAction> (
  559. @"class TestClass
  560. {
  561. void TestMethod ()
  562. {
  563. $System.Console.ImprovedWriteLine (""Think of it"");
  564. }
  565. }
  566. ");
  567. }
  568. [Test()]
  569. public void TestCreateMethodOutOfDelegateCreation ()
  570. {
  571. Test<CreateMethodDeclarationAction> (
  572. @"using System;
  573. class Test
  574. {
  575. public void ATest ()
  576. {
  577. new System.EventHandler<System.EventArgs>($BeginDownloadingImage);
  578. }
  579. }
  580. ", @"using System;
  581. class Test
  582. {
  583. void BeginDownloadingImage (object sender, EventArgs e)
  584. {
  585. throw new NotImplementedException ();
  586. }
  587. public void ATest ()
  588. {
  589. new System.EventHandler<System.EventArgs>(BeginDownloadingImage);
  590. }
  591. }
  592. ");
  593. }
  594. [Test()]
  595. public void TestStaticClassMethod ()
  596. {
  597. // Not 100% correct input code, but should work in that case as well.
  598. Test<CreateMethodDeclarationAction> (@"static class TestClass
  599. {
  600. public TestClass ()
  601. {
  602. $Foo (5);
  603. }
  604. }", @"static class TestClass
  605. {
  606. static void Foo (int i)
  607. {
  608. throw new System.NotImplementedException ();
  609. }
  610. public TestClass ()
  611. {
  612. Foo (5);
  613. }
  614. }");
  615. }
  616. [Test()]
  617. public void TestCreateFromIdentifierNestedInMethodCall ()
  618. {
  619. // Not 100% correct input code, but should work in that case as well.
  620. Test<CreateMethodDeclarationAction> (@"namespace System {
  621. class TestClass
  622. {
  623. public void FooBar (object test)
  624. {
  625. FooBar (new EventHandler ($Foo));
  626. }
  627. }
  628. }", @"namespace System {
  629. class TestClass
  630. {
  631. void Foo (object sender, EventArgs e)
  632. {
  633. throw new NotImplementedException ();
  634. }
  635. public void FooBar (object test)
  636. {
  637. FooBar (new EventHandler (Foo));
  638. }
  639. }
  640. }");
  641. }
  642. [Test]
  643. public void TestEnumCase()
  644. {
  645. TestWrongContext<CreateMethodDeclarationAction>(@"
  646. enum AEnum { A }
  647. class Foo
  648. {
  649. public void Test ()
  650. {
  651. AEnum e = AEnum.B$ar ();
  652. }
  653. }
  654. ");
  655. }
  656. [Test]
  657. public void TestPassNullArgument ()
  658. {
  659. Test<CreateMethodDeclarationAction> (@"class TestClass
  660. {
  661. void TestMethod ()
  662. {
  663. $NonExistantMethod (null);
  664. }
  665. }", @"class TestClass
  666. {
  667. void NonExistantMethod (object o)
  668. {
  669. throw new System.NotImplementedException ();
  670. }
  671. void TestMethod ()
  672. {
  673. NonExistantMethod (null);
  674. }
  675. }");
  676. }
  677. }
  678. }