independent tests for incremental reparsing of blocks and leaves

This commit is contained in:
darksv 2018-09-15 17:05:08 +02:00
parent 46cee0415c
commit ab00639032

View file

@ -187,30 +187,52 @@ fn merge_errors(
res res
} }
#[cfg(test)]
use super::{File, text_utils, test_utils, utils};
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{*, utils::dump_tree}; use super::{
super::{
File,
test_utils::extract_range,
text_utils::replace_range,
utils::dump_tree,
},
reparse_leaf, reparse_block, AtomEdit, GreenNode, SyntaxError, SyntaxNodeRef,
};
fn do_check<F>(
before: &str,
replace_with: &str,
reparser: F,
) where
for<'a> F: Fn(
SyntaxNodeRef<'a>,
&AtomEdit,
) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec<SyntaxError>)>
{
let (range, before) = extract_range(before);
let after = replace_range(before.clone(), range, replace_with);
let fully_reparsed = File::parse(&after);
let incrementally_reparsed = {
let f = File::parse(&before);
let edit = AtomEdit { delete: range, insert: replace_with.to_string() };
let (node, green, new_errors) =
reparser(f.syntax(), &edit).expect("cannot incrementally reparse");
let green_root = node.replace_with(green);
let errors = super::merge_errors(f.errors(), new_errors, node, &edit);
File::new(green_root, errors)
};
assert_eq_text!(
&dump_tree(fully_reparsed.syntax()),
&dump_tree(incrementally_reparsed.syntax()),
)
}
#[test] #[test]
fn reparse_test() { fn reparse_block_tests() {
fn do_check(before: &str, replace_with: &str) { let do_check = |before, replace_to|
let (range, before) = test_utils::extract_range(before); do_check(before, replace_to, reparse_block);
let after = text_utils::replace_range(before.clone(), range, replace_with);
let fully_reparsed = File::parse(&after);
let incrementally_reparsed = {
let f = File::parse(&before);
let edit = AtomEdit { delete: range, insert: replace_with.to_string() };
f.incremental_reparse(&edit).expect("cannot incrementally reparse")
};
assert_eq_text!(
&dump_tree(fully_reparsed.syntax()),
&dump_tree(incrementally_reparsed.syntax()),
)
}
do_check(r" do_check(r"
fn foo() { fn foo() {
@ -243,11 +265,6 @@ mod foo {
trait Foo { trait Foo {
type <|>Foo<|>; type <|>Foo<|>;
} }
", "Output");
do_check(r"
trait Foo {
type<|> Foo<|>;
}
", "Output"); ", "Output");
do_check(r" do_check(r"
impl IntoIterator<Item=i32> for Foo { impl IntoIterator<Item=i32> for Foo {
@ -275,43 +292,37 @@ extern {
fn<|>;<|> fn<|>;<|>
} }
", " exit(code: c_int)"); ", " exit(code: c_int)");
}
#[test]
fn reparse_leaf_tests() {
let do_check = |before, replace_to|
do_check(before, replace_to, reparse_leaf);
do_check(r"<|><|> do_check(r"<|><|>
fn foo() -> i32 { fn foo() -> i32 { 1 }
1
}
", "\n\n\n \n"); ", "\n\n\n \n");
do_check(r" do_check(r"
fn foo() -> <|><|> {} fn foo() -> <|><|> {}
", " \n"); ", " \n");
do_check(r" do_check(r"
fn <|>foo<|>() -> i32 { fn <|>foo<|>() -> i32 { 1 }
1
}
", "bar"); ", "bar");
do_check(r" do_check(r"
fn aa<|><|>bb() { fn foo<|><|>foo() { }
", "bar");
}
", "foofoo");
do_check(r" do_check(r"
fn aabb /* <|><|> */ () { fn foo /* <|><|> */ () {}
}
", "some comment"); ", "some comment");
do_check(r" do_check(r"
fn aabb <|><|> () { fn baz <|><|> () {}
}
", " \t\t\n\n"); ", " \t\t\n\n");
do_check(r" do_check(r"
trait foo { fn baz <|><|> () {}
// comment <|><|> ", " \t\t\n\n");
}
", "\n");
do_check(r" do_check(r"
/// good <|><|>omment /// foo <|><|>omment
mod { mod { }
}
", "c"); ", "c");
do_check(r#" do_check(r#"
fn -> &str { "Hello<|><|>" } fn -> &str { "Hello<|><|>" }
@ -319,5 +330,14 @@ fn -> &str { "Hello<|><|>" }
do_check(r#" do_check(r#"
fn -> &str { // "Hello<|><|>" fn -> &str { // "Hello<|><|>"
"#, ", world"); "#, ", world");
do_check(r##"
fn -> &str { r#"Hello<|><|>"#
"##, ", world");
do_check(r"
#[derive(<|>Copy<|>)]
enum Foo {
}
", "Clone");
} }
} }