-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Improve error messages for raw strings (#60762) #70522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
840a576
Move raw string tests into the raw directory
rcoh 629e97a
Improve error messages for raw strings (#60762)
rcoh c15f86b
Cleanup error messages, improve docstrings
rcoh 82b2989
More raw string tests
rcoh bceab25
Cleanup match expression
rcoh 20e2190
Clean up redudant conditions and match exprs
rcoh 55a5eea
Fix tests to handle debug_assert
rcoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::*; | ||
|
||
fn check_raw_str( | ||
s: &str, | ||
expected: UnvalidatedRawStr, | ||
validated: Result<ValidatedRawStr, LexRawStrError>, | ||
) { | ||
let mut cursor = Cursor::new(s); | ||
let tok = cursor.raw_double_quoted_string(0); | ||
assert_eq!(tok, expected); | ||
assert_eq!(tok.validate(), validated); | ||
} | ||
|
||
#[test] | ||
fn test_naked_raw_str() { | ||
check_raw_str( | ||
r#""abc""#, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 0, | ||
n_end_hashes: 0, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Ok(ValidatedRawStr { n_hashes: 0 }), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_raw_no_start() { | ||
check_raw_str( | ||
r##""abc"#"##, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 0, | ||
n_end_hashes: 0, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Ok(ValidatedRawStr { n_hashes: 0 }), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_too_many_terminators() { | ||
// this error is handled in the parser later | ||
check_raw_str( | ||
r###"#"abc"##"###, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 1, | ||
n_end_hashes: 1, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Ok(ValidatedRawStr { n_hashes: 1 }), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_unterminated() { | ||
check_raw_str( | ||
r#"#"abc"#, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 1, | ||
n_end_hashes: 0, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Err(LexRawStrError::NoTerminator { | ||
expected: 1, | ||
found: 0, | ||
possible_terminator_offset: None, | ||
}), | ||
); | ||
check_raw_str( | ||
r###"##"abc"#"###, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 2, | ||
n_end_hashes: 1, | ||
valid_start: true, | ||
possible_terminator_offset: Some(7), | ||
}, | ||
Err(LexRawStrError::NoTerminator { | ||
expected: 2, | ||
found: 1, | ||
possible_terminator_offset: Some(7), | ||
}), | ||
); | ||
// We're looking for "# not just any # | ||
check_raw_str( | ||
r###"##"abc#"###, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 2, | ||
n_end_hashes: 0, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Err(LexRawStrError::NoTerminator { | ||
expected: 2, | ||
found: 0, | ||
possible_terminator_offset: None, | ||
}), | ||
) | ||
} | ||
|
||
#[test] | ||
fn test_invalid_start() { | ||
check_raw_str( | ||
r##"#~"abc"#"##, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 1, | ||
n_end_hashes: 0, | ||
valid_start: false, | ||
possible_terminator_offset: None, | ||
}, | ||
Err(LexRawStrError::InvalidStarter), | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.