Example #1
0
        public void ParameterNotUsed_AmbiguousName_DoesNotReturnResult()
        {
            const string inputCode = @"
Public Property Get Item()
    Item = 12
End Property

Public Property Let Item(ByVal Item As Variant)
    DoSomething Item
End Property

Private Sub DoSomething(ByVal value As Variant)
    Msgbox(value)
End Sub";
            var          vbe       = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);

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

                Assert.IsFalse(inspectionResults.Any());
            }
        }
        public void MoveCloserToUsageRefactoring_Variable_MultipleLines()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo()
    Dim _
    bar _
    As _
    Boolean
    Dim bat As Integer
    bar = True
End Sub";
            var selection = new Selection(4, 6, 4, 8);

            //Expectation
            const string expectedCode =
                @"Private Sub Foo()
    Dim bat As Integer
    Dim bar As Boolean
    bar = True
End Sub";

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

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

                var refactoring = TestRefactoring(rewritingManager, state);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
Example #3
0
        public void EmptyIfBlock_QuickFixRemovesIf_HasVariable_WithComment()
        {
            const string inputCode =
                @"Sub Foo()
    If True Then
        ' comment
        Dim d
    Else
        Dim b
        b = 0
    End If
End Sub";
            const string expectedCode =
                @"Sub Foo()
    Dim d
    If Not True Then
        ' comment
        
        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());

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
Example #4
0
        public void ImplicitByRefModifier_IgnoredQuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo(arg1 As Integer)
End Sub";

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

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection        = new ImplicitByRefModifierInspection(state);
                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 UndeclaredVariable_ReturnsNoResultIfDeclaredModuleScope()
        {
            const string inputCode =
                @"Private a As Long
            
Sub Test()
    a = 42
    Debug.Print a
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected)
                          .AddComponent("MyClass", ComponentType.ClassModule, inputCode)
                          .Build();
            var vbe = builder.AddProject(project).Build();

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

                Assert.IsFalse(inspectionResults.Any());
            }
        }
Example #6
0
        public void RemovesLastConstantInStatement()
        {
            const string expected = @"
Sub DoSomething()
Const foo As String = ""Something""
End Sub
";
            const string content  = @"
Sub DoSomething()
Const foo As String = ""Something"", bar As Integer = 42
End Sub
";
            IVBComponent component;
            var          vbe    = MockVbeBuilder.BuildFromSingleStandardModule(content, out component).Object;
            var          parser = MockParser.Create(vbe);

            parser.Parse(new CancellationTokenSource());
            if (parser.State.Status != ParserState.Ready)
            {
                Assert.Inconclusive("Parser isn't ready. Test cannot proceed.");
            }

            var declarations = parser.State.AllUserDeclarations;
            var target       = declarations.SingleOrDefault(d => d.IdentifierName == "bar" && d.DeclarationType == DeclarationType.Constant);

            if (target == null)
            {
                Assert.Inconclusive("No 'bar' constant was found in test code.");
            }

            var rewriter = parser.State.GetRewriter(target);

            rewriter.Remove(target);

            Assert.AreEqual(expected, rewriter.GetText());
        }
        public void ImplicitActiveWorkbookReference_ReportsWorksheets()
        {
            const string inputCode =
                @"
Sub foo()
    Dim sheet As Worksheet
    Set sheet = Worksheets(""Sheet1"")
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", "TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode)
                          .AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel, 1, 8, true)
                          .Build();
            var vbe = builder.AddProject(project).Build();

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

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
Example #8
0
        public void UnassignedVariable_WithFollowingCommentLine_DoesNotRemoveCommentLine()
        {
            const string inputCode =
                @"Sub Foo()
Dim var1 As String
' Comment
End Sub";

            const string expectedCode =
                @"Sub Foo()
' Comment
End Sub";

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection = new VariableNotUsedInspection(state);
                new RemoveUnusedDeclarationQuickFix(state).Fix(inspection.GetInspectionResults().First());

                var rewriter = state.GetRewriter(component);
                Assert.AreEqual(expectedCode, rewriter.GetText());
            }
        }
Example #9
0
        public void ImplicitPublicMember_QuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo(ByVal arg1 as Integer)
'Just an inoffensive little comment

End Sub";

            const string expectedCode =
                @"Public Sub Foo(ByVal arg1 as Integer)
'Just an inoffensive little comment

End Sub";

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

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

            inspectionResults.First().QuickFixes.First().Fix();
            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
        public void VariableAnnotationsOnOrAbovePreviousVariableDoNotGetScopedToVariable()
        {
            const string inputCode =
                @"'@Obsolete
Private fooBar As Variant _
'@Obsolete

'SomeComment
Public foo As Long

Public Function Bar() As Variant
End Function";
            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var fooDeclaration = state.DeclarationFinder.UserDeclarations(DeclarationType.Variable).Single(decl => decl.IdentifierName == "foo");

                var expectedAnnotationCount = 0;
                var actualAnnotationCount   = fooDeclaration.Annotations.Count();

                Assert.AreEqual(expectedAnnotationCount, actualAnnotationCount);
            }
        }
Example #11
0
        public void NonReturningFunction_ReturnsResult_InterfaceImplementation_NoQuickFix()
        {
            //Input
            const string inputCode1 =
                @"Function Foo() As Boolean
End Function";
            const string inputCode2 =
                @"Implements IClass1

Function IClass1_Foo() As Boolean
End Function";

            //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)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            var mockHost = new Mock <IHostApplication>();

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

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

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

            Assert.AreEqual(0, inspectionResults.First().QuickFixes.Count());
        }
        public void AllModuleAnnotationsAreModuleAnnotationsIfThereIsNoBody()
        {
            const string inputCode =
                @"
'@TestModule
'@Folder(""Test"")
'SomeComment
'@Enumerator 17, _
12 _
@DefaultMember
";
            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var moduleDeclaration =
                    state.DeclarationFinder.UserDeclarations(DeclarationType.ProceduralModule).Single();

                var expectedAnnotationCount = 2;
                var actualAnnotationCount   = moduleDeclaration.Annotations.Count();

                Assert.AreEqual(expectedAnnotationCount, actualAnnotationCount);
            }
        }
        public void IntroduceParameterRefactoring_PassInTarget_Nonvariable()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo()
    Dim bar As Boolean
End Sub";

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

            var messageBox = new Mock <IMessageBox>();

            messageBox.Setup(m => m.Show(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButtons>(), It.IsAny <MessageBoxIcon>()))
            .Returns(DialogResult.OK);

            var refactoring = new IntroduceParameterRefactoring(vbe.Object, state, messageBox.Object);

            try
            {
                refactoring.Refactor(state.AllUserDeclarations.First(d => d.DeclarationType != DeclarationType.Variable));
            }
            catch (ArgumentException e)
            {
                messageBox.Verify(m =>
                                  m.Show(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButtons>(),
                                         It.IsAny <MessageBoxIcon>()), Times.Once);

                Assert.AreEqual("target", e.ParamName);
                Assert.AreEqual(inputCode, component.CodeModule.Content());
                return;
            }

            Assert.Fail();
        }
        public void OptionBaseZeroStatement_QuickFixWorks_RemoveStatement_InstructionSeparator()
        {
            string inputCode = "Option Explicit: Option Base 0: Option Base 1";

            string expectedCode = "Option Explicit: : Option Base 1";

            var settings = new Mock <IGeneralConfigService>();
            var config   = GetTestConfig();

            settings.Setup(x => x.LoadConfiguration()).Returns(config);

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

            var inspection = new OptionBaseZeroInspection(state);
            var inspector  = new Inspector(settings.Object, new IInspection[] { inspection });

            var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result;

            inspectionResults.First().QuickFixes.First().Fix();

            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
Example #15
0
        public void ObsoleteCommentSyntax_DoesNotReturnResult()
        {
            const string inputCode = @"' test";

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe      = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         mockHost = new Mock <IHostApplication>();

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

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

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

            Assert.AreEqual(0, inspectionResults.Count());
        }
Example #16
0
        public void ResultDisablesFix()
        {
            const string inputCode =
                @"Public Sub Foo()
    Const const1 As Integer = 9
End Sub";

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

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var inspection        = new ConstantNotUsedInspection(state);
                var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);

                var failureNotifier  = new Mock <IQuickFixFailureNotifier>().Object;
                var quickFixProvider = new QuickFixProvider(rewritingManager, failureNotifier, new IQuickFix[] { new RemoveUnusedDeclarationQuickFix() });

                var result = inspectionResults.First();
                result.DisabledQuickFixes.Add(nameof(RemoveUnusedDeclarationQuickFix));

                Assert.AreEqual(0, quickFixProvider.QuickFixes(result).Count());
            }
        }
