|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using NUnit.Framework; |
| 8 | +using Rubberduck.Inspections.Concrete; |
| 9 | +using Rubberduck.Parsing.VBA; |
| 10 | +using Rubberduck.VBEditor.SafeComWrappers; |
| 11 | +using RubberduckTests.Mocks; |
| 12 | + |
| 13 | +namespace RubberduckTests.Inspections |
| 14 | +{ |
| 15 | + [TestFixture] |
| 16 | + public class ImplicitDefaultMemberAssignmentInspectionTests |
| 17 | + { |
| 18 | + [Test] |
| 19 | + [Ignore("Ignored pending #4390")] |
| 20 | + [Category("Inspections")] |
| 21 | + public void ImplicitDefaultMemberAssignment_ReturnsResult() |
| 22 | + { |
| 23 | + const string inputCode = |
| 24 | +@"Public Sub Foo(bar As Range) |
| 25 | + With bar |
| 26 | + .Cells(1, 1) = 42 |
| 27 | + End With |
| 28 | +End Sub |
| 29 | +"; |
| 30 | + |
| 31 | + var builder = new MockVbeBuilder(); |
| 32 | + var project = builder.ProjectBuilder("TestProject1", "TestProject1", ProjectProtection.Unprotected) |
| 33 | + .AddComponent("Module1", ComponentType.StandardModule, inputCode) |
| 34 | + .AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel, 1, 8, true) |
| 35 | + .Build(); |
| 36 | + var vbe = builder.AddProject(project).Build(); |
| 37 | + |
| 38 | + var parser = MockParser.Create(vbe.Object); |
| 39 | + using (var state = parser.State) |
| 40 | + { |
| 41 | + parser.Parse(new CancellationTokenSource()); |
| 42 | + if (state.Status >= ParserState.Error) |
| 43 | + { |
| 44 | + Assert.Inconclusive("Parser Error"); |
| 45 | + } |
| 46 | + |
| 47 | + var inspection = new ImplicitDefaultMemberAssignmentInspection(state); |
| 48 | + var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); |
| 49 | + |
| 50 | + Assert.AreEqual(1, inspectionResults.Count()); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | + [Category("Inspections")] |
| 56 | + public void ImplicitDefaultMemberAssignment_IgnoredDoesNotReturnResult() |
| 57 | + { |
| 58 | + const string inputCode = |
| 59 | +@"Public Sub Foo(bar As Range) |
| 60 | + With bar |
| 61 | + '@Ignore ImplicitDefaultMemberAssignment |
| 62 | + .Cells(1, 1) = 42 |
| 63 | + End With |
| 64 | +End Sub |
| 65 | +"; |
| 66 | + var builder = new MockVbeBuilder(); |
| 67 | + var project = builder.ProjectBuilder("TestProject1", "TestProject1", ProjectProtection.Unprotected) |
| 68 | + .AddComponent("Module1", ComponentType.StandardModule, inputCode) |
| 69 | + .AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel, 1, 8, true) |
| 70 | + .Build(); |
| 71 | + var vbe = builder.AddProject(project).Build(); |
| 72 | + |
| 73 | + using (var state = MockParser.CreateAndParse(vbe.Object)) |
| 74 | + { |
| 75 | + var inspection = new ImplicitDefaultMemberAssignmentInspection(state); |
| 76 | + var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); |
| 77 | + |
| 78 | + Assert.AreEqual(0, inspectionResults.Count()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + [Test] |
| 83 | + [Category("Inspections")] |
| 84 | + public void ImplicitDefaultMemberAssignment_ExplicitCallDoesNotReturnResult() |
| 85 | + { |
| 86 | + const string inputCode = |
| 87 | +@"Public Sub Foo(bar As Range) |
| 88 | + With bar |
| 89 | + .Cells(1, 1).Value = 42 |
| 90 | + End With |
| 91 | +End Sub |
| 92 | +"; |
| 93 | + var builder = new MockVbeBuilder(); |
| 94 | + var project = builder.ProjectBuilder("TestProject1", "TestProject1", ProjectProtection.Unprotected) |
| 95 | + .AddComponent("Module1", ComponentType.StandardModule, inputCode) |
| 96 | + .AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel, 1, 8, true) |
| 97 | + .Build(); |
| 98 | + var vbe = builder.AddProject(project).Build(); |
| 99 | + |
| 100 | + using (var state = MockParser.CreateAndParse(vbe.Object)) |
| 101 | + { |
| 102 | + var inspection = new ImplicitDefaultMemberAssignmentInspection(state); |
| 103 | + var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); |
| 104 | + |
| 105 | + Assert.AreEqual(0, inspectionResults.Count()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + [Category("Inspections")] |
| 111 | + public void InspectionName() |
| 112 | + { |
| 113 | + const string inspectionName = "ImplicitDefaultMemberAssignmentInspection"; |
| 114 | + var inspection = new ImplicitDefaultMemberAssignmentInspection(null); |
| 115 | + |
| 116 | + Assert.AreEqual(inspectionName, inspection.Name); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments