PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/Compilers/CSharp/Test/Semantic/Semantics/UsingStatementTests.cs

https://github.com/EkardNT/Roslyn
C# | 730 lines | 608 code | 82 blank | 40 comment | 0 complexity | e4a2a3ddfcd29ed43ca17217b1c47dfc MD5 | raw file
  1. // Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.CodeAnalysis.CSharp.Symbols;
  6. using Microsoft.CodeAnalysis.CSharp.Syntax;
  7. using Microsoft.CodeAnalysis.Text;
  8. using Roslyn.Test.Utilities;
  9. using Xunit;
  10. namespace Microsoft.CodeAnalysis.CSharp.UnitTests
  11. {
  12. /// <summary>
  13. /// Tests related to binding (but not lowering) using statements (not directives).
  14. /// </summary>
  15. public class UsingStatementTests : CompilingTestBase
  16. {
  17. string ManagedClass = @"
  18. class MyManagedType : System.IDisposable
  19. {
  20. public void Dispose()
  21. { }
  22. }";
  23. string ManagedStruct = @"
  24. struct MyManagedType : System.IDisposable
  25. {
  26. public void Dispose()
  27. { }
  28. }";
  29. [Fact]
  30. public void SemanticModel()
  31. {
  32. var source = @"
  33. class C
  34. {
  35. static void Main()
  36. {
  37. using (System.IDisposable i = null)
  38. {
  39. i.Dispose(); //this makes no sense, but we're only testing binding
  40. }
  41. }
  42. }
  43. ";
  44. var compilation = CreateCompilationWithMscorlib(source);
  45. compilation.VerifyDiagnostics();
  46. var tree = compilation.SyntaxTrees.Single();
  47. var model = compilation.GetSemanticModel(tree);
  48. var usingStatement = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().Single();
  49. var declaredSymbol = model.GetDeclaredSymbol(usingStatement.Declaration.Variables.Single());
  50. Assert.NotNull(declaredSymbol);
  51. Assert.Equal(SymbolKind.Local, declaredSymbol.Kind);
  52. var declaredLocal = (LocalSymbol)declaredSymbol;
  53. Assert.Equal("i", declaredLocal.Name);
  54. Assert.Equal(SpecialType.System_IDisposable, declaredLocal.Type.SpecialType);
  55. var memberAccessExpression = tree.GetCompilationUnitRoot().DescendantNodes().OfType<MemberAccessExpressionSyntax>().Single();
  56. var info = model.GetSymbolInfo(memberAccessExpression.Expression);
  57. Assert.NotNull(info);
  58. Assert.Equal(declaredLocal, info.Symbol);
  59. var lookupSymbol = model.LookupSymbols(memberAccessExpression.SpanStart, name: declaredLocal.Name).Single();
  60. Assert.Equal(declaredLocal, lookupSymbol);
  61. }
  62. [Fact]
  63. public void MethodGroup()
  64. {
  65. var source = @"
  66. class C
  67. {
  68. static void Main()
  69. {
  70. using (Main)
  71. {
  72. }
  73. }
  74. }
  75. ";
  76. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  77. // (6,16): error CS1674: 'method group': type used in a using statement must be implicitly convertible to 'System.IDisposable'
  78. Diagnostic(ErrorCode.ERR_NoConvToIDisp, "Main").WithArguments("method group"));
  79. }
  80. [Fact]
  81. public void Lambda()
  82. {
  83. var source = @"
  84. class C
  85. {
  86. static void Main()
  87. {
  88. using (x => x)
  89. {
  90. }
  91. }
  92. }
  93. ";
  94. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  95. // (6,16): error CS1674: 'lambda expression': type used in a using statement must be implicitly convertible to 'System.IDisposable'
  96. Diagnostic(ErrorCode.ERR_NoConvToIDisp, "x => x").WithArguments("lambda expression"));
  97. }
  98. [Fact]
  99. public void Null()
  100. {
  101. var source = @"
  102. class C
  103. {
  104. static void Main()
  105. {
  106. using (null)
  107. {
  108. }
  109. }
  110. }
  111. ";
  112. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  113. }
  114. [Fact]
  115. public void UnusedVariable()
  116. {
  117. var source = @"
  118. class C
  119. {
  120. static void Main()
  121. {
  122. using (System.IDisposable d = null)
  123. {
  124. }
  125. }
  126. }
  127. ";
  128. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  129. }
  130. [Fact]
  131. public void EmbeddedStatement()
  132. {
  133. var source = @"
  134. class C
  135. {
  136. static void Main()
  137. {
  138. using (System.IDisposable a = null)
  139. using (System.IDisposable b = null)
  140. using (System.IDisposable c = null) ;
  141. }
  142. }
  143. ";
  144. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  145. // (8,53): warning CS0642: Possible mistaken empty statement
  146. Diagnostic(ErrorCode.WRN_PossibleMistakenNullStatement, ";"));
  147. }
  148. [Fact]
  149. public void ModifyUsingLocal()
  150. {
  151. var source = @"
  152. using System;
  153. class C
  154. {
  155. static void Main()
  156. {
  157. using (IDisposable i = null)
  158. {
  159. i = null;
  160. Ref(ref i);
  161. Out(out i);
  162. }
  163. }
  164. static void Ref(ref IDisposable i) { }
  165. static void Out(out IDisposable i) { i = null; }
  166. }
  167. ";
  168. var compilation = CreateCompilationWithMscorlib(source);
  169. compilation.VerifyDiagnostics(
  170. // (10,13): error CS1656: Cannot assign to 'i' because it is a 'using variable'
  171. Diagnostic(ErrorCode.ERR_AssgReadonlyLocalCause, "i").WithArguments("i", "using variable"),
  172. // (11,21): error CS1657: Cannot pass 'i' as a ref or out argument because it is a 'using variable'
  173. Diagnostic(ErrorCode.ERR_RefReadonlyLocalCause, "i").WithArguments("i", "using variable"),
  174. // (12,21): error CS1657: Cannot pass 'i' as a ref or out argument because it is a 'using variable'
  175. Diagnostic(ErrorCode.ERR_RefReadonlyLocalCause, "i").WithArguments("i", "using variable"));
  176. }
  177. [Fact]
  178. public void ImplicitType1()
  179. {
  180. var source = @"
  181. using System.IO;
  182. class C
  183. {
  184. static void Main()
  185. {
  186. using (var a = new StreamWriter(""""))
  187. {
  188. }
  189. }
  190. }
  191. ";
  192. var compilation = CreateCompilationWithMscorlib(source);
  193. compilation.VerifyDiagnostics();
  194. var tree = compilation.SyntaxTrees.Single();
  195. var model = compilation.GetSemanticModel(tree);
  196. var usingStatement = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().Single();
  197. var declaredSymbol = model.GetDeclaredSymbol(usingStatement.Declaration.Variables.Single());
  198. Assert.Equal("System.IO.StreamWriter a", declaredSymbol.ToTestDisplayString());
  199. var typeInfo = model.GetSymbolInfo(usingStatement.Declaration.Type);
  200. Assert.Equal(((LocalSymbol)declaredSymbol).Type, typeInfo.Symbol);
  201. }
  202. [Fact]
  203. public void ImplicitType2()
  204. {
  205. var source = @"
  206. using System.IO;
  207. class C
  208. {
  209. static void Main()
  210. {
  211. using (var a = new StreamWriter(""""), b = new StreamReader(""""))
  212. {
  213. }
  214. }
  215. }
  216. ";
  217. var compilation = CreateCompilationWithMscorlib(source);
  218. compilation.VerifyDiagnostics(
  219. // (8,16): error CS0819: Implicitly-typed variables cannot have multiple declarators
  220. Diagnostic(ErrorCode.ERR_ImplicitlyTypedVariableMultipleDeclarator, @"var a = new StreamWriter(""""), b = new StreamReader("""")"));
  221. var tree = compilation.SyntaxTrees.Single();
  222. var model = compilation.GetSemanticModel(tree);
  223. var usingStatement = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().Single();
  224. var firstDeclaredSymbol = model.GetDeclaredSymbol(usingStatement.Declaration.Variables.First());
  225. Assert.Equal("System.IO.StreamWriter a", firstDeclaredSymbol.ToTestDisplayString());
  226. var typeInfo = model.GetSymbolInfo(usingStatement.Declaration.Type);
  227. // lowest/last bound node with associated syntax is being picked up. Fine for now.
  228. Assert.Equal(((LocalSymbol)model.GetDeclaredSymbol(usingStatement.Declaration.Variables.Last())).Type, typeInfo.Symbol);
  229. }
  230. [Fact]
  231. public void ModifyLocalInUsingExpression()
  232. {
  233. var source = @"
  234. using System;
  235. class C
  236. {
  237. void Main()
  238. {
  239. IDisposable i = null;
  240. using (i)
  241. {
  242. i = null; //CS0728
  243. Ref(ref i); //CS0728
  244. this[out i] = 1; //CS0728
  245. }
  246. }
  247. void Ref(ref IDisposable i) { }
  248. int this[out IDisposable i] { set { i = null; } } //this is illegal, so if we break this test, we may need a metadata indexer
  249. }
  250. ";
  251. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  252. // (18,14): error CS0631: ref and out are not valid in this context
  253. Diagnostic(ErrorCode.ERR_IllegalRefParam, "out"),
  254. // (11,13): warning CS0728: Possibly incorrect assignment to local 'i' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
  255. Diagnostic(ErrorCode.WRN_AssignmentToLockOrDispose, "i").WithArguments("i"),
  256. // (12,21): warning CS0728: Possibly incorrect assignment to local 'i' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
  257. Diagnostic(ErrorCode.WRN_AssignmentToLockOrDispose, "i").WithArguments("i"),
  258. // (13,22): warning CS0728: Possibly incorrect assignment to local 'i' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
  259. Diagnostic(ErrorCode.WRN_AssignmentToLockOrDispose, "i").WithArguments("i"));
  260. }
  261. [Fact]
  262. public void ModifyParameterInUsingExpression()
  263. {
  264. var source = @"
  265. using System;
  266. class C
  267. {
  268. void M(IDisposable i)
  269. {
  270. using (i)
  271. {
  272. i = null; //CS0728
  273. Ref(ref i); //CS0728
  274. this[out i] = 1; //CS0728
  275. }
  276. }
  277. void Ref(ref IDisposable i) { }
  278. int this[out IDisposable i] { set { i = null; } } //this is illegal, so if we break this test, we may need a metadata indexer
  279. }
  280. ";
  281. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  282. // (17,14): error CS0631: ref and out are not valid in this context
  283. Diagnostic(ErrorCode.ERR_IllegalRefParam, "out"),
  284. // (10,13): warning CS0728: Possibly incorrect assignment to local 'i' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
  285. Diagnostic(ErrorCode.WRN_AssignmentToLockOrDispose, "i").WithArguments("i"),
  286. // (11,21): warning CS0728: Possibly incorrect assignment to local 'i' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
  287. Diagnostic(ErrorCode.WRN_AssignmentToLockOrDispose, "i").WithArguments("i"),
  288. // (12,22): warning CS0728: Possibly incorrect assignment to local 'i' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local.
  289. Diagnostic(ErrorCode.WRN_AssignmentToLockOrDispose, "i").WithArguments("i"));
  290. }
  291. // The object could be created outside the "using" statement
  292. [Fact]
  293. public void ResourceCreatedOutsideUsing()
  294. {
  295. var source = @"
  296. using System;
  297. class Program
  298. {
  299. static void Main(string[] args)
  300. {
  301. MyManagedType mnObj1 = null;
  302. using (mnObj1)
  303. {
  304. }
  305. }
  306. }
  307. " + ManagedClass;
  308. var compilation = CreateCompilationWithMscorlib(source);
  309. VerifyDeclaredSymbolForUsingStatements(compilation);
  310. }
  311. // The object created inside the "using" statement but declared no variable
  312. [Fact]
  313. public void ResourceCreatedInsideUsingWithNoVarDecalred()
  314. {
  315. var source = @"
  316. using System;
  317. class Program
  318. {
  319. static void Main(string[] args)
  320. {
  321. using (new MyManagedType())
  322. {
  323. }
  324. }
  325. }
  326. " + ManagedStruct;
  327. var compilation = CreateCompilationWithMscorlib(source);
  328. VerifyDeclaredSymbolForUsingStatements(compilation);
  329. }
  330. // Multiple resource created inside Using
  331. /// <bug id="10509" project="Roslyn"/>
  332. [Fact()]
  333. public void MultipleResourceCreatedInsideUsing()
  334. {
  335. var source = @"
  336. using System;
  337. class Program
  338. {
  339. static void Main(string[] args)
  340. {
  341. using (MyManagedType mnObj1 = null, mnObj2 = default(MyManagedType))
  342. {
  343. }
  344. }
  345. }
  346. " + ManagedStruct;
  347. var compilation = CreateCompilationWithMscorlib(source);
  348. var symbols = VerifyDeclaredSymbolForUsingStatements(compilation, 1, "mnObj1", "mnObj2");
  349. foreach (var x in symbols)
  350. {
  351. VerifySymbolInfoForUsingStatements(compilation, ((LocalSymbol)x).Type);
  352. }
  353. }
  354. [Fact]
  355. public void MultipleResourceCreatedInNestedUsing()
  356. {
  357. var source = @"
  358. using System;
  359. class Program
  360. {
  361. static void Main(string[] args)
  362. {
  363. using (MyManagedType mnObj1 = null, mnObj2 = default(MyManagedType))
  364. {
  365. using (MyManagedType mnObj3 = null, mnObj4 = default(MyManagedType))
  366. {
  367. mnObj3.Dispose();
  368. }
  369. }
  370. }
  371. }
  372. " + ManagedClass;
  373. var compilation = CreateCompilationWithMscorlib(source);
  374. var symbols = VerifyDeclaredSymbolForUsingStatements(compilation, 2, "mnObj3", "mnObj4");
  375. foreach (var x in symbols)
  376. {
  377. var localSymbol = (LocalSymbol)x;
  378. VerifyLookUpSymbolForUsingStatements(compilation, localSymbol, 2);
  379. VerifySymbolInfoForUsingStatements(compilation, ((LocalSymbol)x).Type, 2);
  380. }
  381. }
  382. [Fact]
  383. public void ResourceTypeDerivedFromClassImplementIdisposable()
  384. {
  385. var source = @"
  386. using System;
  387. class Program
  388. {
  389. public static void Main(string[] args)
  390. {
  391. using (MyManagedTypeDerived mnObj = new MyManagedTypeDerived())
  392. {
  393. }
  394. }
  395. }
  396. class MyManagedTypeDerived : MyManagedType
  397. { }
  398. " + ManagedClass;
  399. var compilation = CreateCompilationWithMscorlib(source);
  400. var symbols = VerifyDeclaredSymbolForUsingStatements(compilation, 1, "mnObj");
  401. foreach (var x in symbols)
  402. {
  403. var localSymbol = (LocalSymbol)x;
  404. VerifyLookUpSymbolForUsingStatements(compilation, localSymbol, 1);
  405. VerifySymbolInfoForUsingStatements(compilation, ((LocalSymbol)x).Type, 1);
  406. }
  407. }
  408. [Fact]
  409. public void LinqInUsing()
  410. {
  411. var source = @"
  412. using System;
  413. using System.Linq;
  414. class Program
  415. {
  416. public static void Main(string[] args)
  417. {
  418. using (var mnObj = (from x in ""1"" select new MyManagedType()).First () )
  419. {
  420. }
  421. }
  422. }
  423. " + ManagedClass;
  424. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  425. var symbols = VerifyDeclaredSymbolForUsingStatements(compilation, 1, "mnObj");
  426. foreach (var x in symbols)
  427. {
  428. var localSymbol = (LocalSymbol)x;
  429. VerifyLookUpSymbolForUsingStatements(compilation, localSymbol, 1);
  430. VerifySymbolInfoForUsingStatements(compilation, ((LocalSymbol)x).Type, 1);
  431. }
  432. }
  433. [Fact]
  434. public void LambdaInUsing()
  435. {
  436. var source = @"
  437. using System;
  438. using System.Linq;
  439. class Program
  440. {
  441. public static void Main(string[] args)
  442. {
  443. MyManagedType[] mnObjs = { };
  444. using (var mnObj = mnObjs.Where(x => x.ToString() == "").First())
  445. {
  446. }
  447. }
  448. }
  449. " + ManagedStruct;
  450. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  451. var symbols = VerifyDeclaredSymbolForUsingStatements(compilation, 1, "mnObj");
  452. foreach (var x in symbols)
  453. {
  454. var localSymbol = (LocalSymbol)x;
  455. VerifyLookUpSymbolForUsingStatements(compilation, localSymbol, 1);
  456. VerifySymbolInfoForUsingStatements(compilation, ((LocalSymbol)x).Type, 1);
  457. }
  458. }
  459. [Fact]
  460. public void UsingForGenericType()
  461. {
  462. var source = @"
  463. using System;
  464. using System.Collections.Generic;
  465. class Test<T>
  466. {
  467. public static IEnumerator<T> M<U>(IEnumerable<T> items) where U : IDisposable, new()
  468. {
  469. using (U u = new U())
  470. {
  471. }
  472. return null;
  473. }
  474. }
  475. ";
  476. var compilation = CreateCompilationWithMscorlibAndSystemCore(source);
  477. var symbols = VerifyDeclaredSymbolForUsingStatements(compilation, 1, "u");
  478. foreach (var x in symbols)
  479. {
  480. var localSymbol = (LocalSymbol)x;
  481. VerifyLookUpSymbolForUsingStatements(compilation, localSymbol, 1);
  482. VerifySymbolInfoForUsingStatements(compilation, ((LocalSymbol)x).Type, 1);
  483. }
  484. }
  485. [Fact]
  486. public void UsingForGenericTypeWithClassConstraint()
  487. {
  488. var source = @"using System;
  489. class A { }
  490. class B : A, IDisposable
  491. {
  492. void IDisposable.Dispose() { }
  493. }
  494. class C
  495. {
  496. static void M<T0, T1, T2, T3, T4>(T0 t0, T1 t1, T2 t2, T3 t3, T4 t4)
  497. where T0 : A
  498. where T1 : A, IDisposable
  499. where T2 : B
  500. where T3 : T1
  501. where T4 : T2
  502. {
  503. using (t0) { }
  504. using (t1) { }
  505. using (t2) { }
  506. using (t3) { }
  507. using (t4) { }
  508. }
  509. }";
  510. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  511. // (16,16): error CS1674: 'T0': type used in a using statement must be implicitly convertible to 'System.IDisposable'
  512. Diagnostic(ErrorCode.ERR_NoConvToIDisp, "t0").WithArguments("T0").WithLocation(16, 16));
  513. }
  514. [WorkItem(543168, "DevDiv")]
  515. [Fact]
  516. public void EmbeddedDeclaration()
  517. {
  518. var source = @"
  519. class C
  520. {
  521. static void Main()
  522. {
  523. using(null) object o = new object();
  524. }
  525. }
  526. ";
  527. CreateCompilationWithMscorlib(source).VerifyDiagnostics(
  528. // (6,20): error CS1023: Embedded statement cannot be a declaration or labeled statement
  529. Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "object o = new object();"));
  530. }
  531. [WorkItem(529547, "DevDiv")]
  532. [Fact]
  533. public void UnusedLocal()
  534. {
  535. var source = @"
  536. using System;
  537. class C : IDisposable
  538. {
  539. public void Dispose()
  540. {
  541. }
  542. }
  543. struct S : IDisposable
  544. {
  545. public void Dispose()
  546. {
  547. }
  548. }
  549. public class Test
  550. {
  551. public static void Main()
  552. {
  553. using (S s = new S()) { } //fine
  554. using (C c = new C()) { } //fine
  555. }
  556. }";
  557. CreateCompilationWithMscorlib(source).VerifyDiagnostics();
  558. }
  559. [WorkItem(545331, "DevDiv")]
  560. [Fact]
  561. public void MissingIDisposable()
  562. {
  563. var source = @"
  564. class C
  565. {
  566. void M()
  567. {
  568. using (var v = null) ;
  569. }
  570. }";
  571. CreateCompilation(source).VerifyDiagnostics(
  572. // Related to the using statement:
  573. // (6,30): warning CS0642: Possible mistaken empty statement
  574. // using (var v = null) ;
  575. Diagnostic(ErrorCode.WRN_PossibleMistakenNullStatement, ";"),
  576. // Cascading from the lack of mscorlib:
  577. // (2,7): error CS0518: Predefined type 'System.Object' is not defined or imported
  578. // class C
  579. Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "C").WithArguments("System.Object"),
  580. // (4,5): error CS0518: Predefined type 'System.Void' is not defined or imported
  581. // void M()
  582. Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "void").WithArguments("System.Void"),
  583. // (6,16): error CS0518: Predefined type 'System.Object' is not defined or imported
  584. // using (var v = null) ;
  585. Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "var").WithArguments("System.Object"),
  586. // (6,20): error CS0815: Cannot assign <null> to an implicitly-typed variable
  587. // using (var v = null) ;
  588. Diagnostic(ErrorCode.ERR_ImplicitlyTypedVariableAssignedBadValue, "v = null").WithArguments("<null>"),
  589. // (2,7): error CS1729: 'object' does not contain a constructor that takes 0 arguments
  590. // class C
  591. Diagnostic(ErrorCode.ERR_BadCtorArgCount, "C").WithArguments("object", "0")
  592. );
  593. }
  594. #region help method
  595. UsingStatementSyntax GetUsingStatements(CSharpCompilation compilation, int index = 1)
  596. {
  597. var tree = compilation.SyntaxTrees.Single();
  598. var model = compilation.GetSemanticModel(tree);
  599. var usingStatements = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().ToList();
  600. return usingStatements[index - 1];
  601. }
  602. IEnumerable VerifyDeclaredSymbolForUsingStatements(CSharpCompilation compilation, int index = 1, params string[] variables)
  603. {
  604. var tree = compilation.SyntaxTrees.Single();
  605. var model = compilation.GetSemanticModel(tree);
  606. var usingStatements = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().ToList();
  607. var i = 0;
  608. foreach (var x in usingStatements[index - 1].Declaration.Variables)
  609. {
  610. var symbol = model.GetDeclaredSymbol(x);
  611. Assert.Equal(SymbolKind.Local, symbol.Kind);
  612. Assert.Equal(variables[i++].ToString(), symbol.ToDisplayString());
  613. yield return symbol;
  614. }
  615. }
  616. SymbolInfo VerifySymbolInfoForUsingStatements(CSharpCompilation compilation, Symbol symbol, int index = 1)
  617. {
  618. var tree = compilation.SyntaxTrees.Single();
  619. var model = compilation.GetSemanticModel(tree);
  620. var usingStatements = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().ToList();
  621. var type = model.GetSymbolInfo(usingStatements[index - 1].Declaration.Type);
  622. Assert.Equal(symbol, type.Symbol);
  623. return type;
  624. }
  625. ISymbol VerifyLookUpSymbolForUsingStatements(CSharpCompilation compilation, Symbol symbol, int index = 1)
  626. {
  627. var tree = compilation.SyntaxTrees.Single();
  628. var model = compilation.GetSemanticModel(tree);
  629. var usingStatements = tree.GetCompilationUnitRoot().DescendantNodes().OfType<UsingStatementSyntax>().ToList();
  630. var actualSymbol = model.LookupSymbols(usingStatements[index - 1].SpanStart, name: symbol.Name).Single();
  631. Assert.Equal(SymbolKind.Local, actualSymbol.Kind);
  632. Assert.Equal(symbol, actualSymbol);
  633. return actualSymbol;
  634. }
  635. #endregion
  636. }
  637. }