public void FindAllImplementations_SelectedImplementation_ReturnsCorrectNumber()
        {
            const string inputClass =
                @"Implements IClass1

Public Sub IClass1_Foo()
End Sub";

            const string inputInterface =
                @"Public Sub Foo()
End Sub";

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("Class1", vbext_ComponentType.vbext_ct_ClassModule, inputClass)
                          .AddComponent("Class2", vbext_ComponentType.vbext_ct_ClassModule, inputClass)
                          .AddComponent("IClass1", vbext_ComponentType.vbext_ct_ClassModule, inputInterface)
                          .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 vm      = new SearchResultsWindowViewModel();
            var command = new FindAllImplementationsCommand(null, null, parser.State, vbe.Object, vm, null);

            command.Execute(parser.State.AllUserDeclarations.First(s => s.IdentifierName == "IClass1_Foo"));

            Assert.AreEqual(2, vm.Tabs[0].SearchResults.Count);
        }
        public void ImplementInterface_PropertySet()
        {
            //Input
            const string inputCode1 =
                @"Public Property Set Foo(ByRef value As Variant)
End Property";

            const string inputCode2 =
                @"Implements Class1";

            //Expectation
            const string expectedCode =
                @"Implements Class1

Private Property Set Class1_Foo(ByRef value As Variant)
    Err.Raise 5 'TODO implement interface member
End Property
";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("Class2", ComponentType.ClassModule, inputCode2)
                          .Build();
            var vbe       = builder.AddProject(project).Build();
            var component = project.Object.VBComponents[1];

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

            var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), Selection.Home);

            var refactoring = new ImplementInterfaceRefactoring(vbe.Object, state, null);

            refactoring.Refactor(qualifiedSelection);

            var rewriter = state.GetRewriter(component);

            Assert.AreEqual(expectedCode, rewriter.GetText());
        }
示例#3
0
        public void UIRemovesRemovedTestMethods()
        {
            var testMethods = @"'@TestMethod
Public Sub TestMethod1()
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("TestModule1", ComponentType.StandardModule, GetTestModuleInput + testMethods)
                          .AddComponent("TestModule2", ComponentType.StandardModule, GetTestModuleInput + testMethods);

            builder.AddProject(project.Build());

            var vbe    = builder.Build().Object;
            var parser = MockParser.Create(vbe);

            using (var state = parser.State)
            {
                var model = new TestExplorerModel(vbe, state);

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

                Assert.AreEqual(2, model.Tests.Count);

                project.RemoveComponent(project.MockComponents[1]);

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

                Assert.AreEqual(1, model.Tests.Count);
            }
        }
示例#4
0
        public void HungarianNotation_DoesNotReturnResult_Ignored()
        {
            const string inputCode =
                @"Sub Hungarian()
'@Ignore HungarianNotation
    Dim oFoo As Object
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 HungarianNotationInspection(state, UseMeaningfulNameInspectionTests.GetInspectionSettings().Object);
                var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);

                Assert.AreEqual(0, inspectionResults.Count());
            }
        }
示例#5
0
        public void Discovery_DiscoversAnnotatedTestMethodsInGivenTestModule()
        {
            var testMethods = @"'@TestMethod
Public Sub TestMethod1()
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("TestModule1", ComponentType.StandardModule, GetTestModuleInput + testMethods)
                          .AddComponent("TestModule2", ComponentType.StandardModule, GetTestModuleInput + testMethods);

            var vbe = builder.AddProject(project.Build()).Build().Object;

            using (var state = MockParser.CreateAndParse(vbe))
            {
                var component = project.MockComponents.Single(f => f.Object.Name == "TestModule1").Object;
                var tests     = TestDiscovery.GetTests(vbe, component, state).ToList();

                Assert.AreEqual(1, tests.Count);
                Assert.AreEqual("TestModule1", tests.ElementAt(0).Declaration.ComponentName);
            }
        }
