Example #1
0
        public void HungarianNotation_DoesNotReturnResult_WhenWhitelisted()
        {
            const string inputCode =
                @"Sub Feherlista()
    Dim oRange 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 UseMeaningfulNameInspection(state, UseMeaningfulNameInspectionTests.GetInspectionSettings().Object);
                var inspectionResults = inspection.GetInspectionResults(CancellationToken.None);

                Assert.IsFalse(inspectionResults.Any());
            }
        }
Example #2
0
        public void EnclosingProjectComesBeforeOtherModuleInEnclosingProject()
        {
            var builder = new MockVbeBuilder();
            var enclosingProjectBuilder = builder.ProjectBuilder(BINDING_TARGET_NAME, ProjectProtection.Unprotected);

            enclosingProjectBuilder.AddComponent(TEST_CLASS_NAME, ComponentType.ClassModule, "Public WithEvents anything As  " + BINDING_TARGET_NAME);
            enclosingProjectBuilder.AddComponent("AnyModule", ComponentType.StandardModule, CreateEnumType(BINDING_TARGET_NAME));
            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.Project && d.IdentifierName == BINDING_TARGET_NAME);

                Assert.AreEqual(state.Status, ParserState.Ready);
                Assert.AreEqual(1, declaration.References.Count());
            }
        }
Example #3
0
        public void EnclosingProjectComesBeforeOtherModuleInEnclosingProject()
        {
            var builder = new MockVbeBuilder();
            var enclosingProjectBuilder = builder.ProjectBuilder(BindingTargetName, ProjectProtection.Unprotected);

            enclosingProjectBuilder.AddComponent(TestClassName, ComponentType.ClassModule, CreateTestProcedure(BindingTargetName));
            enclosingProjectBuilder.AddComponent("AnyModule", ComponentType.StandardModule, CreateEnumType(BindingTargetName));
            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.Project && d.IdentifierName == BindingTargetName);

                Assert.AreEqual(state.Status, ParserState.Ready);
                Assert.AreEqual(1, declaration.References.Count());
            }
        }
Example #4
0
        public void HungarianNotation_DoesNotReturnsResult_ThreeLetterVariable()
        {
            const string inputCode =
                @"Sub InExcelSomewhere()
    Dim col As Long
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.IsFalse(inspectionResults.Any());
            }
        }
Example #5
0
        public void LExpressionIsModuleAndUnrestrictedNameIsType()
        {
            var          builder   = new MockVbeBuilder();
            const string className = "AnyName";

            var enclosingProject = builder
                                   .ProjectBuilder("AnyProjectName", ProjectProtection.Unprotected)
                                   .AddComponent(TestClassName, ComponentType.ClassModule, string.Format("Public WithEvents anything As {0}.{1}", className, BindingTargetName))
                                   .AddComponent(className, ComponentType.ClassModule, CreateUdt(BindingTargetName))
                                   .Build();

            builder.AddProject(enclosingProject);

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

            var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.UserDefinedType && d.IdentifierName == BindingTargetName);

            Assert.AreEqual(1, declaration.References.Count());
        }
Example #6
0
        public void WriteOnlyProperty_DoesNotReturnsResult_Get()
        {
            const string inputCode =
                @"Property Get Foo()
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(CancellationToken.None);

                Assert.IsFalse(inspectionResults.Any());
            }
        }
Example #7
0
        public void LExpressionIsVariablePropertyOrFunction()
        {
            var    builder = new MockVbeBuilder();
            var    enclosingProjectBuilder = builder.ProjectBuilder("Any Project", ProjectProtection.Unprotected);
            string code = string.Format("Public Sub Test() {0} Dim {1} As {2} {0} Call {1}.{3} {0}End Sub", Environment.NewLine, "AnyName", BINDING_TARGET_LEXPRESSION, BINDING_TARGET_UNRESTRICTEDNAME);

            enclosingProjectBuilder.AddComponent(TEST_CLASS_NAME, ComponentType.ClassModule, code);
            enclosingProjectBuilder.AddComponent(BINDING_TARGET_LEXPRESSION, ComponentType.ClassModule, CreateFunction(BINDING_TARGET_UNRESTRICTEDNAME));
            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.Function && d.IdentifierName == BINDING_TARGET_UNRESTRICTEDNAME);

                Assert.AreEqual(1, declaration.References.Count());
            }
        }
        [Ignore] // not sure how to handle GetBuiltInDeclarations
        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 component = project.Object.VBComponents[0];
            var parser    = MockParser.Create(vbe.Object);

            // FIXME reinstate and unignore tests
            // refers to "UntypedFunctionUsageInspectionTests.GetBuiltInDeclarations()"
            //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();

            new UntypedFunctionUsageQuickFix(parser.State).Fix(inspectionResults.First());

            Assert.AreEqual(expectedCode, parser.State.GetRewriter(component).GetText());
        }
