This repository was archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathApiActionsDoNotRequireExplicitModelValidationCheckCodeFixProviderTest.cs
62 lines (51 loc) · 2.25 KB
/
ApiActionsDoNotRequireExplicitModelValidationCheckCodeFixProviderTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Analyzer.Testing;
using Microsoft.CodeAnalysis;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Api.Analyzers
{
public class ApiActionsDoNotRequireExplicitModelValidationCheckCodeFixProviderTest
{
private MvcDiagnosticAnalyzerRunner AnalyzerRunner { get; } = new MvcDiagnosticAnalyzerRunner(new ApiActionsDoNotRequireExplicitModelValidationCheckAnalyzer());
private CodeFixRunner CodeFixRunner => CodeFixRunner.Default;
[Fact]
public Task CodeFixRemovesModelStateIsInvalidBlockWithIfNotCheck()
=> RunTest();
[Fact]
public Task CodeFixRemovesModelStateIsInvalidBlockWithEqualityCheck()
=> RunTest();
[Fact]
public Task CodeFixRemovesIfBlockWithoutBraces()
=> RunTest();
private async Task RunTest([CallerMemberName] string testMethod = "")
{
// Arrange
var project = GetProject(testMethod);
var controllerDocument = project.DocumentIds[0];
var expectedOutput = Read(testMethod + ".Output");
// Act
var diagnostics = await AnalyzerRunner.GetDiagnosticsAsync(project);
Assert.NotEmpty(diagnostics);
var actualOutput = await CodeFixRunner.ApplyCodeFixAsync(
new ApiActionsDoNotRequireExplicitModelValidationCheckCodeFixProvider(),
project.GetDocument(controllerDocument),
diagnostics[0]);
Assert.Equal(expectedOutput, actualOutput, ignoreLineEndingDifferences: true);
}
private Project GetProject(string testMethod)
{
var testSource = Read(testMethod + ".Input");
return DiagnosticProject.Create(GetType().Assembly, new[] { testSource });
}
private string Read(string fileName)
{
return MvcTestSource.Read(GetType().Name, fileName)
.Source
.Replace("_INPUT_", "_TEST_")
.Replace("_OUTPUT_", "_TEST_");
}
}
}