Skip to content

Commit b300113

Browse files
authored
Merge pull request #4402 from Hosch250/fixTests
Fixes chained COM wrapper analyzer tests
2 parents ca86442 + 407de69 commit b300113

File tree

4 files changed

+140
-7
lines changed

4 files changed

+140
-7
lines changed

Rubberduck.CodeAnalysis/Inspections/Concrete/SheetAccessedUsingStringInspection.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
6060
var component = GetVBComponentMatchingSheetName(reference);
6161
if (component != null)
6262
{
63-
reference.Properties.CodeName = (string)component.Properties.Single(property => property.Name == "CodeName").Value;
63+
using (var properties = component.Properties)
64+
{
65+
reference.Properties.CodeName = (string)properties.Single(property => property.Name == "CodeName").Value;
66+
}
6467
issues.Add(reference);
6568
}
6669
}
@@ -98,9 +101,35 @@ private IVBComponent GetVBComponentMatchingSheetName(IdentifierReferenceInspecti
98101
var sheetName = FormatSheetName(sheetArgumentContext.GetText());
99102
var project = State.Projects.First(p => p.ProjectId == reference.QualifiedName.ProjectId);
100103

101-
return project.VBComponents.FirstOrDefault(c =>
102-
c.Type == ComponentType.Document &&
103-
(string) c.Properties.First(property => property.Name == "Name").Value == sheetName);
104+
105+
//return project.VBComponents.FirstOrDefault(c =>
106+
// c.Type == ComponentType.Document &&
107+
// (string)c.Properties.First(property => property.Name == "Name").Value == sheetName);
108+
using (var components = project.VBComponents)
109+
{
110+
for (var i = 0; i < components.Count; i++)
111+
{
112+
using (var component = components[i])
113+
using (var properties = component.Properties)
114+
{
115+
if (component.Type == ComponentType.Document)
116+
{
117+
for (var j = 0; j < properties.Count; j++)
118+
{
119+
using (var property = properties[j])
120+
{
121+
if (property.Name == "Name" && (string)property.Value == sheetName)
122+
{
123+
return component;
124+
}
125+
}
126+
}
127+
}
128+
}
129+
}
130+
131+
return null;
132+
}
104133
}
105134

106135
private static string FormatSheetName(string sheetName)

RubberduckCodeAnalysis/ChainedWrapperAnalyzer.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ private static void AnalyzeSymbol(SyntaxNodeAnalysisContext context)
3939
}
4040

4141
var expInterfaces = context.SemanticModel.GetTypeInfo(node.Expression).Type?.AllInterfaces;
42-
var nameInterfaces = context.SemanticModel.GetTypeInfo(node.Name).Type?.AllInterfaces;
42+
43+
var nameValue = node.Name.Parent.Parent is InvocationExpressionSyntax ? node.Name.Parent.Parent : node.Name;
44+
var nameInterfaces = context.SemanticModel.GetTypeInfo(nameValue).Type?.AllInterfaces;
4345

