| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use crate::attr::expand_attr; |
| 8 |
use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree}; |
| 9 |
use std::str::FromStr; |
| 10 |
|
| 11 |
use super::{expand, parse_bracket_as_segments, pasted_to_tokens}; |
| 12 |
|
| 13 |
#[cfg_attr(coverage_nightly, coverage(off))] |
| 14 |
pub(super) fn expand_attr_test(scope: Span) { |
| 15 |
{ |
| 16 |
let mut attr_ts = TokenStream::new(); |
| 17 |
attr_ts.extend([ |
| 18 |
TokenTree::Ident(Ident::new("doc", scope)), |
| 19 |
TokenTree::Punct(Punct::new('=', Spacing::Alone)), |
| 20 |
TokenTree::Group(Group::new(Delimiter::None, TokenStream::new())), |
| 21 |
]); |
| 22 |
let mut flag = false; |
| 23 |
let _ = expand_attr(attr_ts, scope, &mut flag); |
| 24 |
} |
| 25 |
|
| 26 |
{ |
| 27 |
let mut attr_ts = TokenStream::new(); |
| 28 |
attr_ts.extend([ |
| 29 |
TokenTree::Ident(Ident::new("doc", scope)), |
| 30 |
TokenTree::Punct(Punct::new('=', Spacing::Alone)), |
| 31 |
TokenTree::Punct(Punct::new('\'', Spacing::Joint)), |
| 32 |
TokenTree::Punct(Punct::new('\'', Spacing::Alone)), |
| 33 |
]); |
| 34 |
let _ = expand_attr(attr_ts, scope, &mut false); |
| 35 |
} |
| 36 |
|
| 37 |
{ |
| 38 |
let mut attr_ts = TokenStream::new(); |
| 39 |
attr_ts.extend([ |
| 40 |
TokenTree::Ident(Ident::new("allow", scope)), |
| 41 |
TokenTree::Group(Group::new( |
| 42 |
Delimiter::Parenthesis, |
| 43 |
TokenStream::from_str("doc = : \"world\"").unwrap(), |
| 44 |
)), |
| 45 |
]); |
| 46 |
let _ = expand_attr(attr_ts, scope, &mut false); |
| 47 |
} |
| 48 |
|
| 49 |
{ |
| 50 |
let mut paren_ts = TokenStream::from_str("doc = : \"world\"").unwrap(); |
| 51 |
paren_ts.extend([ |
| 52 |
TokenTree::Punct(Punct::new(',', Spacing::Alone)), |
| 53 |
TokenTree::Ident(Ident::new("allow", scope)), |
| 54 |
]); |
| 55 |
let mut attr_ts = TokenStream::new(); |
| 56 |
attr_ts.extend([ |
| 57 |
TokenTree::Ident(Ident::new("cfg_attr", scope)), |
| 58 |
TokenTree::Group(Group::new(Delimiter::Parenthesis, paren_ts)), |
| 59 |
]); |
| 60 |
let _ = expand_attr(attr_ts, scope, &mut false); |
| 61 |
} |
| 62 |
|
| 63 |
{ |
| 64 |
let mut contains = false; |
| 65 |
let _ = expand( |
| 66 |
TokenStream::from_str("# [ doc = : \"world\" ] fn f () { }").unwrap(), |
| 67 |
&mut contains, |
| 68 |
true, |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
{ |
| 73 |
let mut ts = TokenStream::new(); |
| 74 |
ts.extend([ |
| 75 |
TokenTree::Group(Group::new( |
| 76 |
Delimiter::None, |
| 77 |
TokenStream::from_str("x").unwrap(), |
| 78 |
)), |
| 79 |
TokenTree::Punct(Punct::new(':', Spacing::Alone)), |
| 80 |
TokenTree::Punct(Punct::new('@', Spacing::Alone)), |
| 81 |
]); |
| 82 |
let _ = expand(ts, &mut false, false); |
| 83 |
} |
| 84 |
|
| 85 |
{ |
| 86 |
let mut inner_bracket = TokenStream::new(); |
| 87 |
inner_bracket.extend([ |
| 88 |
TokenTree::Punct(Punct::new('<', Spacing::Alone)), |
| 89 |
TokenTree::Ident(Ident::new("foo", scope)), |
| 90 |
TokenTree::Punct(Punct::new('+', Spacing::Alone)), |
| 91 |
TokenTree::Ident(Ident::new("bar", scope)), |
| 92 |
TokenTree::Punct(Punct::new('>', Spacing::Alone)), |
| 93 |
]); |
| 94 |
let bracket = TokenTree::Group(Group::new(Delimiter::Bracket, inner_bracket)); |
| 95 |
let mut paren_inner = TokenStream::new(); |
| 96 |
paren_inner.extend([bracket]); |
| 97 |
let paren_ts = TokenStream::from(TokenTree::Group(Group::new( |
| 98 |
Delimiter::Parenthesis, |
| 99 |
paren_inner, |
| 100 |
))); |
| 101 |
let _ = expand(paren_ts, &mut false, false); |
| 102 |
} |
| 103 |
|
| 104 |
{ |
| 105 |
let mut inner = TokenStream::new(); |
| 106 |
inner.extend([ |
| 107 |
TokenTree::Ident(Ident::new("x", scope)), |
| 108 |
TokenTree::Punct(Punct::new(':', Spacing::Alone)), |
| 109 |
]); |
| 110 |
let mut ts = TokenStream::new(); |
| 111 |
ts.extend([TokenTree::Group(Group::new(Delimiter::None, inner))]); |
| 112 |
let _ = expand(ts, &mut false, true); |
| 113 |
} |
| 114 |
|
| 115 |
{ |
| 116 |
let mut inner = TokenStream::new(); |
| 117 |
inner.extend([ |
| 118 |
TokenTree::Ident(Ident::new("x", scope)), |
| 119 |
TokenTree::Punct(Punct::new(':', Spacing::Joint)), |
| 120 |
TokenTree::Punct(Punct::new('@', Spacing::Alone)), |
| 121 |
]); |
| 122 |
let mut ts = TokenStream::new(); |
| 123 |
ts.extend([TokenTree::Group(Group::new(Delimiter::None, inner))]); |
| 124 |
let _ = expand(ts, &mut false, true); |
| 125 |
} |
| 126 |
|
| 127 |
{ |
| 128 |
let mut inner = TokenStream::new(); |
| 129 |
inner.extend([ |
| 130 |
TokenTree::Ident(Ident::new("x", scope)), |
| 131 |
TokenTree::Punct(Punct::new(':', Spacing::Joint)), |
| 132 |
TokenTree::Punct(Punct::new(':', Spacing::Joint)), |
| 133 |
]); |
| 134 |
let mut ts = TokenStream::new(); |
| 135 |
ts.extend([TokenTree::Group(Group::new(Delimiter::None, inner))]); |
| 136 |
let _ = expand(ts, &mut false, true); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
#[cfg_attr(coverage_nightly, coverage(off))] |
| 141 |
pub(super) fn parse_bracket_as_segments_test(scope: Span) { |
| 142 |
let _ = parse_bracket_as_segments(TokenStream::from_str("foo >").unwrap(), scope); |
| 143 |
let _ = parse_bracket_as_segments(TokenStream::new(), scope); |
| 144 |
let _ = parse_bracket_as_segments(TokenStream::from_str("< foo").unwrap(), scope); |
| 145 |
let _ = parse_bracket_as_segments(TokenStream::from_str("< foo > extra").unwrap(), scope); |
| 146 |
let _ = parse_bracket_as_segments(TokenStream::from_str("< foo +").unwrap(), scope); |
| 147 |
let _ = pasted_to_tokens(String::from("0invalid"), scope); |
| 148 |
let _ = pasted_to_tokens(String::from("0 "), scope); |
| 149 |
let _ = parse_bracket_as_segments(TokenStream::from_str("< env !").unwrap(), scope); |
| 150 |
|
| 151 |
{ |
| 152 |
let mut inner_ts = TokenStream::new(); |
| 153 |
inner_ts.extend([TokenTree::Punct(Punct::new('@', Spacing::Alone))]); |
| 154 |
let mut ts = TokenStream::new(); |
| 155 |
ts.extend([ |
| 156 |
TokenTree::Punct(Punct::new('<', Spacing::Alone)), |
| 157 |
TokenTree::Group(Group::new(Delimiter::None, inner_ts)), |
| 158 |
TokenTree::Punct(Punct::new('>', Spacing::Alone)), |
| 159 |
]); |
| 160 |
let _ = parse_bracket_as_segments(ts, scope); |
| 161 |
} |
| 162 |
|
| 163 |
{ |
| 164 |
let mut ts = TokenStream::new(); |
| 165 |
ts.extend([ |
| 166 |
TokenTree::Punct(Punct::new('*', Spacing::Alone)), |
| 167 |
TokenTree::Ident(Ident::new("foo", scope)), |
| 168 |
TokenTree::Punct(Punct::new('>', Spacing::Alone)), |
| 169 |
]); |
| 170 |
let _ = parse_bracket_as_segments(ts, scope); |
| 171 |
} |
| 172 |
} |