From ab00639032981f8b959e06c4015dd72201df651d Mon Sep 17 00:00:00 2001 From: darksv Date: Sat, 15 Sep 2018 17:05:08 +0200 Subject: [PATCH] independent tests for incremental reparsing of blocks and leaves --- crates/libsyntax2/src/reparsing.rs | 116 +++++++++++++++++------------ 1 file changed, 68 insertions(+), 48 deletions(-) diff --git a/crates/libsyntax2/src/reparsing.rs b/crates/libsyntax2/src/reparsing.rs index 1df2ca0943..da44913c53 100644 --- a/crates/libsyntax2/src/reparsing.rs +++ b/crates/libsyntax2/src/reparsing.rs @@ -187,30 +187,52 @@ fn merge_errors( res } -#[cfg(test)] -use super::{File, text_utils, test_utils, utils}; - #[cfg(test)] 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( + before: &str, + replace_with: &str, + reparser: F, + ) where + for<'a> F: Fn( + SyntaxNodeRef<'a>, + &AtomEdit, + ) -> Option<(SyntaxNodeRef<'a>, GreenNode, Vec)> + { + 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] - fn reparse_test() { - fn do_check(before: &str, replace_with: &str) { - let (range, before) = test_utils::extract_range(before); - 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()), - ) - } + fn reparse_block_tests() { + let do_check = |before, replace_to| + do_check(before, replace_to, reparse_block); do_check(r" fn foo() { @@ -243,11 +265,6 @@ mod foo { trait Foo { type <|>Foo<|>; } -", "Output"); - do_check(r" -trait Foo { - type<|> Foo<|>; -} ", "Output"); do_check(r" impl IntoIterator for Foo { @@ -275,43 +292,37 @@ extern { fn<|>;<|> } ", " 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"<|><|> -fn foo() -> i32 { - 1 -} +fn foo() -> i32 { 1 } ", "\n\n\n \n"); do_check(r" fn foo() -> <|><|> {} ", " \n"); do_check(r" -fn <|>foo<|>() -> i32 { - 1 -} +fn <|>foo<|>() -> i32 { 1 } ", "bar"); do_check(r" -fn aa<|><|>bb() { - -} -", "foofoo"); +fn foo<|><|>foo() { } +", "bar"); do_check(r" -fn aabb /* <|><|> */ () { - -} +fn foo /* <|><|> */ () {} ", "some comment"); do_check(r" -fn aabb <|><|> () { - -} +fn baz <|><|> () {} ", " \t\t\n\n"); do_check(r" -trait foo { -// comment <|><|> -} -", "\n"); +fn baz <|><|> () {} +", " \t\t\n\n"); do_check(r" -/// good <|><|>omment -mod { -} +/// foo <|><|>omment +mod { } ", "c"); do_check(r#" fn -> &str { "Hello<|><|>" } @@ -319,5 +330,14 @@ fn -> &str { "Hello<|><|>" } do_check(r#" fn -> &str { // "Hello<|><|>" "#, ", world"); + do_check(r##" +fn -> &str { r#"Hello<|><|>"# +"##, ", world"); + do_check(r" +#[derive(<|>Copy<|>)] +enum Foo { + +} +", "Clone"); } } \ No newline at end of file