Skip to content

Commit

Permalink
Reduce escaping in literal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Apr 15, 2024
1 parent ade35a6 commit b1a55dc
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions tests/test_lit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(
clippy::float_cmp,
clippy::needless_raw_string_hashes,
clippy::non_ascii_literal,
clippy::single_match_else,
clippy::uninlined_format_args
Expand Down Expand Up @@ -29,6 +30,7 @@ fn lit(s: &str) -> Lit {
fn strings() {
#[track_caller]
fn test_string(s: &str, value: &str) {
let s = s.trim();
match lit(s) {
Lit::Str(lit) => {
assert_eq!(lit.value(), value);
Expand All @@ -41,16 +43,16 @@ fn strings() {
}
}

test_string("\"a\"", "a");
test_string("\"\\n\"", "\n");
test_string("\"\\r\"", "\r");
test_string("\"\\t\"", "\t");
test_string("\"πŸ•\"", "πŸ•"); // NOTE: This is an emoji
test_string("\"\\\"\"", "\"");
test_string("\"'\"", "'");
test_string("\"\"", "");
test_string("\"\\u{1F415}\"", "\u{1F415}");
test_string("\"\\u{1_2__3_}\"", "\u{123}");
test_string(r#" "" "#, "");
test_string(r#" "a" "#, "a");
test_string(r#" "\n" "#, "\n");
test_string(r#" "\r" "#, "\r");
test_string(r#" "\t" "#, "\t");
test_string(r#" "πŸ•" "#, "πŸ•"); // NOTE: This is an emoji
test_string(r#" "\"" "#, "\"");
test_string(r#" "'" "#, "'");
test_string(r#" "\u{1F415}" "#, "\u{1F415}");
test_string(r#" "\u{1_2__3_}" "#, "\u{123}");
test_string(
"\"contains\nnewlines\\\nescaped newlines\"",
"contains\nnewlinesescaped newlines",
Expand All @@ -69,6 +71,7 @@ fn strings() {
fn byte_strings() {
#[track_caller]
fn test_byte_string(s: &str, value: &[u8]) {
let s = s.trim();
match lit(s) {
Lit::ByteStr(lit) => {
assert_eq!(lit.value(), value);
Expand All @@ -81,13 +84,13 @@ fn byte_strings() {
}
}

test_byte_string("b\"a\"", b"a");
test_byte_string("b\"\\n\"", b"\n");
test_byte_string("b\"\\r\"", b"\r");
test_byte_string("b\"\\t\"", b"\t");
test_byte_string("b\"\\\"\"", b"\"");
test_byte_string("b\"'\"", b"'");
test_byte_string("b\"\"", b"");
test_byte_string(r#" b"" "#, b"");
test_byte_string(r#" b"a" "#, b"a");
test_byte_string(r#" b"\n" "#, b"\n");
test_byte_string(r#" b"\r" "#, b"\r");
test_byte_string(r#" b"\t" "#, b"\t");
test_byte_string(r#" b"\"" "#, b"\"");
test_byte_string(r#" b"'" "#, b"'");
test_byte_string(
"b\"contains\nnewlines\\\nescaped newlines\"",
b"contains\nnewlinesescaped newlines",
Expand All @@ -102,6 +105,7 @@ fn byte_strings() {
fn bytes() {
#[track_caller]
fn test_byte(s: &str, value: u8) {
let s = s.trim();
match lit(s) {
Lit::Byte(lit) => {
assert_eq!(lit.value(), value);
Expand All @@ -112,19 +116,20 @@ fn bytes() {
}
}

test_byte("b'a'", b'a');
test_byte("b'\\n'", b'\n');
test_byte("b'\\r'", b'\r');
test_byte("b'\\t'", b'\t');
test_byte("b'\\''", b'\'');
test_byte("b'\"'", b'"');
test_byte("b'a'q", b'a');
test_byte(r#" b'a' "#, b'a');
test_byte(r#" b'\n' "#, b'\n');
test_byte(r#" b'\r' "#, b'\r');
test_byte(r#" b'\t' "#, b'\t');
test_byte(r#" b'\'' "#, b'\'');
test_byte(r#" b'"' "#, b'"');
test_byte(r#" b'a'q "#, b'a');
}

#[test]
fn chars() {
#[track_caller]
fn test_char(s: &str, value: char) {
let s = s.trim();
match lit(s) {
Lit::Char(lit) => {
assert_eq!(lit.value(), value);
Expand All @@ -137,15 +142,15 @@ fn chars() {
}
}

test_char("'a'", 'a');
test_char("'\\n'", '\n');
test_char("'\\r'", '\r');
test_char("'\\t'", '\t');
test_char("'πŸ•'", 'πŸ•'); // NOTE: This is an emoji
test_char("'\\''", '\'');
test_char("'\"'", '"');
test_char("'\\u{1F415}'", '\u{1F415}');
test_char("'a'q", 'a');
test_char(r#" 'a' "#, 'a');
test_char(r#" '\n' "#, '\n');
test_char(r#" '\r' "#, '\r');
test_char(r#" '\t' "#, '\t');
test_char(r#" 'πŸ•' "#, 'πŸ•'); // NOTE: This is an emoji
test_char(r#" '\'' "#, '\'');
test_char(r#" '"' "#, '"');
test_char(r#" '\u{1F415}' "#, '\u{1F415}');
test_char(r#" 'a'q "#, 'a');
}

#[test]
Expand Down

0 comments on commit b1a55dc

Please sign in to comment.