Skip to content

Commit 1af1af0

Browse files
committed
Added supertype tests for AccessibilityCheck and refactored.
1 parent fb76c97 commit 1af1af0

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

Rubberduck.Parsing/Symbols/AccessibilityCheck.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Rubberduck.Parsing.Symbols
1+
using System.Linq;
2+
3+
namespace Rubberduck.Parsing.Symbols
24
{
35
public static class AccessibilityCheck
46
{
@@ -38,7 +40,7 @@ public static bool IsMemberAccessible(Declaration callingProject, Declaration ca
3840
{
3941
return false;
4042
}
41-
if (IsEnclosingModuleOfInstanceMember(callingModule, calleeMember)
43+
if (IsInstanceMemberOfModuleOrOneOfItsSupertypes(callingModule, calleeMember)
4244
|| IsLocalMemberOfTheCallingSubroutineOrProperty(callingParent, calleeMember))
4345
{
4446
return true;
@@ -51,21 +53,16 @@ public static bool IsMemberAccessible(Declaration callingProject, Declaration ca
5153
|| (IsEnclosingProject(callingProject, memberModule) && IsAccessibleThroughoutTheSameProject(calleeMember)));
5254
}
5355

54-
private static bool IsEnclosingModuleOfInstanceMember(Declaration callingModule, Declaration calleeMember)
56+
private static bool IsInstanceMemberOfModuleOrOneOfItsSupertypes(Declaration module, Declaration member)
5557
{
56-
if (callingModule.Equals(calleeMember.ParentScopeDeclaration))
57-
{
58-
return true;
59-
}
60-
foreach (var supertype in ClassModuleDeclaration.GetSupertypes(callingModule))
58+
return IsInstanceMemberOfModule(module, member)
59+
|| ClassModuleDeclaration.GetSupertypes(module).Any(supertype => IsInstanceMemberOfModuleOrOneOfItsSupertypes(supertype, member)); //ClassModuleDeclaration.GetSuperTypes never returns null.
60+
}
61+
62+
private static bool IsInstanceMemberOfModule(Declaration module, Declaration member)
6163
{
62-
if (IsEnclosingModuleOfInstanceMember(supertype, calleeMember))
63-
{
64-
return true;
65-
}
64+
return member.ParentScopeDeclaration.Equals(module);
6665
}
67-
return false;
68-
}
6966

7067
private static bool IsLocalMemberOfTheCallingSubroutineOrProperty(Declaration callingParent, Declaration calleeMember)
7168
{

RubberduckTests/Symbols/AccessibilityCheckTests.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ProjectsAreAlwaysAccessible()
1919
Assert.IsTrue(AccessibilityCheck.IsAccessible(null,null,null,projectDeclatation));
2020
}
2121

22-
private static Declaration GetTestProject(string name)
22+
private static ProjectDeclaration GetTestProject(string name)
2323
{
2424
var qualifiedProjectName = new QualifiedMemberName(StubQualifiedModuleName(), name);
2525
return new ProjectDeclaration(qualifiedProjectName, name, false);
@@ -43,7 +43,7 @@ public void ModulesCanBeAccessedFromWithinThemselves()
4343
Assert.IsTrue(AccessibilityCheck.IsAccessible(projectDeclatation, moduleDeclatation, null, moduleDeclatation));
4444
}
4545

46-
private static Declaration GetTestClassModule(Declaration projectDeclatation, string name, bool isExposed = false)
46+
private static ClassModuleDeclaration GetTestClassModule(Declaration projectDeclatation, string name, bool isExposed = false)
4747
{
4848
var qualifiedClassModuleMemberName = new QualifiedMemberName(StubQualifiedModuleName(), name);
4949
var classModuleAttributes = new Rubberduck.Parsing.VBA.Attributes();
@@ -101,7 +101,7 @@ public void NonPrivateProceduralModulesCanBeAccessedFromOtherProjects()
101101
Assert.IsTrue(AccessibilityCheck.IsAccessible(callingProjectDeclatation, callingModuleDeclatation, null, calleeModuleDeclatation));
102102
}
103103

104-
private static Declaration GetTestProceduralModule(Declaration projectDeclatation, string name)
104+
private static ProceduralModuleDeclaration GetTestProceduralModule(Declaration projectDeclatation, string name)
105105
{
106106
var qualifiedClassModuleMemberName = new QualifiedMemberName(StubQualifiedModuleName(), name);
107107
var proceduralModuleDeclaration = new ProceduralModuleDeclaration(qualifiedClassModuleMemberName, projectDeclatation, name, false, null, null);
@@ -126,15 +126,30 @@ public void PrivateProceduresAreAccessibleFromTheEnclosingModule()
126126
Assert.IsTrue(AccessibilityCheck.IsAccessible(projectDeclatation, moduleDeclatation, privateCallingFunctionDeclaration, privateCalleeFunctionDeclaration));
127127
}
128128

129-
private static Declaration GetTestFunction(Declaration moduleDeclatation, string name, Accessibility functionAccessibility)
129+
private static FunctionDeclaration GetTestFunction(Declaration moduleDeclatation, string name, Accessibility functionAccessibility)
130130
{
131131
var qualifiedFunctionMemberName = new QualifiedMemberName(StubQualifiedModuleName(), name);
132132
return new FunctionDeclaration(qualifiedFunctionMemberName, moduleDeclatation, moduleDeclatation, "test", null, "test", functionAccessibility, null, Selection.Home, false, false, null, null);
133133
}
134134

135+
[TestMethod]
136+
public void PrivateProceduresAreAccessibleIfTheyAreInAClassAboveTheEnclosingModuleOfTheCallerInTheClassHierarchy()
137+
{
138+
var projectDeclatation = GetTestProject("test_project");
139+
var callingModule = GetTestClassModule(projectDeclatation, "callingModule");
140+
var privateCallingFunction = GetTestFunction(callingModule, "callingFoo", Accessibility.Private);
141+
var supertypeOfCallingModule = GetTestClassModule(projectDeclatation, "callingModuleSuper");
142+
callingModule.AddSupertype(supertypeOfCallingModule);
143+
var supertypeOfSupertypeOfCallingModule = GetTestClassModule(projectDeclatation, "callingModuleSuperSuper");
144+
supertypeOfCallingModule.AddSupertype(supertypeOfSupertypeOfCallingModule);
145+
var privateCalleeFunction = GetTestFunction(supertypeOfSupertypeOfCallingModule, "calleeFoo", Accessibility.Private);
146+
147+
Assert.IsTrue(AccessibilityCheck.IsAccessible(projectDeclatation, callingModule, privateCallingFunction, privateCalleeFunction));
148+
}
149+
135150

136151
[TestMethod]
137-
public void PrivateProceduresAreNotAcessibleFromOtherModules()
152+
public void PrivateProceduresAreNotAcessibleFromOtherUnrelatedModules()
138153
{
139154
var projectDeclatation = GetTestProject("test_project");
140155
var calleeModuleDeclatation = GetTestClassModule(projectDeclatation, "callee_test_Module");
@@ -143,7 +158,7 @@ public void PrivateProceduresAreNotAcessibleFromOtherModules()
143158
var callingFunctionDeclaration = GetTestFunction(callingModuleDeclatation, "callingFoo", Accessibility.Private);
144159

145160
Assert.IsFalse(AccessibilityCheck.IsAccessible(projectDeclatation, callingModuleDeclatation, callingFunctionDeclaration, calleeFunctionDeclaration));
146-
}
161+
}
147162

148163

149164
[TestMethod]
@@ -251,7 +266,23 @@ private static Declaration GetTestVariable(Declaration parentDeclatation, string
251266

252267

253268
[TestMethod]
254-
public void PrivateInstanceVariablesAreNotAcessibleFromOtherModules()
269+
public void PrivateInstanceVariablesAreAccessibleIfTheyAreInAClassAboveTheEnclosingModuleOfTheCallerInTheClassHierarchy()
270+
{
271+
var projectDeclatation = GetTestProject("test_project");
272+
var callingModule = GetTestClassModule(projectDeclatation, "callingModule");
273+
var privateCallingFunction = GetTestFunction(callingModule, "callingFoo", Accessibility.Private);
274+
var supertypeOfCallingModule = GetTestClassModule(projectDeclatation, "callingModuleSuper");
275+
callingModule.AddSupertype(supertypeOfCallingModule);
276+
var supertypeOfSupertypeOfCallingModule = GetTestClassModule(projectDeclatation, "callingModuleSuperSuper");
277+
supertypeOfCallingModule.AddSupertype(supertypeOfSupertypeOfCallingModule);
278+
var privateCalleeInstanceVariable = GetTestVariable(supertypeOfSupertypeOfCallingModule, "calleeFoo", Accessibility.Private);
279+
280+
Assert.IsTrue(AccessibilityCheck.IsAccessible(projectDeclatation, callingModule, privateCallingFunction, privateCalleeInstanceVariable));
281+
}
282+
283+
284+
[TestMethod]
285+
public void PrivateInstanceVariablesAreNotAcessibleFromOtherUnrelatedModules()
255286
{
256287
var projectDeclatation = GetTestProject("test_project");
257288
var calleeModuleDeclatation = GetTestClassModule(projectDeclatation, "callee_test_Module");

0 commit comments

Comments
 (0)