PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Src/Workspaces/CoreTest/FindAllDeclarationsTests.TestSolutionsAndProject.cs

https://github.com/EkardNT/Roslyn
C# | 272 lines | 253 code | 18 blank | 1 comment | 9 complexity | cef98f7311af509b103890d1548ca846 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;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.CodeAnalysis.Text;
  6. using Xunit;
  7. namespace Microsoft.CodeAnalysis.UnitTests
  8. {
  9. public partial class FindAllDeclarationsTests
  10. {
  11. private static void Verify(string searchTerm, bool respectCase, WorkspaceKind workspaceKind, IEnumerable<ISymbol> declarations, params string[] expectedResults)
  12. {
  13. var actualResultCount = declarations.Count();
  14. var expectedResultCount = expectedResults.Length;
  15. Assert.True(expectedResultCount == actualResultCount,
  16. string.Format("Search term '{0}' expected '{1}' results, found '{2}. Ignore case was set to '{3}', Workspace {4} was used",
  17. searchTerm,
  18. expectedResultCount,
  19. actualResultCount,
  20. respectCase,
  21. Enum.GetName(typeof(WorkspaceKind), workspaceKind)));
  22. if (actualResultCount > 0)
  23. {
  24. VerifyResults(declarations, expectedResults);
  25. }
  26. }
  27. private static void Verify(WorkspaceKind workspaceKind, IEnumerable<ISymbol> declarations, params string[] expectedResults)
  28. {
  29. var actualResultCount = declarations.Count();
  30. var expectedResultCount = expectedResults.Length;
  31. Assert.True(expectedResultCount == actualResultCount,
  32. string.Format("Expected '{0}' results, found '{1}. Workspace {2} was used",
  33. expectedResultCount,
  34. actualResultCount,
  35. Enum.GetName(typeof(WorkspaceKind), workspaceKind)));
  36. if (actualResultCount > 0)
  37. {
  38. VerifyResults(declarations, expectedResults);
  39. }
  40. }
  41. private static void VerifyResults(IEnumerable<ISymbol> declarations, string[] expectedResults)
  42. {
  43. declarations = declarations.OrderBy(d => d.ToString());
  44. expectedResults = expectedResults.OrderBy(r => r).ToArray();
  45. for (int i = 0; i < expectedResults.Length; i++)
  46. {
  47. var actualResult = declarations.ElementAt(i).ToString();
  48. var expectedResult = expectedResults[i];
  49. Assert.True(
  50. string.Equals(actualResult, expectedResult, StringComparison.Ordinal),
  51. string.Format("Expected result to be {0} was {1}", expectedResult, actualResult));
  52. }
  53. }
  54. private static void VerifyInnerExceptionArgumentNull(AggregateException ex, string argName)
  55. {
  56. var exception = ex.InnerException as ArgumentNullException;
  57. Assert.True(exception != null, string.Format("Expected InnterException to be 'System.ArgumentNullException' was '{0}'", ex.InnerException.ToString()));
  58. Assert.True(exception.ParamName.Contains(argName), string.Format("Expected InnerException ParamName to contain '{0}', actual ParamName is: '{1}'", argName, exception.ParamName));
  59. }
  60. private static void VerifyInnerExceptionIsType<T>(Exception ex) where T : Exception
  61. {
  62. Assert.True(ex.InnerException is T, string.Format("Expected InnterException to be '{0}' was '{1}'", typeof(T).Name, ex.InnerException.ToString()));
  63. }
  64. private static Solution CreateSolution()
  65. {
  66. return new CustomWorkspace().CurrentSolution;
  67. }
  68. private static Solution GetSingleProjectSolution(params string[] sourceTexts)
  69. {
  70. var pid = ProjectId.CreateNewId();
  71. var solution = CreateSolution()
  72. .AddProject(pid, "TestCases", "TestCases", LanguageNames.CSharp)
  73. .AddMetadataReference(pid, MscorlibRef);
  74. for (int i = 0; i < sourceTexts.Length; i++)
  75. {
  76. var did = DocumentId.CreateNewId(pid);
  77. solution = solution.AddDocument(did, "foo" + i + ".cs", SourceText.From(sourceTexts[i]));
  78. }
  79. return solution;
  80. }
  81. private static Solution GetMultipleProjectSolution(params string[] sourceTexts)
  82. {
  83. var solution = CreateSolution();
  84. for (int i = 0; i < sourceTexts.Length; i++)
  85. {
  86. var pid = ProjectId.CreateNewId();
  87. var did = DocumentId.CreateNewId(pid);
  88. solution = solution
  89. .AddProject(pid, "TestCases" + i, "TestCases" + i, LanguageNames.CSharp)
  90. .AddMetadataReference(pid, MscorlibRef);
  91. solution = solution.AddDocument(did, "foo" + i + ".cs", SourceText.From(sourceTexts[i]));
  92. }
  93. return solution;
  94. }
  95. private static Solution GetSolution(WorkspaceKind workspaceKind)
  96. {
  97. switch (workspaceKind)
  98. {
  99. case WorkspaceKind.SingleClass:
  100. return GetSingleProjectSolution(SingleClass);
  101. case WorkspaceKind.SingleClassWithSingleMethod:
  102. return GetSingleProjectSolution(SingleClassWithSingleMethod);
  103. case WorkspaceKind.SingleClassWithSingleProperty:
  104. return GetSingleProjectSolution(SingleClassWithSingleProperty);
  105. case WorkspaceKind.SingleClassWithSingleField:
  106. return GetSingleProjectSolution(SingleClassWithSingleField);
  107. case WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleMethod:
  108. return GetMultipleProjectSolution(SingleClassWithSingleMethod, SingleClassWithSingleMethod);
  109. case WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleProperty:
  110. return GetMultipleProjectSolution(SingleClassWithSingleProperty, SingleClassWithSingleProperty);
  111. case WorkspaceKind.TwoProjectsEachWithASingleClassWithSingleField:
  112. return GetMultipleProjectSolution(SingleClassWithSingleField, SingleClassWithSingleField);
  113. case WorkspaceKind.NestedClass:
  114. return GetSingleProjectSolution(NestedClass);
  115. case WorkspaceKind.TwoNamespacesWithIdenticalClases:
  116. return GetSingleProjectSolution(Namespace1, Namespace2);
  117. default:
  118. return null;
  119. }
  120. }
  121. private static Project GetProject(WorkspaceKind workspaceKind)
  122. {
  123. return GetSolution(workspaceKind).Projects.First();
  124. }
  125. public enum WorkspaceKind
  126. {
  127. SingleClass,
  128. SingleClassWithSingleMethod,
  129. SingleClassWithSingleProperty,
  130. SingleClassWithSingleField,
  131. TwoProjectsEachWithASingleClassWithSingleMethod,
  132. TwoProjectsEachWithASingleClassWithSingleProperty,
  133. TwoProjectsEachWithASingleClassWithSingleField,
  134. NestedClass,
  135. TwoNamespacesWithIdenticalClases
  136. }
  137. private const string SingleClass =
  138. @"
  139. using System;
  140. using System.Collections.Generic;
  141. using System.Linq;
  142. using System.Text;
  143. using System.Threading.Tasks;
  144. namespace TestCases
  145. {
  146. class TestCase
  147. {
  148. }
  149. }
  150. ";
  151. private const string SingleClassWithSingleMethod =
  152. @"
  153. using System;
  154. using System.Collections.Generic;
  155. using System.Linq;
  156. using System.Text;
  157. using System.Threading.Tasks;
  158. namespace TestCases
  159. {
  160. class TestCase
  161. {
  162. static void Test(string[] args)
  163. {
  164. }
  165. }
  166. }
  167. ";
  168. private const string SingleClassWithSingleProperty =
  169. @"
  170. using System;
  171. using System.Collections.Generic;
  172. using System.Linq;
  173. using System.Text;
  174. using System.Threading.Tasks;
  175. namespace TestCases
  176. {
  177. class TestCase
  178. {
  179. public int TestProperty{ get; set; }
  180. }
  181. }
  182. ";
  183. private const string SingleClassWithSingleField =
  184. @"
  185. using System;
  186. using System.Collections.Generic;
  187. using System.Linq;
  188. using System.Text;
  189. using System.Threading.Tasks;
  190. namespace TestCases
  191. {
  192. class TestCase
  193. {
  194. private int TestField = 0;
  195. }
  196. }
  197. ";
  198. private const string NestedClass =
  199. @"
  200. using System;
  201. using System.Collections.Generic;
  202. using System.Linq;
  203. using System.Text;
  204. using System.Threading.Tasks;
  205. namespace TestCases
  206. {
  207. class TestCase
  208. {
  209. class InnerTestCase
  210. {
  211. }
  212. }
  213. }
  214. ";
  215. private const string Namespace1 =
  216. @"
  217. using System;
  218. using System.Collections.Generic;
  219. using System.Linq;
  220. using System.Text;
  221. using System.Threading.Tasks;
  222. namespace TestCase1
  223. {
  224. class TestCase
  225. {
  226. }
  227. }
  228. ";
  229. private const string Namespace2 =
  230. @"
  231. using System;
  232. using System.Collections.Generic;
  233. using System.Linq;
  234. using System.Text;
  235. using System.Threading.Tasks;
  236. namespace TestCase2
  237. {
  238. class TestCase
  239. {
  240. }
  241. }
  242. ";
  243. }
  244. }