示例#6
0
        public void Discovery_IgnoresNonAnnotatedModuleInitInGivenNonTestModule()
        {
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("TestModule1", ComponentType.StandardModule, GetNormalModuleInput.Replace("'@ModuleInitialize", string.Empty));

            var vbe    = builder.AddProject(project.Build()).Build().Object;
            var parser = MockParser.Create(vbe);

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

            var component           = project.MockComponents.Single(f => f.Object.Name == "TestModule1").Object;
            var qualifiedModuleName = new QualifiedModuleName(component);

            var initMethods = qualifiedModuleName.FindModuleInitializeMethods(parser.State);

            Assert.IsFalse(initMethods.Any());
        }
示例#7
0
        public void UISetsProgressBarColor_GoldForInconclusive_IncludesNonFailingAndNonInconclusiveTests()
        {
            var testMethods = @"'@TestMethod
Public Sub TestMethod1()
End Sub

'@TestMethod
Public Sub TestMethod2()
End Sub

'@TestMethod
Public Sub TestMethod3()
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("TestModule1", ComponentType.StandardModule, GetTestModuleInput + testMethods);

            var vbe    = builder.AddProject(project.Build()).Build().Object;
            var parser = MockParser.Create(vbe);

            var model = new TestExplorerModel(vbe, parser.State);

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

            model.Tests[0].Result = new TestResult(TestOutcome.Succeeded);
            model.Tests[1].Result = new TestResult(TestOutcome.Inconclusive);
            model.Tests[2].Result = new TestResult(TestOutcome.Ignored);

            model.AddExecutedTest(model.Tests[0]);
            model.AddExecutedTest(model.Tests[1]);
            model.AddExecutedTest(model.Tests[2]);

            Assert.AreEqual(model.ProgressBarColor, Colors.Gold);
        }
示例#8
0
        public void LExpressionIsProjectAndUnrestrictedNameIsClassModule()
        {
            const string projectName         = "AnyName";
            var          enclosingModuleCode = $"Public WithEvents anything As {projectName}.{BindingTargetName}";

            var builder          = new MockVbeBuilder();
            var enclosingProject = builder
                                   .ProjectBuilder(projectName, ProjectProtection.Unprotected)
                                   .AddComponent(BindingTargetName, ComponentType.ClassModule, enclosingModuleCode)
                                   .Build();

            builder.AddProject(enclosingProject);

            var vbe = builder.Build();

            using (var state = Parse(vbe))
            {
                var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.ClassModule && d.IdentifierName == BindingTargetName);

                Assert.AreEqual(1, declaration.References.Count());
            }
        }
示例#9
0
        public void LExpressionIsProjectAndUnrestrictedNameIsProject()
        {
            var enclosingModuleCode = string.Format("Public WithEvents anything As {0}.{0}", BindingTargetName);

            var builder          = new MockVbeBuilder();
            var enclosingProject = builder
                                   .ProjectBuilder(BindingTargetName, ProjectProtection.Unprotected)
                                   .AddComponent(TestClassName, ComponentType.ClassModule, enclosingModuleCode)
                                   .Build();

            builder.AddProject(enclosingProject);

            var vbe = builder.Build();

            using (var state = Parse(vbe))
            {
                var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.Project && d.ProjectName == BindingTargetName);

                // lExpression adds one reference, the MemberAcecssExpression adds another one.
                Assert.AreEqual(2, declaration.References.Count());
            }
        }
示例#10
0
        public void ImplementInterface_PropertySet_WithParam()
        {
            //Input
            const string inputCode1 =
                @"Public Property Set Foo(a)
End Property";

            const string inputCode2 =
                @"Implements Class1";

            //Expectation
            const string expectedCode =
                @"Implements Class1

Private Property Set Class1_Foo(ByRef a As Variant)
    Err.Raise 5 'TODO implement interface member
End Property
";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("Class2", ComponentType.ClassModule, inputCode2)
                          .Build();
            var vbe       = builder.AddProject(project).Build();
            var component = project.Object.VBComponents[1];

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

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

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
        public void SelfAssignedDeclaration_Ignored_DoesNotReturnResult()
        {
            const string inputCode =
                @"Sub Foo()
    '@Ignore SelfAssignedDeclaration
    Dim b As New Collection
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 SelfAssignedDeclarationInspection(state);
                var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);

                Assert.IsFalse(inspectionResults.Any());
            }
        }
