Skip to content

Commit

Permalink
Tests for ignored inspections and ignore quick fix. A few bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hosch250 committed Jul 13, 2016
1 parent 02e39f3 commit 4eab52d
Show file tree
Hide file tree
Showing 44 changed files with 2,405 additions and 148 deletions.
26 changes: 1 addition & 25 deletions RetailCoder.VBE/Inspections/EmptyStringLiteralInspection.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Antlr4.Runtime;
using Microsoft.Vbe.Interop;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.Grammar;

Expand All @@ -29,33 +27,11 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
return new InspectionResultBase[] { };
}
return ParseTreeResults.EmptyStringLiterals
.Where(s => !HasIgnoreEmptyStringLiteralAnnotations(s.ModuleName.Component, s.Context.Start.Line))
.Where(s => !HasIgnoreAnnotation(s.ModuleName.Component, s.Context.Start.Line))
.Select(context => new EmptyStringLiteralInspectionResult(this,
new QualifiedContext<ParserRuleContext>(context.ModuleName, context.Context)));
}

private bool HasIgnoreEmptyStringLiteralAnnotations(VBComponent component, int line)
{
var annotations = State.GetModuleAnnotations(component).ToList();

if (State.GetModuleAnnotations(component) == null)
{
return false;
}

// VBE 1-based indexing
for (var i = line - 1; i >= 1; i--)
{
var annotation = annotations.SingleOrDefault(a => a.QualifiedSelection.Selection.StartLine == i) as IgnoreAnnotation;
if (annotation != null && annotation.InspectionNames.Contains(AnnotationName))
{
return true;
}
}

return false;
}

public class EmptyStringLiteralListener : VBAParserBaseListener
{
private readonly IList<VBAParser.LiteralExpressionContext> _contexts = new List<VBAParser.LiteralExpressionContext>();
Expand Down
24 changes: 24 additions & 0 deletions RetailCoder.VBE/Inspections/InspectionBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Vbe.Interop;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;

Expand Down Expand Up @@ -81,6 +83,28 @@ protected virtual IEnumerable<Declaration> BuiltInDeclarations
get { return State.AllDeclarations.Where(declaration => declaration.IsBuiltIn); }
}

protected bool HasIgnoreAnnotation(VBComponent component, int line)
{
var annotations = State.GetModuleAnnotations(component).ToList();

if (State.GetModuleAnnotations(component) == null)
{
return false;
}

// VBE 1-based indexing
for (var i = line - 1; i >= 1; i--)
{
var annotation = annotations.SingleOrDefault(a => a.QualifiedSelection.Selection.StartLine == i) as IgnoreAnnotation;
if (annotation != null && annotation.InspectionNames.Contains(AnnotationName))
{
return true;
}
}

return false;
}

/// <summary>
/// Gets all user declarations in the parser state without an @Ignore annotation for this inspection.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public MultilineParameterInspectionResult(IInspection inspection, Declaration ta
_quickFixes = new CodeInspectionQuickFix[]
{
new MakeSingleLineParameterQuickFix(Context, QualifiedSelection),
new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName)
new IgnoreOnceQuickFix(Target.ParentDeclaration.Context, Target.ParentDeclaration.QualifiedSelection, Inspection.AnnotationName)
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.VBA;
Expand Down Expand Up @@ -26,7 +27,7 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()

var results = new List<ObsoleteCallStatementUsageInspectionResult>();

