public void RedundantByRefModifier_QuickFixWorks_LineContinuation()
        {
            const string inputCode =
                @"Sub Foo( ByRef bar _
        As Byte)
    bar = 1
End Sub";

            const string expectedCode =
                @"Sub Foo( bar _
        As Byte)
    bar = 1
End Sub";

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new RedundantByRefModifierInspection(state)
                {
                    Severity = CodeInspectionSeverity.Hint
                };
                var inspector         = InspectionsHelper.GetInspector(inspection);
                var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

                new RemoveExplicitByRefModifierQuickFix(state).Fix(inspectionResults.First());

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
        public void InspectionName()
        {
            const string inspectionName = "RedundantByRefModifierInspection";
            var          inspection     = new RedundantByRefModifierInspection(null);

            Assert.AreEqual(inspectionName, inspection.Name);
        }
        public void RedundantByRefModifier_ReturnsResult_InterfaceImplementation()
        {
            const string inputCode1 =
                @"Sub Foo(ByRef arg1 As Integer)
End Sub";

            const string inputCode2 =
                @"Implements IClass1

Sub IClass1_Foo(ByRef arg1 As Integer)
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("IClass1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode2)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new RedundantByRefModifierInspection(state)
                {
                    Severity = CodeInspectionSeverity.Hint
                };
                var inspector         = InspectionsHelper.GetInspector(inspection);
                var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
Exemple #4
0
        public void RedundantByRefModifier_IgnoredQuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo(ByRef arg1 As Integer)
End Sub";

            const string expectedCode =
                @"'@Ignore RedundantByRefModifier
Sub Foo(ByRef arg1 As Integer)
End Sub";

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new RedundantByRefModifierInspection(state)
                {
                    Severity = CodeInspectionSeverity.Hint
                };
                var inspector         = InspectionsHelper.GetInspector(inspection);
                var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

                new IgnoreOnceQuickFix(state, new[] { inspection }).Fix(inspectionResults.First());

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
        public void RedundantByRefModifier_QuickFixWorks_InterfaceImplementationWithMultipleParameters()
        {
            const string inputCode1 =
                @"Sub Foo(ByRef arg1 As Integer, ByRef arg2 as Integer)
End Sub";

            const string inputCode2 =
                @"Implements IClass1

Sub IClass1_Foo(ByRef arg1 As Integer, ByRef arg2 as Integer)
End Sub";

            const string expectedCode1 =
                @"Sub Foo(arg1 As Integer, ByRef arg2 as Integer)
End Sub";

            const string expectedCode2 =
                @"Implements IClass1

Sub IClass1_Foo(arg1 As Integer, ByRef arg2 as Integer)
End Sub";

            var builder = new MockVbeBuilder();
            var vbe     = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("IClass1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode2)
                          .AddProjectToVbeBuilder()
                          .Build();

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new RedundantByRefModifierInspection(state)
                {
                    Severity = CodeInspectionSeverity.Hint
                };
                var inspector         = InspectionsHelper.GetInspector(inspection);
                var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

                new RemoveExplicitByRefModifierQuickFix(state).Fix(
                    inspectionResults.First(
                        result =>
                        ((VBAParser.ArgContext)result.Context).unrestrictedIdentifier()
                        .identifier()
                        .untypedIdentifier()
                        .identifierValue()
                        .GetText() == "arg1"));

                var project                 = vbe.Object.VBProjects[0];
                var interfaceComponent      = project.VBComponents[0];
                var implementationComponent = project.VBComponents[1];

                Assert.AreEqual(expectedCode1, state.GetRewriter(interfaceComponent).GetText(), "Wrong code in interface");
                Assert.AreEqual(expectedCode2, state.GetRewriter(implementationComponent).GetText(), "Wrong code in implementation");
            }
        }
        public void RedundantByRefModifier_ReturnsResult_SomePassedByRef()
        {
            const string inputCode =
                @"Sub Foo(ByVal arg1 As Integer, ByRef arg2 As Date)
End Sub";

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

            var inspection = new RedundantByRefModifierInspection(state)
            {
                Severity = CodeInspectionSeverity.Hint
            };
            var inspector         = InspectionsHelper.GetInspector(inspection);
            var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

            Assert.AreEqual(1, inspectionResults.Count());
        }
        public void RedundantByRefModifier_Ignored_DoesNotReturnResult()
        {
            const string inputCode =
                @"'@Ignore RedundantByRefModifier
Sub Foo(ByRef arg1 As Integer)
End Sub";

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

            var inspection = new RedundantByRefModifierInspection(state)
            {
                Severity = CodeInspectionSeverity.Hint
            };
            var inspector         = InspectionsHelper.GetInspector(inspection);
            var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

            Assert.IsFalse(inspectionResults.Any());
        }
        public void InspectionType()
        {
            var inspection = new RedundantByRefModifierInspection(null);

            Assert.AreEqual(CodeInspectionType.CodeQualityIssues, inspection.InspectionType);
        }