Skip to content

Commit 4f8df3e

Browse files
committed
Add ImplicitlyTypedConstInspection and tests
1 parent 52afae2 commit 4f8df3e

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Rubberduck.Inspections.Abstract;
4+
using Rubberduck.Parsing.VBA;
5+
using Rubberduck.Parsing.Inspections.Abstract;
6+
using Rubberduck.Inspections.Results;
7+
using Rubberduck.Parsing.Symbols;
8+
9+
namespace Rubberduck.CodeAnalysis.Inspections.Concrete
10+
{
11+
public sealed class ImplicitlyTypedConstInspection : InspectionBase
12+
{
13+
public ImplicitlyTypedConstInspection(RubberduckParserState state)
14+
: base(state) { }
15+
16+
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
17+
{
18+
var declarationFinder = State.DeclarationFinder;
19+
20+
var implicitlyTypedConsts = declarationFinder.AllDeclarations
21+
.Where(declaration => declaration.DeclarationType == DeclarationType.Constant
22+
&& declaration.AsTypeContext == null);
23+
24+
return implicitlyTypedConsts.Select(Result);
25+
}
26+
27+
private IInspectionResult Result(Declaration declaration)
28+
{
29+
return new IdentifierReferenceInspectionResult(
30+
this,
31+
declaration.DescriptionString,
32+
State,
33+
new IdentifierReference(
34+
declaration.QualifiedModuleName,
35+
declaration.ParentScopeDeclaration,
36+
declaration.ParentDeclaration,
37+
declaration.IdentifierName,
38+
declaration.Selection,
39+
declaration.Context,
40+
declaration));
41+
}
42+
}
43+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Linq;
2+
using NUnit.Framework;
3+
using Rubberduck.CodeAnalysis.Inspections.Concrete;
4+
using Rubberduck.Parsing.Inspections.Abstract;
5+
using Rubberduck.Parsing.VBA;
6+
using Rubberduck.VBEditor.SafeComWrappers;
7+
8+
namespace RubberduckTests.Inspections
9+
{
10+
[TestFixture]
11+
public class ImplicitlyTypedConstInspectionTests : InspectionTestsBase
12+
{
13+
[Test]
14+
[Category("Inspections")]
15+
public void InspectionName()
16+
{
17+
var inspection = new ImplicitlyTypedConstInspection(null);
18+
19+
var expected = nameof(ImplicitlyTypedConstInspection);
20+
var actual = inspection.Name;
21+
Assert.AreEqual(expected, actual);
22+
}
23+
24+
[Test]
25+
[Category("Inspections")]
26+
public void ImplicitlyTypedConst_ReturnsResult()
27+
{
28+
const string inputCode =
29+
@"Public Sub Foo()
30+
Const bar = 0
31+
End Sub";
32+
33+
const int expected = 1;
34+
35+
var results = InspectionResultsForModules(("FooClass", inputCode, ComponentType.ClassModule));
36+
var actual = results.Count();
37+
Assert.AreEqual(expected , actual);
38+
}
39+
40+
[Test]
41+
[Category("Inspections")]
42+
public void ImplicitlyTypedConst_DoesNotReturnResult()
43+
{
44+
const string inputCode =
45+
@"Public Sub Foo()
46+
Const bar As Long = 0
47+
End Sub";
48+
49+
const int expected = 0;
50+
51+
var results = InspectionResultsForModules(("FooClass", inputCode, ComponentType.ClassModule));
52+
var actual = results.Count();
53+
Assert.AreEqual(expected, actual);
54+
}
55+
56+
protected override IInspection InspectionUnderTest(RubberduckParserState state)
57+
{
58+
return new ImplicitlyTypedConstInspection(state);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)