Skip to content

Commit

Permalink
Tests for Write Only Property
Browse files Browse the repository at this point in the history
  • Loading branch information
Hosch250 committed Jul 8, 2016
1 parent 3d4b519 commit d73a266
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
184 changes: 184 additions & 0 deletions RubberduckTests/Inspections/WriteOnlyPropertyInspectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
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.VBEHost;
using RubberduckTests.Mocks;

namespace RubberduckTests.Inspections
{
[TestClass]
public class WriteOnlyPropertyInspectionTests
{
[TestMethod]
[TestCategory("Inspections")]
public void WriteOnlyProperty_ReturnsResult_Let()
{
const string inputCode =
@"Property Let Foo(value)
End Property";

//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 WriteOnlyPropertyInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

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

[TestMethod]
[TestCategory("Inspections")]
public void WriteOnlyProperty_ReturnsResult_Set()
{
const string inputCode =
@"Property Set Foo(value)
End Property";

//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 WriteOnlyPropertyInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

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

[TestMethod]
[TestCategory("Inspections")]
public void WriteOnlyProperty_ReturnsResult_LetAndSet()
{
const string inputCode =
@"Property Let Foo(value)
End Property
Property Set Foo(value)
End Property";

//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 WriteOnlyPropertyInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

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

[TestMethod]
[TestCategory("Inspections")]
public void WriteOnlyProperty_DoesNotReturnsResult_Get()
{
const string inputCode =
@"Property Get Foo()
End Property";

//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 WriteOnlyPropertyInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

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

[TestMethod]
[TestCategory("Inspections")]
public void WriteOnlyProperty_DoesNotReturnsResult_GetAndLetAndSet()
{
const string inputCode =
@"Property Get Foo()
End Property
Property Let Foo(value)
End Property
Property Set Foo(value)
End Property";

//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 WriteOnlyPropertyInspection(parser.State);
var inspectionResults = inspection.GetInspectionResults();

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

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

[TestMethod]
[TestCategory("Inspections")]
public void InspectionName()
{
const string inspectionName = "WriteOnlyPropertyInspection";
var inspection = new WriteOnlyPropertyInspection(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\WriteOnlyPropertyInspectionTests.cs" />
<Compile Include="MockParser.cs" />
<Compile Include="Inspections\MoveFieldCloserToUsageInspectionTests.cs" />
<Compile Include="Inspections\AssignedByValParameterInspectionTests.cs" />
Expand Down

0 comments on commit d73a266

Please sign in to comment.