Skip to content

Commit 49db64c

Browse files
authored
Merge pull request #2102 from Hosch250/obsoleteCommentBugs
Fix obsolete comment inspection bugs
2 parents 83b4634 + 0e571b9 commit 49db64c

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

RetailCoder.VBE/Inspections/ObsoleteCommentSyntaxInspectionResult.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public override void Fix()
4848
return;
4949
}
5050

51-
var content = module.get_Lines(Selection.Selection.StartLine, Selection.Selection.LineCount);
51+
var content = module.Lines[Selection.Selection.StartLine, Selection.Selection.LineCount];
5252

5353
int markerPosition;
5454
if (!content.HasComment(out markerPosition))
@@ -59,7 +59,7 @@ public override void Fix()
5959
var code = string.Empty;
6060
if (markerPosition > 0)
6161
{
62-
code = content.Substring(0, markerPosition - 1);
62+
code = content.Substring(0, markerPosition).TrimEnd();
6363
}
6464

6565
if (_comment.QualifiedSelection.Selection.LineCount > 1)
@@ -89,7 +89,7 @@ public override void Fix()
8989
return;
9090
}
9191

92-
var content = module.get_Lines(Selection.Selection.StartLine, Selection.Selection.LineCount);
92+
var content = module.Lines[Selection.Selection.StartLine, Selection.Selection.LineCount];
9393

9494
int markerPosition;
9595
if (!content.HasComment(out markerPosition))
@@ -100,7 +100,7 @@ public override void Fix()
100100
var code = string.Empty;
101101
if (markerPosition > 0)
102102
{
103-
code = content.Substring(0, markerPosition - 1);
103+
code = content.Substring(0, markerPosition);
104104
}
105105

106106
var newContent = code + Tokens.CommentMarker + " " + _comment.CommentText;

RubberduckTests/Inspections/ObsoleteCommentSyntaxInspectionTests.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ public void ObsoleteCommentSyntax_DoesNotReturnResult()
6161
Assert.AreEqual(0, inspectionResults.Count());
6262
}
6363

64+
[TestMethod]
65+
[TestCategory("Inspections")]
66+
public void ObsoleteCommentSyntax_DoesNotReturnResult_RemInStringLiteral()
67+
{
68+
const string inputCode =
69+
@"Sub Foo()
70+
Dim bar As String
71+
bar = ""iejo rem oernp"" ' test
72+
End Sub";
73+
74+
//Arrange
75+
var builder = new MockVbeBuilder();
76+
VBComponent component;
77+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
78+
var mockHost = new Mock<IHostApplication>();
79+
mockHost.SetupAllProperties();
80+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));
81+
82+
parser.Parse(new CancellationTokenSource());
83+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
84+
85+
var inspection = new ObsoleteCommentSyntaxInspection(parser.State);
86+
var inspectionResults = inspection.GetInspectionResults();
87+
88+
Assert.AreEqual(0, inspectionResults.Count());
89+
}
90+
6491
[TestMethod]
6592
[TestCategory("Inspections")]
6693
public void ObsoleteCommentSyntax_ReturnsMultipleResults()
@@ -198,6 +225,68 @@ public void ObsoleteCommentSyntax_QuickFixWorks_RemoveComment()
198225
Assert.AreEqual(expectedCode, module.Lines());
199226
}
200227

228+
[TestMethod]
229+
[TestCategory("Inspections")]
230+
public void ObsoleteCommentSyntax_QuickFixWorks_UpdateComment_LineHasCode()
231+
{
232+
const string inputCode =
233+
@"Dim Foo As Integer: Rem This is a comment";
234+
235+
const string expectedCode =
236+
@"Dim Foo As Integer: ' This is a comment";
237+
238+
//Arrange
239+
var builder = new MockVbeBuilder();
240+
VBComponent component;
241+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
242+
var project = vbe.Object.VBProjects.Item(0);
243+
var module = project.VBComponents.Item(0).CodeModule;
244+
var mockHost = new Mock<IHostApplication>();
245+
mockHost.SetupAllProperties();
246+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));
247+
248+
parser.Parse(new CancellationTokenSource());
249+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
250+
251+
var inspection = new ObsoleteCommentSyntaxInspection(parser.State);
252+
var inspectionResults = inspection.GetInspectionResults();
253+
254+
inspectionResults.First().QuickFixes.First().Fix();
255+
256+
Assert.AreEqual(expectedCode, module.Lines());
257+
}
258+
259+
[TestMethod]
260+
[TestCategory("Inspections")]
261+
public void ObsoleteCommentSyntax_QuickFixWorks_RemoveComment_LineHasCode()
262+
{
263+
const string inputCode =
264+
@"Dim Foo As Integer: Rem This is a comment";
265+
266+
const string expectedCode =
267+
@"Dim Foo As Integer:";
268+
269+
//Arrange
270+
var builder = new MockVbeBuilder();
271+
VBComponent component;
272+
var vbe = builder.BuildFromSingleStandardModule(inputCode, out component);
273+
var project = vbe.Object.VBProjects.Item(0);
274+
var module = project.VBComponents.Item(0).CodeModule;
275+
var mockHost = new Mock<IHostApplication>();
276+
mockHost.SetupAllProperties();
277+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object, new Mock<ISinks>().Object));
278+
279+
parser.Parse(new CancellationTokenSource());
280+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
281+
282+
var inspection = new ObsoleteCommentSyntaxInspection(parser.State);
283+
var inspectionResults = inspection.GetInspectionResults();
284+
285+
inspectionResults.First().QuickFixes.ElementAt(1).Fix();
286+
287+
Assert.AreEqual(expectedCode, module.Lines());
288+
}
289+
201290
[TestMethod]
202291
[TestCategory("Inspections")]
203292
public void ObsoleteCommentSyntax_IgnoreQuickFixWorks()

0 commit comments

Comments
 (0)