|
13 | 13 | /// |
14 | 14 | /// --------------------------------------------------------------------------------- |
15 | 15 | /// |
| 16 | +using GitDensity.Density; |
| 17 | +using GitDensity.Similarity; |
| 18 | +using LibGit2Sharp; |
16 | 19 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
17 | 20 | using System; |
18 | 21 | using System.Collections.Generic; |
| 22 | +using System.IO; |
19 | 23 | using System.Linq; |
| 24 | +using System.Reflection; |
20 | 25 | using System.Text; |
21 | 26 | using System.Text.RegularExpressions; |
22 | 27 | using System.Threading.Tasks; |
| 28 | +using Util.Density; |
| 29 | +using Util.Extensions; |
23 | 30 |
|
24 | 31 | namespace GitDensityTests |
25 | 32 | { |
26 | 33 | [TestClass] |
27 | 34 | public class HunkTests |
28 | 35 | { |
| 36 | + public static DirectoryInfo SolutionDirectory |
| 37 | + => new DirectoryInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)).Parent.Parent.Parent; |
| 38 | + |
| 39 | + |
29 | 40 | [TestMethod] |
30 | 41 | public void TestPatchSplit() |
31 | 42 | { |
@@ -90,5 +101,71 @@ public void TestPatchSplitMultiLines() |
90 | 101 | Assert.AreEqual("-deleted line\n" + |
91 | 102 | "yo", hunks[1].Patch); |
92 | 103 | } |
| 104 | + |
| 105 | + [TestMethod] |
| 106 | + public void TestHunkGetLines() |
| 107 | + { |
| 108 | + using (var span = new Util.GitCommitSpan(SolutionDirectory.FullName.OpenRepository())) |
| 109 | + { |
| 110 | + // Has additions and deletions, also within comments |
| 111 | + var comm1 = span.Where(c => c.Sha.StartsWith("6f18a54", StringComparison.OrdinalIgnoreCase)).First(); |
| 112 | + //// Has mostly additions but also (single-line) comments we wanna remove |
| 113 | + //var comm2 = span.Where(c => c.Sha.StartsWith("f7574a4", StringComparison.OrdinalIgnoreCase)).First(); |
| 114 | + |
| 115 | + var pair = CommitPair.FromChild(comm1, span.Repository); |
| 116 | + // Each change corresponds to a modified file: |
| 117 | + var changes = pair.RelevantTreeChanges.Where(rtc => rtc.Status == ChangeKind.Modified); |
| 118 | + |
| 119 | + // Let's look at 'IMetricsAnalyzer': |
| 120 | + var patch = pair.Patch[changes.Where(c => c.Path.EndsWith("IMetricsAnalyzer.cs")).First().Path]; |
| 121 | + |
| 122 | + TextBlock.CountLinesInPatch( |
| 123 | + patch, out uint add, out uint del, out uint addNoC, out uint delNoC); |
| 124 | + |
| 125 | + Assert.AreEqual(7u, add); |
| 126 | + Assert.AreEqual(3u, del); |
| 127 | + Assert.AreEqual(2u, addNoC); |
| 128 | + Assert.AreEqual(1u, delNoC); |
| 129 | + |
| 130 | + |
| 131 | + // Now assert that this commit does not have any pure adds/renames: |
| 132 | + changes = pair.RelevantTreeChanges.Where(rtc => rtc.Status == ChangeKind.Added || rtc.Status == ChangeKind.Renamed); |
| 133 | + TextBlock.CountLinesInAllPatches( |
| 134 | + changes.Select(c => pair.Patch[c.Path]), out add, out del, out addNoC, out delNoC); |
| 135 | + |
| 136 | + Assert.AreEqual(0u, add); |
| 137 | + Assert.AreEqual(0u, del); |
| 138 | + Assert.AreEqual(0u, addNoC); |
| 139 | + Assert.AreEqual(0u, delNoC); |
| 140 | + |
| 141 | + // .. also pure deletes: |
| 142 | + changes = pair.RelevantTreeChanges.Where(rtc => rtc.Status == ChangeKind.Deleted); |
| 143 | + Assert.IsTrue(!changes.Any()); |
| 144 | + TextBlock.CountLinesInAllPatches( |
| 145 | + changes.Select(c => pair.Patch[c.OldPath]), out add, out del, out addNoC, out delNoC); |
| 146 | + |
| 147 | + Assert.AreEqual(0u, add); |
| 148 | + Assert.AreEqual(0u, del); |
| 149 | + Assert.AreEqual(0u, addNoC); |
| 150 | + Assert.AreEqual(0u, delNoC); |
| 151 | + |
| 152 | + |
| 153 | + //////////////////////////// Let's check a deleted file: |
| 154 | + comm1 = span.Where(c => c.Sha.StartsWith("03e2779", StringComparison.OrdinalIgnoreCase)).First(); |
| 155 | + pair = CommitPair.FromChild(comm1, span.Repository); |
| 156 | + changes = pair.RelevantTreeChanges.Where(rtc => rtc.Status == ChangeKind.Deleted); |
| 157 | + |
| 158 | + Assert.AreEqual(3, changes.Count()); |
| 159 | + |
| 160 | + // Per file, only 2 lines are not comments! |
| 161 | + TextBlock.CountLinesInAllPatches( |
| 162 | + changes.Select(c => pair.Patch[c.Path]), out add, out del, out addNoC, out delNoC); |
| 163 | + |
| 164 | + Assert.AreEqual(0u, add); |
| 165 | + Assert.IsTrue(del >= 90u); // didn't count it, but roughly 3x30 |
| 166 | + Assert.AreEqual(0u, addNoC); |
| 167 | + Assert.AreEqual(6u, delNoC); |
| 168 | + } |
| 169 | + } |
93 | 170 | } |
94 | 171 | } |
0 commit comments