|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using GitHub.InlineReviews.Services; |
| 5 | +using GitHub.InlineReviews.Tags; |
| 6 | +using GitHub.Models; |
| 7 | +using GitHub.Services; |
| 8 | +using Microsoft.VisualStudio.Text; |
| 9 | +using Microsoft.VisualStudio.Text.Editor; |
| 10 | +using NSubstitute; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace GitHub.InlineReviews.UnitTests.Tags |
| 14 | +{ |
| 15 | + public class InlineCommentTaggerTests |
| 16 | + { |
| 17 | + public class WithTextBufferInfo |
| 18 | + { |
| 19 | + [Fact] |
| 20 | + public void FirstPassShouldReturnEmptyTags() |
| 21 | + { |
| 22 | + var target = new InlineCommentTagger( |
| 23 | + Substitute.For<IGitService>(), |
| 24 | + Substitute.For<IGitClient>(), |
| 25 | + Substitute.For<IDiffService>(), |
| 26 | + Substitute.For<ITextView>(), |
| 27 | + Substitute.For<ITextBuffer>(), |
| 28 | + CreateSessionManager(leftHandSide: false)); |
| 29 | + |
| 30 | + var result = target.GetTags(CreateSpan(10)); |
| 31 | + |
| 32 | + Assert.Empty(result); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void ShouldReturnShowCommentTagForRhs() |
| 37 | + { |
| 38 | + var target = new InlineCommentTagger( |
| 39 | + Substitute.For<IGitService>(), |
| 40 | + Substitute.For<IGitClient>(), |
| 41 | + Substitute.For<IDiffService>(), |
| 42 | + Substitute.For<ITextView>(), |
| 43 | + Substitute.For<ITextBuffer>(), |
| 44 | + CreateSessionManager(leftHandSide: false)); |
| 45 | + |
| 46 | + // Line 10 has an existing RHS comment. |
| 47 | + var span = CreateSpan(10); |
| 48 | + var firstPass = target.GetTags(span); |
| 49 | + var result = target.GetTags(span).ToList(); |
| 50 | + |
| 51 | + Assert.Equal(1, result.Count); |
| 52 | + Assert.IsType<ShowInlineCommentTag>(result[0].Tag); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void ShouldReturnAddNewCommentTagForAddedLineOnRhs() |
| 57 | + { |
| 58 | + var target = new InlineCommentTagger( |
| 59 | + Substitute.For<IGitService>(), |
| 60 | + Substitute.For<IGitClient>(), |
| 61 | + Substitute.For<IDiffService>(), |
| 62 | + Substitute.For<ITextView>(), |
| 63 | + Substitute.For<ITextBuffer>(), |
| 64 | + CreateSessionManager(leftHandSide: false)); |
| 65 | + |
| 66 | + // Line 11 has an add diff entry. |
| 67 | + var span = CreateSpan(11); |
| 68 | + var firstPass = target.GetTags(span); |
| 69 | + var result = target.GetTags(span).ToList(); |
| 70 | + |
| 71 | + Assert.Equal(1, result.Count); |
| 72 | + Assert.IsType<AddInlineCommentTag>(result[0].Tag); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void ShouldNotReturnAddNewCommentTagForDeletedLineOnRhs() |
| 77 | + { |
| 78 | + var target = new InlineCommentTagger( |
| 79 | + Substitute.For<IGitService>(), |
| 80 | + Substitute.For<IGitClient>(), |
| 81 | + Substitute.For<IDiffService>(), |
| 82 | + Substitute.For<ITextView>(), |
| 83 | + Substitute.For<ITextBuffer>(), |
| 84 | + CreateSessionManager(leftHandSide: false)); |
| 85 | + |
| 86 | + // Line 13 has an delete diff entry. |
| 87 | + var span = CreateSpan(13); |
| 88 | + var firstPass = target.GetTags(span); |
| 89 | + var result = target.GetTags(span).ToList(); |
| 90 | + |
| 91 | + Assert.Empty(result); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void ShouldReturnShowCommentTagForLhs() |
| 96 | + { |
| 97 | + var target = new InlineCommentTagger( |
| 98 | + Substitute.For<IGitService>(), |
| 99 | + Substitute.For<IGitClient>(), |
| 100 | + Substitute.For<IDiffService>(), |
| 101 | + Substitute.For<ITextView>(), |
| 102 | + Substitute.For<ITextBuffer>(), |
| 103 | + CreateSessionManager(leftHandSide: true)); |
| 104 | + |
| 105 | + // Line 12 has an existing LHS comment. |
| 106 | + var span = CreateSpan(12); |
| 107 | + var firstPass = target.GetTags(span); |
| 108 | + var result = target.GetTags(span).ToList(); |
| 109 | + |
| 110 | + Assert.Equal(1, result.Count); |
| 111 | + Assert.IsType<ShowInlineCommentTag>(result[0].Tag); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void ShouldReturnAddCommentTagForLhs() |
| 116 | + { |
| 117 | + var target = new InlineCommentTagger( |
| 118 | + Substitute.For<IGitService>(), |
| 119 | + Substitute.For<IGitClient>(), |
| 120 | + Substitute.For<IDiffService>(), |
| 121 | + Substitute.For<ITextView>(), |
| 122 | + Substitute.For<ITextBuffer>(), |
| 123 | + CreateSessionManager(leftHandSide: true)); |
| 124 | + |
| 125 | + // Line 13 has an delete diff entry. |
| 126 | + var span = CreateSpan(13); |
| 127 | + var firstPass = target.GetTags(span); |
| 128 | + var result = target.GetTags(span).ToList(); |
| 129 | + |
| 130 | + Assert.Equal(1, result.Count); |
| 131 | + Assert.IsType<AddInlineCommentTag>(result[0].Tag); |
| 132 | + } |
| 133 | + |
| 134 | + static IPullRequestSessionManager CreateSessionManager(bool leftHandSide) |
| 135 | + { |
| 136 | + var diffChunk = new DiffChunk |
| 137 | + { |
| 138 | + Lines = |
| 139 | + { |
| 140 | + // Line numbers here are 1-based. There is an add diff entry on line 11 |
| 141 | + // and a delete entry on line 13. |
| 142 | + new DiffLine { Type = DiffChangeType.Add, NewLineNumber = 11 + 1 }, |
| 143 | + new DiffLine { Type = DiffChangeType.Delete, OldLineNumber = 13 + 1 }, |
| 144 | + } |
| 145 | + }; |
| 146 | + var diff = new List<DiffChunk> { diffChunk }; |
| 147 | + |
| 148 | + var rhsThread = Substitute.For<IInlineCommentThreadModel>(); |
| 149 | + rhsThread.DiffLineType.Returns(DiffChangeType.Add); |
| 150 | + rhsThread.LineNumber.Returns(10); |
| 151 | + |
| 152 | + var lhsThread = Substitute.For<IInlineCommentThreadModel>(); |
| 153 | + lhsThread.DiffLineType.Returns(DiffChangeType.Delete); |
| 154 | + lhsThread.LineNumber.Returns(12); |
| 155 | + |
| 156 | + // We have a comment to display on the right-hand-side of the diff view on line |
| 157 | + // 11 and a comment to display on line 13 on the left-hand-side. |
| 158 | + var threads = new List<IInlineCommentThreadModel> { rhsThread, lhsThread }; |
| 159 | + |
| 160 | + var file = Substitute.For<IPullRequestSessionFile>(); |
| 161 | + file.Diff.Returns(diff); |
| 162 | + file.InlineCommentThreads.Returns(threads); |
| 163 | + |
| 164 | + var session = Substitute.For<IPullRequestSession>(); |
| 165 | + session.GetFile("file.cs").Returns(file); |
| 166 | + |
| 167 | + var info = new PullRequestTextBufferInfo(session, "file.cs", leftHandSide); |
| 168 | + var result = Substitute.For<IPullRequestSessionManager>(); |
| 169 | + result.GetTextBufferInfo(null).ReturnsForAnyArgs(info); |
| 170 | + return result; |
| 171 | + } |
| 172 | + |
| 173 | + static NormalizedSnapshotSpanCollection CreateSpan(int lineNumber) |
| 174 | + { |
| 175 | + var snapshot = Substitute.For<ITextSnapshot>(); |
| 176 | + snapshot.Length.Returns(200); |
| 177 | + |
| 178 | + var line = Substitute.For<ITextSnapshotLine>(); |
| 179 | + var start = new SnapshotPoint(snapshot, lineNumber); |
| 180 | + var end = new SnapshotPoint(snapshot, lineNumber); |
| 181 | + line.LineNumber.Returns(lineNumber); |
| 182 | + line.Start.Returns(start); |
| 183 | + line.End.Returns(end); |
| 184 | + |
| 185 | + snapshot.GetLineFromPosition(0).ReturnsForAnyArgs(line); |
| 186 | + snapshot.GetLineFromLineNumber(lineNumber).Returns(line); |
| 187 | + |
| 188 | + var span = new Span(0, 10); |
| 189 | + return new NormalizedSnapshotSpanCollection(snapshot, span); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments