]>
git.proxmox.com Git - rustc.git/blob - vendor/syn/tests/test_lit.rs
3 use proc_macro2
::{TokenStream, TokenTree}
;
8 fn lit(s
: &str) -> Lit
{
9 match TokenStream
::from_str(s
)
15 TokenTree
::Literal(lit
) => Lit
::new(lit
),
22 fn test_string(s
: &str, value
: &str) {
25 assert_eq
!(lit
.value(), value
);
26 let again
= lit
.into_token_stream().to_string();
28 test_string(&again
, value
);
31 wrong
=> panic
!("{:?}", wrong
),
35 test_string("\"a\"", "a");
36 test_string("\"\\n\"", "\n");
37 test_string("\"\\r\"", "\r");
38 test_string("\"\\t\"", "\t");
39 test_string("\"🐕\"", "🐕"); // NOTE: This is an emoji
40 test_string("\"\\\"\"", "\"");
41 test_string("\"'\"", "'");
42 test_string("\"\"", "");
43 test_string("\"\\u{1F415}\"", "\u{1F415}");
45 "\"contains\nnewlines\\\nescaped newlines\"",
46 "contains\nnewlinesescaped newlines",
48 test_string("r\"raw\nstring\\\nhere\"", "raw\nstring\\\nhere");
53 fn test_byte_string(s
: &str, value
: &[u8]) {
55 Lit
::ByteStr(lit
) => {
56 assert_eq
!(lit
.value(), value
);
57 let again
= lit
.into_token_stream().to_string();
59 test_byte_string(&again
, value
);
62 wrong
=> panic
!("{:?}", wrong
),
66 test_byte_string("b\"a\"", b
"a");
67 test_byte_string("b\"\\n\"", b
"\n");
68 test_byte_string("b\"\\r\"", b
"\r");
69 test_byte_string("b\"\\t\"", b
"\t");
70 test_byte_string("b\"\\\"\"", b
"\"");
71 test_byte_string("b\"'\"", b
"'");
72 test_byte_string("b\"\"", b
"");
74 "b\"contains\nnewlines\\\nescaped newlines\"",
75 b
"contains\nnewlinesescaped newlines",
77 test_byte_string("br\"raw\nstring\\\nhere\"", b
"raw\nstring\\\nhere");
82 fn test_byte(s
: &str, value
: u8) {
85 assert_eq
!(lit
.value(), value
);
86 let again
= lit
.into_token_stream().to_string();
89 wrong
=> panic
!("{:?}", wrong
),
93 test_byte("b'a'", b'a'
);
94 test_byte("b'\\n'", b'
\n'
);
95 test_byte("b'\\r'", b'
\r'
);
96 test_byte("b'\\t'", b'
\t'
);
97 test_byte("b'\\''", b'
\''
);
98 test_byte("b'\"'", b'
"');
103 fn test_char(s: &str, value: char) {
106 assert_eq!(lit.value(), value);
107 let again = lit.into_token_stream().to_string();
109 test_char(&again, value);
112 wrong => panic!("{:?}
", wrong),
116 test_char("'a'
", 'a');
117 test_char("'
\\n'
", '\n');
118 test_char("'
\\r'
", '\r');
119 test_char("'
\\t'
", '\t');
120 test_char("'🐕'
", '🐕'); // NOTE: This is an emoji
121 test_char("'
\\''
", '\'');
122 test_char("'
\"'
", '"'
);
123 test_char("'\\u{1F415}'", '
\u{1F415}'
);
128 fn test_int(s
: &str, value
: u64, suffix
: &str) {
131 assert_eq
!(lit
.base10_digits().parse
::<u64>().unwrap(), value
);
132 assert_eq
!(lit
.suffix(), suffix
);
133 let again
= lit
.into_token_stream().to_string();
135 test_int(&again
, value
, suffix
);
138 wrong
=> panic
!("{:?}", wrong
),
142 test_int("5", 5, "");
143 test_int("5u32", 5, "u32");
144 test_int("5_0", 50, "");
145 test_int("5_____0_____", 50, "");
146 test_int("0x7f", 127, "");
147 test_int("0x7F", 127, "");
148 test_int("0b1001", 9, "");
149 test_int("0o73", 59, "");
150 test_int("0x7Fu8", 127, "u8");
151 test_int("0b1001i8", 9, "i8");
152 test_int("0o73u32", 59, "u32");
153 test_int("0x__7___f_", 127, "");
154 test_int("0x__7___F_", 127, "");
155 test_int("0b_1_0__01", 9, "");
156 test_int("0o_7__3", 59, "");
157 test_int("0x_7F__u8", 127, "u8");
158 test_int("0b__10__0_1i8", 9, "i8");
159 test_int("0o__7__________________3u32", 59, "u32");
164 #[cfg_attr(feature = "cargo-clippy", allow(float_cmp))]
165 fn test_float(s
: &str, value
: f64, suffix
: &str) {
168 assert_eq
!(lit
.base10_digits().parse
::<f64>().unwrap(), value
);
169 assert_eq
!(lit
.suffix(), suffix
);
170 let again
= lit
.into_token_stream().to_string();
172 test_float(&again
, value
, suffix
);
175 wrong
=> panic
!("{:?}", wrong
),
179 test_float("5.5", 5.5, "");
180 test_float("5.5E12", 5.5e12
, "");
181 test_float("5.5e12", 5.5e12
, "");
182 test_float("1.0__3e-12", 1.03e-12, "");
183 test_float("1.03e+12", 1.03e12
, "");