|
| 1 | +using System.Linq; |
| 2 | +using System.Threading; |
| 3 | +using Microsoft.Vbe.Interop; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | +using Moq; |
| 6 | +using Rubberduck.Inspections; |
| 7 | +using Rubberduck.Parsing; |
| 8 | +using Rubberduck.Parsing.VBA; |
| 9 | +using Rubberduck.VBEditor.Extensions; |
| 10 | +using Rubberduck.VBEditor.VBEHost; |
| 11 | +using RubberduckTests.Mocks; |
| 12 | + |
| 13 | +namespace RubberduckTests.Inspections |
| 14 | +{ |
| 15 | + [TestClass] |
| 16 | + public class UnassignedVariableUsageInspectionTests |
| 17 | + { |
| 18 | + [TestMethod] |
| 19 | + [TestCategory("Inspections")] |
| 20 | + public void UnassignedVariableUsage_ReturnsResult() |
| 21 | + { |
| 22 | + const string inputCode = |
| 23 | +@"Sub Foo() |
| 24 | + Dim b As Boolean |
| 25 | + Dim bb As Boolean |
| 26 | + bb = b |
| 27 | +End Sub"; |
| 28 | + |
| 29 | + //Arrange |
| 30 | + var builder = new MockVbeBuilder(); |
| 31 | + var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none) |
| 32 | + .AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode) |
| 33 | + .Build(); |
| 34 | + var vbe = builder.AddProject(project).Build(); |
| 35 | + |
| 36 | + var mockHost = new Mock<IHostApplication>(); |
| 37 | + mockHost.SetupAllProperties(); |
| 38 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 39 | + |
| 40 | + parser.Parse(new CancellationTokenSource()); |
| 41 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 42 | + |
| 43 | + var inspection = new UnassignedVariableUsageInspection(parser.State); |
| 44 | + var inspectionResults = inspection.GetInspectionResults(); |
| 45 | + |
| 46 | + Assert.AreEqual(1, inspectionResults.Count()); |
| 47 | + } |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + [TestCategory("Inspections")] |
| 51 | + public void UnassignedVariableUsage_DoesNotReturnResult() |
| 52 | + { |
| 53 | + const string inputCode = |
| 54 | +@"Sub Foo() |
| 55 | + Dim b As Boolean |
| 56 | + Dim bb As Boolean |
| 57 | + b = True |
| 58 | + bb = b |
| 59 | +End Sub"; |
| 60 | + |
| 61 | + //Arrange |
| 62 | + var builder = new MockVbeBuilder(); |
| 63 | + var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none) |
| 64 | + .AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode) |
| 65 | + .Build(); |
| 66 | + var vbe = builder.AddProject(project).Build(); |
| 67 | + |
| 68 | + var mockHost = new Mock<IHostApplication>(); |
| 69 | + mockHost.SetupAllProperties(); |
| 70 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 71 | + |
| 72 | + parser.Parse(new CancellationTokenSource()); |
| 73 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 74 | + |
| 75 | + var inspection = new UnassignedVariableUsageInspection(parser.State); |
| 76 | + var inspectionResults = inspection.GetInspectionResults(); |
| 77 | + |
| 78 | + Assert.IsFalse(inspectionResults.Any()); |
| 79 | + } |
| 80 | + |
| 81 | + [TestMethod] |
| 82 | + [TestCategory("Inspections")] |
| 83 | + public void UnassignedVariableUsage_QuickFixWorks() |
| 84 | + { |
| 85 | + const string inputCode = |
| 86 | +@"Sub Foo() |
| 87 | + Dim b As Boolean |
| 88 | + Dim bb As Boolean |
| 89 | + bb = b |
| 90 | +End Sub"; |
| 91 | + |
| 92 | + const string expectedCode = |
| 93 | +@"Sub Foo() |
| 94 | + Dim b As Boolean |
| 95 | + Dim bb As Boolean |
| 96 | + TODOTODO = TODO |
| 97 | +End Sub"; |
| 98 | + |
| 99 | + //Arrange |
| 100 | + var builder = new MockVbeBuilder(); |
| 101 | + VBComponent component; |
| 102 | + var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); |
| 103 | + var project = vbe.Object.VBProjects.Item(0); |
| 104 | + var module = project.VBComponents.Item(0).CodeModule; |
| 105 | + var mockHost = new Mock<IHostApplication>(); |
| 106 | + mockHost.SetupAllProperties(); |
| 107 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 108 | + |
| 109 | + parser.Parse(new CancellationTokenSource()); |
| 110 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 111 | + |
| 112 | + var inspection = new UnassignedVariableUsageInspection(parser.State); |
| 113 | + var inspectionResults = inspection.GetInspectionResults(); |
| 114 | + |
| 115 | + inspectionResults.First().QuickFixes.First().Fix(); |
| 116 | + |
| 117 | + Assert.AreEqual(expectedCode, module.Lines()); |
| 118 | + } |
| 119 | + |
| 120 | + [TestMethod] |
| 121 | + [TestCategory("Inspections")] |
| 122 | + public void InspectionType() |
| 123 | + { |
| 124 | + var inspection = new UnassignedVariableUsageInspection(null); |
| 125 | + Assert.AreEqual(CodeInspectionType.CodeQualityIssues, inspection.InspectionType); |
| 126 | + } |
| 127 | + |
| 128 | + [TestMethod] |
| 129 | + [TestCategory("Inspections")] |
| 130 | + public void InspectionName() |
| 131 | + { |
| 132 | + const string inspectionName = "UnassignedVariableUsageInspection"; |
| 133 | + var inspection = new UnassignedVariableUsageInspection(null); |
| 134 | + |
| 135 | + Assert.AreEqual(inspectionName, inspection.Name); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments