public void ObjectVariableNotSet_GivenSetObjectVariable_ReturnsNoResult()
        {
            const string inputCode = @"
Private Sub Workbook_Open()
    
    Dim target As Range
    Set target = Range(""A1"")
    
    target.Value = ""All good""

End Sub";

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

            Assert.AreEqual(0, inspectionResults.Count());
        }
        private void AssertInputCodeYieldsExpectedInspectionResultCount(string inputCode, int expected, params string[] testLibraries)
        {
            var builder        = new MockVbeBuilder();
            var projectBuilder = builder.ProjectBuilder("TestProject1", "TestProject1", ProjectProtection.Unprotected)
                                 .AddComponent("Module1", ComponentType.StandardModule, inputCode);

            foreach (var testLibrary in testLibraries)
            {
                var libraryDescriptionComponents = testLibrary.Split('.');
                var libraryName  = libraryDescriptionComponents[0];
                var libraryPath  = MockVbeBuilder.LibraryPaths[libraryName];
                int majorVersion = Int32.Parse(libraryDescriptionComponents[1]);
                int minorVersion = Int32.Parse(libraryDescriptionComponents[2]);
                projectBuilder.AddReference(libraryName, libraryPath, majorVersion, minorVersion, true);
            }

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

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

                Assert.AreEqual(expected, inspectionResults.Count());
            }
        }
