@@ -13,9 +13,10 @@ mod constants;
1313
1414use constants:: { EXIT_STATUS_DIFFERENCE , EXIT_STATUS_NO_DIFFERENCE } ;
1515use plib:: { run_test, TestPlan } ;
16+ use std:: { collections:: HashMap , path:: PathBuf , process:: Stdio , sync:: LazyLock } ;
1617
1718fn diff_test ( args : & [ & str ] , expected_output : & str , expected_diff_exit_status : u8 ) {
18- let str_args: Vec < String > = args. iter ( ) . map ( |s| String :: from ( * s ) ) . collect ( ) ;
19+ let str_args = args. iter ( ) . cloned ( ) . map ( str :: to_owned ) . collect ( ) ;
1920
2021 run_test ( TestPlan {
2122 cmd : String :: from ( "diff" ) ,
@@ -27,8 +28,6 @@ fn diff_test(args: &[&str], expected_output: &str, expected_diff_exit_status: u8
2728 } ) ;
2829}
2930
30- use std:: { path:: PathBuf , process:: Stdio } ;
31-
3231fn diff_base_path ( ) -> PathBuf {
3332 PathBuf :: from ( "tests" ) . join ( "diff" )
3433}
@@ -74,14 +73,13 @@ fn f1_txt_with_eol_spaces_path() -> String {
7473}
7574
7675struct DiffTestHelper {
77- pub key : String ,
7876 content : String ,
7977 file1_path : String ,
8078 file2_path : String ,
8179}
8280
8381impl DiffTestHelper {
84- fn new ( options : & str , file1_path : String , file2_path : String , key : String ) -> Self {
82+ fn new ( options : & str , file1_path : String , file2_path : String ) -> Self {
8583 let args = format ! (
8684 "run --release --bin diff --{} {} {}" ,
8785 options, file1_path, file2_path
@@ -99,7 +97,6 @@ impl DiffTestHelper {
9997 let content = String :: from_utf8 ( output. stdout ) . expect ( "Failed to read output of Command!" ) ;
10098
10199 Self {
102- key,
103100 file1_path,
104101 file2_path,
105102 content,
@@ -119,20 +116,7 @@ impl DiffTestHelper {
119116 }
120117}
121118
122- static mut DIFF_TEST_INPUT : Vec < DiffTestHelper > = vec ! [ ] ;
123-
124- fn input_by_key ( key : & str ) -> & DiffTestHelper {
125- unsafe {
126- DIFF_TEST_INPUT
127- . iter ( )
128- . filter ( |data| data. key == key)
129- . nth ( 0 )
130- . unwrap ( )
131- }
132- }
133-
134- #[ ctor:: ctor]
135- fn diff_tests_setup ( ) {
119+ fn get_diff_test_helper_hash_map ( ) -> HashMap < String , DiffTestHelper > {
136120 let diff_test_helper_init_data = [
137121 ( "" , f1_txt_path ( ) , f2_txt_path ( ) , "test_diff_normal" ) ,
138122 ( " -c" , f1_txt_path ( ) , f2_txt_path ( ) , "test_diff_context3" ) ,
@@ -210,14 +194,33 @@ fn diff_tests_setup() {
210194 ) ,
211195 ] ;
212196
213- for row in diff_test_helper_init_data {
214- unsafe { DIFF_TEST_INPUT . push ( DiffTestHelper :: new ( row. 0 , row. 1 , row. 2 , row. 3 . to_string ( ) ) ) }
197+ let mut diff_test_helper_hash_map =
198+ HashMap :: < String , DiffTestHelper > :: with_capacity ( diff_test_helper_init_data. len ( ) ) ;
199+
200+ for ( options, file1_path, file2_path, key) in diff_test_helper_init_data {
201+ let insert_option = diff_test_helper_hash_map. insert (
202+ key. to_owned ( ) ,
203+ DiffTestHelper :: new ( options, file1_path, file2_path) ,
204+ ) ;
205+
206+ assert ! ( insert_option. is_none( ) ) ;
215207 }
208+
209+ diff_test_helper_hash_map
210+ }
211+
212+ fn input_by_key ( key : & str ) -> & ' static DiffTestHelper {
213+ static DIFF_TEST_INPUT : LazyLock < HashMap < String , DiffTestHelper > > =
214+ LazyLock :: new ( get_diff_test_helper_hash_map) ;
215+
216+ // Initialized on first access
217+ DIFF_TEST_INPUT . get ( key) . unwrap ( )
216218}
217219
218220#[ test]
219221fn test_diff_normal ( ) {
220222 let data = input_by_key ( "test_diff_normal" ) ;
223+
221224 diff_test (
222225 & [ data. file1_path ( ) , data. file2_path ( ) ] ,
223226 data. content ( ) ,
@@ -316,6 +319,7 @@ fn test_diff_unified10() {
316319#[ test]
317320fn test_diff_file_directory ( ) {
318321 let data = input_by_key ( "test_diff_file_directory" ) ;
322+
319323 diff_test (
320324 & [ data. file1_path ( ) , data. file2_path ( ) ] ,
321325 data. content ( ) ,
@@ -326,6 +330,7 @@ fn test_diff_file_directory() {
326330#[ test]
327331fn test_diff_directories ( ) {
328332 let data = input_by_key ( "test_diff_directories" ) ;
333+
329334 diff_test (
330335 & [ data. file1_path ( ) , data. file2_path ( ) ] ,
331336 data. content ( ) ,
@@ -391,6 +396,7 @@ fn test_diff_directories_recursive_unified() {
391396#[ test]
392397fn test_diff_counting_eol_spaces ( ) {
393398 let data = input_by_key ( "test_diff_counting_eol_spaces" ) ;
399+
394400 diff_test (
395401 & [ data. file1_path ( ) , data. file2_path ( ) ] ,
396402 data. content ( ) ,
0 commit comments