Ejemplo n.º 1
0
            public Test(string testCode, IEnumerable <DiagnosticResult> expectedDiagnostics, IDictionary <string, ReportDiagnostic>?diagnosticOptions = null)
            {
                ExpectedDiagnostics.AddRange(expectedDiagnostics);
                TestCode      = testCode;
                TestBehaviors = TestBehaviors.SkipGeneratedCodeCheck;
                diagnosticOptions ??= _systemAnalyzer.SupportedDiagnostics
                .Select(item => item.Id)
                .ToDictionary(item => item, _ => ReportDiagnostic.Error);

                SolutionTransforms.Add((solution, projectId) =>
                {
                    solution = solution.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(LazyAttribute).Assembly.Location));

                    var project = solution
                                  .GetProject(projectId);

                    var compilationOptions = (CSharpCompilationOptions)project !.CompilationOptions !;

                    compilationOptions = compilationOptions
                                         .WithGeneralDiagnosticOption(ReportDiagnostic.Error)
                                         .WithSpecificDiagnosticOptions(diagnosticOptions)
                                         .WithNullableContextOptions(NullableContextOptions.Enable);

                    solution = solution.WithProjectCompilationOptions(projectId, compilationOptions);

                    return(solution);
                });
            }
Ejemplo n.º 2
0
        public async Task WhenCorrectName_Ignores()
        {
            TestCode = "public class CustomException : System.Exception { }";

            ExpectedDiagnostics.Clear();

            await RunAsync();
        }
            public AnalyzerTest(
                string source,
                params DiagnosticResult[] expected)
            {
                TestCode = source;
                ExpectedDiagnostics.AddRange(expected);
                ReferenceAssemblies = ReferenceAssemblies.Net.Net50;

                TestState.AdditionalReferences.Add(typeof(EnumGenerationAttribute).Assembly);
            }
Ejemplo n.º 4
0
        public async Task WhenInconsistentName_ShowsWarning()
        {
            TestCode = "public class CustomError : System.Exception { }";

            ExpectedDiagnostics.Add(
                new DiagnosticResult(Descriptors.PG0001ExceptionNameFormat.Id, DiagnosticSeverity.Warning)
                .WithMessage("CustomError class name should end with Exception")
                .WithSpan(1, 14, 1, 25));

            await RunAsync();
        }
        public override async Task RunAsync(CancellationToken cancellationToken = default)
        {
            var project = CreateProject();

            var diagnostics = await GetDiagnostics(project, cancellationToken);

            VerifyDiagnostics(diagnostics, ExpectedDiagnostics.ToArray());

            if (FixedCode != null)
            {
                var fixedProject = await ApplyCodeFixes(project, diagnostics, cancellationToken);

                await VerifyFixes(fixedProject, cancellationToken);
            }
        }
        public AnalyzerTest(
            string source,
            IEnumerable <Assembly> additionalReferences,
            params DiagnosticResult[] expected)
        {
            TestCode = source;
            ExpectedDiagnostics.AddRange(expected);
#if NET6_0
            ReferenceAssemblies = new ReferenceAssemblies("net6.0", new PackageIdentity("Microsoft.NETCore.App.Ref", "6.0.0"), Path.Combine("ref", "net6.0"));
#else
            ReferenceAssemblies = ReferenceAssemblies.Net.Net50;
#endif

            foreach (var additionalReference in additionalReferences)
            {
                TestState.AdditionalReferences.Add(additionalReference);
            }

            this.DisableNullableReferenceWarnings();
        }