PageRenderTime 65ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Compilers/Test/Utilities/CSharp/DiagnosticTestUtilities.cs

https://gitlab.com/sharadag/Roslyn
C# | 229 lines | 152 code | 33 blank | 44 comment | 23 complexity | 5de6685909408e72844dccf6a0b01c3d 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;
  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.CSharp.Test.Utilities;
  8. using Microsoft.CodeAnalysis.Test.Utilities;
  9. using Roslyn.Utilities;
  10. using Xunit;
  11. namespace Microsoft.CodeAnalysis.CSharp.UnitTests
  12. {
  13. /// <summary>
  14. /// OBSOLETE: Use DiagnosticDescription instead.
  15. /// </summary>
  16. public struct ErrorDescription
  17. {
  18. public bool IsWarning;
  19. public int Code, Line, Column;
  20. public string[] Parameters;
  21. public override string ToString()
  22. {
  23. return string.Format("Line={0}, Column={1}, Code={2}, IsWarning={3}", this.Line, this.Column, this.Code, this.IsWarning);
  24. }
  25. }
  26. public abstract class DiagnosticsUtils
  27. {
  28. /// <summary>
  29. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  30. /// </summary>
  31. public static void VerifyErrorCodes(CSharpCompilation comp, params ErrorDescription[] expectedErrors)
  32. {
  33. var actualErrors = comp.GetDiagnostics();
  34. DiagnosticsUtils.VerifyErrorCodes(actualErrors, expectedErrors);
  35. }
  36. /// <summary>
  37. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  38. /// </summary>
  39. [Obsolete("Use VerifyDiagnostics", true)]
  40. public static void TestDiagnostics(string source, params string[] diagStrings)
  41. {
  42. var comp = CSharpTestBase.CreateCompilationWithMscorlib(source);
  43. var diagnostics = comp.GetDiagnostics();
  44. CompilingTestBase.TestDiagnostics(diagnostics, diagStrings);
  45. }
  46. /// <summary>
  47. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  48. /// </summary>
  49. [Obsolete("Use VerifyDiagnostics", true)]
  50. public static void TestDiagnosticsExact(string source, params string[] diagStrings)
  51. {
  52. var comp = CSharpTestBase.CreateCompilationWithMscorlib(source);
  53. var diagnostics = comp.GetDiagnostics();
  54. Assert.Equal(diagStrings.Length, diagnostics.Length);
  55. CompilingTestBase.TestDiagnostics(diagnostics, diagStrings);
  56. }
  57. /// <summary>
  58. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  59. /// </summary>
  60. public static CSharpCompilation VerifyErrorsAndGetCompilationWithMscorlib(string text, params ErrorDescription[] expectedErrorDesp)
  61. {
  62. return VerifyErrorsAndGetCompilationWithMscorlib(new string[] { text }, expectedErrorDesp);
  63. }
  64. /// <summary>
  65. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  66. /// </summary>
  67. internal protected static CSharpCompilation VerifyErrorsAndGetCompilationWithMscorlib(string[] srcs, params ErrorDescription[] expectedErrorDesp)
  68. {
  69. var comp = CSharpTestBase.CreateCompilationWithMscorlib(srcs);
  70. var actualErrors = comp.GetDiagnostics();
  71. VerifyErrorCodes(actualErrors, expectedErrorDesp);
  72. return comp;
  73. }
  74. /// <summary>
  75. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  76. /// </summary>
  77. internal protected static CSharpCompilation VerifyErrorsAndGetCompilationWithMscorlib(string text, IEnumerable<MetadataReference> refs, params ErrorDescription[] expectedErrorDesp)
  78. {
  79. return VerifyErrorsAndGetCompilationWithMscorlib(new List<string> { text }, refs, expectedErrorDesp);
  80. }
  81. /// <summary>
  82. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  83. /// </summary>
  84. internal protected static CSharpCompilation VerifyErrorsAndGetCompilationWithMscorlib(List<string> srcs, IEnumerable<MetadataReference> refs, params ErrorDescription[] expectedErrorDesp)
  85. {
  86. var synTrees = (from text in srcs
  87. select SyntaxFactory.ParseSyntaxTree(text)).ToArray();
  88. return VerifyErrorsAndGetCompilationWithMscorlib(synTrees, refs, expectedErrorDesp);
  89. }
  90. /// <summary>
  91. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  92. /// </summary>
  93. internal protected static CSharpCompilation VerifyErrorsAndGetCompilationWithMscorlib(SyntaxTree[] trees, IEnumerable<MetadataReference> refs, params ErrorDescription[] expectedErrorDesp)
  94. {
  95. return VerifyErrorsAndGetCompilation(trees, refs.Concat(CSharpTestBase.MscorlibRef), expectedErrorDesp);
  96. }
  97. /// <summary>
  98. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  99. /// </summary>
  100. internal protected static CSharpCompilation VerifyErrorsAndGetCompilation(IEnumerable<SyntaxTree> synTrees, IEnumerable<MetadataReference> refs = null, params ErrorDescription[] expectedErrorDesp)
  101. {
  102. var comp = CSharpCompilation.Create(assemblyName: "DiagnosticsTest", options: TestOptions.ReleaseDll, syntaxTrees: synTrees, references: refs);
  103. var actualErrors = comp.GetDiagnostics();
  104. VerifyErrorCodes(actualErrors, expectedErrorDesp);
  105. return comp;
  106. }
  107. /// <summary>
  108. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  109. /// </summary>
  110. public static void VerifyErrorCodes(IEnumerable<Diagnostic> actualErrors, params ErrorDescription[] expectedErrorDesp)
  111. {
  112. if (expectedErrorDesp == null)
  113. return;
  114. int expectedLength = expectedErrorDesp.Length;
  115. int actualLength = actualErrors.Count();
  116. Assert.True(
  117. expectedLength == actualLength,
  118. String.Format(
  119. "ErrCount {0} != {1}{2}Actual errors are:{2}{3}",
  120. expectedLength,
  121. actualLength,
  122. Environment.NewLine,
  123. actualLength == 0 ? "<none>" : string.Join(Environment.NewLine, actualErrors)));
  124. var actualSortedDesp = (from ae in
  125. (from e in actualErrors
  126. let lineSpan = e.Location.GetMappedLineSpan()
  127. select new ErrorDescription
  128. {
  129. Code = e.Code,
  130. Line = lineSpan.IsValid ? lineSpan.StartLinePosition.Line + 1 : 0,
  131. Column = lineSpan.IsValid ? lineSpan.StartLinePosition.Character + 1 : 0,
  132. IsWarning = e.Severity == DiagnosticSeverity.Warning,
  133. Parameters = (e.Arguments != null && e.Arguments.Count > 0 && e.Arguments[0] != null) ?
  134. e.Arguments.Select(x => x != null ? x.ToString() : null).ToArray() : SpecializedCollections.EmptyArray<string>()
  135. })
  136. orderby ae.Code, ae.Line, ae.Column
  137. select ae).ToList();
  138. var expectedSortedDesp = (from ee in expectedErrorDesp
  139. orderby ee.Code, ee.Line, ee.Column
  140. select ee).ToList();
  141. int idx = 0;
  142. // actual >= expected
  143. foreach (var experr in expectedSortedDesp)
  144. {
  145. while (idx < actualSortedDesp.Count && actualSortedDesp[idx].Code < experr.Code)
  146. {
  147. idx++;
  148. }
  149. if (idx >= actualSortedDesp.Count)
  150. {
  151. idx = actualSortedDesp.Count - 1;
  152. }
  153. var acterr = actualSortedDesp[idx];
  154. Assert.Equal(experr.Code, acterr.Code);
  155. if (experr.Line > 0 && experr.Column > 0)
  156. {
  157. Assert.True(experr.Line == acterr.Line, String.Format("Line {0}!={1}", experr.Line, acterr.Line));
  158. Assert.True(experr.Column == acterr.Column, String.Format("Col {0}!={1}", experr.Column, acterr.Column));
  159. }
  160. Assert.Equal(experr.IsWarning, acterr.IsWarning);
  161. //if the expected contains parameters, validate those too.
  162. if (experr.Parameters != null)
  163. {
  164. Assert.True(experr.Parameters.SequenceEqual(acterr.Parameters), String.Format("Param: {0}!={1}", experr.Parameters.Count(), acterr.Parameters.Count()));
  165. }
  166. idx++;
  167. }
  168. }
  169. /// <summary>
  170. /// OBSOLETE: Use VerifyDiagnostics from Roslyn.Compilers.CSharp.Test.Utilities instead.
  171. /// </summary>
  172. public static void VerifyErrorCodesNoLineColumn(IEnumerable<Diagnostic> actualErrors, params ErrorDescription[] expectedErrorDesp)
  173. {
  174. if (expectedErrorDesp == null)
  175. return;
  176. // TODO: for now, we only expected actual errors including all expected errors
  177. // Assert.Equal(expectedErrorDesp.Length, actualErrors.Count);
  178. // allow actual errors contain more same errors, no line & column check
  179. Assert.InRange(expectedErrorDesp.Length, 0, actualErrors.Count());
  180. var expectedCodes = (from e in expectedErrorDesp
  181. orderby e.Code
  182. group e by e.Code).ToList();
  183. var actualCodes = (from e in actualErrors
  184. orderby e.Code
  185. group e by e.Code).ToList();
  186. foreach (var expectedGroup in expectedCodes)
  187. {
  188. var actualGroup = actualCodes.SingleOrDefault(x => x.Key == expectedGroup.Key);
  189. var actualGroupCount = actualGroup != null ? actualGroup.Count() : 0;
  190. // Same error code *should* be same error type: error/warning
  191. // In other words, 0 <= # of expected occurrences <= # of actual occurrences
  192. Assert.InRange(expectedGroup.Count(), 0, actualGroupCount);
  193. }
  194. }
  195. }
  196. }