|
3 | 3 | using Rubberduck.Inspections.QuickFixes;
|
4 | 4 | using Rubberduck.Parsing.Inspections.Abstract;
|
5 | 5 | using Rubberduck.Parsing.VBA;
|
| 6 | +using Rubberduck.VBEditor.SafeComWrappers; |
| 7 | +using Rubberduck.VBEditor.SafeComWrappers.Abstract; |
| 8 | +using RubberduckTests.Mocks; |
6 | 9 |
|
7 | 10 | namespace RubberduckTests.QuickFixes
|
8 | 11 | {
|
9 | 12 | [TestFixture]
|
10 | 13 | public class UseSetKeywordForObjectAssignmentQuickFixTests : QuickFixTestBase
|
11 | 14 | {
|
| 15 | + [Test] |
| 16 | + [Category("QuickFixes")] |
| 17 | + public void ObjectVariableNotSet_ReplacesExplicitLetKeyword() |
| 18 | + { |
| 19 | + var inputCode = @" |
| 20 | +Private Sub TextBox1_Change() |
| 21 | + Dim foo As Range |
| 22 | + Set foo = Range(""A1"") |
| 23 | + Let foo.Font = Range(""B1"").Font |
| 24 | +End Sub |
| 25 | +"; |
| 26 | + var expectedCode = @" |
| 27 | +Private Sub TextBox1_Change() |
| 28 | + Dim foo As Range |
| 29 | + Set foo = Range(""A1"") |
| 30 | + Set foo.Font = Range(""B1"").Font |
| 31 | +End Sub |
| 32 | +"; |
| 33 | + var actualCode = ApplyQuickFixToAllInspectionResults(inputCode, state => new ObjectVariableNotSetInspection(state)); |
| 34 | + Assert.AreEqual(expectedCode, actualCode); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + [Category("QuickFixes")] |
| 39 | + public void ObjectVariableNotSet_PlacesKeywordBeforeMemberCall() |
| 40 | + { |
| 41 | + var inputCode = @" |
| 42 | +Private Sub TextBox1_Change() |
| 43 | + Dim foo As Range |
| 44 | + Set foo = Range(""A1"") |
| 45 | + foo.Font = Range(""B1"").Font |
| 46 | +End Sub |
| 47 | +"; |
| 48 | + var expectedCode = @" |
| 49 | +Private Sub TextBox1_Change() |
| 50 | + Dim foo As Range |
| 51 | + Set foo = Range(""A1"") |
| 52 | + Set foo.Font = Range(""B1"").Font |
| 53 | +End Sub |
| 54 | +"; |
| 55 | + var actualCode = ApplyQuickFixToAllInspectionResults(inputCode, state => new ObjectVariableNotSetInspection(state)); |
| 56 | + Assert.AreEqual(expectedCode, actualCode); |
| 57 | + } |
| 58 | + |
12 | 59 | [Test]
|
13 | 60 | [Category("QuickFixes")]
|
14 | 61 | public void ObjectVariableNotSet_ForFunctionAssignment_ReturnsResult()
|
@@ -63,5 +110,19 @@ protected override IQuickFix QuickFix(RubberduckParserState state)
|
63 | 110 | {
|
64 | 111 | return new UseSetKeywordForObjectAssignmentQuickFix();
|
65 | 112 | }
|
| 113 | + |
| 114 | + protected override IVBE TestVbe(string code, out IVBComponent component) |
| 115 | + { |
| 116 | + var builder = new MockVbeBuilder(); |
| 117 | + var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected) |
| 118 | + .AddComponent("Module1", ComponentType.StandardModule, code) |
| 119 | + .AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel, 1, 8, true) |
| 120 | + .Build(); |
| 121 | + |
| 122 | + var vbe = builder.AddProject(project).Build().Object; |
| 123 | + component = project.Object.VBComponents[0]; |
| 124 | + return vbe; |
| 125 | + } |
| 126 | + |
66 | 127 | }
|
67 | 128 | }
|
0 commit comments