示例#12
0
        public void UndeclaredVariable_ReturnsResult()
        {
            const string inputCode =
                @"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(CancellationToken.None);

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
示例#13
0
        private static RubberduckParserState ArrangeParserAndParse(string inputCode)
        {
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected)
                          .AddComponent("Module1", ComponentType.StandardModule, inputCode)
                          .AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel, 1, 8, true)
                          .Build();

            var vbe = builder.AddProject(project).Build();

            var parser = MockParser.Create(vbe.Object);

            parser.State.AddTestLibrary("Excel.1.8.xml");

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

            return(parser.State);
        }
        public void SelfAssignedDeclaration_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo()
    Dim b As New Collection
End Sub";

            const string expectedCode =
                @"Sub Foo()
'@Ignore SelfAssignedDeclaration
    Dim b As New Collection
End Sub";

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
                          .Build();
            var module = project.Object.VBComponents.Item(0).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 SelfAssignedDeclarationInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

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

            Assert.AreEqual(expectedCode, module.Lines());
        }
        public void FindAllImplementations_SelectedReference_ReturnsCorrectNumber()
        {
            const string inputClass =
                @"Implements IClass1

Public Sub IClass1_Foo()
End Sub

Public Sub Buzz()
    IClass1_Foo
End Sub";

            const string inputInterface =
                @"Public Sub Foo()
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputClass, new Selection(7, 5, 7, 5))
                          .AddComponent("Class2", ComponentType.ClassModule, inputClass)
                          .AddComponent("IClass1", ComponentType.ClassModule, inputInterface)
                          .Build();

            var vbe = builder.AddProject(project).Build();

            vbe.Setup(v => v.ActiveCodePane).Returns(project.Object.VBComponents["Class1"].CodeModule.CodePane);

            var uiDispatcher = new Mock <IUiDispatcher>();

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var vm      = new SearchResultsWindowViewModel();
                var command = new FindAllImplementationsCommand(null, null, state, vbe.Object, vm, null, uiDispatcher.Object);

                command.Execute(null);

                Assert.AreEqual(2, vm.Tabs[0].SearchResults.Count);
            }
        }
        public void UntypedFunctionUsage_QuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo()
    Dim str As String
    str = Left(""test"", 1)
End Sub";

            const string expectedCode =
                @"Sub Foo()
    Dim str As String
    str = Left$(""test"", 1)
End Sub";

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

            var module = project.Object.VBComponents[0].CodeModule;
            var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object));

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

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

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

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

            Assert.AreEqual(expectedCode, module.Content());
        }
示例#17
0
        public void DoesNotImplementInterface_SelectionNotOnImplementsStatement()
        {
            //Input
            const string inputCode1 =
                @"Public Sub Foo()
End Sub";

            const string inputCode2 =
                @"Implements Class1
   
";

            //Expectation
            const string expectedCode =
                @"Implements Class1
   
";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, inputCode1)
                          .AddComponent("Class2", ComponentType.ClassModule, inputCode2)
                          .Build();
            var vbe       = builder.AddProject(project).Build();
            var component = project.Object.VBComponents[1];

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

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

                var actualCode = component.CodeModule.Content();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
        public void RedundantByRefModifier_ReturnsResult_MultipleInterfaceImplementation()
        {
            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";

            const string inputCode3 =
                @"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)
                          .AddComponent("Class2", ComponentType.ClassModule, inputCode3)
                          .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());
            }
        }
        public void ImplicitActiveSheetReference_Ignored_DoesNotReportRange()
        {
            const string inputCode =
                @"Sub foo()
    Dim arr1() As Variant

    '@Ignore ImplicitActiveSheetReference
    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 ParameterCanBeByVal_EventMember_MultipleParams_OneCanBeByVal()
        {
            //Input
            const string inputCode1 =
                @"Public Event Foo(ByRef arg1 As Integer, ByRef arg2 As Integer)";

            const string inputCode2 =
                @"Private WithEvents abc As Class1

Private Sub abc_Foo(ByRef arg1 As Integer, ByRef arg2 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.AreEqual("arg2", inspectionResults.Single().Target.IdentifierName);
        }
        public void WriteOnlyProperty_AddPropertyGetQuickFixWorks_MultipleParams()
        {
            const string inputCode =
                @"Public Property Let Foo(value1, ByVal value2 As Integer, ByRef value3 As Long, value4 As Date, ByVal value5, value6 As String)
End Property";

            const string expectedCode =
                @"Public Property Get Foo(ByRef value1 As Variant, ByVal value2 As Integer, ByRef value3 As Long, ByRef value4 As Date, ByVal value5 As Variant) As String
End Property
Public Property Let Foo(value1, ByVal value2 As Integer, ByRef value3 As Long, value4 As Date, ByVal value5, value6 As String)
End Property";

            //Arrange
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
                          .Build();
            var module = project.Object.VBComponents.Item(0).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 WriteOnlyPropertyInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

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

            Assert.AreEqual(expectedCode, module.Lines());
        }
示例#22
0
        public MockedCodeExplorer(ProjectType projectType,
                                  IReadOnlyList <ComponentType> componentTypes,
                                  IReadOnlyList <string> code = null) : this()
        {
            if (code != null && componentTypes.Count != code.Count)
            {
                Assert.Inconclusive("MockedCodeExplorer Setup Error");
            }

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected, projectType);

            for (var index = 0; index < componentTypes.Count; index++)
            {
                var item = componentTypes[index];
                if (item == ComponentType.UserForm)
                {
                    project.MockUserFormBuilder($"{item}{index}", code is null ? string.Empty : code[index]).AddFormToProjectBuilder();
                }
                else
                {
                    project.AddComponent($"{item}{index}", item, code is null ? string.Empty : code[index]);
                }
            }

            VbComponents       = project.MockVBComponents;
            VbComponent        = project.MockComponents.First();
            VbProject          = project.Build();
            Vbe                = builder.AddProject(VbProject).Build();
            VbeEvents          = MockVbeEvents.CreateMockVbeEvents(Vbe);
            ProjectsRepository = new Mock <IProjectsRepository>();
            ProjectsRepository.Setup(x => x.Project(It.IsAny <string>())).Returns(VbProject.Object);
            ProjectsRepository.Setup(x => x.Component(It.IsAny <QualifiedModuleName>())).Returns(VbComponent.Object);

            SetupViewModelAndParse();

            VbProject.SetupGet(m => m.VBComponents.Count).Returns(componentTypes.Count);
        }
        public void ParameterCanBeByVal_InterfaceMember_SingleParamUsedByRef()
        {
            //Input
            const string inputCode1 =
                @"Public Sub DoSomething(ByRef a As Integer)
End Sub";
            const string inputCode2 =
                @"Implements IClass1

Private Sub IClass1_DoSomething(ByRef a As Integer)
    a = 42
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, 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());
        }
        public void IndexedDefaultMember()
        {
            string callerModule = @"
Public Property Get Ok() As Klasse2
End Property

Public Sub Test()
    Call Ok(1)
End Sub
";

            string defaultMemberClass = @"
Public Property Get Test2(a As Integer)
Attribute Test2.VB_UserMemId = 0
    Test2 = 2
End Property
";

            var builder = new MockVbeBuilder();
            var enclosingProjectBuilder = builder.ProjectBuilder("Any Project", ProjectProtection.Unprotected);

            enclosingProjectBuilder.AddComponent("AnyModule1", ComponentType.StandardModule, callerModule);
            enclosingProjectBuilder.AddComponent("Klasse2", ComponentType.ClassModule, defaultMemberClass);
            var enclosingProject = enclosingProjectBuilder.Build();

            builder.AddProject(enclosingProject);
            var vbe = builder.Build();

            using (var state = Parse(vbe))
            {
                var declaration =
                    state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.PropertyGet &&
                                                     d.IdentifierName == "Test2");

                //One for the assignment in the property itself and one for the default member access.
                Assert.AreEqual(2, declaration.References.Count());
            }
        }
