@@ -13,9 +13,10 @@ mod constants;
13
13
14
14
use constants:: { EXIT_STATUS_DIFFERENCE , EXIT_STATUS_NO_DIFFERENCE } ;
15
15
use plib:: { run_test, TestPlan } ;
16
+ use std:: { collections:: HashMap , path:: PathBuf , process:: Stdio , sync:: LazyLock } ;
16
17
17
18
fn 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 ( ) ;
19
20
20
21
run_test ( TestPlan {
21
22
cmd : String :: from ( "diff" ) ,
@@ -27,8 +28,6 @@ fn diff_test(args: &[&str], expected_output: &str, expected_diff_exit_status: u8
27
28
} ) ;
28
29
}
29
30
30
- use std:: { path:: PathBuf , process:: Stdio } ;
31
-
32
31
fn diff_base_path ( ) -> PathBuf {
33
32
PathBuf :: from ( "tests" ) . join ( "diff" )
34
33
}
@@ -74,14 +73,13 @@ fn f1_txt_with_eol_spaces_path() -> String {
74
73
}
75
74
76
75
struct DiffTestHelper {
77
- pub key : String ,
78
76
content : String ,
79
77
file1_path : String ,
80
78
file2_path : String ,
81
79
}
82
80
83
81
impl 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 {
85
83
let args = format ! (
86
84
"run --release --bin diff --{} {} {}" ,
87
85
options, file1_path, file2_path
@@ -99,7 +97,6 @@ impl DiffTestHelper {
99
97
let content = String :: from_utf8 ( output. stdout ) . expect ( "Failed to read output of Command!" ) ;
100
98
101
99
Self {
102
- key,
103
100
file1_path,
104
101
file2_path,
105
102
content,
@@ -119,20 +116,7 @@ impl DiffTestHelper {
119
116
}
120
117
}
121
118
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 > {
136
120
let diff_test_helper_init_data = [
137
121
( "" , f1_txt_path ( ) , f2_txt_path ( ) , "test_diff_normal" ) ,
138
122
( " -c" , f1_txt_path ( ) , f2_txt_path ( ) , "test_diff_context3" ) ,
@@ -210,14 +194,33 @@ fn diff_tests_setup() {
210
194
) ,
211
195
] ;
212
196
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( ) ) ;
215
207
}
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 ( )
216
218
}
217
219
218
220
#[ test]
219
221
fn test_diff_normal ( ) {
220
222
let data = input_by_key ( "test_diff_normal" ) ;
223
+
221
224
diff_test (
222
225
& [ data. file1_path ( ) , data. file2_path ( ) ] ,
223
226
data. content ( ) ,
@@ -316,6 +319,7 @@ fn test_diff_unified10() {
316
319
#[ test]
317
320
fn test_diff_file_directory ( ) {
318
321
let data = input_by_key ( "test_diff_file_directory" ) ;
322
+
319
323
diff_test (
320
324
& [ data. file1_path ( ) , data. file2_path ( ) ] ,
321
325
data. content ( ) ,
@@ -326,6 +330,7 @@ fn test_diff_file_directory() {
326
330
#[ test]
327
331
fn test_diff_directories ( ) {
328
332
let data = input_by_key ( "test_diff_directories" ) ;
333
+
329
334
diff_test (
330
335
& [ data. file1_path ( ) , data. file2_path ( ) ] ,
331
336
data. content ( ) ,
@@ -391,6 +396,7 @@ fn test_diff_directories_recursive_unified() {
391
396
#[ test]
392
397
fn test_diff_counting_eol_spaces ( ) {
393
398
let data = input_by_key ( "test_diff_counting_eol_spaces" ) ;
399
+
394
400
diff_test (
395
401
& [ data. file1_path ( ) , data. file2_path ( ) ] ,
396
402
data. content ( ) ,
0 commit comments