@@ -9,6 +9,7 @@ use proptest::sample::select;
9
9
use proptest:: strategy:: ValueTree ;
10
10
use proptest:: test_runner:: TestRunner ;
11
11
use proptest_derive:: Arbitrary ;
12
+ use quote:: { quote, ToTokens } ;
12
13
13
14
#[ derive( Debug , Arbitrary , Display , Clone ) ]
14
15
#[ allow( non_camel_case_types) ]
@@ -106,40 +107,39 @@ impl Display for FormatArgument {
106
107
}
107
108
108
109
fn test (
109
- value : impl Display ,
110
- converter : impl Display ,
110
+ value : impl ToTokens ,
111
+ converter : impl ToTokens ,
111
112
strategy : impl Strategy < Value = FormatArgument > ,
112
113
) {
113
114
let mut runner = TestRunner :: deterministic ( ) ;
114
- let values: Vec < _ > = iter:: repeat_with ( || strategy. new_tree ( & mut runner) )
115
- . take ( 100 )
116
- . map ( |s| s. unwrap ( ) . current ( ) )
117
- . map ( |format_arg| {
118
- let format_arg = format_arg. to_string ( ) ;
119
- let format_arg = format_arg. escape_default ( ) ;
120
- format ! ( r#"
121
- assert_eq!(
122
- format("{format_arg}", [("ident", Formattable::{converter}(&{value}))].into_iter().collect()).unwrap(),
123
- format!("{format_arg}", ident = {value}),
124
- "{{}}", "{format_arg}"
125
- );"# )
126
- } )
127
- . collect ( ) ;
128
- let values = values. join ( "\n " ) ;
115
+ let format_args = iter:: repeat_with ( || strategy. new_tree ( & mut runner) )
116
+ . take ( 1000 )
117
+ . map ( |s| s. unwrap ( ) . current ( ) . to_string ( ) ) ;
129
118
let t = trybuild2:: TestCases :: new ( ) ;
130
- t. pass_inline ( & converter. to_string ( ) , & format ! { r#"
131
- use template::{{format, Formattable}};
132
- fn main() {{
133
- {values}
134
- }}
135
- "# } ) ;
119
+ t. pass_inline (
120
+ & converter. to_token_stream ( ) . to_string ( ) ,
121
+ & quote ! {
122
+ use template:: { { format, Formattable } } ;
123
+ use std:: thread;
124
+ fn main( ) {
125
+ thread:: spawn( move ||{
126
+ let value = & [ ( "ident" , Formattable :: #converter( & #value) ) ] . into_iter( ) . collect( ) ;
127
+ #( assert!(
128
+ format( #format_args, value) . unwrap( ) == format!( #format_args, ident = #value) ,
129
+ "{}" , #format_args
130
+ ) ; ) *
131
+ } ) . join( ) . unwrap( ) ;
132
+ }
133
+ }
134
+ . to_string ( ) ,
135
+ ) ;
136
136
}
137
137
138
138
#[ test]
139
139
fn string ( ) {
140
140
test (
141
141
"\" test\" " ,
142
- " debug_display" ,
142
+ quote ! ( debug_display) ,
143
143
FormatArgument :: arbitrary_with ( & [
144
144
Trait :: Display ,
145
145
Trait :: Question ,
@@ -153,7 +153,7 @@ fn string() {
153
153
fn integer ( ) {
154
154
test (
155
155
42 ,
156
- " integer" ,
156
+ quote ! ( integer) ,
157
157
FormatArgument :: arbitrary_with ( & [
158
158
Trait :: Display ,
159
159
Trait :: Question ,
@@ -172,7 +172,7 @@ fn integer() {
172
172
fn float ( ) {
173
173
test (
174
174
PI ,
175
- " float" ,
175
+ quote ! ( float) ,
176
176
FormatArgument :: arbitrary_with ( & [
177
177
Trait :: Display ,
178
178
Trait :: Question ,
@@ -186,8 +186,8 @@ fn float() {
186
186
#[ test]
187
187
fn pointer ( ) {
188
188
test (
189
- " &42" ,
190
- " pointer" ,
189
+ quote ! ( & 42 ) ,
190
+ quote ! ( pointer) ,
191
191
FormatArgument :: arbitrary_with ( & [ Trait :: p] ) ,
192
192
) ;
193
193
}
0 commit comments