@@ -18,92 +18,87 @@ use crate::{
18
18
util:: list_rust_files,
19
19
} ;
20
20
21
- const PARSER_CRATE_ROOT : & str = "crates/parser" ;
22
- const PARSER_TEST_DATA : & str = "crates/parser/test_data" ;
23
- const PARSER_TEST_DATA_INLINE : & str = "crates/parser/test_data/parser/inline" ;
24
-
25
21
pub ( crate ) fn generate ( check : bool ) {
26
- let tests = tests_from_dir (
27
- & project_root ( ) . join ( Path :: new ( & format ! ( "{PARSER_CRATE_ROOT}/src/grammar" ) ) ) ,
28
- ) ;
22
+ let parser_crate_root = project_root ( ) . join ( "crates/parser" ) ;
23
+ let parser_test_data = parser_crate_root. join ( "test_data" ) ;
24
+ let parser_test_data_inline = parser_test_data. join ( "parser/inline" ) ;
25
+
26
+ let tests = tests_from_dir ( & parser_crate_root. join ( "src/grammar" ) ) ;
29
27
30
28
let mut some_file_was_updated = false ;
31
29
some_file_was_updated |=
32
- install_tests ( & tests. ok , & format ! ( "{PARSER_TEST_DATA_INLINE}/ ok") , check) . unwrap ( ) ;
30
+ install_tests ( & tests. ok , parser_test_data_inline . join ( " ok") , check) . unwrap ( ) ;
33
31
some_file_was_updated |=
34
- install_tests ( & tests. err , & format ! ( "{PARSER_TEST_DATA_INLINE}/ err") , check) . unwrap ( ) ;
32
+ install_tests ( & tests. err , parser_test_data_inline . join ( " err") , check) . unwrap ( ) ;
35
33
36
34
if some_file_was_updated {
37
- let _ = fs:: File :: open ( format ! ( "{PARSER_CRATE_ROOT}/ src/tests.rs") )
35
+ let _ = fs:: File :: open ( parser_crate_root . join ( " src/tests.rs") )
38
36
. unwrap ( )
39
37
. set_modified ( SystemTime :: now ( ) ) ;
38
+ }
40
39
41
- let ok_tests = tests. ok . values ( ) . sorted_by ( |a, b| a. name . cmp ( & b. name ) ) . map ( |test| {
42
- let test_name = quote:: format_ident!( "{}" , test. name) ;
43
- let test_file = format ! ( "test_data/parser/inline/ok/{test_name}.rs" ) ;
44
- let ( test_func, args) = match & test. edition {
45
- Some ( edition) => {
46
- let edition = quote:: format_ident!( "Edition{edition}" ) ;
47
- (
48
- quote:: format_ident!( "run_and_expect_no_errors_with_edition" ) ,
49
- quote:: quote! { #test_file, crate :: Edition :: #edition} ,
50
- )
51
- }
52
- None => {
53
- ( quote:: format_ident!( "run_and_expect_no_errors" ) , quote:: quote! { #test_file} )
54
- }
55
- } ;
56
- quote:: quote! {
57
- #[ test]
58
- fn #test_name( ) {
59
- #test_func( #args) ;
60
- }
61
- }
62
- } ) ;
63
- let err_tests = tests. err . values ( ) . sorted_by ( |a, b| a. name . cmp ( & b. name ) ) . map ( |test| {
64
- let test_name = quote:: format_ident!( "{}" , test. name) ;
65
- let test_file = format ! ( "test_data/parser/inline/err/{test_name}.rs" ) ;
66
- let ( test_func, args) = match & test. edition {
67
- Some ( edition) => {
68
- let edition = quote:: format_ident!( "Edition{edition}" ) ;
69
- (
70
- quote:: format_ident!( "run_and_expect_errors_with_edition" ) ,
71
- quote:: quote! { #test_file, crate :: Edition :: #edition} ,
72
- )
73
- }
74
- None => ( quote:: format_ident!( "run_and_expect_errors" ) , quote:: quote! { #test_file} ) ,
75
- } ;
76
- quote:: quote! {
77
- #[ test]
78
- fn #test_name( ) {
79
- #test_func( #args) ;
80
- }
40
+ let ok_tests = tests. ok . values ( ) . sorted_by ( |a, b| a. name . cmp ( & b. name ) ) . map ( |test| {
41
+ let test_name = quote:: format_ident!( "{}" , test. name) ;
42
+ let test_file = format ! ( "test_data/parser/inline/ok/{test_name}.rs" ) ;
43
+ let ( test_func, args) = match & test. edition {
44
+ Some ( edition) => {
45
+ let edition = quote:: format_ident!( "Edition{edition}" ) ;
46
+ (
47
+ quote:: format_ident!( "run_and_expect_no_errors_with_edition" ) ,
48
+ quote:: quote! { #test_file, crate :: Edition :: #edition} ,
49
+ )
81
50
}
82
- } ) ;
83
-
84
- let output = quote:: quote! {
85
- mod ok {
86
- use crate :: tests :: * ;
87
- #( #ok_tests ) *
51
+ None => ( quote :: format_ident! ( "run_and_expect_no_errors" ) , quote :: quote! { #test_file } ) ,
52
+ } ;
53
+ quote:: quote! {
54
+ # [ test ]
55
+ fn #test_name ( ) {
56
+ #test_func ( #args ) ;
88
57
}
89
- mod err {
90
- use crate :: tests:: * ;
91
- #( #err_tests) *
58
+ }
59
+ } ) ;
60
+ let err_tests = tests. err . values ( ) . sorted_by ( |a, b| a. name . cmp ( & b. name ) ) . map ( |test| {
61
+ let test_name = quote:: format_ident!( "{}" , test. name) ;
62
+ let test_file = format ! ( "test_data/parser/inline/err/{test_name}.rs" ) ;
63
+ let ( test_func, args) = match & test. edition {
64
+ Some ( edition) => {
65
+ let edition = quote:: format_ident!( "Edition{edition}" ) ;
66
+ (
67
+ quote:: format_ident!( "run_and_expect_errors_with_edition" ) ,
68
+ quote:: quote! { #test_file, crate :: Edition :: #edition} ,
69
+ )
92
70
}
71
+ None => ( quote:: format_ident!( "run_and_expect_errors" ) , quote:: quote! { #test_file} ) ,
93
72
} ;
73
+ quote:: quote! {
74
+ #[ test]
75
+ fn #test_name( ) {
76
+ #test_func( #args) ;
77
+ }
78
+ }
79
+ } ) ;
94
80
95
- let pretty = reformat ( output. to_string ( ) ) ;
96
- ensure_file_contents (
97
- crate :: flags:: CodegenType :: ParserTests ,
98
- format ! ( "{PARSER_TEST_DATA}/generated/runner.rs" ) . as_ref ( ) ,
99
- & pretty,
100
- check,
101
- ) ;
102
- }
81
+ let output = quote:: quote! {
82
+ mod ok {
83
+ use crate :: tests:: * ;
84
+ #( #ok_tests) *
85
+ }
86
+ mod err {
87
+ use crate :: tests:: * ;
88
+ #( #err_tests) *
89
+ }
90
+ } ;
91
+
92
+ let pretty = reformat ( output. to_string ( ) ) ;
93
+ ensure_file_contents (
94
+ crate :: flags:: CodegenType :: ParserTests ,
95
+ parser_test_data. join ( "generated/runner.rs" ) . as_ref ( ) ,
96
+ & pretty,
97
+ check,
98
+ ) ;
103
99
}
104
100
105
- fn install_tests ( tests : & HashMap < String , Test > , into : & str , check : bool ) -> Result < bool > {
106
- let tests_dir = project_root ( ) . join ( into) ;
101
+ fn install_tests ( tests : & HashMap < String , Test > , tests_dir : PathBuf , check : bool ) -> Result < bool > {
107
102
if !tests_dir. is_dir ( ) {
108
103
fs:: create_dir_all ( & tests_dir) ?;
109
104
}
0 commit comments