|
| 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.VBEHost; |
| 10 | +using RubberduckTests.Mocks; |
| 11 | + |
| 12 | +namespace RubberduckTests.Inspections |
| 13 | +{ |
| 14 | + [TestClass] |
| 15 | + public class SelfAssignedDeclarationInspectionTests |
| 16 | + { |
| 17 | + [TestMethod] |
| 18 | + [TestCategory("Inspections")] |
| 19 | + public void SelfAssignedDeclaration_ReturnsResult() |
| 20 | + { |
| 21 | + const string inputCode = |
| 22 | +@"Sub Foo() |
| 23 | + Dim b As New Collection |
| 24 | +End Sub"; |
| 25 | + |
| 26 | + //Arrange |
| 27 | + var builder = new MockVbeBuilder(); |
| 28 | + var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none) |
| 29 | + .AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode) |
| 30 | + .Build(); |
| 31 | + var vbe = builder.AddProject(project).Build(); |
| 32 | + |
| 33 | + var mockHost = new Mock<IHostApplication>(); |
| 34 | + mockHost.SetupAllProperties(); |
| 35 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 36 | + |
| 37 | + parser.Parse(new CancellationTokenSource()); |
| 38 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 39 | + |
| 40 | + var inspection = new SelfAssignedDeclarationInspection(parser.State); |
| 41 | + var inspectionResults = inspection.GetInspectionResults(); |
| 42 | + |
| 43 | + Assert.AreEqual(1, inspectionResults.Count()); |
| 44 | + } |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + [TestCategory("Inspections")] |
| 48 | + public void SelfAssignedDeclaration_DoesNotReturnResult() |
| 49 | + { |
| 50 | + const string inputCode = |
| 51 | +@"Sub Foo() |
| 52 | + Dim b As Collection |
| 53 | +End Sub"; |
| 54 | + |
| 55 | + //Arrange |
| 56 | + var builder = new MockVbeBuilder(); |
| 57 | + var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none) |
| 58 | + .AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode) |
| 59 | + .Build(); |
| 60 | + var vbe = builder.AddProject(project).Build(); |
| 61 | + |
| 62 | + var mockHost = new Mock<IHostApplication>(); |
| 63 | + mockHost.SetupAllProperties(); |
| 64 | + var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object)); |
| 65 | + |
| 66 | + parser.Parse(new CancellationTokenSource()); |
| 67 | + if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } |
| 68 | + |
| 69 | + var inspection = new SelfAssignedDeclarationInspection(parser.State); |
| 70 | + var inspectionResults = inspection.GetInspectionResults(); |
| 71 | + |
| 72 | + Assert.IsFalse(inspectionResults.Any()); |
| 73 | + } |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + [TestCategory("Inspections")] |
| 77 | + public void InspectionType() |
| 78 | + { |
| 79 | + var inspection = new SelfAssignedDeclarationInspection(null); |
| 80 | + Assert.AreEqual(CodeInspectionType.CodeQualityIssues, inspection.InspectionType); |
| 81 | + } |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + [TestCategory("Inspections")] |
| 85 | + public void InspectionName() |
| 86 | + { |
| 87 | + const string inspectionName = "SelfAssignedDeclarationInspection"; |
| 88 | + var inspection = new SelfAssignedDeclarationInspection(null); |
| 89 | + |
| 90 | + Assert.AreEqual(inspectionName, inspection.Name); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments