mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Add lots of tests
This commit is contained in:
parent
fdb9f06880
commit
94796e6447
3 changed files with 125 additions and 6 deletions
|
@ -121,11 +121,13 @@ fn validate_char(node: ast::Char, errors: &mut Vec<SyntaxError>) {
|
|||
errors.push(SyntaxError::new(MalformedUnicodeEscape, range));
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: we really need tests for this
|
||||
}
|
||||
// Code points are always valid
|
||||
CodePoint => (),
|
||||
CodePoint => {
|
||||
// These code points must always be escaped
|
||||
if text == "\t" || text == "\r" {
|
||||
errors.push(SyntaxError::new(UnescapedCodepoint, range));
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -148,3 +150,115 @@ fn is_ascii_escape(code: char) -> bool {
|
|||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::File;
|
||||
|
||||
fn build_file(literal: &str) -> File {
|
||||
let src = format!("const C: char = '{}';", literal);
|
||||
File::parse(&src)
|
||||
}
|
||||
|
||||
fn assert_valid_char(literal: &str) {
|
||||
let file = build_file(literal);
|
||||
assert!(file.errors().len() == 0, "Errors for literal '{}': {:?}", literal, file.errors());
|
||||
}
|
||||
|
||||
fn assert_invalid_char(literal: &str) { //, expected_errors: HashSet<SyntaxErrorKind>) {
|
||||
let file = build_file(literal);
|
||||
assert!(file.errors().len() > 0);
|
||||
//let found_errors = file.errors().iter().map(|e| e.kind()).collect();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ansi_codepoints() {
|
||||
for byte in 0..=255u8 {
|
||||
match byte {
|
||||
b'\n' | b'\r' | b'\t' => assert_invalid_char(&(byte as char).to_string()),
|
||||
b'\'' | b'\\' => { /* Ignore character close and backslash */ }
|
||||
_ => assert_valid_char(&(byte as char).to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unicode_codepoints() {
|
||||
let valid = [
|
||||
"Ƒ", "バ", "メ", "﷽"
|
||||
];
|
||||
for c in &valid {
|
||||
assert_valid_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unicode_multiple_codepoints() {
|
||||
let invalid = [
|
||||
"नी", "👨👨"
|
||||
];
|
||||
for c in &invalid {
|
||||
assert_invalid_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_ascii_escape() {
|
||||
let valid = [
|
||||
r"\'", "\"", "\\\"", r"\n", r"\r", r"\t", r"\0", "a", "b"
|
||||
];
|
||||
for c in &valid {
|
||||
assert_valid_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_ascii_escape() {
|
||||
let invalid = [
|
||||
r"\a", r"\?", r"\"
|
||||
];
|
||||
for c in &invalid {
|
||||
assert_invalid_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_ascii_code_escape() {
|
||||
let valid = [
|
||||
r"\x00", r"\x7F", r"\x55"
|
||||
];
|
||||
for c in &valid {
|
||||
assert_valid_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_ascii_code_escape() {
|
||||
let invalid = [
|
||||
r"\x", r"\x7", r"\xF0"
|
||||
];
|
||||
for c in &invalid {
|
||||
assert_invalid_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_unicode_escape() {
|
||||
let valid = [
|
||||
r"\u{FF}", r"\u{0}", r"\u{F}", r"\u{10FFFF}", r"\u{1_0__FF___FF_____}"
|
||||
];
|
||||
for c in &valid {
|
||||
assert_valid_char(c);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_unicode_escape() {
|
||||
let invalid = [
|
||||
r"\u", r"\u{}", r"\u{", r"\u{FF", r"\u{FFFFFF}", r"\u{_F}", r"\u{00FFFFF}", r"\u{110000}"
|
||||
];
|
||||
for c in &invalid {
|
||||
assert_invalid_char(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue