public void ToMarker_EndPositionExceedsSnapshotLength() { // Defensive: handle the issue end position being beyond the end of the snapshot // (we have not seen this in practice so far) // Arrange var issue = new Sonarlint.Issue { StartLine = 53, StartLineOffset = 2, EndLine = 53, EndLineOffset = 12, }; // The issue is on a single line in this case, but the issue end position // is beyond the end of the line. var vsLine = new VSLineDescription { ZeroBasedLineNumber = 52, LineLength = 10, LineStartPosition = 1599 }; var textSnapshotMock = CreateSnapshotMock(vsLine, vsLine, 1602); // Act var result = new IssueConverter() .ToMarker(issue, textSnapshotMock.Object); // Assert result.Should().NotBeNull(); result.Span.Start.Position.Should().Be(1601); // vsLine.LineStartPosition + issue.StartLineOffset result.Span.End.Position.Should().Be(1602); // snapshot length }
public void CalculateSpan_IssueDoesNotHaveLineHash_HashNotChecked(string lineHash) { var issue = new DummyAnalysisIssue { StartLine = 1, StartLineOffset = 0, EndLine = 0, EndLineOffset = 0, LineHash = lineHash }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 0, LineLength = 10, LineStartPosition = 1, Text = "some text" }; var textSnapshotMock = CreateSnapshotMock(lines: new[] { firstLine }); // Act var result = testSubject.CalculateSpan(issue, textSnapshotMock.Object); result.IsEmpty.Should().BeFalse(); checksumCalculatorMock.VerifyNoOtherCalls(); }
private ITextSnapshotLine CreateLineMock(ITextSnapshot textSnapshot, VSLineDescription firstLine) { var startLineMock = new Mock <ITextSnapshotLine>(); startLineMock.SetupGet(x => x.Start) .Returns(() => new SnapshotPoint(textSnapshot, firstLine.LineStartPosition)); startLineMock.SetupGet(x => x.Length) .Returns(() => new SnapshotPoint(textSnapshot, firstLine.LineLength)); return(startLineMock.Object); }
public void CalculateSpan_StartPositionExceedsSnapshotLength_ReturnsSpanWithEndOfFile() { // These values were taken from a real analysis issue // Rule "cpp:S113 - no newline at end of file" returns an offset after the end of the file. // Arrange var issue = new DummyAnalysisIssueLocation { StartLine = 53, StartLineOffset = 2, EndLine = 53, EndLineOffset = 3, LineHash = "some hash" }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 52, LineLength = 1, LineStartPosition = 1599, Text = "some text" }; // Second line should not be used in this case var secondLine = new VSLineDescription { ZeroBasedLineNumber = -999, LineLength = -999, LineStartPosition = -999 }; checksumCalculatorMock.Setup(x => x.Calculate(firstLine.Text)).Returns(issue.LineHash); var textSnapshotMock = CreateSnapshotMock(snapShotLength: 1600, lines: new[] { firstLine, secondLine }); // Act var result = testSubject.CalculateSpan(issue, textSnapshotMock.Object); // Assert result.IsEmpty.Should().BeFalse(); result.Start.Position.Should().Be(1599); // firstLine.LineStartPosition. Ignore offset because that will take us beyond the end of file result.End.Position.Should().Be(1600); // snapshot length }
public void CalculateSpan_EndLineIsZero_ReturnsSpanOfTheEntireStartLine() { // If issue.EndLine is zero the whole of the start line should be selected // Arrange var issue = new DummyAnalysisIssueLocation { StartLine = 22, StartLineOffset = 2, EndLine = 0, EndLineOffset = 0, LineHash = "some hash" }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 21, LineLength = 34, LineStartPosition = 103, Text = "some text" }; // The second VS line shouldn't be used in this case var secondLine = new VSLineDescription { ZeroBasedLineNumber = 25, LineLength = 100, LineStartPosition = 1010 }; checksumCalculatorMock.Setup(x => x.Calculate(firstLine.Text)).Returns(issue.LineHash); var textSnapshotMock = CreateSnapshotMock(lines: new[] { firstLine, secondLine }); // Act var result = testSubject.CalculateSpan(issue, textSnapshotMock.Object); // Assert result.IsEmpty.Should().BeFalse(); result.Start.Position.Should().Be(103); // firstLine.LineStartPosition. Ignore issue.StartLineOffset in this case result.End.Position.Should().Be(137); // firstLine.LineStartPosition + firstLine.LineLength }
private Mock <ITextSnapshot> CreateSnapshotMock( VSLineDescription startLine, VSLineDescription endLine, int snapShotLength = 10000) { var textSnapshotMock = new Mock <ITextSnapshot>(); var startLineMock = CreateLineMock(textSnapshotMock.Object, startLine); var endLineMock = CreateLineMock(textSnapshotMock.Object, endLine); textSnapshotMock .SetupGet(x => x.Length) .Returns(snapShotLength); textSnapshotMock .Setup(x => x.GetLineFromLineNumber(startLine.ZeroBasedLineNumber)) .Returns(() => startLineMock); textSnapshotMock .Setup(x => x.GetLineFromLineNumber(endLine.ZeroBasedLineNumber)) .Returns(() => endLineMock); return(textSnapshotMock); }
public void ToMarker_StartPositionExceedsSnapshotLength() { // These values were taken from a real analysis issue // Rule "cpp:S113 - no newline at end of file" returns an offset after the end of the file. // Arrange var issue = new Sonarlint.Issue { StartLine = 53, StartLineOffset = 2, EndLine = 53, EndLineOffset = 3, }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 52, LineLength = 1, LineStartPosition = 1599 }; // Second line should not be used in this case var secondLine = new VSLineDescription { ZeroBasedLineNumber = -999, LineLength = -999, LineStartPosition = -999 }; var textSnapshotMock = CreateSnapshotMock(firstLine, secondLine, 1600); // Act var result = new IssueConverter() .ToMarker(issue, textSnapshotMock.Object); // Assert result.Should().NotBeNull(); result.Span.Start.Position.Should().Be(1599); // firstLine.LineStartPosition. Ignore offset because that will take us beyond the end of file result.Span.End.Position.Should().Be(1600); // snapshot length }
public void CalculateSpan_IssueLinesInsideSnapshot_ReturnsSpanWithCorrectPositions() { // Arrange var issue = new DummyAnalysisIssueLocation { StartLine = 3, StartLineOffset = 10, EndLine = 4, EndLineOffset = 20, LineHash = "some hash" }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 2, LineLength = 10, LineStartPosition = 35, Text = "some text" }; var secondLine = new VSLineDescription { ZeroBasedLineNumber = 3, LineLength = 25, LineStartPosition = 47 }; checksumCalculatorMock.Setup(x => x.Calculate(firstLine.Text)).Returns(issue.LineHash); var textSnapshotMock = CreateSnapshotMock(lines: new[] { firstLine, secondLine }); // Act var result = testSubject.CalculateSpan(issue, textSnapshotMock.Object); // Assert result.IsEmpty.Should().BeFalse(); result.Start.Position.Should().Be(45); // firstLine.LineStartPosition + issue.StartLineOffset result.End.Position.Should().Be(67); // secondLine.LineStartPosition + issue.EndLineOffset }
public void ToMarker_EndLineIsZero() { // If issue.EndLine is zero the whole of the start line should be selected // Arrange var issue = new Sonarlint.Issue { StartLine = 22, StartLineOffset = 2, EndLine = 0, EndLineOffset = 0, }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 21, LineLength = 34, LineStartPosition = 103 }; // The second VS line shouldn't be used in this case var secondLine = new VSLineDescription { ZeroBasedLineNumber = 25, LineLength = 100, LineStartPosition = 1010 }; var textSnapshotMock = CreateSnapshotMock(firstLine, secondLine); // Act var result = new IssueConverter() .ToMarker(issue, textSnapshotMock.Object); // Assert result.Should().NotBeNull(); result.Span.Start.Position.Should().Be(103); // firstLine.LineStartPosition. Ignore issue.StartLineOffset in this case result.Span.End.Position.Should().Be(137); // firstLine.LineStartPosition + firstLine.LineLength }
public void CalculateSpan_EndPositionExceedsSnapshotLength() { // Defensive: handle the issue end position being beyond the end of the snapshot // (we have not seen this in practice so far) // Arrange var issue = new DummyAnalysisIssueLocation { StartLine = 53, StartLineOffset = 2, EndLine = 53, EndLineOffset = 12, LineHash = "some hash" }; // The issue is on a single line in this case, but the issue end position // is beyond the end of the line. var vsLine = new VSLineDescription { ZeroBasedLineNumber = 52, LineLength = 10, LineStartPosition = 1599, Text = "some text" }; var textSnapshotMock = CreateSnapshotMock(lines: vsLine, snapShotLength: 1602); checksumCalculatorMock.Setup(x => x.Calculate(vsLine.Text)).Returns(issue.LineHash); // Act var result = testSubject.CalculateSpan(issue, textSnapshotMock.Object); // Assert result.IsEmpty.Should().BeFalse(); result.Start.Position.Should().Be(1601); // vsLine.LineStartPosition + issue.StartLineOffset result.End.Position.Should().Be(1602); // snapshot length }
public void CalculateSpan_IssueLineHashIsDifferent_ReturnsEmptySpan() { var issue = new DummyAnalysisIssue { StartLine = 10, LineHash = "some hash" }; var startLine = new VSLineDescription { LineLength = 34, LineStartPosition = 103, ZeroBasedLineNumber = issue.StartLine - 1, Text = "unimportant" }; var mockSnapshot = CreateSnapshotMock(lines: startLine); checksumCalculatorMock.Setup(x => x.Calculate(startLine.Text)).Returns("some other hash"); var result = testSubject.CalculateSpan(issue, mockSnapshot.Object); result.IsEmpty.Should().BeTrue(); checksumCalculatorMock.VerifyAll(); }
public void ToMarker_Calculates_Span_Positions() { // Arrange var issue = new Sonarlint.Issue { StartLine = 3, StartLineOffset = 10, EndLine = 4, EndLineOffset = 20, }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 2, LineLength = 10, LineStartPosition = 35 }; var secondLine = new VSLineDescription { ZeroBasedLineNumber = 3, LineLength = 25, LineStartPosition = 47 }; var textSnapshotMock = CreateSnapshotMock(firstLine, secondLine); // Act var result = new IssueConverter() .ToMarker(issue, textSnapshotMock.Object); // Assert result.Should().NotBeNull(); result.Span.Start.Position.Should().Be(45); // firstLine.LineStartPosition + issue.StartLineOffset result.Span.End.Position.Should().Be(67); // secondLine.LineStartPosition + issue.EndLineOffset }