Skip to content

Commit ab5231c

Browse files
committed
Add string literal value tests
1 parent 4c039a8 commit ab5231c

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

tests/test.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,30 @@ fn literal_raw_string() {
146146
.unwrap_err();
147147
}
148148

149+
#[cfg(procmacro2_semver_exempt)]
150+
#[test]
151+
fn literal_string_value() {
152+
for string in ["", "...", "...\t...", "...\\...", "...\0...", "...\u{1}..."] {
153+
assert_eq!(string, Literal::string(string).str_value().unwrap());
154+
assert_eq!(
155+
string,
156+
format!("r\"{string}\"")
157+
.parse::<Literal>()
158+
.unwrap()
159+
.str_value()
160+
.unwrap(),
161+
);
162+
assert_eq!(
163+
string,
164+
format!("r##\"{string}\"##")
165+
.parse::<Literal>()
166+
.unwrap()
167+
.str_value()
168+
.unwrap(),
169+
);
170+
}
171+
}
172+
149173
#[test]
150174
fn literal_byte_character() {
151175
#[track_caller]
@@ -191,6 +215,42 @@ fn literal_byte_string() {
191215
"br\"\u{a0}\"".parse::<TokenStream>().unwrap_err();
192216
}
193217

218+
#[cfg(procmacro2_semver_exempt)]
219+
#[test]
220+
fn literal_byte_string_value() {
221+
for bytestr in [
222+
&b""[..],
223+
b"...",
224+
b"...\t...",
225+
b"...\\...",
226+
b"...\0...",
227+
b"...\xF0...",
228+
] {
229+
assert_eq!(
230+
bytestr,
231+
Literal::byte_string(bytestr).byte_str_value().unwrap(),
232+
);
233+
if let Ok(string) = str::from_utf8(bytestr) {
234+
assert_eq!(
235+
bytestr,
236+
format!("br\"{string}\"")
237+
.parse::<Literal>()
238+
.unwrap()
239+
.byte_str_value()
240+
.unwrap(),
241+
);
242+
assert_eq!(
243+
bytestr,
244+
format!("br##\"{string}\"##")
245+
.parse::<Literal>()
246+
.unwrap()
247+
.byte_str_value()
248+
.unwrap(),
249+
);
250+
}
251+
}
252+
}
253+
194254
#[test]
195255
fn literal_c_string() {
196256
#[track_caller]
@@ -262,6 +322,42 @@ fn literal_c_string() {
262322
}
263323
}
264324

325+
#[cfg(procmacro2_semver_exempt)]
326+
#[test]
327+
fn literal_c_string_value() {
328+
for cstr in [
329+
c"",
330+
c"...",
331+
c"...\t...",
332+
c"...\\...",
333+
c"...\u{1}...",
334+
c"...\xF0...",
335+
] {
336+
assert_eq!(
337+
cstr.to_bytes_with_nul(),
338+
Literal::c_string(cstr).cstr_value().unwrap(),
339+
);
340+
if let Ok(string) = cstr.to_str() {
341+
assert_eq!(
342+
cstr.to_bytes_with_nul(),
343+
format!("cr\"{string}\"")
344+
.parse::<Literal>()
345+
.unwrap()
346+
.cstr_value()
347+
.unwrap(),
348+
);
349+
assert_eq!(
350+
cstr.to_bytes_with_nul(),
351+
format!("cr##\"{string}\"##")
352+
.parse::<Literal>()
353+
.unwrap()
354+
.cstr_value()
355+
.unwrap(),
356+
);
357+
}
358+
}
359+
}
360+
265361
#[test]
266362
fn literal_character() {
267363
#[track_caller]

0 commit comments

Comments
 (0)