foreach (var context in ParseTreeResults.ObsoleteCallContexts)
foreach (var context in ParseTreeResults.ObsoleteCallContexts.Where(o => !HasIgnoreAnnotation(o.ModuleName.Component, o.Context.Start.Line)))
{
var lines = context.ModuleName.Component.CodeModule.Lines[
context.Context.Start.Line, context.Context.Stop.Line - context.Context.Start.Line + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public ObsoleteCommentSyntaxInspection(RubberduckParserState state)

public override IEnumerable<InspectionResultBase> GetInspectionResults()
{
return State.AllComments.Where(comment => comment.Marker == Tokens.Rem)
return State.AllComments.Where(comment => comment.Marker == Tokens.Rem &&
!HasIgnoreAnnotation(comment.QualifiedSelection.QualifiedName.Component, comment.QualifiedSelection.Selection.StartLine))
.Select(comment => new ObsoleteCommentSyntaxInspectionResult(this, comment));
}
}
Expand Down
5 changes: 3 additions & 2 deletions RetailCoder.VBE/Inspections/ObsoleteLetStatementInspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
{
return new InspectionResultBase[] { };
}
return ParseTreeResults.ObsoleteLetContexts.Select(context =>
new ObsoleteLetStatementUsageInspectionResult(this, new QualifiedContext<ParserRuleContext>(context.ModuleName, context.Context)));
return ParseTreeResults.ObsoleteLetContexts
.Where(o => !HasIgnoreAnnotation(o.ModuleName.Component, o.Context.Start.Line))
.Select(context => new ObsoleteLetStatementUsageInspectionResult(this, new QualifiedContext<ParserRuleContext>(context.ModuleName, context.Context)));
}

public class ObsoleteLetStatementListener : VBAParserBaseListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
item.Scope.StartsWith("VBE7.DLL;"));

return declarations.SelectMany(declaration => declaration.References
.Where(item => _tokens.Contains(item.IdentifierName))
.Where(item => _tokens.Contains(item.IdentifierName) &&
!HasIgnoreAnnotation(item.QualifiedModuleName.Component, item.Selection.StartLine))
.Select(item => new UntypedFunctionUsageInspectionResult(this, item)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,33 @@ public void AssignedByValParameter_DoesNotReturnResult()
Assert.AreEqual(0, inspectionResults.Count());
}

[TestMethod]
[TestCategory("Inspections")]
public void AssignedByValParameter_Ignored_DoesNotReturnResult_Sub()
{
const string inputCode =
@"'@Ignore AssignedByValParameter
Public Sub Foo(ByVal arg1 As String)
Let arg1 = ""test""
End Sub";

//Arrange
var builder = new MockVbeBuilder();
VBComponent component;
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
var mockHost = new Mock<IHostApplication>();
mockHost.SetupAllProperties();
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));

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

var inspection = new AssignedByValParameterInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

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

[TestMethod]
[TestCategory("Inspections")]
public void AssignedByValParameter_ReturnsResult_SomeAssignedByValParams()
Expand Down Expand Up @@ -183,6 +210,42 @@ public void AssignedByValParameter_QuickFixWorks()
Assert.AreEqual(expectedCode, module.Lines());
}

[TestMethod]
[TestCategory("Inspections")]
public void AssignedByValParameter_IgnoreQuickFixWorks()
{
const string inputCode =
@"Public Sub Foo(ByVal arg1 As String)
Let arg1 = ""test""
End Sub";

const string expectedCode =
@"'@Ignore AssignedByValParameter
Public Sub Foo(ByVal arg1 As String)
Let arg1 = ""test""
End Sub";

//Arrange
var builder = new MockVbeBuilder();
VBComponent component;
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
var project = vbe.Object.VBProjects.Item(0);
var module = project.VBComponents.Item(0).CodeModule;
var mockHost = new Mock<IHostApplication>();
mockHost.SetupAllProperties();
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));

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

var inspection = new AssignedByValParameterInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

inspectionResults.First().QuickFixes.Single(s => s is IgnoreOnceQuickFix).Fix();

Assert.AreEqual(expectedCode, module.Lines());
}

[TestMethod]
[TestCategory("Inspections")]
public void InspectionType()
Expand Down
75 changes: 69 additions & 6 deletions RubberduckTests/Inspections/ConstantNotUsedInspectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ public void ConstantNotUsed_ReturnsResult_MultipleConsts()

[TestMethod]
[TestCategory("Inspections")]
public void GivenReferencedConstant_ReturnsNoInspectionResult()
public void ConstantNotUsed_ReturnsResult_UnusedConstant()
{
const string inputCode =
@"Public Sub Foo()
Const const1 As Integer = 9
Goo const1
Const const2 As String = ""test""
End Sub
Public Sub Goo(ByVal arg1 As Integer)
Expand All @@ -95,19 +97,17 @@ End Sub
var inspection = new ConstantNotUsedInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

Assert.AreEqual(0, inspectionResults.Count());
Assert.AreEqual(1, inspectionResults.Count());
}

