1
+ use std:: f64:: consts:: PI ;
1
2
use std:: fmt:: Display ;
3
+ use std:: iter;
2
4
3
5
use derive_more:: Display ;
4
6
use proptest:: option;
5
7
use proptest:: prelude:: * ;
6
8
use proptest:: sample:: select;
9
+ use proptest:: strategy:: ValueTree ;
10
+ use proptest:: test_runner:: TestRunner ;
7
11
use proptest_derive:: Arbitrary ;
8
12
9
13
#[ derive( Debug , Arbitrary , Display , Clone ) ]
@@ -101,9 +105,97 @@ impl Display for FormatArgument {
101
105
}
102
106
}
103
107
108
+ fn test (
109
+ value : impl Display ,
110
+ converter : impl Display ,
111
+ strategy : impl Strategy < Value = FormatArgument > ,
112
+ ) {
113
+ let mut runner = TestRunner :: default ( ) ;
114
+ let values: Vec < _ > = iter:: repeat_with ( || strategy. new_tree ( & mut runner) )
115
+ . take ( 1000 )
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 " ) ;
129
+ 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
+ "# } ) ;
136
+ }
137
+
138
+ #[ test]
139
+ fn string ( ) {
140
+ test (
141
+ "\" test\" " ,
142
+ "debug_display" ,
143
+ FormatArgument :: arbitrary_with ( & [
144
+ Trait :: Display ,
145
+ Trait :: Question ,
146
+ Trait :: XQuestion ,
147
+ Trait :: xQuestion,
148
+ ] ) ,
149
+ ) ;
150
+ }
151
+
152
+ #[ test]
153
+ fn integer ( ) {
154
+ test (
155
+ 42 ,
156
+ "integer" ,
157
+ FormatArgument :: arbitrary_with ( & [
158
+ Trait :: Display ,
159
+ Trait :: Question ,
160
+ Trait :: XQuestion ,
161
+ Trait :: xQuestion,
162
+ Trait :: x,
163
+ Trait :: X ,
164
+ Trait :: b,
165
+ Trait :: o,
166
+ Trait :: e,
167
+ Trait :: E ,
168
+ ] ) ,
169
+ ) ;
170
+ }
171
+ #[ test]
172
+ fn float ( ) {
173
+ test (
174
+ PI ,
175
+ "float" ,
176
+ FormatArgument :: arbitrary_with ( & [
177
+ Trait :: Display ,
178
+ Trait :: Question ,
179
+ Trait :: XQuestion ,
180
+ Trait :: xQuestion,
181
+ Trait :: e,
182
+ Trait :: E ,
183
+ ] ) ,
184
+ ) ;
185
+ }
186
+ #[ test]
187
+ fn pointer ( ) {
188
+ test (
189
+ "&42" ,
190
+ "pointer" ,
191
+ FormatArgument :: arbitrary_with ( & [ Trait :: p] ) ,
192
+ ) ;
193
+ }
194
+
104
195
proptest ! {
105
196
#[ test]
106
- fn string( format_arg in FormatArgument :: arbitrary_with( & [ Trait :: Display , Trait :: Question , Trait :: XQuestion , Trait :: xQuestion] ) ) {
197
+ #[ ignore = "long runtime" ]
198
+ fn string_pt( format_arg in FormatArgument :: arbitrary_with( & [ Trait :: Display , Trait :: Question , Trait :: XQuestion , Trait :: xQuestion] ) ) {
107
199
let t = trybuild2:: TestCases :: new( ) ;
108
200
let format_arg = format_arg. to_string( ) ;
109
201
let format_arg = format_arg. escape_default( ) ;
@@ -125,7 +217,8 @@ proptest! {
125
217
}
126
218
127
219
#[ test]
128
- fn integer( format_arg in FormatArgument :: arbitrary_with( & [
220
+ #[ ignore = "long runtime" ]
221
+ fn integer_pt( format_arg in FormatArgument :: arbitrary_with( & [
129
222
Trait :: Display , Trait :: Question , Trait :: XQuestion , Trait :: xQuestion,
130
223
Trait :: x, Trait :: X , Trait :: b, Trait :: o, Trait :: e, Trait :: E
131
224
] ) ) {
@@ -145,7 +238,8 @@ proptest! {
145
238
}
146
239
147
240
#[ test]
148
- fn float( format_arg in FormatArgument :: arbitrary_with( & [
241
+ #[ ignore = "long runtime" ]
242
+ fn float_pt( format_arg in FormatArgument :: arbitrary_with( & [
149
243
Trait :: Display , Trait :: Question , Trait :: XQuestion , Trait :: xQuestion, Trait :: e, Trait :: E
150
244
] ) ) {
151
245
let t = trybuild2:: TestCases :: new( ) ;
@@ -164,7 +258,8 @@ proptest! {
164
258
}
165
259
166
260
#[ test]
167
- fn pointer( format_arg in FormatArgument :: arbitrary_with( & [ Trait :: p] ) ) {
261
+ #[ ignore = "long runtime" ]
262
+ fn pointer_pt( format_arg in FormatArgument :: arbitrary_with( & [ Trait :: p] ) ) {
168
263
let t = trybuild2:: TestCases :: new( ) ;
169
264
let format_arg = format_arg. to_string( ) ;
170
265
let format_arg = format_arg. escape_default( ) ;
0 commit comments