Example #9
0
        public void BooleanAssignedInIfElseInspection_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Sub Foo()
    Dim d As Boolean
    If True Then
        d = True
    Else
        d = False
    EndIf
End Sub";

            const string expectedCode =
                @"Sub Foo()
    Dim d As Boolean
'@Ignore BooleanAssignedInIfElse
    If True Then
        d = True
    Else
        d = False
    EndIf
End Sub";

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

            using (var state = MockParser.CreateAndParse(vbe.Object))
            {
                var inspection        = new BooleanAssignedInIfElseInspection(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 ApplicationWorksheetFunction_UseExplicitlyQuickFixWorks()
        {
            const string inputCode =
                @"Sub ExcelSub()
    Dim foo As Double
    foo = Application.Pi
End Sub
";

            const string expectedCode =
                @"Sub ExcelSub()
    Dim foo As Double
    foo = Application.WorksheetFunction.Pi
End Sub
";

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

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

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

                new ApplicationWorksheetFunctionQuickFix(state).Fix(inspectionResults.First());
                Assert.AreEqual(expectedCode, state.GetRewriter(project.Object.VBComponents.First()).GetText());
            }
        }
        public void ImplicitActiveSheetReference_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Sub foo()
    Dim arr1() As Variant
    arr1 = Range(""A1:B2"")
End Sub";

            const string expectedCode =
                @"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 module = project.Object.VBComponents[0].CodeModule;
            var vbe    = builder.AddProject(project).Build();


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

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

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

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

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

            Assert.AreEqual(expectedCode, module.Content());
        }
Example #12
0
        private static IVBE BuildProject(string projectName, List <RenameTestModuleDefinition> testComponents, int activeIndex, bool useLibraries = false)
        {
            var builder = new MockVbeBuilder();
            var enclosingProjectBuilder = builder.ProjectBuilder(projectName, ProjectProtection.Unprotected);

            if (useLibraries)
            {
                enclosingProjectBuilder.AddReference("VBA", MockVbeBuilder.LibraryPathVBA, 4, 1, true);
                enclosingProjectBuilder.AddReference("EXCEL", MockVbeBuilder.LibraryPathMsExcel, 1, 8, true);
            }

            foreach (var comp in testComponents)
            {
                if (comp.ModuleType == ComponentType.UserForm)
                {
                    var form = enclosingProjectBuilder.MockUserFormBuilder(comp.ModuleName, comp.Input);
                    if (!comp.ControlNames.Any())
                    {
                        Assert.Inconclusive("Test incorporates a UserForm but does not define any controls");
                    }
                    foreach (var control in comp.ControlNames)
                    {
                        form.AddControl(control);
                    }
                    (var component, var codeModule) = form.Build();
                    enclosingProjectBuilder.AddComponent(component, codeModule);
                }
                else
                {
                    var selection = comp.RenameSelection.HasValue ? comp.RenameSelection.Value : default;
                    enclosingProjectBuilder.AddComponent(comp.ModuleName, comp.ModuleType, comp.Input, selection);
                }
            }
            var project = enclosingProjectBuilder.Build();

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

            vbe.SetupGet(v => v.ActiveCodePane).Returns(project.Object.VBComponents[activeIndex].CodeModule.CodePane);
            return(vbe.Object);
        }
        public void ApplicationWorksheetFunction_IgnoreQuickFixWorks()
        {
            const string inputCode =
                @"Sub ExcelSub()
    Dim foo As Double
    foo = Application.Pi
End Sub";

            const string expectedCode =
                @"Sub ExcelSub()
    Dim foo As Double
'@Ignore ApplicationWorksheetFunction
    foo = Application.Pi
End Sub";

            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 component = vbe.Object.SelectedVBComponent;

            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");
            }

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

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

            Assert.AreEqual(expectedCode, parser.State.GetRewriter(component).GetText());
        }
        public void MemberNotOnInterface_CatchesInvalidUseOfMember()
        {
            const string userForm1Code = @"
Private mfooBar As String

Public Property Let FooBar(value As String)
    mfooBar = value
End Property

Public Property Get FooBar() As String
    FooBar = mfooBar
End Property
";

            const string analyzedCode = @"Option Explicit

Sub FizzBuzz()

    Dim bar As UserForm1
    Set bar = New UserForm1
    bar.FooBar = ""FooBar""

    Dim foo As UserForm
    Set foo = New UserForm1
    foo.FooBar = ""BarFoo""

End Sub
";

            var vbeBuilder     = new MockVbeBuilder();
            var projectBuilder = vbeBuilder.ProjectBuilder("testproject", ProjectProtection.Unprotected);

            projectBuilder.MockUserFormBuilder("UserForm1", userForm1Code).AddFormToProjectBuilder()
            .AddComponent("ReferencingModule", ComponentType.StandardModule, analyzedCode)
            .AddReference("MSForms", MockVbeBuilder.LibraryPathMsForms, 2, 0, true);

            vbeBuilder.AddProject(projectBuilder.Build());
            var vbe = vbeBuilder.Build();

            Assert.IsTrue(InspectionResults(vbe.Object).Any());
        }
        public void ImplementInterface_PublicVariant(string inputCode1)
        {
            const string inputCode2 =
                @"Implements Class1";

            //Expectation
            const string expectedCode =
                @"Implements Class1

Private Property Get Class1_Foo() As Variant
    Err.Raise 5 'TODO implement interface member
End Property

Private Property Let Class1_Foo(ByVal rhs As Variant)
    Err.Raise 5 'TODO implement interface member
End Property

Private Property Set Class1_Foo(ByVal rhs 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];

            using (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 actualCode = state.GetRewriter(component).GetText();
                Assert.AreEqual(expectedCode, actualCode);
            }
        }