Example #17
0
        public void ObsoleteTypeHint_QuickFixWorks_Field_SingleTypeHint()
        {
            const string inputCode =
                @"Public Foo!";

            const string expectedCode =
                @"Public Foo As Single";

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

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

                var fix = new RemoveTypeHintsQuickFix(state);
                foreach (var result in inspectionResults)
                {
                    fix.Fix(result);
                }

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
Example #18
0
        public void ObsoleteCommentSyntax_QuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo()
    Error 91
End Sub";

            const string expectedCode =
                @"Sub Foo()
    Err.Raise 91
End Sub";

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

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

                new ReplaceObsoleteErrorStatementQuickFix(state).Fix(inspectionResults.First());
                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
Example #19
0
        public void MemberAttributeAnnotationReturnsReturnsCorrectAttribute(string expectedAttribute, string expectedAttributeValues, string annotationName, string annotationValue = null)
        {
            var code = $@"
'@{annotationName} {annotationValue}
Public Function Foo()
End Function";

            var vbe = MockVbeBuilder.BuildFromSingleModule(code, "Class1", ComponentType.ClassModule, out _).Object;

            using (var state = MockParser.CreateAndParse(vbe))
            {
                var memberDeclaration = state.DeclarationFinder.UserDeclarations(DeclarationType.Function).Single();
                var memberAnnotations = memberDeclaration.Annotations
                                        .Select(pta => (pta.Annotation, pta.AnnotationArguments));
                var(annotation, annotationArguments) = memberAnnotations.Single(tpl => tpl.Annotation.Name.Equals(annotationName));

                var actualAttribute            = ((IAttributeAnnotation)annotation).Attribute(annotationArguments);
                var actualAttributeValues      = ((IAttributeAnnotation)annotation).AnnotationToAttributeValues(annotationArguments);
                var actualAttributesValuesText = string.Join(", ", actualAttributeValues);

                Assert.AreEqual(expectedAttribute, actualAttribute);
                Assert.AreEqual(expectedAttributeValues, actualAttributesValuesText);
            }
        }
        public void ImplicitActiveSheetReference_ReportsRange()
        {
            const string inputCode =
                @"Sub foo()
    Dim arr1() As Variant
    arr1 = Range(""A1:B2"")
End Sub
";

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", "TestProject1", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
                          .AddReference("Excel", "C:\\Program Files\\Microsoft Office\\Root\\Office 16\\EXCEL.EXE", true)
                          .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));

            GetExcelRangeDeclarations().ForEach(d => parser.State.AddDeclaration(d));

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

            var inspection        = new ImplicitActiveSheetReferenceInspection(vbe.Object, parser.State);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(1, inspectionResults.Count());
        }
        public void IntroduceFieldRefactoring_MultipleVariablesInStatement_MoveLast()
        {
            //Input
            const string inputCode =
                @"Private Sub Foo(ByVal buz As Integer, _
ByRef baz As Date)
Dim bar As Boolean, _
bat As Date, _
bap As Integer
End Sub";
            var selection = new Selection(5, 10, 5, 13);

            //Expectation
            const string expectedCode =
                @"Private bap As Integer
Private Sub Foo(ByVal buz As Integer, _
ByRef baz As Date)
Dim bar As Boolean, _
bat As Date
End Sub";

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

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

                var refactoring = TestRefactoring(rewritingManager, state);
                refactoring.Refactor(qualifiedSelection);

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
Example #22
0
            public void shouldReturnAnLeastNextMethod()
            {
                #region inputCode
                var inputCode = @"
Option Explicit
Private Sub Foo()
    Dim x As Integer
    x = 1 + 2
End Sub
Private Sub NewMethod
    dim a as string
    Debug.Print a
End Sub
Private Sub NewMethod1
    dim a as string
    Debug.Print a
End Sub
Private Sub NewMethod4
    dim a as string
    Debug.Print a
End Sub";
                #endregion inputCode

                QualifiedModuleName qualifiedModuleName;
                var state        = MockParser.ParseString(inputCode, out qualifiedModuleName);
                var declarations = state.AllDeclarations;

                var SUT = new ExtractedMethod();

                var expected = "NewMethod2";
                //Act
                var actual = SUT.getNewMethodName(declarations);

                //Assert
                Assert.AreEqual(expected, actual);
            }