4446
if (!expInterfaces.HasValue || !nameInterfaces.HasValue)
4547
{

RubberduckTests/Mocks/MockProjectBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private Mock<IVBComponents> CreateComponentsMock()
189189

190190
result.Setup(m => m[It.IsAny<int>()]).Returns<int>(index => Components.ElementAt(index));
191191
result.Setup(m => m[It.IsAny<string>()]).Returns<string>(name => Components.Single(item => item.Name == name));
192-
result.SetupGet(m => m.Count).Returns(Components.Count);
192+
result.SetupGet(m => m.Count).Returns(() => Components.Count);
193193

194194
result.Setup(m => m.Add(It.IsAny<ComponentType>()))
195195
.Callback((ComponentType c) =>
@@ -284,6 +284,8 @@ private Mock<IVBComponent> CreateComponentMock(string name, ComponentType type,
284284

285285
var propertiesMock = new Mock<IProperties>();
286286
propertiesMock.Setup(m => m.GetEnumerator()).Returns(() => properties?.GetEnumerator());
287+
propertiesMock.SetupGet(m => m.Count).Returns(properties?.Count() ?? 0);
288+
propertiesMock.Setup(m => m[It.IsAny<int>()]).Returns<int>(index => properties.ElementAt(index));
287289
result.SetupGet(m => m.Properties).Returns(propertiesMock.Object);
288290

289291
var module = CreateCodeModuleMock(name, content, selection, result);

RubberduckTestsCodeAnalysis/RubberduckCodeAnalysisUnitTests.cs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,6 @@ public class ChainedWrapperUnitTests : ChainedWrapperAnalyzer
724724
{
725725
[Test]
726726
[Category("ChainedWrappers")]
727-
[Ignore("See #4377")]
728727
public void InterfaceContainsInterfaceType()
729728
{
730729
var test = @"namespace Rubberduck.VBEditor.SafeComWrappers.Abstract
@@ -752,6 +751,76 @@ public void B()
752751
v.Execute().Execute();
753752
}
754753
}
754+
}";
755+
var diagnostics = GetSortedDiagnostics(new[] { test }, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer());
756+
Assert.AreEqual("ChainedWrapper", diagnostics.Single().Descriptor.Id);
757+
}
758+
[Test]
759+
[Category("ChainedWrappers")]
760+
public void InterfaceContainsInterfaceType_Property()
761+
{
762+
var test = @"namespace Rubberduck.VBEditor.SafeComWrappers.Abstract
763+
{
764+
public interface ISafeComWrapper
765+
{
766+
FooImp Value { get; }
767+
}
768+
769+
public abstract class Foo : ISafeComWrapper
770+
{
771+
public virtual FooImp Value => new FooImp();
772+
}
773+
774+
public class FooImp : Foo
775+
{
776+
public override FooImp Value => base.Value;
777+
}
778+
779+
public class D
780+
{
781+
public void B()
782+
{
783+
var v = new FooImp();
784+
var x = v.Value.Value;
785+
}
786+
}
787+
}";
788+
var diagnostics = GetSortedDiagnostics(new[] { test }, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer());
789+
Assert.AreEqual("ChainedWrapper", diagnostics.Single().Descriptor.Id);
790+
}
791+
[Test]
792+
[Category("ChainedWrappers")]
793+
public void InterfaceContainsInterfaceType_Property_ReverseAssignment()
794+
{
795+
var test = @"namespace Rubberduck.VBEditor.SafeComWrappers.Abstract
796+
{
797+
public interface ISafeComWrapper
798+
{
799+
FooImp Value { get; set; }
800+
}
801+
802+
public abstract class Foo : ISafeComWrapper
803+
{
804+
public virtual FooImp Value
805+
{
806+
get => new FooImp();
807+
set => new FooImp().Value = value;
808+
}
809+
}
810+
811+
public class FooImp : Foo
812+
{
813+
public override FooImp Value => base.Value;
814+
}
815+
816+
public class D
817+
{
818+
public void B()
819+
{
820+
var v = new FooImp();
821+
v.Value.Value = new FooImp();
822+
}
823+
}
755824
}";
756825
var diagnostics = GetSortedDiagnostics(new[] { test }, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer());
757826
Assert.AreEqual("ChainedWrapper", diagnostics.Single().Descriptor.Id);
@@ -785,4 +854,35 @@ protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
785854

786855
}
787856
}
857+
}
858+
859+
namespace Rubberduck.VBEditor.SafeComWrappers.Abstract1
860+
{
861+
public interface ISafeComWrapper
862+
{
863+
FooImp Value { get; set; }
864+
}
865+
866+
public abstract class Foo : ISafeComWrapper
867+
{
868+
public virtual FooImp Value
869+
{
870+
get => new FooImp();
871+
set => new FooImp().Value = value;
872+
}
873+
}
874+
875+
public class FooImp : Foo
876+
{
877+
public override FooImp Value => base.Value;
878+
}
879+
880+
public class D
881+
{
882+
public void B()
883+
{
884+
var v = new FooImp();
885+
v.Value.Value = new FooImp();
886+
}
887+
}
788888
}

0 commit comments

Comments
 (0)