PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/src/EditorFeatures/CSharpTest/CodeActions/ReplaceMethodWithProperty/ReplaceMethodWithPropertyTests.cs

https://gitlab.com/sharadag/Roslyn
C# | 425 lines | 379 code | 45 blank | 1 comment | 0 complexity | 741b0798634381f6cc8ba2e2f6b99f1a MD5 | raw file
  1. // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System.Threading.Tasks;
  3. using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings;
  4. using Microsoft.CodeAnalysis.ReplaceMethodWithProperty;
  5. using Roslyn.Test.Utilities;
  6. using Xunit;
  7. namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeActions.ReplaceMethodWithProperty
  8. {
  9. public class ReplaceMethodWithPropertyTests : AbstractCSharpCodeActionTest
  10. {
  11. protected override object CreateCodeRefactoringProvider(Workspace workspace)
  12. {
  13. return new ReplaceMethodWithPropertyCodeRefactoringProvider();
  14. }
  15. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  16. public async Task TestMethodWithGetName()
  17. {
  18. await TestAsync(
  19. @"class C { int [||]GetFoo() { } }",
  20. @"class C { int Foo { get { } } }");
  21. }
  22. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  23. public async Task TestMethodWithoutGetName()
  24. {
  25. await TestAsync(
  26. @"class C { int [||]Foo() { } }",
  27. @"class C { int Foo { get { } } }");
  28. }
  29. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  30. [WorkItem(6034, "https://github.com/dotnet/roslyn/issues/6034")]
  31. public async Task TestMethodWithArrowBody()
  32. {
  33. await TestAsync(
  34. @"class C { int [||]GetFoo() => 0; }",
  35. @"class C { int Foo => 0; }");
  36. }
  37. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  38. public async Task TestMethodWithoutBody()
  39. {
  40. await TestAsync(
  41. @"class C { int [||]GetFoo(); }",
  42. @"class C { int Foo { get; } }");
  43. }
  44. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  45. public async Task TestMethodWithModifiers()
  46. {
  47. await TestAsync(
  48. @"class C { public static int [||]GetFoo() { } }",
  49. @"class C { public static int Foo { get { } } }");
  50. }
  51. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  52. public async Task TestMethodWithAttributes()
  53. {
  54. await TestAsync(
  55. @"class C { [A]int [||]GetFoo() { } }",
  56. @"class C { [A]int Foo { get { } } }");
  57. }
  58. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  59. public async Task TestMethodWithTrivia_1()
  60. {
  61. await TestAsync(
  62. @"class C
  63. {
  64. // Foo
  65. int [||]GetFoo()
  66. {
  67. }
  68. }",
  69. @"class C
  70. {
  71. // Foo
  72. int Foo
  73. {
  74. get
  75. {
  76. }
  77. }
  78. }",
  79. compareTokens: false);
  80. }
  81. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  82. public async Task TestIfDefMethod()
  83. {
  84. await TestAsync(
  85. @"class C
  86. {
  87. #if true
  88. int [||]GetFoo()
  89. {
  90. }
  91. #endif
  92. }",
  93. @"class C
  94. {
  95. #if true
  96. int Foo
  97. {
  98. get
  99. {
  100. }
  101. }
  102. #endif
  103. }");
  104. }
  105. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  106. public async Task TestMethodWithTrivia_2()
  107. {
  108. await TestAsync(
  109. @"class C
  110. {
  111. // Foo
  112. int [||]GetFoo()
  113. {
  114. }
  115. // SetFoo
  116. void SetFoo(int i)
  117. {
  118. }
  119. }",
  120. @"class C
  121. {
  122. // Foo
  123. // SetFoo
  124. int Foo
  125. {
  126. get
  127. {
  128. }
  129. set
  130. {
  131. }
  132. }
  133. }",
  134. index: 1,
  135. compareTokens: false);
  136. }
  137. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  138. public async Task TestExplicitInterfaceMethod_1()
  139. {
  140. await TestAsync(
  141. @"class C { int [||]I.GetFoo() { } }",
  142. @"class C { int I.Foo { get { } } }");
  143. }
  144. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  145. public async Task TestExplicitInterfaceMethod_2()
  146. {
  147. await TestAsync(
  148. @"interface I { int GetFoo(); } class C : I { int [||]I.GetFoo() { } }",
  149. @"interface I { int Foo { get; } } class C : I { int I.Foo { get { } } }");
  150. }
  151. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  152. public async Task TestExplicitInterfaceMethod_3()
  153. {
  154. await TestAsync(
  155. @"interface I { int [||]GetFoo(); } class C : I { int I.GetFoo() { } }",
  156. @"interface I { int Foo { get; } } class C : I { int I.Foo { get { } } }");
  157. }
  158. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  159. public async Task TestInAttribute()
  160. {
  161. await TestMissingAsync(
  162. @"class C { [At[||]tr]int GetFoo() { } }");
  163. }
  164. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  165. public async Task TestInMethod()
  166. {
  167. await TestMissingAsync(
  168. @"class C { int GetFoo() { [||] } }");
  169. }
  170. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  171. public async Task TestVoidMethod()
  172. {
  173. await TestMissingAsync(
  174. @"class C { void [||]GetFoo() { } }");
  175. }
  176. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  177. public async Task TestAsyncMethod()
  178. {
  179. await TestMissingAsync(
  180. @"class C { async Task [||]GetFoo() { } }");
  181. }
  182. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  183. public async Task TestGenericMethod()
  184. {
  185. await TestMissingAsync(
  186. @"class C { int [||]GetFoo<T>() { } }");
  187. }
  188. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  189. public async Task TestExtensionMethod()
  190. {
  191. await TestMissingAsync(
  192. @"static class C { int [||]GetFoo(this int i) { } }");
  193. }
  194. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  195. public async Task TestMethodWithParameters_1()
  196. {
  197. await TestMissingAsync(
  198. @"class C { int [||]GetFoo(int i) { } }");
  199. }
  200. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  201. public async Task TestMethodWithParameters_2()
  202. {
  203. await TestMissingAsync(
  204. @"class C { int [||]GetFoo(int i = 0) { } }");
  205. }
  206. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  207. public async Task TestNotInSignature_1()
  208. {
  209. await TestMissingAsync(
  210. @"class C { [At[||]tr]int GetFoo() { } }");
  211. }
  212. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  213. public async Task TestNotInSignature_2()
  214. {
  215. await TestMissingAsync(
  216. @"class C { int GetFoo() { [||] } }");
  217. }
  218. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  219. public async Task TestUpdateGetReferenceNotInMethod()
  220. {
  221. await TestAsync(
  222. @"class C { int [||]GetFoo() { } void Bar() { var x = GetFoo(); } }",
  223. @"class C { int Foo { get { } } void Bar() { var x = Foo; } }");
  224. }
  225. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  226. public async Task TestUpdateGetReferenceSimpleInvocation()
  227. {
  228. await TestAsync(
  229. @"class C { int [||]GetFoo() { } void Bar() { var x = GetFoo(); } }",
  230. @"class C { int Foo { get { } } void Bar() { var x = Foo; } }");
  231. }
  232. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  233. public async Task TestUpdateGetReferenceMemberAccessInvocation()
  234. {
  235. await TestAsync(
  236. @"class C { int [||]GetFoo() { } void Bar() { var x = this.GetFoo(); } }",
  237. @"class C { int Foo { get { } } void Bar() { var x = this.Foo; } }");
  238. }
  239. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  240. public async Task TestUpdateGetReferenceBindingMemberInvocation()
  241. {
  242. await TestAsync(
  243. @"class C { int [||]GetFoo() { } void Bar() { C x; var v = x?.GetFoo(); } }",
  244. @"class C { int Foo { get { } } void Bar() { C x; var v = x?.Foo; } }");
  245. }
  246. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  247. public async Task TestUpdateGetReferenceInMethod()
  248. {
  249. await TestAsync(
  250. @"class C { int [||]GetFoo() { return GetFoo(); } }",
  251. @"class C { int Foo { get { return Foo; } } }");
  252. }
  253. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  254. public async Task TestOverride()
  255. {
  256. await TestAsync(
  257. @"class C { public virtual int [||]GetFoo() { } } class D : C { public override int GetFoo() { } }",
  258. @"class C { public virtual int Foo { get { } } } class D : C { public override int Foo { get { } } }");
  259. }
  260. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  261. public async Task TestUpdateGetReference_NonInvoked()
  262. {
  263. await TestAsync(
  264. @"using System; class C { int [||]GetFoo() { } void Bar() { Action<int> i = GetFoo; } }",
  265. @"using System; class C { int Foo { get { } } void Bar() { Action<int> i = {|Conflict:Foo|}; } }");
  266. }
  267. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  268. public async Task TestUpdateGetReference_ImplicitReference()
  269. {
  270. await TestAsync(
  271. @"using System.Collections; class C { public IEnumerator [||]GetEnumerator() { } void Bar() { foreach (var x in this) { } } }",
  272. @"using System.Collections; class C { public IEnumerator Enumerator { get { } } void Bar() { {|Conflict:foreach (var x in this) { }|} } }");
  273. }
  274. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  275. public async Task TestUpdateGetSet()
  276. {
  277. await TestAsync(
  278. @"using System; class C { int [||]GetFoo() { } void SetFoo(int i) { } }",
  279. @"using System; class C { int Foo { get { } set { } } }",
  280. index: 1);
  281. }
  282. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  283. public async Task TestUpdateGetSetReference_NonInvoked()
  284. {
  285. await TestAsync(
  286. @"using System; class C { int [||]GetFoo() { } void SetFoo(int i) { } void Bar() { Action<int> i = SetFoo; } }",
  287. @"using System; class C { int Foo { get { } set { } } void Bar() { Action<int> i = {|Conflict:Foo|}; } }",
  288. index: 1);
  289. }
  290. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  291. public async Task TestUpdateGetSet_SetterAccessibility()
  292. {
  293. await TestAsync(
  294. @"using System; class C { public int [||]GetFoo() { } private void SetFoo(int i) { } }",
  295. @"using System; class C { public int Foo { get { } private set { } } }",
  296. index: 1);
  297. }
  298. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  299. public async Task TestUpdateGetSet_ExpressionBodies()
  300. {
  301. await TestAsync(
  302. @"using System; class C { int [||]GetFoo() => 0; void SetFoo(int i) => Bar(); }",
  303. @"using System; class C { int Foo { get { return 0; } set { Bar(); } } }",
  304. index: 1);
  305. }
  306. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  307. public async Task TestUpdateGetSet_GetInSetReference()
  308. {
  309. await TestAsync(
  310. @"using System; class C { int [||]GetFoo() { } void SetFoo(int i) { } void Bar() { SetFoo(GetFoo() + 1); } }",
  311. @"using System; class C { int Foo { get { } set { } } void Bar() { Foo = Foo + 1; } }",
  312. index: 1);
  313. }
  314. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  315. public async Task TestUpdateGetSet_UpdateSetParameterName_1()
  316. {
  317. await TestAsync(
  318. @"using System; class C { int [||]GetFoo() { } void SetFoo(int i) { v = i; } }",
  319. @"using System; class C { int Foo { get { } set { v = value; } } }",
  320. index: 1);
  321. }
  322. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  323. public async Task TestUpdateGetSet_UpdateSetParameterName_2()
  324. {
  325. await TestAsync(
  326. @"using System; class C { int [||]GetFoo() { } void SetFoo(int value) { v = value; } }",
  327. @"using System; class C { int Foo { get { } set { v = value; } } }",
  328. index: 1);
  329. }
  330. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  331. public async Task TestUpdateGetSet_SetReferenceInSetter()
  332. {
  333. await TestAsync(
  334. @"using System; class C { int [||]GetFoo() { } void SetFoo(int i) { SetFoo(i - 1); } }",
  335. @"using System; class C { int Foo { get { } set { Foo = value - 1; } } }",
  336. index: 1);
  337. }
  338. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  339. public async Task TestVirtualGetWithOverride_1()
  340. {
  341. await TestAsync(
  342. @"class C { protected virtual int [||]GetFoo() { } } class D : C { protected override int GetFoo() { } }",
  343. @"class C { protected virtual int Foo { get { } } } class D : C { protected override int Foo{ get { } } }",
  344. index: 0);
  345. }
  346. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  347. public async Task TestVirtualGetWithOverride_2()
  348. {
  349. await TestAsync(
  350. @"class C { protected virtual int [||]GetFoo() { } } class D : C { protected override int GetFoo() { base.GetFoo(); } }",
  351. @"class C { protected virtual int Foo { get { } } } class D : C { protected override int Foo { get { base.Foo; } } }",
  352. index: 0);
  353. }
  354. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  355. public async Task TestGetWithInterface()
  356. {
  357. await TestAsync(
  358. @"interface I { int [||]GetFoo(); } class C : I { public int GetFoo() { } }",
  359. @"interface I { int Foo { get; } } class C : I { public int Foo { get { } } }",
  360. index: 0);
  361. }
  362. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  363. public async Task TestWithPartialClasses()
  364. {
  365. await TestAsync(
  366. @"partial class C { int [||]GetFoo() { } } partial class C { void SetFoo(int i) { } }",
  367. @"partial class C { int Foo { get { } set { } } } partial class C { }",
  368. index: 1);
  369. }
  370. [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsReplaceMethodWithProperty)]
  371. public async Task TestUpdateGetSetCaseInsensitive()
  372. {
  373. await TestAsync(
  374. @"using System; class C { int [||]getFoo() { } void setFoo(int i) { } }",
  375. @"using System; class C { int Foo { get { } set { } } }",
  376. index: 1);
  377. }
  378. }
  379. }