コード例 #1
0
        public void MultipleDeclarations_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Public Sub Foo()
    Dim var1 As Integer, var2 As String
End Sub";

            const string expectedCode =
                @"Public Sub Foo()
'@Ignore MultipleDeclarations
    Dim var1 As Integer, var2 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 MultipleDeclarationsInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

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

            Assert.AreEqual(expectedCode, module.Lines());
        }
コード例 #2
0
        public void ReturnsResultForExpressionOnLeftHandSide()
        {
            const string code    = @"
Public Sub DoSomething()
    [A1] = 42
End Sub
";
            var          builder = new MockVbeBuilder();
            var          project = builder.ProjectBuilder("TestProject", ProjectProtection.Unprotected)
                                   .AddComponent("Module1", ComponentType.StandardModule, code)
                                   .AddReference("VBA", MockVbeBuilder.LibraryPathVBA, 4, 2, true)
                                   .AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel, 1, 7, true)
                                   .Build();
            var vbe      = builder.AddProject(project).Build();
            var mockHost = new Mock <IHostApplication>();

            mockHost.SetupGet(m => m.ApplicationName).Returns("Excel");
            vbe.Setup(m => m.HostApplication()).Returns(() => mockHost.Object);

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

            using (var state = parser.State)
            {
                state.AddTestLibrary("VBA.4.2.xml");
                state.AddTestLibrary("Excel.1.8.xml");
                parser.Parse(new CancellationTokenSource());
                if (state.Status >= ParserState.Error)
                {
                    Assert.Inconclusive("Parser Error");
                }

                var inspection        = new HostSpecificExpressionInspection(state);
                var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
コード例 #3
0
        public void UnassignedVariable_VariableOnMultipleLines_QuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo()
    Dim _
    var1 _
    as _
    Integer
End Sub";

            const string expectedCode =
                @"Sub Foo()
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 VariableNotAssignedInspection(parser.State);

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

            Assert.AreEqual(expectedCode, module.Lines());
        }
コード例 #4
0
        public void UnassignedVariableUsage_ReturnsSingleResult_MultipleReferences()
        {
            const string inputCode =
                @"Sub tester()
    Dim myarr() As Variant
    Dim i As Long

    ReDim myarr(1 To 10)

    For i = LBound(myarr) To UBound(myarr)
    Next

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 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 UnassignedVariableUsageInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(1, inspectionResults.Count());
        }
コード例 #5
0
        public void RemovesLocalConstantDeclarationStatement()
        {
            const string expected = @"
Sub DoSomething()
End Sub
";
            const string content  = @"
Sub DoSomething()
Const foo As String = ""Something""
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.DeclarationType == DeclarationType.Constant);

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

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

            rewriter.Remove(target);

            var rewrittenCode = rewriter.GetText();

            Assert.AreEqual(expected, rewrittenCode);
        }
