Skip to content

Commit b013f65

Browse files
committed
Fixed the expectation of the three failing tests and fixed the inspection to make two of them pass.
1 parent 5ed7ffc commit b013f65

File tree

2 files changed

+118
-13
lines changed

2 files changed

+118
-13
lines changed

Rubberduck.Inspections/Concrete/ShadowedDeclarationInspection.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,31 @@ private static bool DeclarationInAnotherComponentCanBeShadowed(Declaration origi
201201

202202
var originalDeclarationComponentType = originalDeclaration.QualifiedName.QualifiedModuleName.ComponentType;
203203

204-
// It is not possible to directly access a Parameter, UDT Member or Label declared in another component
204+
// It is not possible to directly access a Parameter, UDT Member or Label declared in another component.
205205
if (originalDeclaration.DeclarationType == DeclarationType.Parameter || originalDeclaration.DeclarationType == DeclarationType.UserDefinedTypeMember ||
206206
originalDeclaration.DeclarationType == DeclarationType.LineLabel)
207207
{
208208
return false;
209209
}
210210

211-
// It is not possible to directly access any declarations placed inside a Class Module
211+
// It is not possible to directly access any declarations placed inside a Class Module.
212212
if (originalDeclaration.DeclarationType != DeclarationType.ClassModule && originalDeclarationComponentType == ComponentType.ClassModule)
213213
{
214214
return false;
215215
}
216216

217+
// It is not possible to directly access any declarations placed inside a Document Module. (Document Modules have DeclarationType ClassMoodule.)
218+
if (originalDeclaration.DeclarationType != DeclarationType.ClassModule && originalDeclarationComponentType == ComponentType.Document)
219+
{
220+
return false;
221+
}
222+
223+
// It is not possible to directly access any declarations placed inside a User Form. (User Forms have DeclarationType ClassMoodule.)
224+
if (originalDeclaration.DeclarationType != DeclarationType.ClassModule && originalDeclarationComponentType == ComponentType.UserForm)
225+
{
226+
return false;
227+
}
228+
217229
if (originalDeclaration.DeclarationType == DeclarationType.ClassModule)
218230
{
219231
// Syntax of instantiating a new class makes it impossible to be shadowed
@@ -234,9 +246,15 @@ private static bool DeclarationInAnotherComponentCanBeShadowed(Declaration origi
234246
return false;
235247
}
236248
}
237-
else if (!OtherComponentTypeShadowingRelations[originalDeclaration.DeclarationType].Contains(userDeclaration.DeclarationType))
249+
else
238250
{
239-
return false;
251+
HashSet<DeclarationType> shadowedTypes;
252+
if (!OtherComponentTypeShadowingRelations.TryGetValue(originalDeclaration.DeclarationType,
253+
out shadowedTypes)
254+
|| !shadowedTypes.Contains(userDeclaration.DeclarationType))
255+
{
256+
return false;
257+
}
240258
}
241259

242260
// Events don't have a body, so their parameters can't be accessed

RubberduckTests/Inspections/ShadowedDeclarationInspectionTests.cs

Lines changed: 96 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4842,8 +4842,34 @@ public void ShadowedDeclaration_DoesNotReturnResult_DeclarationsInsideClassModul
48424842

48434843
[TestMethod]
48444844
[TestCategory("Inspections")]
4845-
public void ShadowedDeclaration_DoesNotReturnResult_DeclarationsInsideClassModuleInContainingProject()
4845+
public void ShadowedDeclaration_ReturnsCorrectResult_DeclarationsInsideClassModuleInContainingProject()
48464846
{
4847+
var expectedResultCountsByDeclarationIdentifierName = new Dictionary<string, int>
4848+
{
4849+
[ProjectName] = 0,
4850+
[ProceduralModuleName] = 0,
4851+
[ClassModuleName] = 0,
4852+
[UserFormName] = 0,
4853+
[DocumentName] = 0,
4854+
[ProcedureName] = 1,
4855+
[FunctionName] = 1,
4856+
[PropertyGetName] = 1,
4857+
[PropertySetName] = 1,
4858+
[PropertyLetName] = 1,
4859+
[ParameterName] = 0,
4860+
[VariableName] = 1,
4861+
[LocalVariableName] = 0,
4862+
[ConstantName] = 0,
4863+
[LocalConstantName] = 0,
4864+
[EnumerationName] = 1,
4865+
[EnumerationMemberName] = 1,
4866+
[UserDefinedTypeName] = 0,
4867+
[UserDefinedTypeMemberName] = 0,
4868+
[LibraryProcedureName] = 0,
4869+
[LibraryFunctionName] = 0,
4870+
[LineLabelName] = 0
4871+
};
4872+
48474873
var builder = TestVbeWithUserProjectWithAdditionalComponent(
48484874
additionalComponentName: "Foo",
48494875
additionalComponentComponentType: ComponentType.ClassModule,
@@ -4854,10 +4880,13 @@ public void ShadowedDeclaration_DoesNotReturnResult_DeclarationsInsideClassModul
48544880
using (var state = MockParser.CreateAndParse(vbe.Object))
48554881
{
48564882
var inspection = new ShadowedDeclarationInspection(state);
4857-
inspectionResults = inspection.GetInspectionResults();
4883+
inspectionResults = inspection.GetInspectionResults().ToList();
48584884
}
4885+
var inspectionResultCounts = InspectionResultCountsByTargetIdentifierName(inspectionResults);
48594886

4860-
Assert.IsFalse(inspectionResults.Any());
4887+
AssertResultCountsEqualForThoseWithExpectation(expectedResultCountsByDeclarationIdentifierName, inspectionResultCounts);
4888+
//All shadowing happens inside the class module.
4889+
Assert.IsTrue(inspectionResults.All(result => result.Target.QualifiedName.QualifiedModuleName.ComponentName == "Foo"));
48614890
}
48624891

48634892
[TestMethod]
@@ -4883,8 +4912,34 @@ public void ShadowedDeclaration_DoesNotReturnResult_DeclarationsInsideUserFormIn
48834912

48844913
[TestMethod]
48854914
[TestCategory("Inspections")]
4886-
public void ShadowedDeclaration_DoesNotReturnResult_DeclarationsInsideUserFormInContainingProject()
4915+
public void ShadowedDeclaration_ReturnsCorrectResult_DeclarationsInsideUserFormInContainingProject()
48874916
{
4917+
var expectedResultCountsByDeclarationIdentifierName = new Dictionary<string, int>
4918+
{
4919+
[ProjectName] = 0,
4920+
[ProceduralModuleName] = 0,
4921+
[ClassModuleName] = 0,
4922+
[UserFormName] = 0,
4923+
[DocumentName] = 0,
4924+
[ProcedureName] = 1,
4925+
[FunctionName] = 1,
4926+
[PropertyGetName] = 1,
4927+
[PropertySetName] = 1,
4928+
[PropertyLetName] = 1,
4929+
[ParameterName] = 0,
4930+
[VariableName] = 1,
4931+
[LocalVariableName] = 0,
4932+
[ConstantName] = 0,
4933+
[LocalConstantName] = 0,
4934+
[EnumerationName] = 1,
4935+
[EnumerationMemberName] = 1,
4936+
[UserDefinedTypeName] = 0,
4937+
[UserDefinedTypeMemberName] = 0,
4938+
[LibraryProcedureName] = 0,
4939+
[LibraryFunctionName] = 0,
4940+
[LineLabelName] = 0
4941+
};
4942+
48884943
var builder = TestVbeWithUserProjectWithAdditionalComponent(
48894944
additionalComponentName: "Foo",
48904945
additionalComponentComponentType: ComponentType.UserForm,
@@ -4895,10 +4950,13 @@ public void ShadowedDeclaration_DoesNotReturnResult_DeclarationsInsideUserFormIn
48954950
using (var state = MockParser.CreateAndParse(vbe.Object))
48964951
{
48974952
var inspection = new ShadowedDeclarationInspection(state);
4898-
inspectionResults = inspection.GetInspectionResults();
4953+
inspectionResults = inspection.GetInspectionResults().ToList();
48994954
}
4955+
var inspectionResultCounts = InspectionResultCountsByTargetIdentifierName(inspectionResults);
49004956

4901-
Assert.IsFalse(inspectionResults.Any());
4957+
AssertResultCountsEqualForThoseWithExpectation(expectedResultCountsByDeclarationIdentifierName, inspectionResultCounts);
4958+
//All shadowing happens inside the user form.
4959+
Assert.IsTrue(inspectionResults.All(result => result.Target.QualifiedName.QualifiedModuleName.ComponentName == "Foo"));
49024960
}
49034961

49044962
[TestMethod]
@@ -4924,8 +4982,34 @@ public void ShadowedDeclaration_DoesNotReturnResult_DeclarationsInsideDocumentIn
49244982

49254983
[TestMethod]
49264984
[TestCategory("Inspections")]
4927-
public void ShadowedDeclaration_DoesNotReturnResult_DeclarationsInsideDocumentInContainingProject()
4985+
public void ShadowedDeclaration_ReturnsCorrectResult_DeclarationsInsideDocumentInContainingProject()
49284986
{
4987+
var expectedResultCountsByDeclarationIdentifierName = new Dictionary<string, int>
4988+
{
4989+
[ProjectName] = 0,
4990+
[ProceduralModuleName] = 0,
4991+
[ClassModuleName] = 0,
4992+
[UserFormName] = 0,
4993+
[DocumentName] = 0,
4994+
[ProcedureName] = 1,
4995+
[FunctionName] = 1,
4996+
[PropertyGetName] = 1,
4997+
[PropertySetName] = 1,
4998+
[PropertyLetName] = 1,
4999+
[ParameterName] = 0,
5000+
[VariableName] = 1,
5001+
[LocalVariableName] = 0,
5002+
[ConstantName] = 0,
5003+
[LocalConstantName] = 0,
5004+
[EnumerationName] = 1,
5005+
[EnumerationMemberName] = 1,
5006+
[UserDefinedTypeName] = 0,
5007+
[UserDefinedTypeMemberName] = 0,
5008+
[LibraryProcedureName] = 0,
5009+
[LibraryFunctionName] = 0,
5010+
[LineLabelName] = 0
5011+
};
5012+
49295013
var builder = TestVbeWithUserProjectWithAdditionalComponent(
49305014
additionalComponentName: "Foo",
49315015
additionalComponentComponentType: ComponentType.Document,
@@ -4936,10 +5020,13 @@ public void ShadowedDeclaration_DoesNotReturnResult_DeclarationsInsideDocumentIn
49365020
using (var state = MockParser.CreateAndParse(vbe.Object))
49375021
{
49385022
var inspection = new ShadowedDeclarationInspection(state);
4939-
inspectionResults = inspection.GetInspectionResults();
5023+
inspectionResults = inspection.GetInspectionResults().ToList();
49405024
}
5025+
var inspectionResultCounts = InspectionResultCountsByTargetIdentifierName(inspectionResults);
49415026

4942-
Assert.IsFalse(inspectionResults.Any());
5027+
AssertResultCountsEqualForThoseWithExpectation(expectedResultCountsByDeclarationIdentifierName, inspectionResultCounts);
5028+
//All shadowing happens inside the document module.
5029+
Assert.IsTrue(inspectionResults.All(result => result.Target.QualifiedName.QualifiedModuleName.ComponentName == "Foo"));
49435030
}
49445031

49455032
[TestMethod]

0 commit comments

Comments
 (0)