rust-analyzer/crates/syntax/src/fuzz.rs

76 lines
2.6 KiB
Rust
Raw Normal View History

2021-05-22 13:53:47 +00:00
//! Some infrastructure for fuzzy testing.
//!
//! We don't normally run fuzzying, so this is hopelessly bitrotten :(
2020-04-24 22:57:47 +00:00
use std::{
convert::TryInto,
str::{self, FromStr},
};
2020-08-12 15:03:06 +00:00
use text_edit::Indel;
2020-04-24 22:57:47 +00:00
use crate::{validation, AstNode, SourceFile, TextRange};
2019-03-21 17:05:12 +00:00
fn check_file_invariants(file: &SourceFile) {
let root = file.syntax();
validation::validate_block_structure(root);
}
pub fn check_parser(text: &str) {
let file = SourceFile::parse(text);
2019-07-18 16:23:05 +00:00
check_file_invariants(&file.tree());
2019-03-21 17:05:12 +00:00
}
2019-03-21 17:06:48 +00:00
#[derive(Debug, Clone)]
pub struct CheckReparse {
text: String,
2020-05-05 21:15:49 +00:00
edit: Indel,
2019-03-21 17:06:48 +00:00
edited_text: String,
}
impl CheckReparse {
pub fn from_data(data: &[u8]) -> Option<Self> {
2019-07-04 17:26:44 +00:00
const PREFIX: &str = "fn main(){\n\t";
const SUFFIX: &str = "\n}";
2019-03-21 17:06:48 +00:00
let data = str::from_utf8(data).ok()?;
let mut lines = data.lines();
let delete_start = usize::from_str(lines.next()?).ok()? + PREFIX.len();
2019-03-21 17:06:48 +00:00
let delete_len = usize::from_str(lines.next()?).ok()?;
let insert = lines.next()?.to_string();
let text = lines.collect::<Vec<_>>().join("\n");
let text = format!("{}{}{}", PREFIX, text, SUFFIX);
2019-03-21 17:06:48 +00:00
text.get(delete_start..delete_start.checked_add(delete_len)?)?; // make sure delete is a valid range
2020-04-24 21:40:41 +00:00
let delete =
2020-04-24 22:57:47 +00:00
TextRange::at(delete_start.try_into().unwrap(), delete_len.try_into().unwrap());
2019-03-21 17:06:48 +00:00
let edited_text =
format!("{}{}{}", &text[..delete_start], &insert, &text[delete_start + delete_len..]);
2021-03-21 14:33:18 +00:00
let edit = Indel { insert, delete };
2019-03-21 17:06:48 +00:00
Some(CheckReparse { text, edit, edited_text })
}
pub fn run(&self) {
2019-05-28 14:34:28 +00:00
let parse = SourceFile::parse(&self.text);
let new_parse = parse.reparse(&self.edit);
2019-07-18 16:23:05 +00:00
check_file_invariants(&new_parse.tree());
assert_eq!(&new_parse.tree().syntax().text().to_string(), &self.edited_text);
2019-03-21 17:06:48 +00:00
let full_reparse = SourceFile::parse(&self.edited_text);
2019-05-28 14:34:28 +00:00
for (a, b) in
2019-07-18 16:23:05 +00:00
new_parse.tree().syntax().descendants().zip(full_reparse.tree().syntax().descendants())
2019-05-28 14:34:28 +00:00
{
2019-07-20 09:58:27 +00:00
if (a.kind(), a.text_range()) != (b.kind(), b.text_range()) {
2019-07-20 09:48:24 +00:00
eprint!("original:\n{:#?}", parse.tree().syntax());
eprint!("reparsed:\n{:#?}", new_parse.tree().syntax());
eprint!("full reparse:\n{:#?}", full_reparse.tree().syntax());
2019-03-21 18:15:16 +00:00
assert_eq!(
format!("{:?}", a),
format!("{:?}", b),
"different syntax tree produced by the full reparse"
);
}
2019-03-21 17:06:48 +00:00
}
2019-03-21 18:15:16 +00:00
// FIXME
// assert_eq!(new_file.errors(), full_reparse.errors());
2019-03-21 17:06:48 +00:00
}
}