コード例 #6
0
        public void EmptyStringLiteral_ReturnsResult_PassToProcedure()
        {
            const string inputCode =
                @"Public Sub Bar()
    Foo """"
End Sub

Public Sub Foo(ByRef arg1 As String)
End Sub";

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

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

            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 EmptyStringLiteralInspection(null);
            var inspector  = new Inspector(settings.Object, new IInspection[] { inspection });

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

            Assert.AreEqual(1, inspectionResults.Count());
        }
コード例 #7
0
        public void AddsTestModule()
        {
            var expected = @"
Option Explicit
Option Private Module

'@TestModule
Private Assert As New Rubberduck.AssertClass

";

            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe      = builder.BuildFromSingleStandardModule("", out component);
            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 settings = new Mock <ConfigurationLoader>(null, null, null, null, null, null);
            var config   = GetUnitTestConfig();

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

            var addTestModuleCommand = new AddTestModuleCommand(vbe.Object, parser.State, settings.Object);

            addTestModuleCommand.Execute(null);

            // mock suite auto-assigns "TestModule1" to the first component when we create the mock
            Assert.AreEqual(expected, vbe.Object.VBProjects.Item(0).VBComponents.Item("TestModule2").CodeModule.Lines());
        }
コード例 #8
0
        public void ParameterCanBeByVal_InterfaceMember_SingleByValParam()
        {
            //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)
                          .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());
        }
コード例 #9
0
        public void TestEngine_ExposesTestMethod_AndRaisesRefresh()
        {
            var testMethods = @"'@TestMethod
Public Sub TestMethod1()
End Sub";

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

            var vbe             = builder.Build().Object;
            var parser          = MockParser.Create(vbe);
            var interaction     = new Mock <IVBEInteraction>();
            var wrapperProvider = new Mock <ITypeLibWrapperProvider>();
            var fakesFactory    = new Mock <IFakesFactory>();
            var dispatcher      = new Mock <IUiDispatcher>();

            dispatcher.Setup(d => d.InvokeAsync(It.IsAny <Action>()))
            .Callback((Action action) => action.Invoke())
            .Verifiable();

            using (var state = parser.State)
            {
                var engine    = new TestEngine(state, fakesFactory.Object, interaction.Object, wrapperProvider.Object, dispatcher.Object, vbe);
                int refreshes = 0;
                engine.TestsRefreshed += (sender, args) => refreshes++;
                parser.Parse(new CancellationTokenSource());
                if (!engine.CanRun)
                {
                    Assert.Inconclusive("Parser Error");
                }

                Assert.AreEqual(1, engine.Tests.Count());
                Assert.AreEqual(1, refreshes);
            }
        }
コード例 #10
0
        public void ParameterCanBeByVal_EventMember_SingleParam()
        {
            //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)
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(1, inspectionResults.Count());
        }
コード例 #11
0
        public void ObsoleteLetStatement_ReturnsResult()
        {
            const string inputCode =
                @"Public Sub Foo()
    Dim var1 As Integer
    Dim var2 As Integer
    
    Let var2 = var1
End Sub";

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

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

            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(new Mock <ISinks>().Object));

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

            var inspection = new ObsoleteLetStatementInspection(parser.State);
            var inspector  = new Inspector(settings.Object, new IInspection[] { inspection });

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

            Assert.AreEqual(1, inspectionResults.Count());
        }
コード例 #12
0
        public void UseMeaningfulName_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Sub Ffffff()
End Sub";

            const string expectedCode =
                @"'@Ignore UseMeaningfulName
Sub Ffffff()
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 UseMeaningfulNameInspection(null, parser.State, GetInspectionSettings().Object);
            var inspectionResults = inspection.GetInspectionResults();

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

            Assert.AreEqual(expectedCode, module.Lines());
        }
コード例 #13
0
        public void AddNoIndentAnnotation_ModuleContainsCode()
        {
            var input =
                @"Option Explicit
Public Foo As Boolean

Sub Foo()
End Sub";

            var expected =
                @"'@NoIndent
Option Explicit
Public Foo As Boolean

Sub Foo()
End Sub";

            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe      = builder.BuildFromSingleStandardModule(input, out component);
            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 noIndentAnnotationCommand = new NoIndentAnnotationCommand(vbe.Object, parser.State);

            noIndentAnnotationCommand.Execute(null);

            Assert.AreEqual(expected, component.CodeModule.Lines());
        }
コード例 #14
0
        public void UIRemovesRemovedTestMethods()
        {
            var testMethods = @"'@TestMethod
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)
                          .AddComponent("TestModule2", 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));

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

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

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

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

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

            Assert.AreEqual(1, model.Tests.Count);
        }
コード例 #15
0
        public void FindAllImplementations_SingleResult_Navigates()
        {
            const string inputClass =
                @"Implements IClass1

Public Sub 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)
                          .AddComponent("IClass1", ComponentType.ClassModule, inputInterface)
                          .Build();

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

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

            var navigateCommand = new Mock <INavigateCommand>();

            var vm      = new SearchResultsWindowViewModel();
            var command = new FindAllImplementationsCommand(navigateCommand.Object, null, parser.State, vbe.Object, vm, null);

            command.Execute(parser.State.AllUserDeclarations.Single(s => s.IdentifierName == "Foo"));

            navigateCommand.Verify(n => n.Execute(It.IsAny <object>()), Times.Once);
        }
コード例 #16
0
        public void Discovery_IgnoresAnnotatedTestMethodsNotInTestModule()
        {
            var testMethods = @"'@TestMethod
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, GetNormalModuleInput + 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());
        }
コード例 #17
0
        public void ImplicitActiveSheetReference_Ignored_DoesNotReportRange()
        {
            const string inputCode =
                @"Sub foo()
    Dim arr1() As Variant

    '@Ignore ImplicitActiveSheetReference
    arr1 = Range(""A1:B2"")
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();

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

            using (var state = parser.State)
            {
                state.AddTestLibrary("Excel.1.8.xml");

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

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

                Assert.AreEqual(0, inspectionResults.Count());
            }
        }
コード例 #18
0
        public void UntypedFunctionUsage_Ignored_DoesNotReturnResult()
        {
            const string inputCode =
                @"Sub Foo()
    Dim str As String

    '@Ignore UntypedFunctionUsage
    str = Left(""test"", 1)
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)
                          .AddReference("VBA", "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA7.1\\VBE7.DLL", 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));

            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();

            Assert.IsFalse(inspectionResults.Any());
        }
コード例 #19
0
        public void UISetsProgressBarColor_LimeGreenForSuccess_IncludesIgnoredTests()
        {
            var testMethods = @"'@TestMethod
Public Sub TestMethod1()
End Sub

'@TestMethod
Public Sub TestMethod2()
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));

            var model = new TestExplorerModel(vbe.Object, 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.Ignored);

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

            Assert.AreEqual(model.ProgressBarColor, Colors.LimeGreen);
        }
コード例 #20
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, new RubberduckParserState(vbe));

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

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

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

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

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

            Assert.AreEqual(1, model.Tests.Count);
        }
コード例 #21
0
        public void OptionBaseZeroSpecified_DoesNotReturnResult()
        {
            const string inputCode = @"Option Base 0";

            //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 OptionBaseInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(0, inspectionResults.Count());
        }
コード例 #22
0
        private static string Parse(string code, string filename)
        {
            IVBComponent component;
            var          vbe = MockVbeBuilder.BuildFromSingleStandardModule(code, out component);

            var state  = new RubberduckParserState(vbe.Object, new DeclarationFinderFactory());
            var parser = MockParser.Create(vbe.Object, state);

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

            var tree       = state.GetParseTree(new QualifiedModuleName(component));
            var parsed     = tree.GetText();
            var withoutEOF = parsed;

            while (withoutEOF.Length >= 5 && String.Equals(withoutEOF.Substring(withoutEOF.Length - 5, 5), "<EOF>"))
            {
                withoutEOF = withoutEOF.Substring(0, withoutEOF.Length - 5);
            }
            return(withoutEOF);
        }
コード例 #23
0
        public void ImplicitByRefParameter_ReturnsResult_InterfaceImplementation()
        {
            //Input
            const string inputCode1 =
                @"Sub Foo(arg1 As Integer)
End Sub";
            const string inputCode2 =
                @"Implements IClass1

Sub IClass1_Foo(arg1 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 ImplicitByRefParameterInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(1, inspectionResults.Count());
        }
コード例 #24
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(new Mock <ISinks>().Object));

            parser.Parse(new CancellationTokenSource());
            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());
        }
コード例 #25
0
        public void AssignedByValParameter_QuickFixWorks()
        {
            const string inputCode =
                @"Public Sub Foo(ByVal arg1 As String)
    Let arg1 = ""test""
End Sub";

            const string expectedCode =
                @"Public Sub Foo(ByRef arg1 As String)
    Let arg1 = ""test""
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 AssignedByValParameterInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

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

            Assert.AreEqual(expectedCode, module.Lines());
        }
コード例 #26
0
        private void AssertInputCodeYieldsExpectedInspectionResultCount(string inputCode, int expected, params string[] testLibraries)
        {
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", "TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Module1", ComponentType.StandardModule, inputCode)
                          .AddReference("VBA", MockVbeBuilder.LibraryPathVBA, 4, 2, true)
                          .AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel, 1, 8, true)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            using (var coordinator = MockParser.Create(vbe.Object))
            {
                foreach (var testLibrary in testLibraries)
                {
                    coordinator.State.AddTestLibrary(testLibrary);
                }
                coordinator.Parse(new CancellationTokenSource());

                var inspection        = new ObjectVariableNotSetInspection(coordinator.State);
                var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);

                Assert.AreEqual(expected, inspectionResults.Count());
            }
        }
コード例 #27
0
        public void RemoveRemovesComment()
        {
            const string inputCode =
                @"Dim d As Variant  ' bug should be Integer";

            const string expected =
                @"Dim d As Variant  ";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("Module1", ComponentType.StandardModule, inputCode)
                          .Build();

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

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

            using (var state = parser.State)
            {
                var cs = GetConfigService(new[] { "TODO", "NOTE", "BUG" });
                var vm = new ToDoExplorerViewModel(state, cs, null);

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

                vm.SelectedItem = vm.Items.Single();
                vm.RemoveCommand.Execute(null);

                var module = project.Object.VBComponents[0].CodeModule;
                Assert.AreEqual(expected, module.Content());
                Assert.IsFalse(vm.Items.Any());
            }
        }
コード例 #28
0
        public void NotAlreadySpecified_ReturnsResult()
        {
            const string inputCode = @"";

            //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(new Mock <ISinks>().Object));

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

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

            Assert.AreEqual(1, inspectionResults.Count());
        }
コード例 #29
0
        public void ProcedureShouldBeFunction_ReturnsResult_MultipleSubs()
        {
            const string inputCode =
                @"Private Sub Foo(ByRef foo As Boolean)
End Sub

Private Sub Goo(ByRef foo As Integer)
End Sub";

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

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

            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(new Mock <ISinks>().Object));

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

            var inspection = new ProcedureCanBeWrittenAsFunctionInspection(parser.State);
            var inspector  = new Inspector(settings.Object, new IInspection[] { inspection });

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

            Assert.AreEqual(2, inspectionResults.Count());
        }
コード例 #30
0
        public void ProcedureNotUsed_DoesNotReturnResult_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 ProcedureNotUsedInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

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