Example #23
0
        public void AssignedByValParameter_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Public Sub Foo(ByVal arg1 As String)
    Let arg1 = ""test""
End Sub";

            const string expectedCode =
                @"'@Ignore AssignedByValParameter
Public Sub Foo(ByVal arg1 As String)
    Let arg1 = ""test""
End Sub";

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

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

                new IgnoreOnceQuickFix(state, new[] { inspection }).Fix(inspectionResults.First());
                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
Example #24
0
        public void AnnotationsRightAboveFirstMemberAreNotModuleAnnotations_WithoutDeclarationOnTop()
        {
            const string inputCode =
                @"'@TestMethod
'@Enumerator 17, _
12 _
@DefaultMember
Public Sub Foo()
End Sub

Public Function Bar() As Variant
End Function";
            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var moduleDeclaration = state.DeclarationFinder.UserDeclarations(DeclarationType.ProceduralModule).Single();

                var expectedAnnotationCount = 0;
                var actualAnnotationCount   = moduleDeclaration.Annotations.Count();

                Assert.AreEqual(expectedAnnotationCount, actualAnnotationCount);
            }
        }
Example #25
0
        public void EmptyIfBlock_IfAndElseIfBlockHaveExecutableStatement()
        {
            const string inputCode =
                @"Sub Foo()
    If True Then
        Dim d
        d = 0
    ElseIf False Then
        Dim b
        b = 0
    End If
End Sub";

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

            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;

                Assert.IsFalse(inspectionResults.Any());
            }
        }
Example #26
0
        public void ParameterNotUsed_ReturnsResult_InterfaceImplementation()
        {
            //Input
            const string inputCode1 =
                @"Public Sub DoSomething(ByVal a As Integer)
End Sub";
            const string inputCode2 =
                @"Implements IClass1

Private Sub IClass1_DoSomething(ByVal a 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)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            var mockHost = new Mock <IHostApplication>();

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

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

            var inspection        = new ParameterNotUsedInspection(vbe.Object, parser.State, null);
            var inspectionResults = inspection.GetInspectionResults().ToList();

            Assert.AreEqual(1, inspectionResults.Count);
        }
        public void IntroduceFieldRefactoring_PassInTarget_Nonvariable()
        {
            //Input
            const string inputCode =
                @"Private bar As Boolean
Private Sub Foo()
    bar = True
End Sub";

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

            var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object);
            using (state)
            {
                var messageBox = new Mock <IMessageBox>();

                var refactoring = TestRefactoring(rewritingManager, state, messageBox.Object);
                refactoring.Refactor(state.AllUserDeclarations.First(d => d.DeclarationType != DeclarationType.Variable));
                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(inputCode, actualCode);

                messageBox.Verify(m => m.NotifyWarn(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
            }
        }
Example #28
0
        public void EmptyStringLiteral_QuickFixWorks()
        {
            const string inputCode =
                @"Public Sub Foo(ByRef arg1 As String)
    arg1 = """"
End Sub";

            const string expectedCode =
                @"Public Sub Foo(ByRef arg1 As String)
    arg1 = vbNullString
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());

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

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

            inspectionResults.First().QuickFixes.First().Fix();

            Assert.AreEqual(expectedCode, module.Lines());
        }
Example #29
0
        public void ModuleAnnotationOnTopMostMember_NoResult()
        {
            const string inputCode = @"
Option Explicit
Option Private Module

'@TestModule
Public Sub Test1()
End Sub

Public Sub Test2()
End Sub
";
            var          vbe       = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);

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

                Assert.IsFalse(inspectionResults.Any());
            }
        }
        public void FindAllImplementations_CanExecute_NullActiveCodePane()
        {
            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe = builder.BuildFromSingleStandardModule(string.Empty, out component);

            vbe.Setup(s => s.ActiveCodePane).Returns(value: null);
            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 vm      = new SearchResultsWindowViewModel();
            var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null);

            Assert.IsFalse(command.CanExecute(null));
        }