1 extern crate proc_macro2
;
7 use proc_macro2
::{TokenStream, TokenTree}
;
10 use syn
::{FloatSuffix, IntSuffix, Lit}
;
12 fn lit(s
: &str) -> Lit
{
13 match TokenStream
::from_str(s
)
19 TokenTree
::Literal(lit
) => Lit
::new(lit
),
26 fn test_string(s
: &str, value
: &str) {
29 assert_eq
!(lit
.value(), value
);
30 let again
= lit
.into_token_stream().to_string();
32 test_string(&again
, value
);
35 wrong
=> panic
!("{:?}", wrong
),
39 test_string("\"a\"", "a");
40 test_string("\"\\n\"", "\n");
41 test_string("\"\\r\"", "\r");
42 test_string("\"\\t\"", "\t");
43 test_string("\"🐕\"", "🐕"); // NOTE: This is an emoji
44 test_string("\"\\\"\"", "\"");
45 test_string("\"'\"", "'");
46 test_string("\"\"", "");
47 test_string("\"\\u{1F415}\"", "\u{1F415}");
49 "\"contains\nnewlines\\\nescaped newlines\"",
50 "contains\nnewlinesescaped newlines",
52 test_string("r\"raw\nstring\\\nhere\"", "raw\nstring\\\nhere");
57 fn test_byte_string(s
: &str, value
: &[u8]) {
59 Lit
::ByteStr(lit
) => {
60 assert_eq
!(lit
.value(), value
);
61 let again
= lit
.into_token_stream().to_string();
63 test_byte_string(&again
, value
);
66 wrong
=> panic
!("{:?}", wrong
),
70 test_byte_string("b\"a\"", b
"a");
71 test_byte_string("b\"\\n\"", b
"\n");
72 test_byte_string("b\"\\r\"", b
"\r");
73 test_byte_string("b\"\\t\"", b
"\t");
74 test_byte_string("b\"\\\"\"", b
"\"");
75 test_byte_string("b\"'\"", b
"'");
76 test_byte_string("b\"\"", b
"");
78 "b\"contains\nnewlines\\\nescaped newlines\"",
79 b
"contains\nnewlinesescaped newlines",
81 test_byte_string("br\"raw\nstring\\\nhere\"", b
"raw\nstring\\\nhere");
86 fn test_byte(s
: &str, value
: u8) {
89 assert_eq
!(lit
.value(), value
);
90 let again
= lit
.into_token_stream().to_string();
93 wrong
=> panic
!("{:?}", wrong
),
97 test_byte("b'a'", b'a'
);
98 test_byte("b'\\n'", b'
\n'
);
99 test_byte("b'\\r'", b'
\r'
);
100 test_byte("b'\\t'", b'
\t'
);
101 test_byte("b'\\''", b'
\''
);
102 test_byte("b'\"'", b'
"');
107 fn test_char(s: &str, value: char) {
110 assert_eq!(lit.value(), value);
111 let again = lit.into_token_stream().to_string();
113 test_char(&again, value);
116 wrong => panic!("{:?}
", wrong),
120 test_char("'a'
", 'a');
121 test_char("'
\\n'
", '\n');
122 test_char("'
\\r'
", '\r');
123 test_char("'
\\t'
", '\t');
124 test_char("'🐕'
", '🐕'); // NOTE: This is an emoji
125 test_char("'
\\''
", '\'');
126 test_char("'
\"'
", '"'
);
127 test_char("'\\u{1F415}'", '
\u{1F415}'
);
132 fn test_int(s
: &str, value
: u64, suffix
: IntSuffix
) {
135 assert_eq
!(lit
.value(), value
);
136 assert_eq
!(lit
.suffix(), suffix
);
137 let again
= lit
.into_token_stream().to_string();
139 test_int(&again
, value
, suffix
);
142 wrong
=> panic
!("{:?}", wrong
),
146 use syn
::IntSuffix
::*;
147 test_int("5", 5, None
);
148 test_int("5u32", 5, U32
);
149 test_int("5_0", 50, None
);
150 test_int("5_____0_____", 50, None
);
151 test_int("0x7f", 127, None
);
152 test_int("0x7F", 127, None
);
153 test_int("0b1001", 9, None
);
154 test_int("0o73", 59, None
);
155 test_int("0x7Fu8", 127, U8
);
156 test_int("0b1001i8", 9, I8
);
157 test_int("0o73u32", 59, U32
);
158 test_int("0x__7___f_", 127, None
);
159 test_int("0x__7___F_", 127, None
);
160 test_int("0b_1_0__01", 9, None
);
161 test_int("0o_7__3", 59, None
);
162 test_int("0x_7F__u8", 127, U8
);
163 test_int("0b__10__0_1i8", 9, I8
);
164 test_int("0o__7__________________3u32", 59, U32
);
169 #[cfg_attr(feature = "cargo-clippy", allow(float_cmp))]
170 fn test_float(s
: &str, value
: f64, suffix
: FloatSuffix
) {
173 assert_eq
!(lit
.value(), value
);
174 assert_eq
!(lit
.suffix(), suffix
);
175 let again
= lit
.into_token_stream().to_string();
177 test_float(&again
, value
, suffix
);
180 wrong
=> panic
!("{:?}", wrong
),
184 use syn
::FloatSuffix
::*;
185 test_float("5.5", 5.5, None
);
186 test_float("5.5E12", 5.5e12
, None
);
187 test_float("5.5e12", 5.5e12
, None
);
188 test_float("1.0__3e-12", 1.03e-12, None
);
189 test_float("1.03e+12", 1.03e12
, None
);