Skip to content
/ rust Public
forked from rust-lang/rust

Commit ec4b3c2

Browse files
Add test for new proc_macro literal methods
1 parent 3c33cbe commit ec4b3c2

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

library/literal-escaper/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# literal-escaper
22

33
This crate provides code to unescape string literals. It is used by `rustc_lexer`
4-
and `proc-macro`.
4+
and `proc_macro`.

library/proc_macro/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use crate::escape::{EscapeOptions, escape_bytes};
6161

6262
/// Errors returned when trying to retrieve a literal unescaped value.
6363
#[unstable(feature = "proc_macro_value", issue = "136652")]
64+
#[derive(Debug, PartialEq, Eq)]
6465
pub enum ConversionErrorKind {
6566
/// The literal failed to be escaped, take a look at [`EscapeError`] for more information.
6667
FailedToUnescape(EscapeError),

tests/ui/proc-macro/auxiliary/api/literal.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// ignore-tidy-linelength
22

3-
use proc_macro::Literal;
3+
use proc_macro::{ConversionErrorKind, Literal};
44

55
pub fn test() {
66
test_display_literal();
77
test_parse_literal();
8+
test_str_value_methods();
89
}
910

1011
fn test_display_literal() {
@@ -81,3 +82,53 @@ fn test_parse_literal() {
8182
assert!("- 10".parse::<Literal>().is_err());
8283
assert!("-'x'".parse::<Literal>().is_err());
8384
}
85+
86+
fn test_str_value_methods() {
87+
// Testing `str_value`
88+
let lit = "\"\n\"".parse::<Literal>().unwrap();
89+
assert_eq!(lit.str_value(), Ok("\n".to_string()));
90+
91+
let lit = "r#\"\n\"#".parse::<Literal>().unwrap();
92+
assert_eq!(lit.str_value(), Ok("\n".to_string()));
93+
94+
let lit = "1".parse::<Literal>().unwrap();
95+
assert_eq!(lit.str_value(), Err(ConversionErrorKind::InvalidLiteralKind));
96+
97+
let lit = "b\"\n\"".parse::<Literal>().unwrap();
98+
assert_eq!(lit.str_value(), Err(ConversionErrorKind::InvalidLiteralKind));
99+
100+
let lit = "c\"\n\"".parse::<Literal>().unwrap();
101+
assert_eq!(lit.str_value(), Err(ConversionErrorKind::InvalidLiteralKind));
102+
103+
// Testing `cstr_value`
104+
let lit = "\"\n\"".parse::<Literal>().unwrap();
105+
assert_eq!(lit.cstr_value(), Err(ConversionErrorKind::InvalidLiteralKind));
106+
107+
let lit = "r#\"\n\"#".parse::<Literal>().unwrap();
108+
assert_eq!(lit.cstr_value(), Err(ConversionErrorKind::InvalidLiteralKind));
109+
110+
let lit = "1".parse::<Literal>().unwrap();
111+
assert_eq!(lit.cstr_value(), Err(ConversionErrorKind::InvalidLiteralKind));
112+
113+
let lit = "b\"\n\"".parse::<Literal>().unwrap();
114+
assert_eq!(lit.cstr_value(), Err(ConversionErrorKind::InvalidLiteralKind));
115+
116+
let lit = "c\"\n\"".parse::<Literal>().unwrap();
117+
assert_eq!(lit.cstr_value(), Ok(vec![b'\n', 0]));
118+
119+
// Testing `byte_str_value`
120+
let lit = "\"\n\"".parse::<Literal>().unwrap();
121+
assert_eq!(lit.byte_str_value(), Err(ConversionErrorKind::InvalidLiteralKind));
122+
123+
let lit = "r#\"\n\"#".parse::<Literal>().unwrap();
124+
assert_eq!(lit.byte_str_value(), Err(ConversionErrorKind::InvalidLiteralKind));
125+
126+
let lit = "1".parse::<Literal>().unwrap();
127+
assert_eq!(lit.byte_str_value(), Err(ConversionErrorKind::InvalidLiteralKind));
128+
129+
let lit = "b\"\n\"".parse::<Literal>().unwrap();
130+
assert_eq!(lit.byte_str_value(), Ok(vec![b'\n']));
131+
132+
let lit = "c\"\n\"".parse::<Literal>().unwrap();
133+
assert_eq!(lit.byte_str_value(), Err(ConversionErrorKind::InvalidLiteralKind));
134+
}

tests/ui/proc-macro/auxiliary/api/proc_macro_api_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@ edition: 2021
22

33
#![feature(proc_macro_span)]
4+
#![feature(proc_macro_value)]
45
#![deny(dead_code)] // catch if a test function is never called
56

67
extern crate proc_macro;

0 commit comments

Comments
 (0)