11use gdscript_formatter:: FormatterConfig ;
2- use gdscript_formatter:: formatter:: { format_gdscript , format_gdscript_with_config} ;
2+ use gdscript_formatter:: formatter:: format_gdscript_with_config;
33use similar:: { ChangeTag , TextDiff } ;
44use std:: fs;
55use std:: path:: Path ;
@@ -13,13 +13,15 @@ fn make_whitespace_visible(s: &str) -> String {
1313 . replace ( '\n' , "↲\n " )
1414}
1515
16- fn assert_formatted_eq ( result : & str , expected : & str , file_path : & Path ) {
16+ fn assert_formatted_eq (
17+ result : & str ,
18+ expected : & str ,
19+ file_path : & Path ,
20+ error_context_message : & str ,
21+ ) {
1722 if result != expected {
18- eprintln ! (
19- "\n Formatted output doesn't match expected for {}" ,
20- file_path. display( )
21- ) ;
22- eprintln ! ( "Diff between expected(-) and formatted output(+):" ) ;
23+ eprintln ! ( "\n {} - {}" , error_context_message, file_path. display( ) ) ;
24+ eprintln ! ( "Diff between expected(-) and actual output(+):" ) ;
2325 let diff = TextDiff :: from_lines ( expected, result) ;
2426 for change in diff. iter_all_changes ( ) {
2527 let text = make_whitespace_visible ( & change. to_string ( ) ) ;
@@ -34,34 +36,26 @@ fn assert_formatted_eq(result: &str, expected: &str, file_path: &Path) {
3436 eprintln ! ( "{:?}" , expected) ;
3537 eprintln ! ( "\n GOT (raw):" ) ;
3638 eprintln ! ( "{:?}" , result) ;
37- panic ! ( "Assertion failed" ) ;
39+ panic ! ( "Assertion failed: {}" , error_context_message ) ;
3840 }
3941}
4042
4143fn test_file ( file_path : & Path ) {
42- let file_name = file_path. file_name ( ) . expect ( "path is not a file path" ) ;
43-
44- let input_path = file_path;
45- let expected_path = file_path
46- . parent ( )
47- . unwrap ( )
48- . parent ( )
49- . unwrap ( )
50- . join ( "expected/" )
51- . join ( file_name) ;
52-
53- let input_content =
54- fs:: read_to_string ( & input_path) . expect ( & format ! ( "Failed to read {}" , input_path. display( ) ) ) ;
55- let expected_content = fs:: read_to_string ( & expected_path)
56- . expect ( & format ! ( "Failed to read {}" , expected_path. display( ) ) ) ;
57-
58- let result = format_gdscript ( & input_content)
59- . expect ( & format ! ( "Failed to format {}" , input_path. display( ) ) ) ;
60-
61- assert_formatted_eq ( & result, & expected_content, & input_path) ;
44+ test_file_with_config ( file_path, & FormatterConfig :: default ( ) , true ) ;
6245}
6346
6447fn test_reorder_file ( file_path : & Path ) {
48+ test_file_with_config (
49+ file_path,
50+ & FormatterConfig {
51+ reorder_code : true ,
52+ ..Default :: default ( )
53+ } ,
54+ true ,
55+ ) ;
56+ }
57+
58+ fn test_file_with_config ( file_path : & Path , config : & FormatterConfig , check_idempotence : bool ) {
6559 let file_name = file_path. file_name ( ) . expect ( "path is not a file path" ) ;
6660
6761 let input_path = file_path;
@@ -78,14 +72,24 @@ fn test_reorder_file(file_path: &Path) {
7872 let expected_content = fs:: read_to_string ( & expected_path)
7973 . expect ( & format ! ( "Failed to read {}" , expected_path. display( ) ) ) ;
8074
81- let result = format_gdscript_with_config (
82- & input_content,
83- & FormatterConfig {
84- reorder_code : true ,
85- ..Default :: default ( )
86- } ,
87- )
88- . expect ( & format ! ( "Failed to format {}" , input_path. display( ) ) ) ;
75+ let result = format_gdscript_with_config ( & input_content, config)
76+ . expect ( & format ! ( "Failed to format {}" , input_path. display( ) ) ) ;
77+
78+ assert_formatted_eq (
79+ & result,
80+ & expected_content,
81+ & input_path,
82+ "First formatting output doesn't match expected" ,
83+ ) ;
8984
90- assert_formatted_eq ( & result, & expected_content, & input_path) ;
85+ if check_idempotence {
86+ let second_result = format_gdscript_with_config ( & result, config)
87+ . expect ( & format ! ( "Failed to format {}" , input_path. display( ) ) ) ;
88+ assert_formatted_eq (
89+ & second_result,
90+ & result,
91+ & input_path,
92+ "Idempotence check failed, formatting a second time gave different results" ,
93+ ) ;
94+ }
9195}
0 commit comments