Example #16
0
        public void HungarianNotation_ReturnsResult_ForClass()
        {
            const string inputCode =
                @"Sub Test()
    Debug.Print ""Ez egy objektum""
End Sub";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected)
                          .AddComponent("clsMagyar", 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();

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
Example #17
0
        public void HungarianNotation_ReturnsResult_VariableWithOneLetterPrefix()
        {
            const string inputCode =
                @"Sub Hungarian()
    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();

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
        public void SelfAssignedDeclaration_DoesNotReturnResult()
        {
            const string inputCode =
                @"Sub Foo()
    Dim b As 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();

                Assert.IsFalse(inspectionResults.Any());
            }
        }
Example #19
0
        public void EnclosingProcedureComesBeforeEnclosingModule()
        {
            string testCode = string.Format(@"
Public Sub Test()
    Dim {0} As Long
    Dim a As String * {0}
End Sub", BINDING_TARGET_NAME);

            var builder = new MockVbeBuilder();
            var enclosingProjectBuilder = builder.ProjectBuilder(BINDING_TARGET_NAME, vbext_ProjectProtection.vbext_pp_none);

            enclosingProjectBuilder.AddComponent(TEST_CLASS_NAME, vbext_ComponentType.vbext_ct_ClassModule, testCode);
            var enclosingProject = enclosingProjectBuilder.Build();

            builder.AddProject(enclosingProject);
            var vbe         = builder.Build();
            var state       = Parse(vbe);
            var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.Variable && d.IdentifierName == BINDING_TARGET_NAME);

            Assert.AreEqual(1, declaration.References.Count());
        }
        public void OptionBaseOnePartiallySpecified_ReturnsResults()
        {
            const string inputCode1 = @"";
            const string inputCode2 = @"Option Base 1";

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

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

                Assert.AreEqual(1, inspectionResults.Count());
            }
        }
Example #21
0
        public void Discovery_DiscoversAnnotatedModuleCleanupInGivenTestModule()
        {
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected)
                          .AddComponent("TestModule1", ComponentType.StandardModule, GetTestModuleInput)
                          .AddComponent("TestModule2", ComponentType.StandardModule, GetTestModuleInput);

            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 qualifiedModuleName = new QualifiedModuleName(component);

                var initMethods = TestDiscovery.FindModuleCleanupMethods(qualifiedModuleName, state).ToList();

                Assert.AreEqual(1, initMethods.Count);
                Assert.AreEqual("TestModule1", initMethods.ElementAt(0).QualifiedName.QualifiedModuleName.ComponentName);
                Assert.AreEqual("ModuleCleanup", initMethods.ElementAt(0).QualifiedName.MemberName);
            }
        }
