@@ -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
@@ -145,6 +149,7 @@ function main(): void
145
149
$ user_tests , $ valgrind , $ sum_results , $ shuffle , $ file_cache ;
146
150
// Parallel testing
147
151
global $ workers , $ workerID ;
152
+ global $ context_line_count ;
148
153
149
154
define ('IS_WINDOWS ' , substr (PHP_OS , 0 , 3 ) == "WIN " );
150
155
@@ -396,6 +401,7 @@ function main(): void
396
401
$ file_cache = null ;
397
402
$ shuffle = false ;
398
403
$ workers = null ;
404
+ $ context_line_count = 3 ;
399
405
400
406
$ cfgtypes = ['show ' , 'keep ' ];
401
407
$ cfgfiles = ['skip ' , 'php ' , 'clean ' , 'out ' , 'diff ' , 'exp ' , 'mem ' ];
@@ -561,6 +567,13 @@ function main(): void
561
567
case '--set-timeout ' :
562
568
$ environment ['TEST_TIMEOUT ' ] = $ argv [++$ i ];
563
569
break ;
570
+ case '--context ' :
571
+ $ context_line_count = $ argv [++$ i ] ?? '' ;
572
+ if (!preg_match ('/^\d+$/ ' , $ context_line_count )) {
573
+ error ("' $ context_line_count' is not a valid number of lines of context, try e.g. --context 3 for 3 lines " );
574
+ }
575
+ $ context_line_count = intval ($ context_line_count , 10 );
576
+ break ;
564
577
case '--show-all ' :
565
578
foreach ($ cfgfiles as $ file ) {
566
579
$ cfg ['show ' ][$ file ] = true ;
@@ -2878,6 +2891,7 @@ function count_array_diff(
2878
2891
function generate_array_diff (array $ ar1 , array $ ar2 , bool $ is_reg , array $ w ): array
2879
2892
{
2880
2893
global $ colorize ;
2894
+ global $ context_line_count ;
2881
2895
$ idx1 = 0 ;
2882
2896
$ cnt1 = @count ($ ar1 );
2883
2897
$ idx2 = 0 ;
@@ -2904,8 +2918,14 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
2904
2918
}
2905
2919
return $ output ;
2906
2920
};
2921
+ $ format_context_line = function (int $ line_number , string $ contents ): string {
2922
+ // return sprintf("%03d ", $line_number) . $contents; // to print the line number in the expected output
2923
+ return str_repeat (' ' , max (strlen ((string )$ line_number ), 3 ) + 2 ) . $ contents ;
2924
+ };
2925
+ $ mapping = [];
2907
2926
2908
2927
while ($ idx1 < $ cnt1 && $ idx2 < $ cnt2 ) {
2928
+ $ mapping [$ idx2 ] = $ idx1 ;
2909
2929
if (comp_line ($ ar1 [$ idx1 ], $ ar2 [$ idx2 ], $ is_reg )) {
2910
2930
$ idx1 ++;
2911
2931
$ idx2 ++;
@@ -2922,43 +2942,72 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
2922
2942
$ old1 [$ idx1 ] = $ format_expected_line ($ idx1 + 1 , $ w [$ idx1 ++]);
2923
2943
$ old2 [$ idx2 ] = $ format_actual_line ($ idx2 + 1 , $ ar2 [$ idx2 ++]);
2924
2944
}
2945
+ $ last_printed_context_line = $ idx1 ;
2925
2946
}
2926
2947
}
2948
+ $ mapping [$ idx2 ] = $ idx1 ;
2927
2949
2928
2950
reset ($ old1 );
2929
2951
$ k1 = key ($ old1 );
2930
2952
$ l1 = -2 ;
2931
2953
reset ($ old2 );
2932
2954
$ k2 = key ($ old2 );
2933
2955
$ l2 = -2 ;
2956
+ $ old_k1 = -1 ;
2957
+ $ add_context_lines = function (int $ new_k1 ) use (&$ old_k1 , &$ diff , $ w , $ context_line_count , $ format_context_line ) {
2958
+ $ end = $ new_k1 - 1 ;
2959
+ $ range_end = min ($ end , $ old_k1 + $ context_line_count );
2960
+ if ($ old_k1 >= 0 ) {
2961
+ while ($ old_k1 < $ range_end ) {
2962
+ $ diff [] = $ format_context_line ($ old_k1 + 1 , $ w [$ old_k1 ++]);
2963
+ }
2964
+ }
2965
+ if ($ end - $ context_line_count > $ old_k1 ) {
2966
+ $ old_k1 = $ end - $ context_line_count ;
2967
+ $ diff [] = '-- ' ;
2968
+ }
2969
+ $ old_k1 = max ($ old_k1 , 0 );
2970
+ while ($ old_k1 < $ end ) {
2971
+ $ diff [] = $ format_context_line ($ old_k1 + 1 , $ w [$ old_k1 ++]);
2972
+ }
2973
+ $ old_k1 = $ new_k1 ;
2974
+ };
2934
2975
2935
2976
while ($ k1 !== null || $ k2 !== null ) {
2936
2977
if ($ k1 == $ l1 + 1 || $ k2 === null ) {
2978
+ $ add_context_lines ($ k1 );
2937
2979
$ l1 = $ k1 ;
2938
2980
$ diff [] = current ($ old1 );
2981
+ $ old_k1 = $ k1 ;
2939
2982
$ k1 = next ($ old1 ) ? key ($ old1 ) : null ;
2940
2983
} elseif ($ k2 == $ l2 + 1 || $ k1 === null ) {
2984
+ $ add_context_lines ($ mapping [$ k2 ]);
2941
2985
$ l2 = $ k2 ;
2942
2986
$ diff [] = current ($ old2 );
2943
2987
$ k2 = next ($ old2 ) ? key ($ old2 ) : null ;
2944
2988
} elseif ($ k1 < $ k2 ) {
2989
+ $ add_context_lines ($ k1 );
2945
2990
$ l1 = $ k1 ;
2946
2991
$ diff [] = current ($ old1 );
2947
2992
$ k1 = next ($ old1 ) ? key ($ old1 ) : null ;
2948
2993
} else {
2994
+ $ add_context_lines ($ mapping [$ k2 ]);
2949
2995
$ l2 = $ k2 ;
2950
2996
$ diff [] = current ($ old2 );
2951
2997
$ k2 = next ($ old2 ) ? key ($ old2 ) : null ;
2952
2998
}
2953
2999
}
2954
3000
2955
3001
while ($ idx1 < $ cnt1 ) {
3002
+ $ add_context_lines ($ idx1 + 1 );
2956
3003
$ diff [] = $ format_expected_line ($ idx1 + 1 , $ w [$ idx1 ++]);
2957
3004
}
2958
3005
2959
3006
while ($ idx2 < $ cnt2 ) {
3007
+ $ add_context_lines ($ mapping [$ idx2 ] + 1 );
2960
3008
$ diff [] = $ format_actual_line ($ idx2 + 1 , $ ar2 [$ idx2 ++]);
2961
3009
}
3010
+ $ add_context_lines (min ($ old_k1 + $ context_line_count + 1 , $ cnt1 + 1 ));
2962
3011
2963
3012
return $ diff ;
2964
3013
}
0 commit comments