Skip to content

Commit a065d3d

Browse files
committed
Add tests for MissingAttributeInspection
1 parent 3d2e6a7 commit a065d3d

File tree

1 file changed

+358
-0
lines changed

1 file changed

+358
-0
lines changed
Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading;
4+
using NUnit.Framework;
5+
using Rubberduck.Inspections.Concrete;
6+
using Rubberduck.Parsing.Inspections.Abstract;
7+
using RubberduckTests.Mocks;
8+
9+
namespace RubberduckTests.Inspections
10+
{
11+
[TestFixture]
12+
public class MissingAttributeInspectionTests
13+
{
14+
[Test]
15+
[Category("Inspections")]
16+
public void NoAnnotation_NoResult()
17+
{
18+
const string inputCode =
19+
@"Public Sub Foo()
20+
Const const1 As Integer = 9
21+
End Sub";
22+
23+
var inspectionResults = InspectionResults(inputCode);
24+
Assert.IsFalse(inspectionResults.Any());
25+
}
26+
27+
[Test]
28+
[Category("Inspections")]
29+
public void ModuleAttributeAnnotationWithoutAttributeReturnsResult()
30+
{
31+
const string inputCode =
32+
@"'@ModuleAttribute VB_Description, ""Desc""
33+
Public Sub Foo()
34+
Const const1 As Integer = 9
35+
End Sub";
36+
37+
var inspectionResults = InspectionResults(inputCode);
38+
Assert.AreEqual(1, inspectionResults.Count());
39+
}
40+
41+
[Test]
42+
[Category("Inspections")]
43+
public void ModuleAttributeAnnotationWithAttributeReturnsNoResult()
44+
{
45+
const string inputCode =
46+
@"Attribute VB_Description = ""NotDesc""
47+
'@ModuleAttribute VB_Description, ""Desc""
48+
Public Sub Foo()
49+
Const const1 As Integer = 9
50+
End Sub";
51+
52+
var inspectionResults = InspectionResults(inputCode);
53+
Assert.IsFalse(inspectionResults.Any());
54+
}
55+
56+
[Test]
57+
[Category("Inspections")]
58+
public void VbExtKeyModuleAttributeAnnotationWithAttributeButWithoutKeyReturnsResult()
59+
{
60+
const string inputCode =
61+
@"Attribute VB_Ext_Key = ""OtherKey"", ""Value""
62+
'@ModuleAttribute VB_Ext_Key, ""Key"", ""Value""
63+
Public Sub Foo()
64+
Const const1 As Integer = 9
65+
End Sub";
66+
67+
var inspectionResults = InspectionResults(inputCode);
68+
Assert.AreEqual(1, inspectionResults.Count());
69+
}
70+
71+
[Test]
72+
[Category("Inspections")]
73+
public void VbExtKeyModuleAttributeAnnotationWithAttributeAndKeyReturnsNoResult()
74+
{
75+
const string inputCode =
76+
@"Attribute VB_Ext_Key = ""Key"", ""OtherValue""
77+
'@ModuleAttribute VB_Ext_Key, ""Key"", ""Value""
78+
Public Sub Foo()
79+
Const const1 As Integer = 9
80+
End Sub";
81+
82+
var inspectionResults = InspectionResults(inputCode);
83+
Assert.IsFalse(inspectionResults.Any());
84+
}
85+
86+
[Test]
87+
[Category("Inspections")]
88+
public void ModuleAttributeAnnotationWithMissingArgumentsNoResult()
89+
{
90+
const string inputCode =
91+
@"'@ModuleAttribute
92+
Public Sub Foo()
93+
Const const1 As Integer = 9
94+
End Sub";
95+
96+
var inspectionResults = InspectionResults(inputCode);
97+
Assert.IsFalse(inspectionResults.Any());
98+
}
99+
100+
[Test]
101+
[Category("Inspections")]
102+
public void MemberAttributeAnnotationWithoutAttributeReturnsResult()
103+
{
104+
const string inputCode =
105+
@"'@MemberAttribute VB_Description, ""Desc""
106+
Public Sub Foo()
107+
Const const1 As Integer = 9
108+
End Sub";
109+
110+
var inspectionResults = InspectionResults(inputCode);
111+
Assert.AreEqual(1, inspectionResults.Count());
112+
}
113+
114+
[Test]
115+
[Category("Inspections")]
116+
public void MemberAttributeAnnotationWithAttributeReturnsNoResult()
117+
{
118+
const string inputCode =
119+
@"'@MemberAttribute VB_Description, ""Desc""
120+
Public Sub Foo()
121+
Attribute Foo.VB_Description = ""NotDesc""
122+
Const const1 As Integer = 9
123+
End Sub";
124+
125+
var inspectionResults = InspectionResults(inputCode);
126+
Assert.IsFalse(inspectionResults.Any());
127+
}
128+
129+
[Test]
130+
[Category("Inspections")]
131+
public void VbExtKeyMemberAttributeAnnotationWithAttributeButWithoutKeyReturnsResult()
132+
{
133+
const string inputCode =
134+
@"'@MemberAttribute VB_Ext_Key, ""Key"", ""Value""
135+
Public Sub Foo()
136+
Attribute Foo.VB_Ext_Key = ""OtherKey"", ""Value""
137+
Const const1 As Integer = 9
138+
End Sub";
139+
140+
var inspectionResults = InspectionResults(inputCode);
141+
Assert.AreEqual(1, inspectionResults.Count());
142+
}
143+
144+
[Test]
145+
[Category("Inspections")]
146+
public void VbExtKeyMemberAttributeAnnotationWithAttributeAndKeyReturnsNoResult()
147+
{
148+
const string inputCode =
149+
@"'@MemberAttribute VB_Ext_Key, ""Key"", ""Value""
150+
Public Sub Foo()
151+
Attribute Foo.VB_Ext_Key = ""Key"", ""OtherValue""
152+
Const const1 As Integer = 9
153+
End Sub";
154+
155+
var inspectionResults = InspectionResults(inputCode);
156+
Assert.IsFalse(inspectionResults.Any());
157+
}
158+
159+
[Test]
160+
[Category("Inspections")]
161+
public void MemberAttributeAnnotationWithMissingArgumentsNoResult()
162+
{
163+
const string inputCode =
164+
@"'@MemberAttribute
165+
Public Sub Foo()
166+
Const const1 As Integer = 9
167+
End Sub";
168+
169+
var inspectionResults = InspectionResults(inputCode);
170+
Assert.IsFalse(inspectionResults.Any());
171+
}
172+
173+
[Test]
174+
[Category("Inspections")]
175+
public void DefaultMemberAnnotationWithoutAttributeReturnsResult()
176+
{
177+
const string inputCode =
178+
@"'@DefaultMember
179+
Public Sub Foo()
180+
Const const1 As Integer = 9
181+
End Sub";
182+
183+
var inspectionResults = InspectionResults(inputCode);
184+
Assert.AreEqual(1, inspectionResults.Count());
185+
}
186+
187+
[Test]
188+
[Category("Inspections")]
189+
public void DefaultMemberAnnotationWithAttributeReturnsNoResult()
190+
{
191+
const string inputCode =
192+
@"'@DefaultMember
193+
Public Sub Foo()
194+
Attribute Foo.VB_UserMemId = 42
195+
Const const1 As Integer = 9
196+
End Sub";
197+
198+
var inspectionResults = InspectionResults(inputCode);
199+
Assert.IsFalse(inspectionResults.Any());
200+
}
201+
202+
[Test]
203+
[Category("Inspections")]
204+
public void EnumeratorAnnotationWithoutAttributeReturnsResult()
205+
{
206+
const string inputCode =
207+
@"'@Enumerator
208+
Public Sub Foo()
209+
Const const1 As Integer = 9
210+
End Sub";
211+
212+
var inspectionResults = InspectionResults(inputCode);
213+
Assert.AreEqual(1, inspectionResults.Count());
214+
}
215+
216+
[Test]
217+
[Category("Inspections")]
218+
public void EnumeratorAnnotationWithAttributeReturnsNoResult()
219+
{
220+
const string inputCode =
221+
@"'@Enumerator
222+
Public Sub Foo()
223+
Attribute Foo.VB_UserMemId = 42
224+
Const const1 As Integer = 9
225+
End Sub";
226+
227+
var inspectionResults = InspectionResults(inputCode);
228+
Assert.IsFalse(inspectionResults.Any());
229+
}
230+
231+
[Test]
232+
[Category("Inspections")]
233+
public void DescriptionAnnotationWithoutAttributeReturnsResult()
234+
{
235+
const string inputCode =
236+
@"'@Description ""Desc""
237+
Public Sub Foo()
238+
Const const1 As Integer = 9
239+
End Sub";
240+
241+
var inspectionResults = InspectionResults(inputCode);
242+
Assert.AreEqual(1, inspectionResults.Count());
243+
}
244+
245+
[Test]
246+
[Category("Inspections")]
247+
public void DescriptionAnnotationWithAttributeReturnsNoResult()
248+
{
249+
const string inputCode =
250+
@"'@Description, ""Desc""
251+
Public Sub Foo()
252+
Attribute Foo.VB_Description = ""NotDesc""
253+
Const const1 As Integer = 9
254+
End Sub";
255+
256+
var inspectionResults = InspectionResults(inputCode);
257+
Assert.IsFalse(inspectionResults.Any());
258+
}
259+
260+
[Test]
261+
[Category("Inspections")]
262+
public void ModuleDescriptionAnnotationWithoutAttributeReturnsResult()
263+
{
264+
const string inputCode =
265+
@"'@ModuleDescription ""Desc""
266+
Public Sub Foo()
267+
Const const1 As Integer = 9
268+
End Sub";
269+
270+
var inspectionResults = InspectionResults(inputCode);
271+
Assert.AreEqual(1, inspectionResults.Count());
272+
}
273+
274+
[Test]
275+
[Category("Inspections")]
276+
public void ModuleDescriptionAnnotationWithAttributeReturnsNoResult()
277+
{
278+
const string inputCode =
279+
@"Attribute VB_Description = ""NotDesc""
280+
'@ModuleDescription ""Desc""
281+
Public Sub Foo()
282+
Const const1 As Integer = 9
283+
End Sub";
284+
285+
var inspectionResults = InspectionResults(inputCode);
286+
Assert.IsFalse(inspectionResults.Any());
287+
}
288+
289+
[Test]
290+
[Category("Inspections")]
291+
public void PredeclaredIdAnnotationWithoutAttributeReturnsResult()
292+
{
293+
const string inputCode =
294+
@"'@PredeclaredId
295+
Public Sub Foo()
296+
Const const1 As Integer = 9
297+
End Sub";
298+
299+
var inspectionResults = InspectionResults(inputCode);
300+
Assert.AreEqual(1, inspectionResults.Count());
301+
}
302+
303+
[Test]
304+
[Category("Inspections")]
305+
public void PredeclaredIdAnnotationWithAttributeReturnsNoResult()
306+
{
307+
const string inputCode =
308+
@"Attribute VB_PredeclaredId = False
309+
'@PredeclaredId
310+
Public Sub Foo()
311+
Const const1 As Integer = 9
312+
End Sub";
313+
314+
var inspectionResults = InspectionResults(inputCode);
315+
Assert.IsFalse(inspectionResults.Any());
316+
}
317+
318+
[Test]
319+
[Category("Inspections")]
320+
public void ExposedAnnotationWithoutAttributeReturnsResult()
321+
{
322+
const string inputCode =
323+
@"'@Exposed
324+
Public Sub Foo()
325+
Const const1 As Integer = 9
326+
End Sub";
327+
328+
var inspectionResults = InspectionResults(inputCode);
329+
Assert.AreEqual(1, inspectionResults.Count());
330+
}
331+
332+
[Test]
333+
[Category("Inspections")]
334+
public void ExposedAnnotationWithAttributeReturnsNoResult()
335+
{
336+
const string inputCode =
337+
@"Attribute VB_Exposed = False
338+
'@Exposed
339+
Public Sub Foo()
340+
Const const1 As Integer = 9
341+
End Sub";
342+
343+
var inspectionResults = InspectionResults(inputCode);
344+
Assert.IsFalse(inspectionResults.Any());
345+
}
346+
347+
348+
private IEnumerable<IInspectionResult> InspectionResults(string inputCode)
349+
{
350+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);
351+
using (var state = MockParser.CreateAndParse(vbe.Object))
352+
{
353+
var inspection = new MissingAttributeInspection(state);
354+
return inspection.GetInspectionResults(CancellationToken.None);
355+
}
356+
}
357+
}
358+
}

0 commit comments

Comments
 (0)