comparison test/org/tmatesoft/hg/test/TestBlame.java @ 553:093a2022dad5

Test (with tailored repository) for annotation of multiple revisions
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 21 Feb 2013 20:25:26 +0100
parents 45751456b471
children a5fd757d1b5d
comparison
equal deleted inserted replaced
552:45751456b471 553:093a2022dad5
22 import static org.tmatesoft.hg.repo.HgRepository.TIP; 22 import static org.tmatesoft.hg.repo.HgRepository.TIP;
23 23
24 import java.io.ByteArrayOutputStream; 24 import java.io.ByteArrayOutputStream;
25 import java.io.PrintStream; 25 import java.io.PrintStream;
26 import java.util.Arrays; 26 import java.util.Arrays;
27 import java.util.LinkedHashSet;
28 import java.util.LinkedList;
27 import java.util.regex.Pattern; 29 import java.util.regex.Pattern;
28 30
29 import org.junit.Assert; 31 import org.junit.Assert;
30 import org.junit.Rule; 32 import org.junit.Rule;
31 import org.junit.Test; 33 import org.junit.Test;
32 import org.tmatesoft.hg.console.Bundle.Dump;
33 import org.tmatesoft.hg.core.HgIterateDirection; 34 import org.tmatesoft.hg.core.HgIterateDirection;
34 import org.tmatesoft.hg.internal.AnnotateFacility; 35 import org.tmatesoft.hg.internal.AnnotateFacility;
36 import org.tmatesoft.hg.internal.IntVector;
35 import org.tmatesoft.hg.internal.AnnotateFacility.AddBlock; 37 import org.tmatesoft.hg.internal.AnnotateFacility.AddBlock;
36 import org.tmatesoft.hg.internal.AnnotateFacility.Block; 38 import org.tmatesoft.hg.internal.AnnotateFacility.Block;
37 import org.tmatesoft.hg.internal.AnnotateFacility.ChangeBlock; 39 import org.tmatesoft.hg.internal.AnnotateFacility.ChangeBlock;
38 import org.tmatesoft.hg.internal.AnnotateFacility.DeleteBlock; 40 import org.tmatesoft.hg.internal.AnnotateFacility.DeleteBlock;
39 import org.tmatesoft.hg.internal.AnnotateFacility.EqualBlock; 41 import org.tmatesoft.hg.internal.AnnotateFacility.EqualBlock;
40 import org.tmatesoft.hg.internal.AnnotateFacility.LineDescriptor; 42 import org.tmatesoft.hg.internal.AnnotateFacility.LineDescriptor;
41 import org.tmatesoft.hg.internal.IntMap;
42 import org.tmatesoft.hg.repo.HgDataFile; 43 import org.tmatesoft.hg.repo.HgDataFile;
43 import org.tmatesoft.hg.repo.HgLookup; 44 import org.tmatesoft.hg.repo.HgLookup;
44 import org.tmatesoft.hg.repo.HgRepository; 45 import org.tmatesoft.hg.repo.HgRepository;
45 46
46 /** 47 /**
94 for (int i = 0; i < fa.lineRevisions.length; i++) { 95 for (int i = 0; i < fa.lineRevisions.length; i++) {
95 int hgAnnotateRevIndex = Integer.parseInt(hgAnnotateLines[i].substring(0, hgAnnotateLines[i].indexOf(':'))); 96 int hgAnnotateRevIndex = Integer.parseInt(hgAnnotateLines[i].substring(0, hgAnnotateLines[i].indexOf(':')));
96 errorCollector.assertEquals(String.format("Revision mismatch for line %d", i+1), hgAnnotateRevIndex, fa.lineRevisions[i]); 97 errorCollector.assertEquals(String.format("Revision mismatch for line %d", i+1), hgAnnotateRevIndex, fa.lineRevisions[i]);
97 } 98 }
98 } 99 }
100 }
101
102 @Test
103 public void testComplexHistoryAnnotate() throws Exception {
104 HgRepository repo = Configuration.get().find("test-annotate");
105 HgDataFile df = repo.getFileNode("file1");
106 AnnotateFacility af = new AnnotateFacility();
107 ByteArrayOutputStream bos = new ByteArrayOutputStream();
108 DiffOutInspector dump = new DiffOutInspector(new PrintStream(bos));
109 af.annotate(df, TIP, dump, HgIterateDirection.OldToNew);
110 LinkedList<String> apiResult = new LinkedList<String>(Arrays.asList(splitLines(bos.toString())));
111
112 LineGrepOutputParser gp = new LineGrepOutputParser("^@@.+");
113 ExecHelper eh = new ExecHelper(gp, repo.getWorkingDir());
114 System.out.println(Arrays.toString(dump.getReportedTargetRevisions()));
115 for (int cs : dump.getReportedTargetRevisions()) {
116 gp.reset();
117 eh.run("hg", "diff", "-c", String.valueOf(cs), "-U", "0", df.getPath().toString());
118 for (String expected : splitLines(gp.result())) {
119 if (!apiResult.remove(expected)) {
120 errorCollector.fail(String.format("Expected diff output '%s' for changes in revision %d", expected, cs));
121 }
122 }
123 }
124 errorCollector.assertTrue(String.format("Annotate API reported excessive diff: %s ", apiResult.toString()), apiResult.isEmpty());
99 } 125 }
100 126
101 private static String[] splitLines(CharSequence seq) { 127 private static String[] splitLines(CharSequence seq) {
102 int lineCount = 0; 128 int lineCount = 0;
103 for (int i = 0, x = seq.length(); i < x; i++) { 129 for (int i = 0, x = seq.length(); i < x; i++) {
164 // dump.needRevisions(true); 190 // dump.needRevisions(true);
165 // af.annotateChange(df, checkChangeset, dump); 191 // af.annotateChange(df, checkChangeset, dump);
166 dump.needRevisions(true); 192 dump.needRevisions(true);
167 af.annotate(df, checkChangeset, dump, HgIterateDirection.OldToNew); 193 af.annotate(df, checkChangeset, dump, HgIterateDirection.OldToNew);
168 } 194 }
169 195
170 private void leftovers() throws Exception { 196 private void ccc() throws Exception {
171 IntMap<String> linesOld = new IntMap<String>(100), linesNew = new IntMap<String>(100); 197 HgRepository repo = new HgLookup().detect("/home/artem/hg/test-annotate/");
172 System.out.println("Changes to old revision:"); 198 HgDataFile df = repo.getFileNode("file1");
173 for (int i = linesOld.firstKey(), x = linesOld.lastKey(); i < x; i++) { 199 AnnotateFacility af = new AnnotateFacility();
174 if (linesOld.containsKey(i)) { 200 DiffOutInspector dump = new DiffOutInspector(System.out);
175 System.out.println(linesOld.get(i)); 201 dump.needRevisions(true);
176 } 202 af.annotate(df, TIP, dump, HgIterateDirection.OldToNew);
177 } 203 System.out.println();
178 204 FileAnnotateInspector fa = new FileAnnotateInspector();
179 System.out.println("Changes in the new revision:"); 205 af.annotate(df, TIP, fa);
180 for (int i = linesNew.firstKey(), x = linesNew.lastKey(); i < x; i++) { 206 for (int i = 0; i < fa.lineRevisions.length; i++) {
181 if (linesNew.containsKey(i)) { 207 System.out.printf("%d: LINE %d\n", fa.lineRevisions[i], i+1);
182 System.out.println(linesNew.get(i)); 208 }
183 } 209 }
184 } 210
185 }
186
187 public static void main(String[] args) throws Exception { 211 public static void main(String[] args) throws Exception {
188 // System.out.println(Arrays.equals(new String[0], splitLines(""))); 212 // System.out.println(Arrays.equals(new String[0], splitLines("")));
189 // System.out.println(Arrays.equals(new String[] { "abc" }, splitLines("abc"))); 213 // System.out.println(Arrays.equals(new String[] { "abc" }, splitLines("abc")));
190 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc"))); 214 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc")));
191 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc\n"))); 215 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc\n")));
192 new TestBlame().bbb(); 216 new TestBlame().ccc();
193 } 217 }
194 218
195 static class DiffOutInspector implements AnnotateFacility.BlockInspector { 219 static class DiffOutInspector implements AnnotateFacility.BlockInspector {
196 private final PrintStream out; 220 private final PrintStream out;
197 private boolean dumpRevs; 221 private boolean dumpRevs;
222 private IntVector reportedRevisionPairs = new IntVector();
198 223
199 DiffOutInspector(PrintStream ps) { 224 DiffOutInspector(PrintStream ps) {
200 out = ps; 225 out = ps;
201 } 226 }
202 227
207 232
208 private void printRevs(Block b) { 233 private void printRevs(Block b) {
209 if (dumpRevs) { 234 if (dumpRevs) {
210 out.printf("[%3d -> %3d] ", b.originChangesetIndex(), b.targetChangesetIndex()); 235 out.printf("[%3d -> %3d] ", b.originChangesetIndex(), b.targetChangesetIndex());
211 } 236 }
237 reportedRevisionPairs.add(b.originChangesetIndex(), b.targetChangesetIndex());
238 }
239
240 int[] getReportedTargetRevisions() {
241 LinkedHashSet<Integer> rv = new LinkedHashSet<Integer>();
242 for (int i = 1; i < reportedRevisionPairs.size(); i += 2) {
243 rv.add(reportedRevisionPairs.get(i));
244 }
245 int[] x = new int[rv.size()];
246 int i = 0;
247 for (int v : rv) {
248 x[i++] = v;
249 }
250 return x;
212 } 251 }
213 252
214 public void same(EqualBlock block) { 253 public void same(EqualBlock block) {
215 // nothing 254 // nothing
216 } 255 }
248 private final Pattern pattern; 287 private final Pattern pattern;
249 private final StringBuilder result = new StringBuilder(); 288 private final StringBuilder result = new StringBuilder();
250 289
251 public LineGrepOutputParser(String regexp) { 290 public LineGrepOutputParser(String regexp) {
252 pattern = Pattern.compile(regexp); 291 pattern = Pattern.compile(regexp);
292 }
293
294 public void reset() {
295 result.setLength(0);
253 } 296 }
254 297
255 public CharSequence result() { 298 public CharSequence result() {
256 return result; 299 return result;
257 } 300 }