public void GivenArrayParameter_ReturnsNoResult()
        {
            const string inputCode =
                @"Sub Foo(ByRef arg1() As Variant)
End Sub";
            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new ParameterCanBeByValInspection(state);
                var results    = inspection.GetInspectionResults(CancellationToken.None).ToList();

                Assert.AreEqual(0, results.Count);
            }
        }
        public void ParameterCanBeByVal_ReturnsResult_SomeParams()
        {
            const string inputCode =
                @"Sub Foo(arg1 As String, ByVal arg2 As Integer)
End Sub";
            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection        = new ParameterCanBeByValInspection(state);
                var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
        public void ParameterCanBeByVal_DoesNotReturnResult_PassedByValExplicitly()
        {
            const string inputCode =
                @"Sub Foo(ByVal arg1 As String)
End Sub";
            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection        = new ParameterCanBeByValInspection(state);
                var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);

                Assert.AreEqual(0, inspectionResults.Count());
            }
        }
예제 #4
0
        public void ParameterCanBeByVal_Ignored_DoesNotReturnResult()
        {
            const string inputCode =
                @"'@Ignore ParameterCanBeByVal
Sub Foo(arg1 As String)
End Sub";

            IVBComponent component;
            var          vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
            var          state = MockParser.CreateAndParse(vbe.Object);

            var inspection        = new ParameterCanBeByValInspection(state);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.IsFalse(inspectionResults.Any());
        }
예제 #5
0
        public void ParameterCanBeByVal_DoesNotReturnResult_BuiltInEventParam()
        {
            const string inputCode =
                @"Sub Foo(ByRef arg1 As String)
    arg1 = ""test""
End Sub";

            IVBComponent component;
            var          vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
            var          state = MockParser.CreateAndParse(vbe.Object);

            var inspection        = new ParameterCanBeByValInspection(state);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(0, inspectionResults.Count());
        }
        public void ParameterCanBeByVal_ReturnsResult_PassedToByRefProc_NoAssignment()
        {
            const string inputCode =
                @"Sub DoSomething(foo As Integer)
    DoSomethingElse foo
End Sub

Sub DoSomethingElse(ByVal bar As Integer)
End Sub";
            var vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);
            var state = MockParser.CreateAndParse(vbe.Object);

            var inspection        = new ParameterCanBeByValInspection(state);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(1, inspectionResults.Count());
        }
        public void ParameterCanBeByVal_InterfaceMember_MultipleParams_OneCanBeByVal()
        {
            //Input
            const string inputCode1 =
                @"Public Sub DoSomething(ByRef a As Integer, ByRef b As Integer)
End Sub";
            const string inputCode2 =
                @"Implements IClass1

Private Sub IClass1_DoSomething(ByRef a As Integer, ByRef b As Integer)
    b = 42
End Sub";
            const string inputCode3 =
                @"Implements IClass1

Private Sub IClass1_DoSomething(ByRef a As Integer, ByRef b As Integer)
End Sub";

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("IClass1", vbext_ComponentType.vbext_ct_ClassModule, inputCode1)
                          .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
                          .AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputCode3)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            var mockHost = new Mock <IHostApplication>();

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

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

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

            Assert.AreEqual("a", inspectionResults.Single().Target.IdentifierName);
        }
        public void ParameterCanBeByVal_QuickFixWorks_PassedByRef()
        {
            const string inputCode =
                @"Sub Foo(ByRef arg1 As String)
End Sub";

            const string expectedCode =
                @"Sub Foo(ByVal arg1 As String)
End Sub";

            var vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
            var state = MockParser.CreateAndParse(vbe.Object);

            var inspection = new ParameterCanBeByValInspection(state);

            new PassParameterByValueQuickFix(state).Fix(inspection.GetInspectionResults().First());

            Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
        }
예제 #9
0
        public void ParameterCanBeByVal_DoesNotReturnResult_PassedToByRefProc_WithAssignment()
        {
            const string inputCode =
                @"Sub DoSomething(foo As Integer)
    DoSomethingElse foo
End Sub

Sub DoSomethingElse(ByRef bar As Integer)
    bar = 42
End Sub";

            IVBComponent component;
            var          vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
            var          state = MockParser.CreateAndParse(vbe.Object);

            var inspection        = new ParameterCanBeByValInspection(state);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.IsFalse(inspectionResults.Any());
        }
