forked from FritzAndFriends/BlazorWebFormsComponents
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathViewStateUsageAnalyzerTests.cs
More file actions
165 lines (140 loc) · 4.12 KB
/
ViewStateUsageAnalyzerTests.cs
File metadata and controls
165 lines (140 loc) · 4.12 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
namespace BlazorWebFormsComponents.Analyzers.Test;
using AnalyzerTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerTest<
ViewStateUsageAnalyzer,
DefaultVerifier>;
using CodeFixTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpCodeFixTest<
ViewStateUsageAnalyzer,
ViewStateUsageCodeFixProvider,
DefaultVerifier>;
/// <summary>
/// Tests for BWFC002: ViewState usage detected.
/// </summary>
public class ViewStateUsageAnalyzerTests
{
private const string StubSource = @"
using System.Collections.Generic;
public class PageBase
{
public Dictionary<string, object> ViewState { get; } = new Dictionary<string, object>();
}
";
private static DiagnosticResult ExpectBWFC002(string memberName) =>
new DiagnosticResult(ViewStateUsageAnalyzer.DiagnosticId, DiagnosticSeverity.Info)
.WithArguments(memberName);
#region Positive cases — BWFC002 SHOULD fire
[Fact]
public async Task ViewStateAccess_InMethodBody_ReportsDiagnostic()
{
var source = @"
public class MyPage : PageBase
{
public void Page_Load()
{
{|#0:ViewState[""key""]|} = ""value"";
}
}";
var test = new AnalyzerTest
{
TestState = { Sources = { source, StubSource } },
ExpectedDiagnostics = { ExpectBWFC002("Page_Load").WithLocation(0) }
};
await test.RunAsync();
}
[Fact]
public async Task ThisViewStateAccess_ReportsDiagnostic()
{
var source = @"
public class MyPage : PageBase
{
public void Page_Load()
{
{|#0:this.ViewState[""x""]|} = ""value"";
}
}";
var test = new AnalyzerTest
{
TestState = { Sources = { source, StubSource } },
ExpectedDiagnostics = { ExpectBWFC002("Page_Load").WithLocation(0) }
};
await test.RunAsync();
}
#endregion
#region Negative cases — BWFC002 should NOT fire
[Fact]
public async Task LowercaseViewState_NoDiagnostic()
{
var source = @"
using System.Collections.Generic;
public class MyPage
{
public void DoWork()
{
var viewState = new Dictionary<string, object>();
viewState[""key""] = ""value"";
}
}";
var test = new AnalyzerTest
{
TestState = { Sources = { source, StubSource } }
};
await test.RunAsync();
}
[Fact]
public async Task DictionaryNamedViewState_InNonPageClass_NoDiagnostic()
{
// A plain class with a Dictionary named ViewState should not fire
// because there is no ElementAccessExpression on a member called "ViewState"
// inherited from PageBase — it's a local variable
var source = @"
using System.Collections.Generic;
public class PlainHelper
{
public void DoWork()
{
var ViewState = new Dictionary<string, object>();
ViewState[""key""] = ""value"";
}
}";
// This WILL fire because we detect the syntactic pattern ViewState["key"]
// regardless of class hierarchy — ViewState is a Web Forms-specific name.
var test = new AnalyzerTest
{
TestState = { Sources = { source, StubSource } },
ExpectedDiagnostics = { ExpectBWFC002("DoWork").WithLocation(9, 9) }
};
await test.RunAsync();
}
#endregion
#region Code fix tests
[Fact]
public async Task CodeFix_CommentsOutViewStateUsage()
{
var testSource = @"
public class MyPage : PageBase
{
public void Page_Load()
{
{|#0:ViewState[""key""]|} = ""value"";
}
}";
var fixedSource = @"
public class MyPage : PageBase
{
public void Page_Load()
{
// TODO: Replace ViewState[""key""] with component state
// ViewState[""key""] = ""value"";
}
}";
var test = new CodeFixTest
{
TestState = { Sources = { testSource, StubSource } },
FixedState = { Sources = { fixedSource, StubSource } },
ExpectedDiagnostics = { ExpectBWFC002("Page_Load").WithLocation(0) }
};
await test.RunAsync();
}
#endregion
}