forked from FritzAndFriends/BlazorWebFormsComponents
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIsPostBackUsageAnalyzerTests.cs
More file actions
143 lines (123 loc) · 3.2 KB
/
IsPostBackUsageAnalyzerTests.cs
File metadata and controls
143 lines (123 loc) · 3.2 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
namespace BlazorWebFormsComponents.Analyzers.Test;
using AnalyzerTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerTest<
IsPostBackUsageAnalyzer,
DefaultVerifier>;
using CodeFixTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixTest<
IsPostBackUsageAnalyzer,
IsPostBackUsageCodeFixProvider,
DefaultVerifier>;
/// <summary>
/// Tests for BWFC003: IsPostBack usage detected.
/// </summary>
public class IsPostBackUsageAnalyzerTests
{
private const string StubSource = @"
public class PageBase
{
public bool IsPostBack { get; set; }
public PageBase Page => this;
protected void DoInit() { }
}
";
private static DiagnosticResult ExpectBWFC003(string memberName) =>
new DiagnosticResult(IsPostBackUsageAnalyzer.DiagnosticId, DiagnosticSeverity.Info)
.WithArguments(memberName);
#region Positive cases — BWFC003 SHOULD fire
[Fact]
public async Task IsPostBack_InIfCondition_ReportsDiagnostic()
{
var source = @"
public class MyPage : PageBase
{
public void Page_Load()
{
if (!{|#0:IsPostBack|})
{
DoInit();
}
}
}";
var test = new AnalyzerTest
{
TestState = { Sources = { source, StubSource } },
ExpectedDiagnostics = { ExpectBWFC003("Page_Load").WithLocation(0) }
};
await test.RunAsync();
}
[Fact]
public async Task PageIsPostBack_InIfCondition_ReportsDiagnostic()
{
var source = @"
public class MyPage : PageBase
{
public void Page_Load()
{
if (!{|#0:Page.IsPostBack|})
{
DoInit();
}
}
}";
var test = new AnalyzerTest
{
TestState = { Sources = { source, StubSource } },
ExpectedDiagnostics = { ExpectBWFC003("Page_Load").WithLocation(0) }
};
await test.RunAsync();
}
#endregion
#region Negative cases — BWFC003 should NOT fire
[Fact]
public async Task LowercaseIsPostBack_NoDiagnostic()
{
var source = @"
public class MyPage
{
public void DoWork()
{
var isPostBack = false;
if (!isPostBack)
{
}
}
}";
var test = new AnalyzerTest
{
TestState = { Sources = { source, StubSource } }
};
await test.RunAsync();
}
#endregion
#region Code fix tests
[Fact]
public async Task CodeFix_CommentsOutIsPostBackCheck()
{
var testSource = @"
public class MyPage : PageBase
{
public void Page_Load()
{
var check = {|#0:IsPostBack|};
}
}";
var fixedSource = @"
public class MyPage : PageBase
{
public void Page_Load()
{
// TODO: Replace IsPostBack check with Blazor lifecycle (OnInitialized/OnParametersSet)
// var check = IsPostBack;
}
}";
var test = new CodeFixTest
{
TestState = { Sources = { testSource, StubSource } },
FixedState = { Sources = { fixedSource, StubSource } },
ExpectedDiagnostics = { ExpectBWFC003("Page_Load").WithLocation(0) }
};
await test.RunAsync();
}
#endregion
}