-
Notifications
You must be signed in to change notification settings - Fork 222
/
RemoveObsoleteCodeTest.cs
150 lines (136 loc) · 5.71 KB
/
RemoveObsoleteCodeTest.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
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
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using CS = SonarAnalyzer.Rules.CSharp;
using VB = SonarAnalyzer.Rules.VisualBasic;
namespace SonarAnalyzer.UnitTest.Rules;
[TestClass]
public class RemoveObsoleteCodeTest
{
private readonly VerifierBuilder builderCS = new VerifierBuilder<CS.RemoveObsoleteCode>();
private readonly VerifierBuilder builderVB = new VerifierBuilder<VB.RemoveObsoleteCode>();
[TestMethod]
public void RemoveObsoleteCode_CS() =>
builderCS.AddPaths("RemoveObsoleteCode.cs").Verify();
[TestMethod]
public void RemoveObsoleteCode_CSharp10() =>
builderCS.AddPaths("RemoveObsoleteCode.CSharp10.cs")
.WithTopLevelStatements()
.WithOptions(ParseOptionsHelper.FromCSharp10)
.Verify();
[TestMethod]
public void RemoveObsoleteCode_VB() =>
builderVB.AddPaths("RemoveObsoleteCode.vb").Verify();
[DataTestMethod]
// All attribute targets of [Obsolete]
[DataRow("bool field;")] // AttributeTargets.Field
[DataRow("event EventHandler SomeEvent;")] // AttributeTargets.Event
[DataRow("bool Prop { get; set; }")] // AttributeTargets.Property
[DataRow("void Method() { }")] // AttributeTargets.Method
[DataRow("class C { }")] // AttributeTargets.Class
[DataRow("struct S { }")] // AttributeTargets.Struct
[DataRow("interface I { }")] // AttributeTargets.Interface
[DataRow("enum E { A }")] // AttributeTargets.Enum
[DataRow("public Test() { }")] // AttributeTargets.Constructor
[DataRow("delegate void Del();")] // AttributeTargets.Delegate
[DataRow("int this[int i] => 1;")] // Indexer
public void RemoveObsoleteCode_AttributeTargetTest_CS(string attributeTargetDeclaration)
{
builderCS.AddSnippet(WrapInTestCode(string.Empty)).VerifyNoIssueReported();
builderCS.AddSnippet(WrapInTestCode("[Obsolete] // Noncompliant")).Verify();
builderCS.AddSnippet(WrapInTestCode("[Custom]")).VerifyNoIssueReported();
builderCS.AddSnippet(WrapInTestCode("""
[Obsolete] // Noncompliant
[Custom]
""")).Verify();
string WrapInTestCode(string attribute) =>
$$"""
using System;
[AttributeUsage(AttributeTargets.All)]
public sealed class CustomAttribute: Attribute
{
}
public class Test
{
{{attribute}}
{{attributeTargetDeclaration}}
}
""";
}
[DataTestMethod]
// All attribute targets of [Obsolete]
[DataRow("Private field As Boolean")] // AttributeTargets.Field
[DataRow("Event SomeEvent As EventHandler")] // AttributeTargets.Event
[DataRow("Property Prop As Boolean")] // AttributeTargets.Property
[DataRow("""
Private Sub Method()
End Sub
""")] // AttributeTargets.Method
[DataRow("""
Class C
End Class
""")] // AttributeTargets.Class
[DataRow("""
Structure S
End Structure
""")] // AttributeTargets.Struct
[DataRow("""
Interface I
End Interface
""")] // AttributeTargets.Interface
[DataRow("""
Enum E
A
End Enum
""")] // AttributeTargets.Enum
[DataRow("""
Public Sub New()
End Sub
""")] // AttributeTargets.Constructor
[DataRow("Delegate Sub Del()")] // AttributeTargets.Delegate
[DataRow("""
Default ReadOnly Property Item(ByVal i As Integer) As Integer
Get
Return 1
End Get
End Property
""")] // Indexer
public void RemoveObsoleteCode_AttributeTargetTest_VB(string attributeTargetDeclaration)
{
builderVB.AddSnippet(WrapInTestCode(string.Empty)).VerifyNoIssueReported();
builderVB.AddSnippet(WrapInTestCode("<Obsolete> ' Noncompliant")).Verify();
builderVB.AddSnippet(WrapInTestCode("<Custom>")).VerifyNoIssueReported();
builderVB.AddSnippet(WrapInTestCode("""
<Obsolete> ' Noncompliant
<Custom>
""")).Verify();
string WrapInTestCode(string attribute) =>
$$"""
Imports System
<AttributeUsage(AttributeTargets.All)>
Public NotInheritable Class CustomAttribute
Inherits Attribute
End Class
Public Class Test
{{attribute}}
{{attributeTargetDeclaration}}
End Class
""";
}
}