Example #22
0
        public void Discovery_IgnoresAnnotatedTestMethodsNotInTestModule()
        {
            var testMethods = @"'@TestMethod
Public Sub TestMethod1()
End Sub";

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

            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");
            }

            Assert.IsFalse(UnitTestUtils.GetAllTests(vbe, parser.State).Any());
        }
        public void LExpressionIsEnum()
        {
            const string PROJECT_NAME            = "AnyProject";
            var          builder                 = new MockVbeBuilder();
            var          enclosingProjectBuilder = builder.ProjectBuilder(PROJECT_NAME, ProjectProtection.Unprotected);
            string       code = string.Format("Public Sub Test() {0} a = {1}.{2} {0}End Sub", Environment.NewLine, BINDING_TARGET_LEXPRESSION, BINDING_TARGET_UNRESTRICTEDNAME);

            enclosingProjectBuilder.AddComponent(TEST_CLASS_NAME, ComponentType.ClassModule, code);
            enclosingProjectBuilder.AddComponent("AnyModule", ComponentType.StandardModule, CreateEnumType(BINDING_TARGET_LEXPRESSION, BINDING_TARGET_UNRESTRICTEDNAME));
            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.EnumerationMember && d.IdentifierName == BINDING_TARGET_UNRESTRICTEDNAME);

                Assert.AreEqual(1, declaration.References.Count());
            }
        }
Example #24
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 tests = project.MockComponents.Single(f => f.Object.Name == "TestModule1").Object.GetTests(vbe, state).ToList();

                Assert.AreEqual(1, tests.Count);
                Assert.AreEqual("TestModule1", tests.ElementAt(0).Declaration.ComponentName);
            }
        }
Example #25
0
        public void DefaultProjectName_DoesNotReturnResult()
        {
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("TestProject", ProjectProtection.Unprotected)
                          .AddComponent("Class1", ComponentType.ClassModule, string.Empty)
                          .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 inspection        = new DefaultProjectNameInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

            Assert.AreEqual(0, inspectionResults.Count());
        }
Example #26
0
        public void HungarianNotation_DoesNotReturnsResult_AllLowerCase()
        {
            const string inputCode =
                @"Sub NoHungarianHere()
    Dim strong As Variant
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();

                Assert.IsFalse(inspectionResults.Any());
            }
        }
Example #27
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();
            var state       = Parse(vbe);
            var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.Variable && d.IdentifierName == BindingTargetName);

            Assert.AreEqual(1, declaration.References.Count());
        }
Example #28
0
        public void ProceduralModuleWithAccessibleMember()
        {
            var    builder = new MockVbeBuilder();
            var    enclosingProjectBuilder = builder.ProjectBuilder(BINDING_TARGET_NAME, ProjectProtection.Unprotected);
            string code = CreateCaller(TEST_CLASS_NAME) + Environment.NewLine + CreateCallee();

            enclosingProjectBuilder.AddComponent(TEST_CLASS_NAME, ComponentType.StandardModule, code);
            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.ProceduralModule && d.IdentifierName == TEST_CLASS_NAME);
                Assert.AreEqual(1, declaration.References.Count(), "Procedural Module should have reference");

                declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.Function && d.IdentifierName == BINDING_TARGET_NAME);
                Assert.AreEqual(1, declaration.References.Count(), "Function should have reference");
            }
        }
        private void AssertVbaFragmentYieldsExpectedInspectionResultCount(string inputCode, int expectedCount)
        {
            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected)
                          .AddComponent("MyClass", ComponentType.ClassModule, inputCode)
                          .Build();
            var vbe = builder.AddProject(project).Build();

            var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.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();

            Assert.AreEqual(expectedCount, inspectionResults.Count());
        }
Example #30
0
        public void HungarianNotation_DoesNotReturnResult_LibraryFunction()
        {
            const string inputCode =
                @"
Private Declare Sub chkVoid Lib ""somelib.dll"" Alias ""chkVoidA"" (number As Long)
";

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