1+ use std:: f64:: consts:: PI ;
12use std:: fmt:: Display ;
3+ use std:: iter;
24
35use derive_more:: Display ;
46use proptest:: option;
57use proptest:: prelude:: * ;
68use proptest:: sample:: select;
9+ use proptest:: strategy:: ValueTree ;
10+ use proptest:: test_runner:: TestRunner ;
711use proptest_derive:: Arbitrary ;
812
913#[ derive( Debug , Arbitrary , Display , Clone ) ]
@@ -101,9 +105,97 @@ impl Display for FormatArgument {
101105 }
102106}
103107
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+
104195proptest ! {
105196 #[ 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] ) ) {
107199 let t = trybuild2:: TestCases :: new( ) ;
108200 let format_arg = format_arg. to_string( ) ;
109201 let format_arg = format_arg. escape_default( ) ;
@@ -125,7 +217,8 @@ proptest! {
125217 }
126218
127219 #[ test]
128- fn integer( format_arg in FormatArgument :: arbitrary_with( & [
220+ #[ ignore = "long runtime" ]
221+ fn integer_pt( format_arg in FormatArgument :: arbitrary_with( & [
129222 Trait :: Display , Trait :: Question , Trait :: XQuestion , Trait :: xQuestion,
130223 Trait :: x, Trait :: X , Trait :: b, Trait :: o, Trait :: e, Trait :: E
131224 ] ) ) {
@@ -145,7 +238,8 @@ proptest! {
145238 }
146239
147240 #[ test]
148- fn float( format_arg in FormatArgument :: arbitrary_with( & [
241+ #[ ignore = "long runtime" ]
242+ fn float_pt( format_arg in FormatArgument :: arbitrary_with( & [
149243 Trait :: Display , Trait :: Question , Trait :: XQuestion , Trait :: xQuestion, Trait :: e, Trait :: E
150244 ] ) ) {
151245 let t = trybuild2:: TestCases :: new( ) ;
@@ -164,7 +258,8 @@ proptest! {
164258 }
165259
166260 #[ 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] ) ) {
168263 let t = trybuild2:: TestCases :: new( ) ;
169264 let format_arg = format_arg. to_string( ) ;
170265 let format_arg = format_arg. escape_default( ) ;
0 commit comments