1#![allow(clippy::uninlined_format_args)]
2
3use syn::punctuated::{Pair, Punctuated};
4use syn::Token;
5
6#[macro_use]
7mod macros;
8
9macro_rules! check_exact_size_iterator {
10 ($iter:expr) => {{
11 let iter = $iter;
12 let size_hint = iter.size_hint();
13 let len = iter.len();
14 let count = iter.count();
15 assert_eq!(len, count);
16 assert_eq!(size_hint, (count, Some(count)));
17 }};
18}
19
20#[test]
21fn pairs() {
22 let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4);
23
24 check_exact_size_iterator!(p.pairs());
25 check_exact_size_iterator!(p.pairs_mut());
26 check_exact_size_iterator!(p.into_pairs());
27
28 let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4);
29
30 assert_eq!(p.pairs().next_back().map(Pair::into_value), Some(&4));
31 assert_eq!(
32 p.pairs_mut().next_back().map(Pair::into_value),
33 Some(&mut 4)
34 );
35 assert_eq!(p.into_pairs().next_back().map(Pair::into_value), Some(4));
36}
37
38#[test]
39fn iter() {
40 let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4);
41
42 check_exact_size_iterator!(p.iter());
43 check_exact_size_iterator!(p.iter_mut());
44 check_exact_size_iterator!(p.into_iter());
45
46 let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4);
47
48 assert_eq!(p.iter().next_back(), Some(&4));
49 assert_eq!(p.iter_mut().next_back(), Some(&mut 4));
50 assert_eq!(p.into_iter().next_back(), Some(4));
51}
52
53#[test]
54fn may_dangle() {
55 let p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4);
56 for element in &p {
57 if *element == 2 {
58 drop(p);
59 break;
60 }
61 }
62
63 let mut p: Punctuated<_, Token![,]> = punctuated!(2, 3, 4);
64 for element in &mut p {
65 if *element == 2 {
66 drop(p);
67 break;
68 }
69 }
70}
71