Skip to content

Commit 8cb31a4

Browse files
author
Anh Thi Dao
committed
added my test file and add some implementation of my analyzer
1 parent c190256 commit 8cb31a4

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis.Testing;
10+
using VerifyCS = Microsoft.AspNetCore.Analyzers.RouteHandlers.CSharpRouteHandlerCodeFixVerifier<
11+
Microsoft.AspNetCore.Analyzers.RouteHandlers.RouteHandlerAnalyzer,
12+
Microsoft.AspNetCore.Analyzers.RouteHandlers.Fixers.DetectMismatchedParameterOptionalityFixer>;
13+
14+
namespace Microsoft.AspNetCore.Analyzers.WebApplicationBuilder;
15+
public partial class DisallowConfigureAppConfigureHostBuilderTest
16+
{
17+
private TestDiagnosticAnalyzerRunner Runner { get; } = new(new WebApplicationBuilderAnalyzer());
18+
/**
19+
* Verify that the correct code produces no diagnostic
20+
*/
21+
[Fact]
22+
public async Task ConfigurationBuilderRunsWithoutDiagnostic()
23+
{
24+
// Arrange
25+
var source = @"
26+
using Microsoft.AspNetCore.Builder;
27+
using Microsoft.Extensions.Hosting;
28+
var builder = WebApplication.CreateBuilder(args);
29+
builder.Configuration.AddJSonFile(fileName, optional: true);
30+
";
31+
// Act
32+
var diagnostic = await Runner.GetDiagnosticsAsync(source);
33+
34+
// Assert
35+
Assert.Empty(diagnostic);
36+
}
37+
/**
38+
* Verify the fixed code and the diagnostic for builder.Host.ConfigureAppConfiguration
39+
*/
40+
[Fact]
41+
public async Task ConfigureAppHostBuilderProducesDiagnostic()
42+
{
43+
// Arrange
44+
var source = @"
45+
using Microsoft.AspNetCore.Builder;
46+
using Microsoft.Extensions.Hosting;
47+
var builder = WebApplication.CreateBuilder(args);
48+
builder.Host.ConfigureAppConfiguration(builder =>
49+
{
50+
builder.AddJsonFile(fileName, optional: true);
51+
});
52+
";
53+
var fixedSource = @"
54+
using Microsoft.AspNetCore.Builder;
55+
using Microsoft.Extensions.Hosting;
56+
var builder = WebApplication.CreateBuilder(args);
57+
builder.Configuration.AddJsonFile(fileName, optional: true);
58+
";
59+
// Act
60+
var expectedDiagnostic = new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder);
61+
62+
// Assert
63+
await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnostic, fixedSource);
64+
}
65+
66+
/**
67+
* Verify the fixed code and the diagnostic for builder.Host.ConfigureHostConfiguration
68+
*/
69+
[Fact]
70+
public async Task ConfigureHostHostBuilderProducesDiagnostic()
71+
{
72+
// Arrange
73+
var source = @"
74+
using Microsoft.AspNetCore.Builder;
75+
using Microsoft.Extensions.Hosting;
76+
var builder = WebApplication.CreateBuilder(args);
77+
builder.Host.ConfigureHostConfiguration(builder =>
78+
{
79+
builder.AddJsonFile(fileName, optional: true);
80+
});
81+
";
82+
var fixedSource = @"
83+
using Microsoft.AspNetCore.Builder;
84+
using Microsoft.Extensions.Hosting;
85+
var builder = WebApplication.CreateBuilder(args);
86+
builder.Configuration.AddJsonFile(fileName, optional: true);
87+
";
88+
// Act
89+
var expectedDiagnostic = new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder);
90+
91+
// Assert
92+
await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnostic, fixedSource);
93+
}
94+
95+
/**
96+
* Verify the fixed code and the diagnostic for builder.WebHost.ConfigureAppConfiguration
97+
*/
98+
[Fact]
99+
public async Task ConfigureAppWebHostBuilderProducesDiagnostic()
100+
{
101+
// Arrange
102+
var source = @"
103+
using Microsoft.AspNetCore.Builder;
104+
using Microsoft.Extensions.Hosting;
105+
using Microsoft.AspNetCore.Hosting;
106+
using Microsoft.Extensions.DependencyInjection;
107+
var builder = WebApplication.CreateBuilder(args);
108+
builder.WebHost.ConfigureAppConfiguration(builder =>
109+
{
110+
builder.AddJsonFile(fileName, optional: true);
111+
});
112+
";
113+
var fixedSource = @"
114+
using Microsoft.AspNetCore.Builder;
115+
using Microsoft.Extensions.Hosting;
116+
using Microsoft.AspNetCore.Hosting;
117+
using Microsoft.Extensions.DependencyInjection;
118+
var builder = WebApplication.CreateBuilder(args);
119+
builder.Configuration.AddJsonFile(fileName, optional: true);
120+
";
121+
// Act
122+
var expectedDiagnostic = new DiagnosticResult(DiagnosticDescriptors.DisallowConfigureAppConfigureHostBuilder);
123+
124+
// Assert
125+
await VerifyCS.VerifyCodeFixAsync(source, expectedDiagnostic, fixedSource);
126+
}
127+
}

0 commit comments

Comments
 (0)