Skip to content

Commit

Permalink
Merge branch 'next' of https://github.com/rubberduck-vba/rubberduck i…
Browse files Browse the repository at this point in the history
…nto Rubberdeck.Inspections_C#6&7_Update
  • Loading branch information
IvenBach committed Nov 27, 2017
2 parents 499ad93 + bc12cf4 commit 2cab0fc
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 6 deletions.
4 changes: 3 additions & 1 deletion RetailCoder.VBE/Refactorings/Rename/RenameRefactoring.cs
Expand Up @@ -476,7 +476,9 @@ private void RenameStandardElements(Declaration target, string newName)

private void RenameReferences(Declaration target, string newName)
{
var modules = target.References.GroupBy(r => r.QualifiedModuleName);
var modules = target.References
.Where(reference => reference.Context.GetText() != "Me")
.GroupBy(r => r.QualifiedModuleName);
foreach (var grouping in modules)
{
_modulesToRewrite.Add(grouping.Key);
Expand Down
Expand Up @@ -22,16 +22,15 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()

var targets = Declarations.Where(decl => decl.AsTypeDeclaration != null &&
!decl.AsTypeDeclaration.IsUserDefined &&
decl.AsTypeDeclaration.DeclarationType == DeclarationType.ClassModule &&
decl.AsTypeDeclaration.DeclarationType.HasFlag(DeclarationType.ClassModule) &&
((ClassModuleDeclaration)decl.AsTypeDeclaration).IsExtensible)
.SelectMany(decl => decl.References).ToList();

return from access in unresolved
let callingContext = targets.FirstOrDefault(usage => usage.Context.Equals(access.CallingContext))
where callingContext != null
select new DeclarationInspectionResult(this,
string.Format(InspectionsUI.MemberNotOnInterfaceInspectionResultFormat, access.IdentifierName, callingContext.Declaration.AsTypeDeclaration.IdentifierName),
access);
string.Format(InspectionsUI.MemberNotOnInterfaceInspectionResultFormat, access.IdentifierName, callingContext.Declaration.AsTypeDeclaration.IdentifierName),
access);
}
}
}
2 changes: 1 addition & 1 deletion Rubberduck.Parsing/Symbols/IdentifierReferenceResolver.cs
Expand Up @@ -174,7 +174,7 @@ private void ResolveLabel(ParserRuleContext context, string label)
{
var lexpression = expression as VBAParser.LExpressionContext
?? expression.GetChild<VBAParser.LExpressionContext>(0)
?? (expression as VBAParser.LExprContext
?? (expression as VBAParser.LExprContext
?? expression.GetChild<VBAParser.LExprContext>(0))?.lExpression();

if (lexpression != null)
Expand Down
62 changes: 62 additions & 0 deletions RubberduckTests/Inspections/MemberNotOnInterfaceInspectionTests.cs
Expand Up @@ -265,5 +265,67 @@ public void MemberNotOnInterface_Ignored_DoesNotReturnResult()
Assert.IsFalse(inspectionResults.Any());
}
}

[TestMethod]
[DeploymentItem(@"Testfiles\")]
[TestCategory("Inspections")]
public void MemberNotOnInterface_CatchesInvalidUseOfMember()
{
const string userForm1Code = @"
Private _fooBar As String
Public Property Let FooBar(value As String)
_fooBar = value
End Property
Public Property Get FooBar() As String
FooBar = _fooBar
End Property
";

const string analyzedCode = @"Option Explicit
Sub FizzBuzz()
Dim bar As UserForm1
Set bar = New UserForm1
bar.FooBar = ""FooBar""
Dim foo As UserForm
Set foo = New UserForm1
foo.FooBar = ""BarFoo""
End Sub
";
var mockVbe = new MockVbeBuilder();
var projectBuilder = mockVbe.ProjectBuilder("testproject", ProjectProtection.Unprotected);
projectBuilder.MockUserFormBuilder("UserForm1", userForm1Code).MockProjectBuilder()
.AddComponent("ReferencingModule", ComponentType.StandardModule, analyzedCode)
//.AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel)
.AddReference("MSForms", MockVbeBuilder.LibraryPathMsForms);

mockVbe.AddProject(projectBuilder.Build());


var parser = MockParser.Create(mockVbe.Build().Object);

//parser.State.AddTestLibrary("Excel.1.8.xml");
parser.State.AddTestLibrary("MSForms.2.0.xml");

parser.Parse(new CancellationTokenSource());
if (parser.State.Status >= ParserState.Error)
{
Assert.Inconclusive("Parser Error");
}

using (var state = parser.State)
{
var inspection = new MemberNotOnInterfaceInspection(state);
var inspectionResults = inspection.GetInspectionResults();

Assert.IsTrue(inspectionResults.Any());
}

}
}
}
42 changes: 42 additions & 0 deletions RubberduckTests/Refactoring/RenameTests.cs
Expand Up @@ -1962,6 +1962,48 @@ public void RenameRefactoring_RenameViewModel_IsValidName_ChangeCasingNotValid()
Assert.IsFalse(renameViewModel.IsValidName);
}


[TestMethod]
[TestCategory("Refactorings")]
[TestCategory("Rename")]
public void RenameRefactoring_RenameClassModule_DoesNotChangeMeReferences()
{
const string newName = "RenamedClassModule";

//Input
const string inputCode =
@"Property Get Self() As IClassModule
Set Self = Me
End Property";

var selection = new Selection(3, 27, 3, 27);

IVBComponent component;
var vbe = MockVbeBuilder.BuildFromSingleModule(inputCode, "ClassModule1", ComponentType.ClassModule, out component, selection);
using (var state = MockParser.CreateAndParse(vbe.Object))
{

var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

var msgbox = new Mock<IMessageBox>();
msgbox.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), MessageBoxButtons.YesNo, It.IsAny<MessageBoxIcon>()))
.Returns(DialogResult.Yes);

var vbeWrapper = vbe.Object;
var model = new RenameModel(vbeWrapper, state, qualifiedSelection) { NewName = newName };
model.Target = model.Declarations.FirstOrDefault(i => i.DeclarationType == DeclarationType.ClassModule && i.IdentifierName == "ClassModule1");

//SetupFactory
var factory = SetupFactory(model);

var refactoring = new RenameRefactoring(vbeWrapper, factory.Object, msgbox.Object, state);
refactoring.Refactor(model.Target);

Assert.AreSame(newName, component.CodeModule.Name);
Assert.AreEqual(inputCode, component.CodeModule.GetLines(0, component.CodeModule.CountOfLines));
}

}
#endregion

#region Test Execution
Expand Down

0 comments on commit 2cab0fc

Please sign in to comment.