From 0cd3f46f6a2b9cee789d09c4027ad9fb9e15e2ea Mon Sep 17 00:00:00 2001 From: Mathieu Guindon Date: Mon, 20 Jan 2025 02:08:52 -0500 Subject: [PATCH] added unit tests --- .../InconsistentArrayBaseInspection.cs | 6 +- .../InconsistentParamArrayBaseInspection.cs | 7 +- .../VariableTypeNotDeclaredInspection.cs | 2 +- .../AddRemoveReferencesViewModelTests.cs | 14 +-- .../InconsistentParamArrayInspectionTests.cs | 110 ++++++++++++++++++ .../MultilineParameterInspectionTests.cs | 2 +- .../SmartIndenter/EndOfLineCommentTests.cs | 4 +- 7 files changed, 130 insertions(+), 15 deletions(-) create mode 100644 RubberduckTests/Inspections/InconsistentParamArrayInspectionTests.cs diff --git a/Rubberduck.CodeAnalysis/Inspections/Concrete/InconsistentArrayBaseInspection.cs b/Rubberduck.CodeAnalysis/Inspections/Concrete/InconsistentArrayBaseInspection.cs index 283490123a..ffb3392a45 100644 --- a/Rubberduck.CodeAnalysis/Inspections/Concrete/InconsistentArrayBaseInspection.cs +++ b/Rubberduck.CodeAnalysis/Inspections/Concrete/InconsistentArrayBaseInspection.cs @@ -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()?.numberLiteral()?.GetText() == "1"; + var hasOptionBase1 = reference.Context + .GetAncestor() + .GetDescendent()? + .numberLiteral()?.GetText() == "1"; if (hasOptionBase1 && reference.Declaration.ProjectName == "VBA" && reference.Declaration.IdentifierName == "Array") { diff --git a/Rubberduck.CodeAnalysis/Inspections/Concrete/InconsistentParamArrayBaseInspection.cs b/Rubberduck.CodeAnalysis/Inspections/Concrete/InconsistentParamArrayBaseInspection.cs index a72ef3eb57..807fafb4bf 100644 --- a/Rubberduck.CodeAnalysis/Inspections/Concrete/InconsistentParamArrayBaseInspection.cs +++ b/Rubberduck.CodeAnalysis/Inspections/Concrete/InconsistentParamArrayBaseInspection.cs @@ -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()?.numberLiteral()?.GetText() == "1"; + var hasOptionBase1 = declaration.Context + .GetAncestor() + .GetDescendent()? + .numberLiteral()?.GetText() == "1"; + if (hasOptionBase1 && declaration is ParameterDeclaration parameter) { diff --git a/Rubberduck.CodeAnalysis/Inspections/Concrete/VariableTypeNotDeclaredInspection.cs b/Rubberduck.CodeAnalysis/Inspections/Concrete/VariableTypeNotDeclaredInspection.cs index c0f2bbb08a..1a5ee4fbd2 100644 --- a/Rubberduck.CodeAnalysis/Inspections/Concrete/VariableTypeNotDeclaredInspection.cs +++ b/Rubberduck.CodeAnalysis/Inspections/Concrete/VariableTypeNotDeclaredInspection.cs @@ -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); } diff --git a/RubberduckTests/AddRemoveReferences/AddRemoveReferencesViewModelTests.cs b/RubberduckTests/AddRemoveReferences/AddRemoveReferencesViewModelTests.cs index 71d153dba6..ed707bbf3c 100644 --- a/RubberduckTests/AddRemoveReferences/AddRemoveReferencesViewModelTests.cs +++ b/RubberduckTests/AddRemoveReferences/AddRemoveReferencesViewModelTests.cs @@ -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 { @@ -403,7 +403,7 @@ public void ViewModelMoveDownCommand_MovesPriorityDown() [Test] [Category("AddRemoveReferences")] public void ViewModelMoveDownCommand_DoesNotMoveLastReference() - + { var viewModel = AddRemoveReferencesSetup.ArrangeViewModel(); var referenced = viewModel.ProjectReferences.OfType().ToDictionary(model => model.Priority.GetValueOrDefault()); diff --git a/RubberduckTests/Inspections/InconsistentParamArrayInspectionTests.cs b/RubberduckTests/Inspections/InconsistentParamArrayInspectionTests.cs new file mode 100644 index 0000000000..2668b59312 --- /dev/null +++ b/RubberduckTests/Inspections/InconsistentParamArrayInspectionTests.cs @@ -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 InspectionResults(string inputCode) + { + return InspectionResultsForModules(("TestModule1", inputCode, ComponentType.StandardModule), ReferenceLibrary.VBA); + } + + protected override IInspection InspectionUnderTest(RubberduckParserState state) + { + return new InconsistentArrayBaseInspection(state); + } + } +} diff --git a/RubberduckTests/Inspections/MultilineParameterInspectionTests.cs b/RubberduckTests/Inspections/MultilineParameterInspectionTests.cs index f0d3217242..8626b61f3d 100644 --- a/RubberduckTests/Inspections/MultilineParameterInspectionTests.cs +++ b/RubberduckTests/Inspections/MultilineParameterInspectionTests.cs @@ -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 { diff --git a/RubberduckTests/SmartIndenter/EndOfLineCommentTests.cs b/RubberduckTests/SmartIndenter/EndOfLineCommentTests.cs index fde495a52c..2964c0aba6 100644 --- a/RubberduckTests/SmartIndenter/EndOfLineCommentTests.cs +++ b/RubberduckTests/SmartIndenter/EndOfLineCommentTests.cs @@ -1,7 +1,7 @@ -using System.Linq; using NUnit.Framework; using Rubberduck.SmartIndenter; using RubberduckTests.Settings; +using System.Linq; namespace RubberduckTests.SmartIndenter { @@ -220,7 +220,7 @@ public void WorksOutsideOfProcedures() { "#Const Foo = Bar 'Comment", "", - "Private Sub Test()", + "Private Sub Test()", "End Sub" };