コード例 #1
0
 private static bool IsIncluded(Diagnostic diagnostic, AllowedDiagnostics allowedDiagnostics)
 {
     return(allowedDiagnostics switch
     {
         AllowedDiagnostics.Warnings => diagnostic.Severity == DiagnosticSeverity.Error,
         AllowedDiagnostics.None => diagnostic.Severity == DiagnosticSeverity.Error || diagnostic.Severity == DiagnosticSeverity.Warning,
         AllowedDiagnostics.WarningsAndErrors => false,
         _ => throw new ArgumentOutOfRangeException(),
     });
コード例 #2
0
        private static bool IsIncluded(Diagnostic diagnostic, AllowedDiagnostics allowedDiagnostics)
        {
            switch (allowedDiagnostics)
            {
            case AllowedDiagnostics.Warnings:
                return(diagnostic.Severity == DiagnosticSeverity.Error);

            case AllowedDiagnostics.None:
                return(diagnostic.Severity == DiagnosticSeverity.Error || diagnostic.Severity == DiagnosticSeverity.Warning);

            case AllowedDiagnostics.WarningsAndErrors:
                return(false);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AllowedDiagnosticsAttribute"/> class.
 /// </summary>
 /// <param name="allowedDiagnostics">The diagnostics to allow in the code generated by a code fix when calling AnalyzerAssert.CodeFix and AnalyzerAssert.FixAll.</param>
 public AllowedDiagnosticsAttribute(AllowedDiagnostics allowedDiagnostics)
 {
     this.AllowedDiagnostics = allowedDiagnostics;
 }
コード例 #4
0
        private static void NoCompilerErrors(IReadOnlyList <ImmutableArray <Diagnostic> > diagnostics, IReadOnlyList <string> allowedIds, AllowedDiagnostics allowedDiagnostics)
        {
            var introducedDiagnostics = diagnostics
                                        .SelectMany(x => x)
                                        .Where(x => IsIncluded(x, allowedDiagnostics))
                                        .Where(x => !IsExcluded(x))
                                        .ToArray();

            if (introducedDiagnostics.Select(x => x.Id)
                .Except(allowedIds ?? Enumerable.Empty <string>())
                .Any())
            {
                var error = StringBuilderPool.Borrow();
                error.AppendLine($"Found error{(introducedDiagnostics.Length > 1 ? "s" : string.Empty)}.");
                foreach (var introducedDiagnostic in introducedDiagnostics)
                {
                    error.AppendLine($"{introducedDiagnostic.ToErrorString()}");
                }

                throw new AssertException(StringBuilderPool.Return(error));
            }

            bool IsExcluded(Diagnostic diagnostic)
            {
                if (allowedIds.Contains(diagnostic.Id))
                {
                    return(true);
                }

                switch (diagnostic.Id)
                {
                case "CS1061" when diagnostic.GetMessage(CultureInfo.InvariantCulture).Contains("does not contain a definition for 'InitializeComponent' and no accessible extension method 'InitializeComponent' accepting a first argument of type"):
                    return(true);
                }

                return(false);
            }
        }
コード例 #5
0
        /// <summary>
        /// Check the solution for compiler errors and warnings.
        /// </summary>
        public static void NoCompilerErrors(Solution solution, IReadOnlyList <string> allowedIds, AllowedDiagnostics allowedDiagnostics)
        {
            var diagnostics = Analyze.GetDiagnostics(solution);

            NoCompilerErrors(diagnostics, allowedIds, allowedDiagnostics);
        }
コード例 #6
0
        /// <summary>
        /// Check the solution for compiler errors and warnings, uses:
        /// </summary>
        public static async Task NoCompilerErrorsAsync(Solution solution, IReadOnlyList <string> allowedIds, AllowedDiagnostics allowedDiagnostics)
        {
            var diagnostics = await Analyze.GetDiagnosticsAsync(solution).ConfigureAwait(false);

            var introducedDiagnostics = diagnostics
                                        .SelectMany(x => x)
                                        .Where(x => IsIncluded(x, allowedDiagnostics))
                                        .ToArray();

            if (introducedDiagnostics.Select(x => x.Id)
                .Except(allowedIds ?? Enumerable.Empty <string>())
                .Any())
            {
                var error = StringBuilderPool.Borrow();
                error.AppendLine($"Found error{(introducedDiagnostics.Length > 1 ? "s" : string.Empty)}.");
                foreach (var introducedDiagnostic in introducedDiagnostics)
                {
                    var errorInfo = await introducedDiagnostic.ToStringAsync(solution).ConfigureAwait(false);

                    error.AppendLine($"{errorInfo}");
                }

                throw AssertException.Create(StringBuilderPool.Return(error));
            }
        }