Skip to content

Commit 44e54ab

Browse files
committed
working on windows
1 parent f1092b4 commit 44e54ab

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ documentation = "https://docs.rs/template"
1414
derive_more = "0.99.17"
1515
proptest = "1.1.0"
1616
proptest-derive = "0.3.0"
17+
quote = "1.0.23"
1718
trybuild2 = "1.0.0"

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl<'a> FormatArgument<'a> {
261261

262262
pub fn format<K: Borrow<str> + Eq + Hash>(
263263
mut format: &str,
264-
context: HashMap<K, Formattable>,
264+
context: &HashMap<K, Formattable>,
265265
) -> Option<String> {
266266
let format = &mut format;
267267
let mut out = String::with_capacity(format.len());
@@ -339,7 +339,7 @@ fn test_format() {
339339
assert_eq!(
340340
format(
341341
"{ident:#p}",
342-
[("ident", Formattable::pointer(&p))].into_iter().collect(),
342+
&[("ident", Formattable::pointer(&p))].into_iter().collect(),
343343
)
344344
.unwrap(),
345345
format!("{ident:#p}", ident = p)
@@ -353,13 +353,13 @@ fn test_format() {
353353
// format!("{ident:a>+09.15}", ident = "hi")
354354
// );
355355
assert_eq!(
356-
format("{{hello}}", HashMap::<String, Formattable>::new()).unwrap(),
356+
format("{{hello}}", &HashMap::<String, Formattable>::new()).unwrap(),
357357
"{hello}"
358358
);
359359
assert_eq!(
360360
format(
361361
"{hi:10} {hi:?} {int:#x?} {int:b} {int:#X} {int:4o} {display} {display:5} {display:05}",
362-
[
362+
&[
363363
("hi", Formattable::debug_display(&"hello")),
364364
("int", Formattable::integer(&123u8)),
365365
("display", (&10).into())

tests/test.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use proptest::sample::select;
99
use proptest::strategy::ValueTree;
1010
use proptest::test_runner::TestRunner;
1111
use proptest_derive::Arbitrary;
12+
use quote::{quote, ToTokens};
1213

1314
#[derive(Debug, Arbitrary, Display, Clone)]
1415
#[allow(non_camel_case_types)]
@@ -106,40 +107,39 @@ impl Display for FormatArgument {
106107
}
107108

108109
fn test(
109-
value: impl Display,
110-
converter: impl Display,
110+
value: impl ToTokens,
111+
converter: impl ToTokens,
111112
strategy: impl Strategy<Value = FormatArgument>,
112113
) {
113114
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());
129118
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+
);
136136
}
137137

138138
#[test]
139139
fn string() {
140140
test(
141141
"\"test\"",
142-
"debug_display",
142+
quote!(debug_display),
143143
FormatArgument::arbitrary_with(&[
144144
Trait::Display,
145145
Trait::Question,
@@ -153,7 +153,7 @@ fn string() {
153153
fn integer() {
154154
test(
155155
42,
156-
"integer",
156+
quote!(integer),
157157
FormatArgument::arbitrary_with(&[
158158
Trait::Display,
159159
Trait::Question,
@@ -172,7 +172,7 @@ fn integer() {
172172
fn float() {
173173
test(
174174
PI,
175-
"float",
175+
quote!(float),
176176
FormatArgument::arbitrary_with(&[
177177
Trait::Display,
178178
Trait::Question,
@@ -186,8 +186,8 @@ fn float() {
186186
#[test]
187187
fn pointer() {
188188
test(
189-
"&42",
190-
"pointer",
189+
quote!(&42),
190+
quote!(pointer),
191191
FormatArgument::arbitrary_with(&[Trait::p]),
192192
);
193193
}

0 commit comments

Comments
 (0)