Debian Codegraph @

src/tests/strfmt.rs

raw symbols SCIP
1 #![allow(unused_variables)]
2
3 use superrust-analyzer cargo strfmt 0.2.4 tests/::superrust-analyzer cargo strfmt 0.2.4 crate/::*;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
6
7 macro_rules! matchesrust-analyzer cargo strfmt 0.2.4 tests/strfmt/matches! {
8 ($e:expr, $p:pat) => {
9 match $e {
10 $p => true,
11 _ => false,
12 }
13 };
14 }
15
16 fn run_testsrust-analyzer cargo strfmt 0.2.4 tests/strfmt/run_tests().<T: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&str, &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, T>) -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>(
17 values: &Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(&str, &str, u8)>,
18 vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, T>,
19 call: F,
20 ) {
21 let mut call = call;
22 for &(fmtstr, expected, expect_err) in values.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
23 let result = call(fmtstr, vars);
24 let mut failure = match expect_err {
25 0 => result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().(),
26 1 => !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo strfmt 0.2.4 tests/strfmt/matches!!(result, Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(FmtErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#::Invalidrust-analyzer cargo strfmt 0.2.4 types/FmtError#Invalid#(_))),
27 2 => !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo strfmt 0.2.4 tests/strfmt/matches!!(result, Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(FmtErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#::KeyErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#KeyError#(_))),
28 3 => !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo strfmt 0.2.4 tests/strfmt/matches!!(result, Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(FmtErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#::TypeErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#TypeError#(_))),
29 c @ _ => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("error code {} DNE", c),
30 };
31 let result = match result {
32 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
33 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(s) => s,
34 };
35 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().failure && expect_err rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0 {
36 failure = !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().(expected rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`&str`][`PartialEq<String>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`&str`][`PartialEq<String>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[`&str`][`PartialEq<String>`]eq().result);
37 }
38
39 if failure {
40 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("FAIL:");
41 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" input: {:?}", fmtstr);
42 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" output: {:?}", result);
43 if expect_err rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().0 {
44 let expected = match expect_err {
45 1 => "FmtError::Invalid",
46 2 => "FmtError::KeyError",
47 3 => "FmtError::TypeError",
48 _ => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(),
49 };
50 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" expected: {}", expected)
51 } else {
52 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!(" expected: {:?}", expected);
53 }
54 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(false);
55 }
56 }
57 }
58
59 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
60 fn test_valuesrust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_values().() {
61 let mut vars: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().();
62 let too_long = "toooloooong".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
63 let rust_unicode = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(">{:^7}<", "ಠ_ಠ");
64 vars.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().("x".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "X".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
65 vars.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().("long".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), too_long.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().()); // len=10
66 vars.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().("hi".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "hi".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
67 vars.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().("unicode".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "ಠ_ಠ".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
68
69 // format, expected, error
70 // error codes: 0 == no error, 1 == Invalid, 2 == KeyError
71 let values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(&str, &str, u8)> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
72 // simple positioning
73 ("{x}", "X", 0),
74 ("{x:}", "X", 0),
75 ("{x:3}", "X ", 0),
76 ("{x:>3}", " X", 0),
77 ("{x:<3}", "X ", 0),
78 ("{x:^3}", " X ", 0),
79 ("{x:^4}", " X ", 0),
80 // extra text
81 (" {x}yz", " Xyz", 0),
82 (" hi {x:^4}-you rock", " hi X -you rock", 0),
83 // unicode
84 (">{unicode:^7}<", "> ಠ_ಠ <", 0),
85 (">{unicode:^7}<", &rust_unicode, 0),
86 ("{unicode:<5}", "ಠ_ಠ ", 0),
87 ("{unicode:>5}", " ಠ_ಠ", 0),
88 // fill confusion
89 ("{x:10}", "X ", 0),
90 ("{x:>10}", " X", 0),
91 ("{x:0<5}", "X0000", 0),
92 ("{x:0>5}", "0000X", 0),
93 ("{long:.3}", "too", 0),
94 ("{long:5.3}", "too ", 0),
95 ("{long:>5.3}", " too", 0),
96 ("{long:5.7}", "toooloo", 0),
97 ("{long:<5.7}", "toooloo", 0),
98 ("{long:>5.7}", "toooloo", 0),
99 ("{long:^5.7}", "toooloo", 0),
100 ("{long:<}", &too_long, 0),
101 ("{long:<<}", &too_long, 0),
102 ("{long:<<5}", &too_long, 0),
103 // valid types
104 ("{x:<4s}", "X ", 0),
105 // escape
106 ("{{}}", "{}", 0),
107 ("{{long}}", "{long}", 0),
108 ("{{{x}}}", "{X}", 0),
109 // fun
110 ("{x:<>}", "X", 0),
111 ("{x:<>3}", "<<X", 0),
112 ("{{}}", "{}", 0),
113 ("{{{x}}}", "{X}", 0),
114 ("{{{x}{{{{{{", "{X{{{", 0),
115 ("{x}}}}}", "X}}", 0),
116 // invalid fmt
117 ("{}", "", 1),
118 ("{:3}", "", 1),
119 ("{xxx: <88.3}", "", 1),
120 // invalid escape
121 ("}", "", 1),
122 ("{{}}}", "", 1),
123 ("hi } there", "", 1),
124 ("hi }", "", 1),
125 ("w { ho", "", 1),
126 // invalid keys
127 ("{what}", "{}", 2),
128 ("{who}", "{}", 2),
129 ("{x} {where}", "{}", 2),
130 // invalid types
131 ("{x:<<<}", "", 3),
132 ("{x:*}", "", 3),
133 ("{x::}", "", 3),
134 ("{x:#}", "", 3),
135 ("{x:<4n}", "", 3),
136 ("{x:<4d}", "", 3),
137 ("{x:,}", "", 3),
138 ("{x:<-10}", "", 3),
139 // TODO
140 ("{x:0=5}", "00X00", 1),
141 ("{x:03}", "00X", 1),
142 ];
143
144 run_testsrust-analyzer cargo strfmt 0.2.4 tests/strfmt/run_tests().(&values, &vars, &strfmtrust-analyzer cargo strfmt 0.2.4 strfmt().);
145 }
146
147 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
148 /// test using integers directly into format (uses Display)
149 fn test_ints_basicrust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_ints_basic().() {
150 let mut vars: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, u64> = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().();
151 vars.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().("x".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), 6);
152 vars.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().("long".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), 100000); // len=10
153 vars.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().("hi".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), 42);
154
155 // format, expected, error
156 // error codes: 0 == no error, 1 == Invalid, 2 == KeyError
157 let values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(&str, &str, u8)> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
158 // simple positioning
159 ("{x}", "6", 0),
160 ("{long}", "100000", 0),
161 (
162 " the answer is {hi}, haven't you read anything?",
163 " the answer is 42, haven't you read anything?",
164 0,
165 ),
166 ];
167
168 run_testsrust-analyzer cargo strfmt 0.2.4 tests/strfmt/run_tests().(&values, &vars, &strfmtrust-analyzer cargo strfmt 0.2.4 strfmt().);
169 }
170
171 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
172 fn test_ignore_missingrust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_ignore_missing().() {
173 let mut vars: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().();
174 vars.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().("x".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "X".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
175 let values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<(&str, &str, u8)> = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
176 // simple positioning
177 ("{y}", "{y}", 0),
178 ("{y} {x}", "{y} X", 0),
179 (
180 "{x} {longish:<32.3} {x} is nice",
181 "X {longish:<32.3} X is nice",
182 0,
183 ),
184 ];
185 let f = |mut fmt: Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#| match vars.getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(fmt.keyrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#key.) {
186 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(v) => fmt.strrust-analyzer cargo strfmt 0.2.4 fmtstr/impl#[`Formatter<'a, 'b>`]str().(v),
187 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => fmt.skiprust-analyzer cargo strfmt 0.2.4 formatter/impl#[`Formatter<'a, 'b>`]skip().(),
188 };
189
190 let strfmt_ignore =
191 |fmtstr: &str, vars: &HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>| -> Resultrust-analyzer cargo strfmt 0.2.4 types/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { strfmt_maprust-analyzer cargo strfmt 0.2.4 fmtstr/strfmt_map().(fmtstr, &f) };
192 run_testsrust-analyzer cargo strfmt 0.2.4 tests/strfmt/run_tests().(&values, &vars, &strfmt_ignore);
193 }
194
195 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
196 fn test_mut_closurerust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_mut_closure().() {
197
198 let mut key_list = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![];
199 let f = |fmt: Formatterrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#| {
200 match fmt.keyrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#key..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>() {
201 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(key) => {
202 key_list.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(key);
203 },
204 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {
205 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(FmtErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#::KeyErrorrust-analyzer cargo strfmt 0.2.4 types/FmtError#KeyError#(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Invalid key: {}", fmt.keyrust-analyzer cargo strfmt 0.2.4 formatter/Formatter#key.)));
206 }
207 };
208 fmt.skiprust-analyzer cargo strfmt 0.2.4 formatter/impl#[`Formatter<'a, 'b>`]skip().()
209 };
210
211 let _ = strfmt_maprust-analyzer cargo strfmt 0.2.4 fmtstr/strfmt_map().("{one} two {three:4.4}", f).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
212
213 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(key_list, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["one", "three"]);
214 }
215
216
217 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
218 fn test_trailing_commarust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_trailing_comma().() {
219 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(strfmtrust-analyzer cargo strfmt 0.2.4 macros/strfmt!!("{foo}", foo => "bar", ) == Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#("bar".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()));
220 let foo = "bar";
221 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(strfmtrust-analyzer cargo strfmt 0.2.4 macros/strfmt!!("{foo}", foo,) == Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#("bar".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()));
222 }
223
224 macro_rules! test_floatrust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_float! {
225 ($($name:ident $t:ident),*) => ($(
226 #[test]
227 fn $name() {
228 let mut vars: HashMap<String, $t> = HashMap::new();
229 vars.insert("x".to_string(), 42.4242);
230 vars.insert("y".to_string(), -100.11111);
231 vars.insert("z".to_string(), 0.);
232 let values: Vec<(&str, &str, u8)> = vec![
233 // simple valid
234 ("{x}", "42.4242", 0),
235 ("{x:.2}", "42.42", 0),
236 ("{x:<7.2}", "42.42 ", 0),
237 ("{x:.2e}", "4.24e1", 0),
238 ("{x:.2E}", "4.24E1", 0),
239 ("{x:+}", "+42.4242", 0),
240 ("{y:.2E}", "-1.00E2", 0),
241 ("{y:+.2E}", "-1.00E2", 0),
242 ("{z:+.2E}", "+0.00E0", 0),
243
244 // invalid
245 ("{x:s}", "", 3),
246 ("{x:#}", "", 3),
247
248 // TODO
249 ("{x:+010.2}", "+0042.4242", 1),
250 ];
251 let f = |mut fmt: Formatter| {
252 match vars.get(fmt.key) {
253 Some(v) => fmt.$t(*v),
254 None => panic!(),
255 }
256 };
257
258 let strfmt_float = |fmtstr: &str, vars: &HashMap<String, $t>| -> Result<String> {
259 strfmt_map(fmtstr, &f)
260 };
261 run_tests(&values, &vars, &strfmt_float);
262 }
263 )*)
264 }
265
266 test_floatrust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_float!!(test_f32rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_f32(). f32, test_f64rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_f64(). f64);
267
268 macro_rules! test_uintrust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_uint! {
269 ($($name:ident $t:ident),*) => ($(
270 #[test]
271 fn $name() {
272 let mut vars: HashMap<String, $t> = HashMap::new();
273 vars.insert("x".to_string(), 42);
274 vars.insert("y".to_string(), 0);
275 let values: Vec<(&str, &str, u8)> = vec![
276 ("{x}", "42", 0),
277 ("{x:<7}", "42 ", 0),
278 ("{x:>7}", " 42", 0),
279 ("{x:^7}", " 42 ", 0),
280 ("{x:x}", "2a", 0),
281 ("{x:X}", "2A", 0),
282 ("{x:+x}", "+2a", 0),
283 ("{x:#x}", "0x2a", 0),
284 ("{x:#X}", "0x2A", 0),
285 ("{x:b}", "101010", 0),
286 ("{x:#b}", "0b101010", 0),
287 ("{x:o}", "52", 0),
288 ("{x:#o}", "0o52", 0),
289
290 ("{x:+}", "+42", 0),
291 ("{y:-}", "0", 0),
292 ("{y:+}", "+0", 0),
293
294 // invalid
295 ("{x:.2}", "", 3),
296 ("{x:s}", "", 3),
297
298 // TODO
299 ("{x:+010}", "+000000042", 1),
300 ];
301 let f = |mut fmt: Formatter| {
302 match vars.get(fmt.key) {
303 Some(v) => fmt.$t(*v),
304 None => panic!(),
305 }
306 };
307
308 let strfmt_int = |fmtstr: &str, vars: &HashMap<String, $t>| -> Result<String> {
309 strfmt_map(fmtstr, &f)
310 };
311 run_tests(&values, &vars, &strfmt_int);
312 }
313 )*)
314 }
315
316 macro_rules! test_intrust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_int! {
317 ($($name:ident $t:ident),*) => ($(
318 #[test]
319 fn $name() {
320 let mut vars: HashMap<String, $t> = HashMap::new();
321 vars.insert("x".to_string(), 42);
322 vars.insert("y".to_string(), -100);
323 vars.insert("z".to_string(), 0);
324 let values: Vec<(&str, &str, u8)> = vec![
325 // simple valid
326 ("{x}", "42", 0),
327 ("{x:<7}", "42 ", 0),
328 ("{x:X}", "2A", 0),
329 ("{x:#x}", "0x2a", 0),
330 ("{x:#X}", "0x2A", 0),
331 ("{x:b}", "101010", 0),
332 ("{x:#b}", "0b101010", 0),
333 ("{x:o}", "52", 0),
334 ("{x:#o}", "0o52", 0),
335
336 ("{x:+}", "+42", 0),
337 ("{y}", "-100", 0),
338 ("{y:+}", "-100", 0),
339 ("{z}", "0", 0),
340 ("{z:-}", "0", 0),
341 ("{z:+}", "+0", 0),
342
343 // invalid
344 ("{x:.2}", "", 3),
345 ("{x:s}", "", 3),
346
347 // TODO
348 ("{x:+010}", "+000000042", 1),
349 ];
350 let f = |mut fmt: Formatter| {
351 match vars.get(fmt.key) {
352 Some(v) => fmt.$t(*v),
353 None => panic!(),
354 }
355 };
356
357 let strfmt_uint = |fmtstr: &str, vars: &HashMap<String, $t>| -> Result<String> {
358 strfmt_map(fmtstr, &f)
359 };
360 run_tests(&values, &vars, &strfmt_uint);
361 }
362 )*)
363 }
364
365 test_uintrust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_uint!!(test_u8rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_u8(). u8, test_u16rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_u16(). u16, test_u32rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_u32(). u32, test_u64rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_u64(). u64, test_usizerust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_usize(). usize);
366 test_intrust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_int!!(test_i8rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_i8(). i8, test_i16rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_i16(). i16, test_i32rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_i32(). i32, test_i64rust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_i64(). i64, test_isizerust-analyzer cargo strfmt 0.2.4 tests/strfmt/test_isize(). isize);
367
368 // #[bench]
369 // fn bench_strfmt(b: &mut Bencher) {
370 // let mut vars: HashMap<String, String> = HashMap::new();
371 // let too_long = "toooloooong".to_string();
372 // vars.insert("x".to_string(), "X".to_string());
373 // let fmtstr = "short: {x:*^10.3} long: {long:%<14.9}";
374 // b.iter(|| strfmt(fmtstr, &vars));
375 // }