示例#25
0
        public void RecursiveDefaultMember()
        {
            string callerModule = @"
Public Property Get Ok() As Klasse1
End Property

Public Sub Test()
    Call Ok(1)
End Sub
";

            string middleman = @"
Public Property Get Test1() As Klasse2
End Property
";

            string defaultMemberClass = @"
Public Property Get Test2(a As Integer)
    Test2 = 2
End Property
";

            var builder = new MockVbeBuilder();
            var enclosingProjectBuilder = builder.ProjectBuilder("Any Project", ProjectProtection.Unprotected);

            enclosingProjectBuilder.AddComponent("AnyModule1", ComponentType.StandardModule, callerModule);
            enclosingProjectBuilder.AddComponent("AnyClass", ComponentType.ClassModule, middleman);
            enclosingProjectBuilder.AddComponent("AnyClass2", ComponentType.ClassModule, defaultMemberClass);
            var enclosingProject = enclosingProjectBuilder.Build();

            builder.AddProject(enclosingProject);
            var vbe   = builder.Build();
            var state = Parse(vbe);

            var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.PropertyGet && d.IdentifierName == "Test2");

            Assert.AreEqual(1, declaration.References.Count());
        }
示例#26
0
        public void ExtractInterface_CanExecute_ClassWithMembers_SameNameAsClassWithMembers()
        {
            var input =
                @"Sub foo()
End Sub";

            var builder = new MockVbeBuilder();
            var proj1   = builder.ProjectBuilder("TestProj1", ProjectProtection.Unprotected).AddComponent("Comp1", ComponentType.ClassModule, input, Selection.Home).Build();
            var proj2   = builder.ProjectBuilder("TestProj2", ProjectProtection.Unprotected).AddComponent("Comp1", ComponentType.ClassModule, string.Empty).Build();

            var vbe = builder
                      .AddProject(proj1)
                      .AddProject(proj2)
                      .Build();

            vbe.Setup(s => s.ActiveCodePane).Returns(proj1.Object.VBComponents[0].CodeModule.CodePane);

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var extractInterfaceCommand = new RefactorExtractInterfaceCommand(vbe.Object, state, null);
                Assert.IsTrue(extractInterfaceCommand.CanExecute(null));
            }
        }
示例#27
0
        public void Discovery_IgnoresNonAnnotatedTestMethods()
        {
            var testMethods = @"Public Sub TestMethod1()
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none)
                          .AddComponent("TestModule1", vbext_ComponentType.vbext_ct_StdModule, GetTestModuleInput + testMethods);

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

            Assert.IsFalse(UnitTestUtils.GetAllTests(vbe.Object, parser.State).Any());
        }
示例#28
0
        public void EnclosingProcedureComesBeforeEnclosingModule()
        {
            string testCode = string.Format(@"
Public Sub Test()
    Dim {0} As Long
    Dim a As String * {0}
End Sub", BindingTargetName);

            var builder = new MockVbeBuilder();
            var enclosingProjectBuilder = builder.ProjectBuilder(BindingTargetName, ProjectProtection.Unprotected);

            enclosingProjectBuilder.AddComponent(TestClassName, ComponentType.ClassModule, testCode);
            var enclosingProject = enclosingProjectBuilder.Build();

            builder.AddProject(enclosingProject);
            var vbe = builder.Build();

            using (var state = Parse(vbe))
            {
                var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.Variable && d.IdentifierName == BindingTargetName);
                Assert.AreEqual(1, declaration.References.Count());
            }
        }
        public void WriteOnlyProperty_ReturnsResult_LetAndSet()
        {
            const string inputCode =
                @"Property Let Foo(value)
End Property

Property Set Foo(value)
End Property";

            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 WriteOnlyPropertyInspection(state);
                var inspectionResults = inspection.GetInspectionResults();

                Assert.AreEqual(2, inspectionResults.Count());
            }
        }
        public void UndeclaredVariable_ReturnsNoResultIfAnnotated()
        {
            const string inputCode =
                @"Sub Test()
    '@Ignore UndeclaredVariable
    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());
            }
        }