@@ -102,6 +102,10 @@ function show_usage(): void
102
102
seconds. The default value is 60 seconds, or 300 seconds when
103
103
testing for memory leaks.
104
104
105
+ --context [n]
106
+ Sets the number of lines of surrounding context to print for diffs.
107
+ The default value is 3.
108
+
105
109
--show-[all|php|skip|clean|exp|diff|out|mem]
106
110
Show 'all' files, 'php' test file, 'skip' or 'clean' file. You
107
111
can also use this to show the output 'out', the expected result
@@ -146,6 +150,7 @@ function main(): void
146
150
$ user_tests , $ valgrind , $ sum_results , $ shuffle , $ file_cache ;
147
151
// Parallel testing
148
152
global $ workers , $ workerID ;
153
+ global $ context_line_count ;
149
154
150
155
define ('IS_WINDOWS ' , substr (PHP_OS , 0 , 3 ) == "WIN " );
151
156
@@ -400,6 +405,7 @@ function main(): void
400
405
$ file_cache = null ;
401
406
$ shuffle = false ;
402
407
$ workers = null ;
408
+ $ context_line_count = 3 ;
403
409
404
410
$ cfgtypes = ['show ' , 'keep ' ];
405
411
$ cfgfiles = ['skip ' , 'php ' , 'clean ' , 'out ' , 'diff ' , 'exp ' , 'mem ' ];
@@ -568,6 +574,13 @@ function main(): void
568
574
case '--set-timeout ' :
569
575
$ environment ['TEST_TIMEOUT ' ] = $ argv [++$ i ];
570
576
break ;
577
+ case '--context ' :
578
+ $ context_line_count = $ argv [++$ i ] ?? '' ;
579
+ if (!preg_match ('/^\d+$/ ' , $ context_line_count )) {
580
+ error ("' $ context_line_count' is not a valid number of lines of context, try e.g. --context 3 for 3 lines " );
581
+ }
582
+ $ context_line_count = intval ($ context_line_count , 10 );
583
+ break ;
571
584
case '--show-all ' :
572
585
foreach ($ cfgfiles as $ file ) {
573
586
$ cfg ['show ' ][$ file ] = true ;
@@ -2884,15 +2897,22 @@ function count_array_diff(
2884
2897
2885
2898
function generate_array_diff (array $ ar1 , array $ ar2 , bool $ is_reg , array $ w ): array
2886
2899
{
2900
+ global $ context_line_count ;
2887
2901
$ idx1 = 0 ;
2888
2902
$ cnt1 = @count ($ ar1 );
2889
2903
$ idx2 = 0 ;
2890
2904
$ cnt2 = @count ($ ar2 );
2891
2905
$ diff = [];
2892
2906
$ old1 = [];
2893
2907
$ old2 = [];
2908
+ $ number_len = max (3 , strlen ((string )max ($ cnt1 + 1 , $ cnt2 + 1 )));
2909
+ $ line_number_spec = '%0 ' . $ number_len . 'd ' ;
2910
+
2911
+ /** Mapping from $idx2 to $idx1, including indexes of idx2 that are identical to idx1 as well as entries that don't have matches */
2912
+ $ mapping = [];
2894
2913
2895
2914
while ($ idx1 < $ cnt1 && $ idx2 < $ cnt2 ) {
2915
+ $ mapping [$ idx2 ] = $ idx1 ;
2896
2916
if (comp_line ($ ar1 [$ idx1 ], $ ar2 [$ idx2 ], $ is_reg )) {
2897
2917
$ idx1 ++;
2898
2918
$ idx2 ++;
@@ -2902,50 +2922,85 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
2902
2922
$ c2 = @count_array_diff ($ ar1 , $ ar2 , $ is_reg , $ w , $ idx1 , $ idx2 + 1 , $ cnt1 , $ cnt2 , 10 );
2903
2923
2904
2924
if ($ c1 > $ c2 ) {
2905
- $ old1 [$ idx1 ] = sprintf ("%03d - " , $ idx1 + 1 ) . $ w [$ idx1 ++];
2925
+ $ old1 [$ idx1 ] = sprintf ("{ $ line_number_spec } - " , $ idx1 + 1 ) . $ w [$ idx1 ++];
2906
2926
} elseif ($ c2 > 0 ) {
2907
- $ old2 [$ idx2 ] = sprintf ("%03d + " , $ idx2 + 1 ) . $ ar2 [$ idx2 ++];
2927
+ $ old2 [$ idx2 ] = sprintf ("{ $ line_number_spec } + " , $ idx2 + 1 ) . $ ar2 [$ idx2 ++];
2908
2928
} else {
2909
- $ old1 [$ idx1 ] = sprintf ("%03d - " , $ idx1 + 1 ) . $ w [$ idx1 ++];
2910
- $ old2 [$ idx2 ] = sprintf ("%03d + " , $ idx2 + 1 ) . $ ar2 [$ idx2 ++];
2929
+ $ old1 [$ idx1 ] = sprintf ("{ $ line_number_spec } - " , $ idx1 + 1 ) . $ w [$ idx1 ++];
2930
+ $ old2 [$ idx2 ] = sprintf ("{ $ line_number_spec } + " , $ idx2 + 1 ) . $ ar2 [$ idx2 ++];
2911
2931
}
2932
+ $ last_printed_context_line = $ idx1 ;
2912
2933
}
2913
2934
}
2935
+ $ mapping [$ idx2 ] = $ idx1 ;
2914
2936
2915
2937
reset ($ old1 );
2916
2938
$ k1 = key ($ old1 );
2917
2939
$ l1 = -2 ;
2918
2940
reset ($ old2 );
2919
2941
$ k2 = key ($ old2 );
2920
2942
$ l2 = -2 ;
2943
+ $ old_k1 = -1 ;
2944
+ $ add_context_lines = function (int $ new_k1 ) use (&$ old_k1 , &$ diff , $ w , $ context_line_count , $ number_len ) {
2945
+ if ($ old_k1 >= $ new_k1 || !$ context_line_count ) {
2946
+ return ;
2947
+ }
2948
+ $ end = $ new_k1 - 1 ;
2949
+ $ range_end = min ($ end , $ old_k1 + $ context_line_count );
2950
+ if ($ old_k1 >= 0 ) {
2951
+ while ($ old_k1 < $ range_end ) {
2952
+ $ diff [] = str_repeat (' ' , $ number_len + 2 ) . $ w [$ old_k1 ++];
2953
+ }
2954
+ }
2955
+ if ($ end - $ context_line_count > $ old_k1 ) {
2956
+ $ old_k1 = $ end - $ context_line_count ;
2957
+ // Add a '--' to mark sections where the common areas were truncated
2958
+ $ diff [] = '-- ' ;
2959
+ }
2960
+ $ old_k1 = max ($ old_k1 , 0 );
2961
+ while ($ old_k1 < $ end ) {
2962
+ $ diff [] = str_repeat (' ' , $ number_len + 2 ) . $ w [$ old_k1 ++];
2963
+ }
2964
+ $ old_k1 = $ new_k1 ;
2965
+ };
2921
2966
2922
2967
while ($ k1 !== null || $ k2 !== null ) {
2923
2968
if ($ k1 == $ l1 + 1 || $ k2 === null ) {
2969
+ $ add_context_lines ($ k1 );
2924
2970
$ l1 = $ k1 ;
2925
2971
$ diff [] = current ($ old1 );
2972
+ $ old_k1 = $ k1 ;
2926
2973
$ k1 = next ($ old1 ) ? key ($ old1 ) : null ;
2927
2974
} elseif ($ k2 == $ l2 + 1 || $ k1 === null ) {
2975
+ $ add_context_lines ($ mapping [$ k2 ]);
2928
2976
$ l2 = $ k2 ;
2929
2977
$ diff [] = current ($ old2 );
2930
2978
$ k2 = next ($ old2 ) ? key ($ old2 ) : null ;
2931
- } elseif ($ k1 < $ k2 ) {
2979
+ } elseif ($ k1 < $ mapping [$ k2 ]) {
2980
+ $ add_context_lines ($ k1 );
2932
2981
$ l1 = $ k1 ;
2933
2982
$ diff [] = current ($ old1 );
2934
2983
$ k1 = next ($ old1 ) ? key ($ old1 ) : null ;
2935
2984
} else {
2985
+ $ add_context_lines ($ mapping [$ k2 ]);
2936
2986
$ l2 = $ k2 ;
2937
2987
$ diff [] = current ($ old2 );
2938
2988
$ k2 = next ($ old2 ) ? key ($ old2 ) : null ;
2939
2989
}
2940
2990
}
2941
2991
2942
2992
while ($ idx1 < $ cnt1 ) {
2943
- $ diff [] = sprintf ("%03d- " , $ idx1 + 1 ) . $ w [$ idx1 ++];
2993
+ $ add_context_lines ($ idx1 + 1 );
2994
+ $ diff [] = sprintf ("{$ line_number_spec }- " , $ idx1 + 1 ) . $ w [$ idx1 ++];
2944
2995
}
2945
2996
2946
2997
while ($ idx2 < $ cnt2 ) {
2947
- $ diff [] = sprintf ("%03d+ " , $ idx2 + 1 ) . $ ar2 [$ idx2 ++];
2998
+ if (isset ($ mapping [$ idx2 ])) {
2999
+ $ add_context_lines ($ mapping [$ idx2 ] + 1 );
3000
+ }
3001
+ $ diff [] = sprintf ("{$ line_number_spec }+ " , $ idx2 + 1 ) . $ ar2 [$ idx2 ++];
2948
3002
}
3003
+ $ add_context_lines (min ($ old_k1 + $ context_line_count + 1 , $ cnt1 + 1 ));
2949
3004
2950
3005
return $ diff ;
2951
3006
}
0 commit comments