예제 #10
0
        public void ParameterCanBeByVal_QuickFixWorks_SubNameStartsWithParamName()
        {
            const string inputCode =
                @"Sub foo(f)
End Sub";

            const string expectedCode =
                @"Sub foo(ByVal f)
End Sub";

            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new ParameterCanBeByValInspection(state);
                new PassParameterByValueQuickFix(state).Fix(inspection.GetInspectionResults(CancellationToken.None).First());

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
예제 #11
0
        public void ParameterCanBeByVal_QuickFixWorks_PassedByRef()
        {
            const string inputCode =
                @"Sub Foo(ByRef arg1 As String)
End Sub";

            const string expectedCode =
                @"Sub Foo(ByVal arg1 As String)
End Sub";

            IVBComponent component;
            var          vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
            var          state = MockParser.CreateAndParse(vbe.Object);

            var inspection = new ParameterCanBeByValInspection(state);

            inspection.GetInspectionResults().First().QuickFixes.First().Fix();

            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
        public void ParameterCanBeByVal_QuickFixWithOptionalByRefWorks()
        {
            const string inputCode =
                @"Sub Test(Optional ByRef foo As String = ""bar"")
    Debug.Print foo
End Sub";

            const string expectedCode =
                @"Sub Test(Optional ByVal foo As String = ""bar"")
    Debug.Print foo
End Sub";

            var vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
            var state = MockParser.CreateAndParse(vbe.Object);

            var inspection = new ParameterCanBeByValInspection(state);

            new PassParameterByValueQuickFix(state).Fix(inspection.GetInspectionResults().First());

            Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
        }
예제 #13
0
        public void ParameterCanBeByVal_QuickFixWithOptionalWorks()
        {
            const string inputCode =
                @"Sub Test(Optional foo As String = ""bar"")
    Debug.Print foo
End Sub";

            const string expectedCode =
                @"Sub Test(Optional ByVal foo As String = ""bar"")
    Debug.Print foo
End Sub";

            IVBComponent component;
            var          vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
            var          state = MockParser.CreateAndParse(vbe.Object);

            var inspection = new ParameterCanBeByValInspection(state);

            inspection.GetInspectionResults().First().QuickFixes.Single(s => s is PassParameterByValueQuickFix).Fix();

            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
        public void ParameterCanBeByVal_EventMember_SingleParamUsedByRef()
        {
            //Input
            const string inputCode1 =
                @"Public Event Foo(ByRef arg1 As Integer)";

            const string inputCode2 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef arg1 As Integer)
    arg1 = 42
End Sub";

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode1)
                          .AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
                          .AddComponent("Class3", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            var mockHost = new Mock <IHostApplication>();

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

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

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

            Assert.IsFalse(inspectionResults.Any());
        }
예제 #15
0
        public void ParameterCanBeByVal_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo(ByRef _
arg1 As String)
End Sub";

            const string expectedCode =
                @"'@Ignore ParameterCanBeByVal
Sub Foo(ByRef _
arg1 As String)
End Sub";

            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new ParameterCanBeByValInspection(state);
                new IgnoreOnceQuickFix(state, new[] { inspection }).Fix(inspection.GetInspectionResults(CancellationToken.None).First());

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
        public void ParameterCanBeByVal_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo(ByRef _
arg1 As String)
End Sub";

            const string expectedCode =
                @"'@Ignore ParameterCanBeByVal
Sub Foo(ByRef _
arg1 As String)
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(new Mock <ISinks>().Object));

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

            var inspection = new ParameterCanBeByValInspection(parser.State);

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

            Assert.AreEqual(expectedCode, module.Lines());
        }
        public void ParameterCanBeByVal_EVentMember_MultipleParams_OneCanBeByVal_QuickFixWorks()
        {
            //Input
            const string inputCode1 =
                @"Public Event Foo(ByRef a As Integer, ByRef b As Integer)";
            const string inputCode2 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef a As Integer, ByRef b As Integer)
    a = 42
End Sub";
            const string inputCode3 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef a As Integer, ByRef b As Integer)
End Sub";

            //Expected
            const string expectedCode1 =
                @"Public Event Foo(ByRef a As Integer, ByVal b As Integer)";
            const string expectedCode2 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef a As Integer, ByVal b As Integer)
    a = 42
