Skip to content

Commit a2a7632

Browse files
committed
Added failing unit test
Modified common test code to support multiple project test scenarios
1 parent ccf17f5 commit a2a7632

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

RubberduckTests/Symbols/DeclarationFinderTests.cs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Rubberduck.Common;
1313
using Rubberduck.Parsing;
1414
using Rubberduck.Parsing.Grammar;
15+
using System;
1516

1617
namespace RubberduckTests.Symbols
1718
{
@@ -445,6 +446,44 @@ End Sub
445446
Assert.AreEqual(isConflict, conflicts.Where(cf => cf.IdentifierName.Equals(nameToCheck)).Any(), ConflictMessage(isConflict, nameToCheck, conflicts));
446447
}
447448

449+
450+
//https://github.com/rubberduck-vba/Rubberduck/issues/4969
451+
private const string projectOneModuleName = "projectOneModule";
452+
private const string projectTwoModuleName = "projectTwoModule";
453+
[TestCase(projectOneModuleName, ExpectedResult = 0)] //Duplicate module name found in a separate project
454+
[TestCase(projectTwoModuleName, ExpectedResult = 1)] //Duplicate module name found in the same project
455+
[Category("Resolver")]
456+
public int DeclarationFinder_RenameCodeModule_RespectsProjectScope(string proposedTestModuleName)
457+
{
458+
459+
string renameTargetModuleName = "TargetModule";
460+
461+
string moduleContent = $"Private Sub Foo(){Environment.NewLine}End Sub";
462+
463+
var projectOneContent = new TestComponentSpecification[]
464+
{
465+
new TestComponentSpecification(projectOneModuleName, moduleContent, ComponentType.StandardModule)
466+
};
467+
468+
var projectTwoContent = new TestComponentSpecification[]
469+
{
470+
new TestComponentSpecification(renameTargetModuleName, moduleContent, ComponentType.StandardModule),
471+
new TestComponentSpecification(projectTwoModuleName, moduleContent, ComponentType.StandardModule)
472+
};
473+
474+
var vbe = BuildProjects(new (string, IEnumerable<TestComponentSpecification>)[]
475+
{("ProjectOne", projectOneContent),("ProjectTwo", projectTwoContent)});
476+
477+
using(var parser = MockParser.CreateAndParse(vbe))
478+
{
479+
var target = parser.DeclarationFinder.AllDeclarations
480+
.FirstOrDefault(item => item.IdentifierName.Equals(renameTargetModuleName));
481+
482+
var results = parser.DeclarationFinder.FindNewDeclarationNameConflicts(proposedTestModuleName, target);
483+
return results.Count();
484+
}
485+
}
486+
448487
private static string ConflictMessage(bool isConflict, string name, IEnumerable<Declaration> conflicts)
449488
{
450489
return isConflict ? $"Identifier '{name}' is a conflict but was not identified" : $"Identifier '{name}' was incorrectly found as a conflict";
@@ -498,14 +537,33 @@ private void AddTestComponent(AccessibilityTestsDataObject tdo, string moduleIde
498537
}
499538

500539
private IVBE BuildProject(string projectName, List<TestComponentSpecification> testComponents)
540+
{
541+
var projectDefs = new (string, IEnumerable<TestComponentSpecification>)[] { (projectName, testComponents) };
542+
return BuildProjects(projectDefs);
543+
}
544+
545+
private IVBE BuildProjects(IEnumerable<(string ProjectName, IEnumerable<TestComponentSpecification> TestComponents)> projectDefinitions)
501546
{
502547
var builder = new MockVbeBuilder();
548+
foreach (var projectDef in projectDefinitions)
549+
{
550+
builder = AddProject(builder, projectDef.ProjectName, projectDef.TestComponents);
551+
}
552+
return builder.Build().Object;
553+
}
554+
555+
private MockVbeBuilder AddProject(MockVbeBuilder builder, string projectName, IEnumerable<TestComponentSpecification> testComponents)
556+
{
503557
var enclosingProjectBuilder = builder.ProjectBuilder(projectName, ProjectProtection.Unprotected);
504558

505-
testComponents.ForEach(c => enclosingProjectBuilder.AddComponent(c.Name, c.ModuleType, c.Content));
559+
foreach (var testComponent in testComponents)
560+
{
561+
enclosingProjectBuilder.AddComponent(testComponent.Name, testComponent.ModuleType, testComponent.Content);
562+
}
563+
506564
var enclosingProject = enclosingProjectBuilder.Build();
507565
builder.AddProject(enclosingProject);
508-
return builder.Build().Object;
566+
return builder;
509567
}
510568

511569
private IVBComponent RetrieveComponent(AccessibilityTestsDataObject tdo, string componentName)

0 commit comments

Comments
 (0)