Exemplo n.º 3
0
        public void ObjectVariableNotSet_FunctionReturnsArrayOfType_ReturnsNoResult()
        {
            var expectedResultCount = 0;
            var input =
                @"
Private Function GetSomeDictionaries() As Dictionary()
    Dim temp(0 To 1) As Worksheet
    Set temp(0) = New Dictionary
    Set temp(1) = New Dictionary
    GetSomeDictionaries = temp
End Function";

            var builder = new MockVbeBuilder();
            var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected)
                          .AddComponent("Codez", ComponentType.StandardModule, input)
                          .AddReference("Scripting", "", 1, 0, true)
                          .Build();

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

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

            parser.State.AddTestLibrary("Scripting.1.0.xml");

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

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

            Assert.AreEqual(expectedResultCount, inspectionResults.Count());
        }
        public void ObjectVariableNotSet_ForFunctionAssignment_ReturnsResult()
        {
            const string inputCode = @"
Private Function CombineRanges(ByVal source As Range, ByVal toCombine As Range) As Range
    If source Is Nothing Then
        CombineRanges = toCombine 'no inspection result (but there should be one!)
    Else
        CombineRanges = Union(source, toCombine) 'no inspection result (but there should be one!)
    End If
End Function";

            const string expectedCode = @"
Private Function CombineRanges(ByVal source As Range, ByVal toCombine As Range) As Range
    If source Is Nothing Then
        Set CombineRanges = toCombine 'no inspection result (but there should be one!)
    Else
        Set CombineRanges = Union(source, toCombine) 'no inspection result (but there should be one!)
    End If
End Function";

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

            var inspection = new ObjectVariableNotSetInspection(state);
            var inspectionResults = inspection.GetInspectionResults().ToList();

            Assert.AreEqual(2, inspectionResults.Count);
            foreach (var fix in inspectionResults.SelectMany(result => result.QuickFixes.Where(s => s is UseSetKeywordForObjectAssignmentQuickFix)))
            {
                fix.Fix();
            }
            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
        public void InspectionName()
        {
            const string inspectionName = "ObjectVariableNotSetInspection";
            var          inspection     = new ObjectVariableNotSetInspection(null);

            Assert.AreEqual(inspectionName, inspection.Name);
        }
        public void ObjectVariableNotSet_ForPropertyGetAssignment_ReturnsResults()
        {
            const string inputCode = @"
Private example As MyObject
Public Property Get Example() As MyObject
    Example = example
End Property
";
            const string expectedCode = @"
Private example As MyObject
Public Property Get Example() As MyObject
    Set Example = example
End Property
";
            IVBComponent component;
            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
            var state = MockParser.CreateAndParse(vbe.Object);

            var inspection = new ObjectVariableNotSetInspection(state);
            var inspectionResults = inspection.GetInspectionResults().ToList();

            Assert.AreEqual(1, inspectionResults.Count);
            foreach (var fix in inspectionResults.SelectMany(result => result.QuickFixes.Where(s => s is UseSetKeywordForObjectAssignmentQuickFix)))
            {
                fix.Fix();
            }
            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
        public void ObjectVariableNotSet_ForPropertyGetAssignment_ReturnsResults()
        {
            var expectedResultCount = 1;
            var input        = @"
Private example As MyObject
Public Property Get Example() As MyObject
    Example = example
End Property
";
            var expectedCode =
                @"
Private example As MyObject
Public Property Get Example() As MyObject
    Set Example = example
End Property
";

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

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

                Assert.AreEqual(expectedResultCount, inspectionResults.Count);
                var fix = new UseSetKeywordForObjectAssignmentQuickFix(state);
                foreach (var result in inspectionResults)
                {
                    fix.Fix(result);
                }

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
        public void ObjectVariableNotSet_IgnoreQuickFixWorks()
        {
            const string inputCode = @"
Private Sub Workbook_Open()
    
    Dim target As Range
    target = Range(""A1"")
    
    target.Value = ""forgot something?""

End Sub";

            const string expectedCode = @"
Private Sub Workbook_Open()
    
    Dim target As Range
'@Ignore ObjectVariableNotSet
    target = Range(""A1"")
    
    target.Value = ""forgot something?""

End Sub";

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

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

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

            Assert.AreEqual(expectedCode, component.CodeModule.Content());
        }
Exemplo n.º 9
0
        public void ObjectVariableNotSet_IgnoreQuickFixWorks()
        {
            var inputCode =
                @"
Private Sub DoSomething()
    
    Dim target As Object
    target = New Object
    
    target.Value = ""forgot something?""

End Sub";
            var expectedCode =
                @"
Private Sub DoSomething()
    
    Dim target As Object
'@Ignore ObjectVariableNotSet
    target = New Object
    
    target.Value = ""forgot something?""

End Sub";

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

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

                new IgnoreOnceQuickFix(state, new[] { inspection }).Fix(inspectionResults.First());
                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
Exemplo n.º 10
0
        private void AssertInputCodeYieldsExpectedInspectionResultCount(string inputCode, int expected)
        {
            var vbe   = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _);
            var state = MockParser.CreateAndParse(vbe.Object);

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

            Assert.AreEqual(expected, inspectionResults.Count());
        }
        public void ObjectVariableNotSet_LongPtrVariable_ReturnsNoResult()
        {
            const string inputCode = @"
Private Sub TestLongPtr()
    Dim handle as LongPtr
    handle = 123456
End Sub";

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

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

            Assert.AreEqual(0, inspectionResults.Count());
        }
        public void ObjectVariableNotSet_IgnoreQuickFixWorks()
        {
            const string inputCode = @"
Private Sub Workbook_Open()
    
    Dim target As Range
    target = Range(""A1"")
    
    target.Value = ""forgot something?""

End Sub";

            const string expectedCode = @"
Private Sub Workbook_Open()
    
    Dim target As Range
'@Ignore ObjectVariableNotSet
    target = Range(""A1"")
    
    target.Value = ""forgot something?""

End Sub";

            //Arrange
            var         builder = new MockVbeBuilder();
            VBComponent component;
            var         vbe      = builder.BuildFromSingleStandardModule(inputCode, out component);
            var         module   = vbe.Object.VBProjects.Item(0).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 ObjectVariableNotSetInspection(parser.State);
            var inspectionResults = inspection.GetInspectionResults();

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

            Assert.AreEqual(expectedCode, module.Lines());
        }
        public void ObjectVariableNotSet_GivenIndexerObjectAccess_ReturnsNoResult()
        {
            const string inputCode = @"
Private Sub DoSomething()
    Dim target As Object
    Set target = CreateObject(""Scripting.Dictionary"")
    target(""foo"") = 42
End Sub
";
            IVBComponent component;
            var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component);
            var state = MockParser.CreateAndParse(vbe.Object);

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

            Assert.AreEqual(0, inspectionResults.Count());
        }
        public void ObjectVariableNotSet_GivenObjectVariableNotSet_ReturnsResult()
        {
            const string inputCode = @"
Private Sub Workbook_Open()
    
    Dim target As Range
    target = Range(""A1"")
    
    target.Value = ""forgot something?""

End Sub";

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

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

            Assert.AreEqual(1, inspectionResults.Count());
        }
        public void ObjectVariableNotSet_ForFunctionAssignment_ReturnsResult()
        {
            var expectedResultCount = 2;
            var input =
                @"
Private Function CombineRanges(ByVal source As Range, ByVal toCombine As Range) As Range
    If source Is Nothing Then
        CombineRanges = toCombine 'no inspection result (but there should be one!)
    Else
        CombineRanges = Union(source, toCombine) 'no inspection result (but there should be one!)
    End If
End Function";
            var expectedCode =
                @"
Private Function CombineRanges(ByVal source As Range, ByVal toCombine As Range) As Range
    If source Is Nothing Then
        Set CombineRanges = toCombine 'no inspection result (but there should be one!)
    Else
        Set CombineRanges = Union(source, toCombine) 'no inspection result (but there should be one!)
    End If
End Function";

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

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

                Assert.AreEqual(expectedResultCount, inspectionResults.Count);
                var fix = new UseSetKeywordForObjectAssignmentQuickFix(state);
                foreach (var result in inspectionResults)
                {
                    fix.Fix(result);
                }

                Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText());
            }
        }
        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());
            }
        }