Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
retailcoder committed Jan 20, 2025
1 parent d59e8ee commit 0cd3f46
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public InconsistentArrayBaseInspection(IDeclarationFinderProvider declarationFin

protected override bool IsResultReference(IdentifierReference reference, DeclarationFinder finder)
{
var parentModule = finder.ModuleDeclaration(reference.QualifiedModuleName);
var hasOptionBase1 = parentModule.Context.GetDescendent<VBAParser.OptionBaseStmtContext>()?.numberLiteral()?.GetText() == "1";
var hasOptionBase1 = reference.Context
.GetAncestor<VBAParser.ModuleContext>()
.GetDescendent<VBAParser.OptionBaseStmtContext>()?
.numberLiteral()?.GetText() == "1";

if (hasOptionBase1 && reference.Declaration.ProjectName == "VBA" && reference.Declaration.IdentifierName == "Array")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ public InconsistentParamArrayBaseInspection(IDeclarationFinderProvider declarati

protected override bool IsResultDeclaration(Declaration declaration, DeclarationFinder finder)
{
var parentModule = finder.ModuleDeclaration(declaration.QualifiedModuleName);
var hasOptionBase1 = parentModule.Context.GetDescendent<VBAParser.OptionBaseStmtContext>()?.numberLiteral()?.GetText() == "1";
var hasOptionBase1 = declaration.Context
.GetAncestor<VBAParser.ModuleContext>()
.GetDescendent<VBAParser.OptionBaseStmtContext>()?
.numberLiteral()?.GetText() == "1";


if (hasOptionBase1 && declaration is ParameterDeclaration parameter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected override string ResultDescription(Declaration declaration)
var declarationType = declaration.DeclarationType.ToLocalizedString();
var declarationName = declaration.IdentifierName;
return string.Format(
InspectionResults.ResourceManager.GetString(nameof(ImplicitVariantDeclarationInspection), CultureInfo.CurrentUICulture),
InspectionResults.ResourceManager.GetString(nameof(InspectionResults.ImplicitVariantDeclarationInspection), CultureInfo.CurrentUICulture),
declarationType,
declarationName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Moq;
using Moq;
using NUnit.Framework;
using Rubberduck.AddRemoveReferences;
using Rubberduck.UI;
using Rubberduck.UI.AddRemoveReferences;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.SafeComWrappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace RubberduckTests.AddRemoveReferences
{
Expand Down Expand Up @@ -403,7 +403,7 @@ public void ViewModelMoveDownCommand_MovesPriorityDown()
[Test]
[Category("AddRemoveReferences")]
public void ViewModelMoveDownCommand_DoesNotMoveLastReference()

{
var viewModel = AddRemoveReferencesSetup.ArrangeViewModel();
var referenced = viewModel.ProjectReferences.OfType<ReferenceModel>().ToDictionary(model => model.Priority.GetValueOrDefault());
Expand Down
110 changes: 110 additions & 0 deletions RubberduckTests/Inspections/InconsistentParamArrayInspectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using NUnit.Framework;
using Rubberduck.CodeAnalysis.Inspections;
using Rubberduck.CodeAnalysis.Inspections.Concrete;
using Rubberduck.Parsing.VBA;
using Rubberduck.VBEditor.SafeComWrappers;
using RubberduckTests.Mocks;
using System.Collections.Generic;
using System.Linq;

namespace RubberduckTests.Inspections
{
[TestFixture]
public class InconsistentParamArrayBaseInspectionTests : InspectionTestsBase
{
[Test]
[Category("Inspections")]
public void InconsistentParamArrayBase_ReturnsResult()
{
const string inputCode =
@"Option Base 1
Public Sub DoSomething(ParamArray Values)
End Sub";
Assert.AreEqual(1, InspectionResultsForStandardModule(inputCode).Count());
}

[Test]
[Category("Inspections")]
public void WithoutOptionBase_NoResult()
{
const string inputCode =
@"Public Sub DoSomething(ParamArray Values)
End Sub";

Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count());

}

[Test]
[Category("Inspections")]
public void NonParamArrayParameter_NoResult()
{
const string inputCode =
@"Option Base 1
Public Sub DoSomething(ByRef Values() As Variant)
End Sub";
Assert.AreEqual(0, InspectionResultsForStandardModule(inputCode).Count());
}

protected override IInspection InspectionUnderTest(RubberduckParserState state)
{
return new InconsistentParamArrayBaseInspection(state);
}
}

[TestFixture]
public class InconsistentArrayBaseInspectionTests : InspectionTestsBase
{
[Test]
[Category("Inspections")]
public void InconsistentArrayBase_ReturnsResult()
{
const string inputCode =
@"Option Base 1
Public Sub DoSomething()
Dim Values As Variant
Values = VBA.Array(42)
End Sub";

Assert.AreEqual(1, InspectionResults(inputCode).Count());
}

[Test]
[Category("Inspections")]
public void WithoutOptionBase_NoResult()
{
const string inputCode =
@"Public Sub DoSomething()
Dim Values As Variant
Values = VBA.Array(42)
End Sub";

Assert.AreEqual(0, InspectionResults(inputCode).Count());

}

[Test]
[Category("Inspections")]
public void WithoutQualifier_NoResult()
{
const string inputCode =
@"Public Sub DoSomething()
Dim Values As Variant
Values = Array(42)
End Sub";

Assert.AreEqual(0, InspectionResults(inputCode).Count());

}

private IEnumerable<IInspectionResult> InspectionResults(string inputCode)
{
return InspectionResultsForModules(("TestModule1", inputCode, ComponentType.StandardModule), ReferenceLibrary.VBA);
}

protected override IInspection InspectionUnderTest(RubberduckParserState state)
{
return new InconsistentArrayBaseInspection(state);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Linq;
using NUnit.Framework;
using Rubberduck.CodeAnalysis.Inspections;
using Rubberduck.CodeAnalysis.Inspections.Concrete;
using Rubberduck.Parsing.VBA;
using System.Linq;

namespace RubberduckTests.Inspections
{
Expand Down
4 changes: 2 additions & 2 deletions RubberduckTests/SmartIndenter/EndOfLineCommentTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Linq;
using NUnit.Framework;
using Rubberduck.SmartIndenter;
using RubberduckTests.Settings;
using System.Linq;

namespace RubberduckTests.SmartIndenter
{
Expand Down Expand Up @@ -220,7 +220,7 @@ public void WorksOutsideOfProcedures()
{
"#Const Foo = Bar 'Comment",
"",
"Private Sub Test()",
"Private Sub Test()",
"End Sub"
};

Expand Down

0 comments on commit 0cd3f46

Please sign in to comment.