End Sub";
            const string expectedCode3 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef a As Integer, ByVal b As Integer)
End Sub";

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode1)
                          .AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputCode2)
                          .AddComponent("Class3", vbext_ComponentType.vbext_ct_ClassModule, inputCode3)
                          .Build();

            var module1 = project.Object.VBComponents.Item("Class1").CodeModule;
            var module2 = project.Object.VBComponents.Item("Class2").CodeModule;
            var module3 = project.Object.VBComponents.Item("Class3").CodeModule;
            var vbe     = builder.AddProject(project).Build();

            var mockHost = new Mock <IHostApplication>();

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

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

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

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

            Assert.AreEqual(expectedCode1, module1.Lines());
            Assert.AreEqual(expectedCode2, module2.Lines());
            Assert.AreEqual(expectedCode3, module3.Lines());
        }
        public void ParameterCanBeByVal_EventMember_MultipleParams_OneCanBeByVal_QuickFixWorks()
        {
            //Input
            const string inputCode1 =
                @"Public Event Foo(ByRef a As Integer, ByRef b As Integer)";
            const string inputCode2 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef a As Integer, ByRef b As Integer)
    a = 42
End Sub";
            const string inputCode3 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef a As Integer, ByRef b As Integer)
End Sub";

            //Expected
            const string expectedCode1 =
                @"Public Event Foo(ByRef a As Integer, ByVal b As Integer)";
            const string expectedCode2 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef a As Integer, ByVal b As Integer)
    a = 42
End Sub";
            const string expectedCode3 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef a As Integer, ByVal b As Integer)
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("Class2", ComponentType.ClassModule, inputCode2)
                          .AddComponent("Class3", ComponentType.ClassModule, inputCode3)
                          .Build();

            var component1 = project.Object.VBComponents["Class1"];
            var component2 = project.Object.VBComponents["Class2"];
            var component3 = project.Object.VBComponents["Class3"];
            var vbe        = builder.AddProject(project).Build();

            var(state, rewriteManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var inspection     = new ParameterCanBeByValInspection(state);
                var rewriteSession = rewriteManager.CheckOutCodePaneSession();

                new PassParameterByValueQuickFix(state).Fix(inspection.GetInspectionResults(CancellationToken.None).First(), rewriteSession);

                var actualCode1 = rewriteSession.CheckOutModuleRewriter(component1.QualifiedModuleName).GetText();
                var actualCode2 = rewriteSession.CheckOutModuleRewriter(component2.QualifiedModuleName).GetText();
                var actualCode3 = rewriteSession.CheckOutModuleRewriter(component3.QualifiedModuleName).GetText();

                Assert.AreEqual(expectedCode1, actualCode1);
                Assert.AreEqual(expectedCode2, actualCode2);
                Assert.AreEqual(expectedCode3, actualCode3);
            }
        }
예제 #19
0
        public void ParameterCanBeByVal_NoResultForByValObjectInInterfaceImplementationProperty()
        {
            const string modelCode = @"
Option Explicit
Public Foo As Long
Public Bar As String
";

            const string interfaceCode = @"
Option Explicit

Public Property Get Model() As MyModel
End Property

Public Property Set Model(ByVal value As MyModel)
End Property

Public Property Get IsCancelled() As Boolean
End Property

Public Sub Show()
End Sub
";

            const string implementationCode = @"
Option Explicit
Private Type TView
    Model As MyModel
    IsCancelled As Boolean
End Type
Private this As TView
Implements IView

Private Property Get IView_IsCancelled() As Boolean
    IView_IsCancelled = this.IsCancelled
End Property

Private Property Set IView_Model(ByVal value As MyModel)
    Set this.Model = value
End Property

Private Property Get IView_Model() As MyModel
    Set IView_Model = this.Model
End Property

Private Sub IView_Show()
    Me.Show vbModal
End Sub
";

            var builder = new MockVbeBuilder();
            var vbe     = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("IView", ComponentType.ClassModule, interfaceCode)
                          .AddComponent("MyModel", ComponentType.ClassModule, modelCode)
                          .AddComponent("MyForm", ComponentType.UserForm, implementationCode)
                          .MockVbeBuilder().Build();

            var state = MockParser.CreateAndParse(vbe.Object);

            var inspection        = new ParameterCanBeByValInspection(state);
            var inspectionResults = inspection.GetInspectionResults();

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