Skip to content

Commit

Permalink
UnassignedVariableUsageInspection tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hosch250 committed Jul 8, 2016
1 parent d73a266 commit 9810c10
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
138 changes: 138 additions & 0 deletions RubberduckTests/Inspections/UnassignedVariableUsageInspectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System.Linq;
using System.Threading;
using Microsoft.Vbe.Interop;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Rubberduck.Inspections;
using Rubberduck.Parsing;
using Rubberduck.Parsing.VBA;
using Rubberduck.VBEditor.Extensions;
using Rubberduck.VBEditor.VBEHost;
using RubberduckTests.Mocks;

namespace RubberduckTests.Inspections
{
[TestClass]
public class UnassignedVariableUsageInspectionTests
{
[TestMethod]
[TestCategory("Inspections")]
public void UnassignedVariableUsage_ReturnsResult()
{
const string inputCode =
@"Sub Foo()
Dim b As Boolean
Dim bb As Boolean
bb = b
End Sub";

//Arrange
var builder = new MockVbeBuilder();
var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
.AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
.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 UnassignedVariableUsageInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

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

[TestMethod]
[TestCategory("Inspections")]
public void UnassignedVariableUsage_DoesNotReturnResult()
{
const string inputCode =
@"Sub Foo()
Dim b As Boolean
Dim bb As Boolean
b = True
bb = b
End Sub";

//Arrange
var builder = new MockVbeBuilder();
var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
.AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
.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 UnassignedVariableUsageInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

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

[TestMethod]
[TestCategory("Inspections")]
public void UnassignedVariableUsage_QuickFixWorks()
{
const string inputCode =
@"Sub Foo()
Dim b As Boolean
Dim bb As Boolean
bb = b
End Sub";

const string expectedCode =
@"Sub Foo()
Dim b As Boolean
Dim bb As Boolean
TODOTODO = TODO
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 UnassignedVariableUsageInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

inspectionResults.First().QuickFixes.First().Fix();

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

[TestMethod]
[TestCategory("Inspections")]
public void InspectionType()
{
var inspection = new UnassignedVariableUsageInspection(null);
Assert.AreEqual(CodeInspectionType.CodeQualityIssues, inspection.InspectionType);
}

[TestMethod]
[TestCategory("Inspections")]
public void InspectionName()
{
const string inspectionName = "UnassignedVariableUsageInspection";
var inspection = new UnassignedVariableUsageInspection(null);

Assert.AreEqual(inspectionName, inspection.Name);
}
}
}
1 change: 1 addition & 0 deletions RubberduckTests/RubberduckTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Inspections\ImplicitActiveSheetReferenceInspectionTests.cs" />
<Compile Include="Inspections\MultipleFolderAnnotationsInspectionTests.cs" />
<Compile Include="Inspections\ObjectVariableNotSetInpsectionTests.cs" />
<Compile Include="Inspections\UnassignedVariableUsageInspectionTests.cs" />
<Compile Include="Inspections\WriteOnlyPropertyInspectionTests.cs" />
<Compile Include="MockParser.cs" />
<Compile Include="Inspections\MoveFieldCloserToUsageInspectionTests.cs" />
Expand Down

0 comments on commit 9810c10

Please sign in to comment.