26
26
import java .io .InputStream ;
27
27
import java .io .InputStreamReader ;
28
28
import java .io .Reader ;
29
+ import java .util .ArrayList ;
29
30
import java .util .List ;
30
31
import org .apache .lucene .analysis .tokenattributes .CharTermAttribute ;
31
32
import static org .junit .Assert .assertEquals ;
@@ -52,17 +53,31 @@ protected CustomAssertions() {
52
53
public static void assertLinesEqual (String messagePrefix ,
53
54
String expecteds [], String actuals []) {
54
55
55
- for (int i = 0 ; i < expecteds .length && i < actuals .length ; i ++) {
56
- if (!expecteds [i ].equals (actuals [i ])) {
57
- System .out .print ("- " );
58
- System .out .println (expecteds [i ]);
59
- System .out .print ("+ " );
60
- System .out .println (actuals [i ]);
56
+ List <Integer > diffLines = new ArrayList <>();
57
+
58
+ final int SHOW_N_DIFFS = 10 ;
59
+ int ndiffs = 0 ;
60
+ int lastDiff = -2 ;
61
+ for (int i = 0 ; i < expecteds .length || i < actuals .length ; i ++) {
62
+ if (i >= expecteds .length || i >= actuals .length ||
63
+ !expecteds [i ].equals (actuals [i ])) {
64
+
65
+ if (lastDiff + 1 != i && !diffLines .isEmpty ()) {
66
+ printDiffs (expecteds , actuals , diffLines );
67
+ diffLines .clear ();
68
+ }
69
+ ++ndiffs ;
70
+ lastDiff = i ;
71
+ diffLines .add (i );
72
+ if (ndiffs >= SHOW_N_DIFFS ) break ;
61
73
}
62
- assertEquals (messagePrefix + ":line " + (i + 1 ), expecteds [i ],
63
- actuals [i ]);
74
+ }
75
+ if (!diffLines .isEmpty ()) {
76
+ printDiffs (expecteds , actuals , diffLines );
77
+ diffLines .clear ();
64
78
}
65
79
80
+ assertTrue ("should have no diffs" , ndiffs == 0 );
66
81
assertEquals (messagePrefix + ":number of lines" , expecteds .length ,
67
82
actuals .length );
68
83
}
@@ -97,4 +112,45 @@ public static void assertSymbolStream(Class<? extends JFlexTokenizer> klass,
97
112
98
113
assertEquals ("wrong number of tokens" , expectedTokens .size (), count );
99
114
}
115
+
116
+ private static void printDiffs (String expecteds [], String actuals [],
117
+ List <Integer > diffLines ) {
118
+
119
+ if (diffLines .size () < 1 ) return ;
120
+
121
+ int ln0 = diffLines .get (0 );
122
+ int numln = diffLines .size ();
123
+ int loff = ln0 < expecteds .length ? ln0 : expecteds .length ;
124
+ int lnum = count_within (expecteds .length , ln0 , numln );
125
+ int roff = ln0 < actuals .length ? ln0 : actuals .length ;
126
+ int rnum = count_within (actuals .length , ln0 , numln );
127
+
128
+ System .out .format ("@@ -%d,%d +%d,%d @@" , loff , lnum , roff , rnum );
129
+ System .out .println ();
130
+
131
+ for (int i : diffLines ) {
132
+ if (i >= expecteds .length ) {
133
+ break ;
134
+ } else {
135
+ System .out .print ("- " );
136
+ System .out .println (expecteds [i ]);
137
+ }
138
+ }
139
+ for (int i : diffLines ) {
140
+ if (i >= actuals .length ) {
141
+ break ;
142
+ } else {
143
+ System .out .print ("+ " );
144
+ System .out .println (actuals [i ]);
145
+ }
146
+ }
147
+ }
148
+
149
+ private static int count_within (int maxoffset , int ln0 , int numln ) {
150
+ while (numln > 0 ) {
151
+ if (ln0 + numln <= maxoffset ) return numln ;
152
+ --numln ;
153
+ }
154
+ return 0 ;
155
+ }
100
156
}
0 commit comments