[TestMethod]
[TestCategory("Inspections")]
public void GivenConstantNotUsed_ReturnsResultForUnusedConstant()
public void ConstantNotUsed_DoesNotReturnResult()
{
const string inputCode =
@"Public Sub Foo()
Const const1 As Integer = 9
Goo const1
Const const2 As String = ""test""
End Sub
Public Sub Goo(ByVal arg1 As Integer)
Expand All @@ -127,7 +127,34 @@ End Sub
var inspection = new ConstantNotUsedInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

Assert.AreEqual(1, inspectionResults.Count());
Assert.AreEqual(0, inspectionResults.Count());
}

[TestMethod]
[TestCategory("Inspections")]
public void ConstantNotUsed_Ignored_DoesNotReturnResult()
{
const string inputCode =
@"Public Sub Foo()
'@Ignore ConstantNotUsed
Const const1 As Integer = 9
End Sub";

//Arrange
var builder = new MockVbeBuilder();
VBComponent component;
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
var mockHost = new Mock<IHostApplication>();
mockHost.SetupAllProperties();
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));

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

var inspection = new ConstantNotUsedInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

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

[TestMethod]
Expand Down Expand Up @@ -164,6 +191,42 @@ public void ConstantNotUsed_QuickFixWorks()
Assert.AreEqual(expectedCode, module.Lines());
}

[TestMethod]
[TestCategory("Inspections")]
public void ConstantNotUsed_IgnoreQuickFixWorks()
{
const string inputCode =
@"Public Sub Foo()
Const const1 As Integer = 9
End Sub";

const string expectedCode =
@"Public Sub Foo()
'@Ignore ConstantNotUsed
Const const1 As Integer = 9
End Sub";

//Arrange
var builder = new MockVbeBuilder();
VBComponent component;
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
var project = vbe.Object.VBProjects.Item(0);
var module = project.VBComponents.Item(0).CodeModule;
var mockHost = new Mock<IHostApplication>();
mockHost.SetupAllProperties();
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));

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

var inspection = new ConstantNotUsedInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

inspectionResults.First().QuickFixes.Single(s => s is IgnoreOnceQuickFix).Fix();

Assert.AreEqual(expectedCode, module.Lines());
}

[TestMethod]
[TestCategory("Inspections")]
public void InspectionType()
Expand Down
32 changes: 26 additions & 6 deletions RubberduckTests/Inspections/DefaultProjectNameInspectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ public class DefaultProjectNameInspectionTests
[TestCategory("Inspections")]
public void DefaultProjectName_ReturnsResult()
{
const string inputCode = @"";

//Arrange
var builder = new MockVbeBuilder();
var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, string.Empty)
.Build();
var vbe = builder.AddProject(project).Build();

Expand All @@ -44,12 +42,10 @@ public void DefaultProjectName_ReturnsResult()
[TestCategory("Inspections")]
public void DefaultProjectName_DoesNotReturnResult()
{
const string inputCode = @"";

//Arrange
var builder = new MockVbeBuilder();
var project = builder.ProjectBuilder("TestProject", vbext_ProjectProtection.vbext_pp_none)
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, string.Empty)
.Build();
var vbe = builder.AddProject(project).Build();

Expand All @@ -66,6 +62,30 @@ public void DefaultProjectName_DoesNotReturnResult()
Assert.AreEqual(0, inspectionResults.Count());
}

[TestMethod]
[TestCategory("Inspections")]
public void DefaultProjectName_NoIgnoreQuickFix()
{
//Arrange
var builder = new MockVbeBuilder();
var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
.AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, string.Empty)
.Build();
var vbe = builder.AddProject(project).Build();

var mockHost = new Mock<IHostApplication>();
mockHost.SetupAllProperties();
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));

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

var inspection = new DefaultProjectNameInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

Assert.IsFalse(inspectionResults.ElementAt(0).QuickFixes.Any(q => q is IgnoreOnceQuickFix));
}

[TestMethod]
[TestCategory("Inspections")]
public void InspectionType()
Expand Down

0 comments on commit 4eab52d

Please sign in to comment.