PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/VisualStudio/CSharp/Test/Debugging/LocationInfoGetterTests.cs

https://gitlab.com/sharadag/TestProject2
C# | 495 lines | 457 code | 37 blank | 1 comment | 0 complexity | df8a166310d98c6390ec4e1243fc87fd 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.Linq;
  3. using System.Threading;
  4. using Microsoft.CodeAnalysis.CSharp;
  5. using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
  6. using Microsoft.VisualStudio.LanguageServices.CSharp.Debugging;
  7. using Roslyn.Test.Utilities;
  8. using Roslyn.Utilities;
  9. using Xunit;
  10. namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Debugging
  11. {
  12. public class LocationInfoGetterTests
  13. {
  14. private void Test(string markup, string expectedName, int expectedLineOffset, CSharpParseOptions parseOptions = null)
  15. {
  16. using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromLines(new[] { markup }, parseOptions))
  17. {
  18. var testDocument = workspace.Documents.Single();
  19. var position = testDocument.CursorPosition.Value;
  20. var locationInfo = LocationInfoGetter.GetInfoAsync(
  21. workspace.CurrentSolution.Projects.Single().Documents.Single(),
  22. position,
  23. CancellationToken.None).WaitAndGetResult(CancellationToken.None);
  24. Assert.Equal(expectedName, locationInfo.Name);
  25. Assert.Equal(expectedLineOffset, locationInfo.LineOffset);
  26. }
  27. }
  28. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  29. public void TestClass()
  30. {
  31. Test("class F$$oo { }", "Foo", 0);
  32. }
  33. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  34. [WorkItem(527668), WorkItem(538415)]
  35. public void TestMethod()
  36. {
  37. Test(
  38. @"class Class
  39. {
  40. public static void Meth$$od()
  41. {
  42. }
  43. }
  44. ", "Class.Method()", 0);
  45. }
  46. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  47. [WorkItem(527668)]
  48. public void TestNamespace()
  49. {
  50. Test(
  51. @"namespace Namespace
  52. {
  53. class Class
  54. {
  55. void Method()
  56. {
  57. }$$
  58. }
  59. }", "Namespace.Class.Method()", 2);
  60. }
  61. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  62. [WorkItem(527668)]
  63. public void TestDottedNamespace()
  64. {
  65. Test(
  66. @"namespace Namespace.Another
  67. {
  68. class Class
  69. {
  70. void Method()
  71. {
  72. }$$
  73. }
  74. }", "Namespace.Another.Class.Method()", 2);
  75. }
  76. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  77. public void TestNestedNamespace()
  78. {
  79. Test(
  80. @"namespace Namespace
  81. {
  82. namespace Another
  83. {
  84. class Class
  85. {
  86. void Method()
  87. {
  88. }$$
  89. }
  90. }
  91. }", "Namespace.Another.Class.Method()", 2);
  92. }
  93. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  94. [WorkItem(527668)]
  95. public void TestNestedType()
  96. {
  97. Test(
  98. @"class Outer
  99. {
  100. class Inner
  101. {
  102. void Quux()
  103. {$$
  104. }
  105. }
  106. }", "Outer.Inner.Quux()", 1);
  107. }
  108. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  109. [WorkItem(527668)]
  110. public void TestPropertyGetter()
  111. {
  112. Test(
  113. @"class Class
  114. {
  115. string Property
  116. {
  117. get
  118. {
  119. return null;$$
  120. }
  121. }
  122. }", "Class.Property", 4);
  123. }
  124. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  125. [WorkItem(527668)]
  126. public void TestPropertySetter()
  127. {
  128. Test(
  129. @"class Class
  130. {
  131. string Property
  132. {
  133. get
  134. {
  135. return null;
  136. }
  137. set
  138. {
  139. string s = $$value;
  140. }
  141. }
  142. }", "Class.Property", 9);
  143. }
  144. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  145. [WorkItem(538415)]
  146. public void TestField()
  147. {
  148. Test(
  149. @"class Class
  150. {
  151. int fi$$eld;
  152. }", "Class.field", 0);
  153. }
  154. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  155. [WorkItem(543494)]
  156. public void TestLambdaInFieldInitializer()
  157. {
  158. Test(
  159. @"class Class
  160. {
  161. Action<int> a = b => { in$$t c; };
  162. }", "Class.a", 0);
  163. }
  164. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  165. [WorkItem(543494)]
  166. public void TestMultipleFields()
  167. {
  168. Test(
  169. @"class Class
  170. {
  171. int a1, a$$2;
  172. }", "Class.a2", 0);
  173. }
  174. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  175. public void TestConstructor()
  176. {
  177. Test(
  178. @"class C1
  179. {
  180. C1()
  181. {
  182. $$}
  183. }
  184. ", "C1.C1()", 3);
  185. }
  186. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  187. public void TestDestructor()
  188. {
  189. Test(
  190. @"class C1
  191. {
  192. ~C1()
  193. {
  194. $$}
  195. }
  196. ", "C1.~C1()", 2);
  197. }
  198. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  199. public void TestOperator()
  200. {
  201. Test(
  202. @"namespace N1
  203. {
  204. class C1
  205. {
  206. public static int operator +(C1 x, C1 y)
  207. {
  208. $$return 42;
  209. }
  210. }
  211. }
  212. ", "N1.C1.+(C1 x, C1 y)", 2); // Old implementation reports "operator +" (rather than "+")...
  213. }
  214. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  215. public void TestConversionOperator()
  216. {
  217. Test(
  218. @"namespace N1
  219. {
  220. class C1
  221. {
  222. public static explicit operator N1.C2(N1.C1 x)
  223. {
  224. $$return null;
  225. }
  226. }
  227. class C2
  228. {
  229. }
  230. }
  231. ", "N1.C1.N1.C2(N1.C1 x)", 2); // Old implementation reports "explicit operator N1.C2" (rather than "N1.C2")...
  232. }
  233. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  234. public void TestEvent()
  235. {
  236. Test(
  237. @"class C1
  238. {
  239. delegate void D1();
  240. event D1 e1$$;
  241. }
  242. ", "C1.e1", 0);
  243. }
  244. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  245. public void TextExplicitInterfaceImplementation()
  246. {
  247. Test(
  248. @"interface I1
  249. {
  250. void M1();
  251. }
  252. class C1
  253. {
  254. void I1.M1()
  255. {
  256. $$}
  257. }
  258. ", "C1.M1()", 2);
  259. }
  260. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  261. public void TextIndexer()
  262. {
  263. Test(
  264. @"class C1
  265. {
  266. C1 this[int x]
  267. {
  268. get
  269. {
  270. $$return null;
  271. }
  272. }
  273. }
  274. ", "C1.this[int x]", 4);
  275. }
  276. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  277. public void TestParamsParameter()
  278. {
  279. Test(
  280. @"class C1
  281. {
  282. void M1(params int[] x) { $$ }
  283. }
  284. ", "C1.M1(params int[] x)", 0);
  285. }
  286. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  287. public void TestArglistParameter()
  288. {
  289. Test(
  290. @"class C1
  291. {
  292. void M1(__arglist) { $$ }
  293. }
  294. ", "C1.M1(__arglist)", 0); // Old implementation does not show "__arglist"...
  295. }
  296. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  297. public void TestRefAndOutParameters()
  298. {
  299. Test(
  300. @"class C1
  301. {
  302. void M1( ref int x, out int y )
  303. {
  304. $$y = x;
  305. }
  306. }
  307. ", "C1.M1( ref int x, out int y )", 2); // Old implementation did not show extra spaces around the parameters...
  308. }
  309. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  310. public void TestOptionalParameters()
  311. {
  312. Test(
  313. @"class C1
  314. {
  315. void M1(int x =1)
  316. {
  317. $$y = x;
  318. }
  319. }
  320. ", "C1.M1(int x =1)", 2);
  321. }
  322. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  323. public void TestExtensionMethod()
  324. {
  325. Test(
  326. @"static class C1
  327. {
  328. static void M1(this int x)
  329. {
  330. }$$
  331. }
  332. ", "C1.M1(this int x)", 2);
  333. }
  334. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  335. public void TestGenericType()
  336. {
  337. Test(
  338. @"class C1<T, U>
  339. {
  340. static void M1() { $$ }
  341. }
  342. ", "C1.M1()", 0);
  343. }
  344. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  345. public void TestGenericMethod()
  346. {
  347. Test(
  348. @"class C1<T, U>
  349. {
  350. static void M1<V>() { $$ }
  351. }
  352. ", "C1.M1()", 0);
  353. }
  354. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  355. public void TestGenericParameters()
  356. {
  357. Test(
  358. @"class C1<T, U>
  359. {
  360. static void M1<V>(C1<int, V> x, V y) { $$ }
  361. }
  362. ", "C1.M1(C1<int, V> x, V y)", 0);
  363. }
  364. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  365. public void TestMissingNamespace()
  366. {
  367. Test(
  368. @"{
  369. class Class
  370. {
  371. int a1, a$$2;
  372. }
  373. }", "Class.a2", 0);
  374. }
  375. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  376. public void TestMissingNamespaceName()
  377. {
  378. Test(
  379. @"namespace
  380. {
  381. class C1
  382. {
  383. int M1()
  384. $${
  385. }
  386. }
  387. }", "?.C1.M1()", 1);
  388. }
  389. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  390. public void TestMissingClassName()
  391. {
  392. Test(
  393. @"namespace N1
  394. class
  395. {
  396. int M1()
  397. $${
  398. }
  399. }
  400. }", "N1.M1()", 1); // Old implementation displayed "N1.?.M1", but we don't see a class declaration in the syntax tree...
  401. }
  402. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  403. public void TestMissingMethodName()
  404. {
  405. Test(
  406. @"namespace N1
  407. {
  408. class C1
  409. {
  410. static void (int x)
  411. {
  412. $$}
  413. }
  414. }", "N1.C1", 4);
  415. }
  416. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  417. public void TestMissingParameterList()
  418. {
  419. Test(
  420. @"namespace N1
  421. {
  422. class C1
  423. {
  424. static void M1
  425. {
  426. $$}
  427. }
  428. }", "N1.C1.M1", 2);
  429. }
  430. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  431. public void TopLevelField()
  432. {
  433. Test(
  434. @"$$int f1;
  435. ", "f1", 0, new CSharpParseOptions(kind: SourceCodeKind.Script));
  436. }
  437. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  438. public void TopLevelMethod()
  439. {
  440. Test(
  441. @"int M1(int x)
  442. {
  443. $$}
  444. ", "M1(int x)", 2, new CSharpParseOptions(kind: SourceCodeKind.Script));
  445. }
  446. [Fact, Trait(Traits.Feature, Traits.Features.DebuggingLocationName)]
  447. public void TopLevelStatement()
  448. {
  449. Test(
  450. @"
  451. $$System.Console.WriteLine(""Hello"")
  452. ", null, 0, new CSharpParseOptions(kind: SourceCodeKind.Interactive));
  453. }
  454. }
  455. }