|
1 | 1 | // ignore-tidy-linelength
|
2 | 2 |
|
3 |
| -use proc_macro::Literal; |
| 3 | +use proc_macro::{ConversionErrorKind, Literal}; |
4 | 4 |
|
5 | 5 | pub fn test() {
|
6 | 6 | test_display_literal();
|
7 | 7 | test_parse_literal();
|
| 8 | + test_str_value_methods(); |
8 | 9 | }
|
9 | 10 |
|
10 | 11 | fn test_display_literal() {
|
@@ -81,3 +82,53 @@ fn test_parse_literal() {
|
81 | 82 | assert!("- 10".parse::<Literal>().is_err());
|
82 | 83 | assert!("-'x'".parse::<Literal>().is_err());
|
83 | 84 | }
|
| 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 | +} |
0 commit comments