@@ -2,7 +2,7 @@ use syn::spanned::Spanned;
2
2
use syn:: visit:: Visit ;
3
3
4
4
use crate :: ffi_items:: FfiItems ;
5
- use crate :: translator:: Translator ;
5
+ use crate :: translator:: { TranslationErrorKind , Translator } ;
6
6
use crate :: { Result , TestGenerator , TranslationError , cdecl} ;
7
7
8
8
const ALL_ITEMS : & str = r#"
@@ -44,13 +44,13 @@ fn r2cdecl(s: &str, name: &str) -> Result<String, TranslationError> {
44
44
let translator = Translator :: new ( & ffi_items, & generator) ;
45
45
let ty: syn:: Type = syn:: parse_str ( s) . unwrap ( ) ;
46
46
let translated = translator. translate_type ( & ty) ?;
47
- cdecl:: cdecl ( & translated, name. to_string ( ) ) . map_err ( |_| {
48
- TranslationError :: new (
49
- crate :: translator :: TranslationErrorKind :: InvalidReturn ,
50
- s ,
51
- ty . span ( ) ,
52
- )
53
- } )
47
+ cdecl:: cdecl ( & translated, name. to_string ( ) )
48
+ . map_err ( |_| TranslationError :: new ( TranslationErrorKind :: InvalidReturn , s , ty . span ( ) ) )
49
+ }
50
+
51
+ # [ track_caller ]
52
+ fn assert_r2cdecl ( rust : & str , expected : & str ) {
53
+ assert_eq ! ( r2cdecl ( rust , "foo" ) . unwrap ( ) , expected )
54
54
}
55
55
56
56
#[ test]
@@ -70,55 +70,37 @@ fn test_extraction_ffi_items() {
70
70
71
71
#[ test]
72
72
fn test_translation_type_ptr ( ) {
73
- assert_eq ! (
74
- r2cdecl( "*const *mut i32" , "" ) . unwrap( ) ,
75
- "int32_t *const *" . to_string( )
76
- ) ;
77
- assert_eq ! (
78
- r2cdecl( "*const [u128; 2 + 3]" , "" ) . unwrap( ) ,
79
- "unsigned __int128 (*)[2 + 3]" . to_string( )
80
- ) ;
81
- assert_eq ! (
82
- r2cdecl( "*const *mut [u8; 5]" , "" ) . unwrap( ) ,
83
- "uint8_t (*const *)[5]" . to_string( )
84
- ) ;
73
+ assert_r2cdecl ( "*const *mut i32" , "int32_t *const *foo" ) ;
74
+ assert_r2cdecl ( "*const [u128; 2 + 3]" , "unsigned __int128 (*foo)[2 + 3]" ) ;
75
+ assert_r2cdecl ( "*const *mut [u8; 5]" , "uint8_t (*const *foo)[5]" ) ;
76
+ assert_r2cdecl ( "*mut *const [u8; 5]" , "uint8_t (**foo)[5]" ) ;
77
+ assert_r2cdecl ( "*const *const [u8; 5]" , "uint8_t (*const *foo)[5]" ) ;
78
+ assert_r2cdecl ( "*mut *mut [u8; 5]" , "uint8_t (**foo)[5]" ) ;
85
79
}
86
80
87
81
#[ test]
88
82
fn test_translation_type_reference ( ) {
89
- assert_eq ! ( r2cdecl( "&u8" , "" ) . unwrap( ) , "const uint8_t *" . to_string( ) ) ;
90
- assert_eq ! (
91
- r2cdecl( "&&u8" , "" ) . unwrap( ) ,
92
- "const uint8_t *const *" . to_string( )
93
- ) ;
94
- assert_eq ! (
95
- r2cdecl( "*mut &u8" , "" ) . unwrap( ) ,
96
- "const uint8_t **" . to_string( )
97
- ) ;
98
- assert_eq ! (
99
- r2cdecl( "& &mut u8" , "" ) . unwrap( ) ,
100
- "uint8_t *const *" . to_string( )
101
- ) ;
83
+ assert_r2cdecl ( "&u8" , "const uint8_t *foo" ) ;
84
+ assert_r2cdecl ( "&&u8" , "const uint8_t *const *foo" ) ;
85
+ assert_r2cdecl ( "*mut &u8" , "const uint8_t **foo" ) ;
86
+ assert_r2cdecl ( "& &mut u8" , "uint8_t *const *foo" ) ;
102
87
}
103
88
104
89
#[ test]
105
90
fn test_translation_type_bare_fn ( ) {
106
- assert_eq ! (
107
- r2cdecl ( "fn(*mut u8, i16) -> *const char" , "" ) . unwrap ( ) ,
108
- "const char *(*)(uint8_t *, int16_t)" . to_string ( )
91
+ assert_r2cdecl (
92
+ "fn(*mut u8, i16) -> *const char" ,
93
+ "const char *(*foo )(uint8_t *, int16_t)" ,
109
94
) ;
110
- assert_eq ! (
111
- r2cdecl ( "*const fn(*mut u8, &mut [u8; 16]) -> &mut *mut u8" , "" ) . unwrap ( ) ,
112
- "uint8_t **(*const *)(uint8_t *, uint8_t (*)[16])" . to_string ( )
95
+ assert_r2cdecl (
96
+ "*const fn(*mut u8, &mut [u8; 16]) -> &mut *mut u8" ,
97
+ "uint8_t **(*const *foo )(uint8_t *, uint8_t (*)[16])" ,
113
98
) ;
114
99
}
115
100
116
101
#[ test]
117
102
fn test_translation_type_array ( ) {
118
- assert_eq ! (
119
- r2cdecl( "[&u8; 2 + 2]" , "" ) . unwrap( ) ,
120
- "const uint8_t *[2 + 2]" . to_string( )
121
- ) ;
103
+ assert_r2cdecl ( "[&u8; 2 + 2]" , "const uint8_t *foo[2 + 2]" ) ;
122
104
}
123
105
124
106
#[ test]
@@ -129,25 +111,19 @@ fn test_translation_fails_for_unsupported() {
129
111
130
112
#[ test]
131
113
fn test_translate_helper_function_pointer ( ) {
132
- assert_eq ! (
133
- r2cdecl ( "extern \" C\" fn(c_int) -> *const c_void" , "test_make_cdecl" ) . unwrap ( ) ,
134
- "const void *(*test_make_cdecl )(int)"
114
+ assert_r2cdecl (
115
+ "extern \" C\" fn(c_int) -> *const c_void" ,
116
+ "const void *(*foo )(int)" ,
135
117
) ;
136
118
// FIXME(ctest): Reimplement support for ABI in a more robust way.
137
- // assert_eq! (
138
- // cdecl( "Option<extern \"stdcall\" fn(*const c_char, [u32; 16]) -> u8>").unwrap() ,
139
- // "uint8_t (__stdcall **test_make_cdecl )(const char *, uint32_t [16])"
119
+ // assert_r2cdecl (
120
+ // "Option<extern \"stdcall\" fn(*const c_char, [u32; 16]) -> u8>",
121
+ // "uint8_t (__stdcall **foo )(const char *, uint32_t [16])"
140
122
// );
141
123
}
142
124
143
125
#[ test]
144
126
fn test_translate_helper_array_1d_2d ( ) {
145
- assert_eq ! (
146
- r2cdecl( "[u8; 10]" , "test_make_cdecl" ) . unwrap( ) ,
147
- "uint8_t test_make_cdecl[10]" ,
148
- ) ;
149
- assert_eq ! (
150
- r2cdecl( "[[u8; 64]; 32]" , "test_make_cdecl" ) . unwrap( ) ,
151
- "uint8_t test_make_cdecl[32][64]"
152
- ) ;
127
+ assert_r2cdecl ( "[u8; 10]" , "uint8_t foo[10]" ) ;
128
+ assert_r2cdecl ( "[[u8; 64]; 32]" , "uint8_t foo[32][64]" ) ;
153
129
}
0 commit comments