-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMissingParamLessConstructorCodeFixProviderTests.cs
More file actions
106 lines (93 loc) · 2.82 KB
/
MissingParamLessConstructorCodeFixProviderTests.cs
File metadata and controls
106 lines (93 loc) · 2.82 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using VerifyXunit;
using Xunit;
namespace EntityFrameworkCore.Projectables.CodeFixes.Tests;
/// <summary>
/// Tests for <see cref="MissingParameterlessConstructorCodeFixProvider"/> (EFP0008).
/// The fix inserts a <c>public ClassName() { }</c> constructor at the top of the class body.
/// </summary>
public class MissingParamLessConstructorCodeFixProviderTests : CodeFixTestBase
{
private readonly static MissingParameterlessConstructorCodeFixProvider _provider = new();
// Locates the first constructor identifier — the code fix walks up to TypeDeclarationSyntax.
private static TextSpan FirstConstructorIdentifierSpan(SyntaxNode root) =>
root.DescendantNodes()
.OfType<ConstructorDeclarationSyntax>()
.First()
.Identifier
.Span;
[Fact]
public void FixableDiagnosticIds_ContainsEFP0008() =>
Assert.Contains("EFP0008", _provider.FixableDiagnosticIds);
[Fact]
public async Task RegistersCodeFix_WithTitleContainingClassName()
{
var actions = await GetCodeFixActionsAsync(
@"
namespace Foo {
class MyClass {
[Projectable]
public MyClass(int value) { }
}
}",
"EFP0008",
FirstConstructorIdentifierSpan,
_provider);
var action = Assert.Single(actions);
Assert.Contains("MyClass", action.Title, StringComparison.Ordinal);
}
[Fact]
public Task AddParamLessConstructor_ToClassWithSingleParamConstructor() =>
Verifier.Verify(
ApplyCodeFixAsync(
@"
namespace Foo {
class Person {
[Projectable]
public Person(string name)
{
Name = name;
}
public string Name { get; set; }
}
}",
"EFP0008",
FirstConstructorIdentifierSpan,
_provider));
[Fact]
public Task AddParamLessConstructor_IsInsertedBeforeExistingMembers() =>
Verifier.Verify(
ApplyCodeFixAsync(
@"
namespace Foo {
class Person {
public string Name { get; set; }
public int Age { get; set; }
[Projectable]
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
}",
"EFP0008",
FirstConstructorIdentifierSpan,
_provider));
[Fact]
public Task AddParamLessConstructor_ToClassWithNoOtherMembers() =>
Verifier.Verify(
ApplyCodeFixAsync(
@"
namespace Foo {
class Empty {
[Projectable]
public Empty(int value) { }
}
}",
"EFP0008",
FirstConstructorIdentifierSpan,
_provider));
}