public void EmptyIfBlock_QuickFixRemovesSingleLineIf()
        {
            const string inputCode =
                @"Sub Foo()
    If True Then Else Bar
End Sub

Sub Bar()
End Sub";

            const string expectedCode =
                @"Sub Foo()
    If Not True Then Bar
End Sub

Sub Bar()
End Sub";

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

            var inspection        = new EmptyIfBlockInspection(state);
            var inspector         = InspectionsHelper.GetInspector(inspection);
            var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

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

            Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
        }
        public void EmptyIfBlock_QuickFixRemovesElse_InvertsIf_ComplexCondition2()
        {
            const string inputCode =
                @"Sub Foo()
    If 1 > 2 And 3 = 3 Or 4 <> 5 And 8 - 6 = 2 Then
    Else
    End If
End Sub";
            const string expectedCode =
                @"Sub Foo()
    If 1 > 2 And 3 = 3 And 4 <> 5 And 8 - 6 = 2 Then
    
    End If
End Sub";

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

            var inspection        = new EmptyIfBlockInspection(state);
            var inspector         = InspectionsHelper.GetInspector(inspection);
            var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

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

            Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
        }
Пример #3
0
        public void InspectionName()
        {
            const string inspectionName = nameof(EmptyIfBlockInspection);
            var          inspection     = new EmptyIfBlockInspection(null);

            Assert.AreEqual(inspectionName, inspection.Name);
        }
Пример #4
0
        public void EmptyIfBlock_FiresOnEmptyIfBlock()
        {
            const string inputCode =
                @"Sub Foo()
    If True Then
    End If
End Sub";

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

            var inspection        = new EmptyIfBlockInspection(state);
            var inspector         = InspectionsHelper.GetInspector(inspection);
            var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

            Assert.AreEqual(1, inspectionResults.Count());
        }
Пример #5
0
        public void EmptyIfBlock_SingleLineIfBlockHasExecutableStatement()
        {
            const string inputCode =
                @"Sub Foo()
    If True Then Bar
End Sub

Sub Bar()
End Sub";

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

            var inspection        = new EmptyIfBlockInspection(state);
            var inspector         = InspectionsHelper.GetInspector(inspection);
            var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

            Assert.IsFalse(inspectionResults.Any());
        }
Пример #6
0
        public void EmptyIfBlock_QuickFixRemovesIf_HasVariable_WithLabel()
        {
            const string inputCode =
                @"Sub Foo()
    If True Then
5       Dim d
a:      Dim e
15 b:   Dim f
    Else
        Dim b
        b = 0
    End If
End Sub";
            const string expectedCode =
                @"Sub Foo()
    
5       Dim d
a:      Dim e
15 b:   Dim f
    If Not True Then

        Dim b
        b = 0
    End If
End Sub";

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

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

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

                var rewrittenCode = state.GetRewriter(component).GetText();
                Assert.AreEqual(expectedCode, rewrittenCode);
            }
        }
        public void InspectionName()
        {
            var inspection = new EmptyIfBlockInspection(null);

            Assert.AreEqual(nameof(EmptyIfBlockInspection), inspection.Name);
        }
Пример #8
0
        public void InspectionType()
        {
            var inspection = new EmptyIfBlockInspection(null);

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