move reparsing tests

This commit is contained in:
darksv 2018-09-15 14:35:30 +02:00
parent 16ad5384f0
commit 46cee0415c
3 changed files with 140 additions and 129 deletions

View file

@ -27,6 +27,10 @@ extern crate parking_lot;
extern crate smol_str;
extern crate text_unit;
#[cfg(test)]
#[macro_use]
extern crate test_utils;
pub mod algo;
pub mod ast;
mod lexer;

View file

@ -186,3 +186,138 @@ fn merge_errors(
}
res
}
#[cfg(test)]
use super::{File, text_utils, test_utils, utils};
#[cfg(test)]
mod tests {
use super::{*, utils::dump_tree};
#[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()),
)
}
do_check(r"
fn foo() {
let x = foo + <|>bar<|>
}
", "baz");
do_check(r"
fn foo() {
let x = foo<|> + bar<|>
}
", "baz");
do_check(r"
struct Foo {
f: foo<|><|>
}
", ",\n g: (),");
do_check(r"
fn foo {
let;
1 + 1;
<|>92<|>;
}
", "62");
do_check(r"
mod foo {
fn <|><|>
}
", "bar");
do_check(r"
trait Foo {
type <|>Foo<|>;
}
", "Output");
do_check(r"
trait Foo {
type<|> Foo<|>;
}
", "Output");
do_check(r"
impl IntoIterator<Item=i32> for Foo {
f<|><|>
}
", "n next(");
do_check(r"
use a::b::{foo,<|>,bar<|>};
", "baz");
do_check(r"
pub enum A {
Foo<|><|>
}
", "\nBar;\n");
do_check(r"
foo!{a, b<|><|> d}
", ", c[3]");
do_check(r"
fn foo() {
vec![<|><|>]
}
", "123");
do_check(r"
extern {
fn<|>;<|>
}
", " exit(code: c_int)");
do_check(r"<|><|>
fn foo() -> i32 {
1
}
", "\n\n\n \n");
do_check(r"
fn foo() -> <|><|> {}
", " \n");
do_check(r"
fn <|>foo<|>() -> i32 {
1
}
", "bar");
do_check(r"
fn aa<|><|>bb() {
}
", "foofoo");
do_check(r"
fn aabb /* <|><|> */ () {
}
", "some comment");
do_check(r"
fn aabb <|><|> () {
}
", " \t\t\n\n");
do_check(r"
trait foo {
// comment <|><|>
}
", "\n");
do_check(r"
/// good <|><|>omment
mod {
}
", "c");
do_check(r#"
fn -> &str { "Hello<|><|>" }
"#, ", world");
do_check(r#"
fn -> &str { // "Hello<|><|>"
"#, ", world");
}
}

View file

@ -9,9 +9,8 @@ use std::{
fmt::Write,
};
use test_utils::extract_range;
use libsyntax2::{
File, AtomEdit,
File,
utils::{dump_tree, check_fuzz_invariants},
};
@ -23,133 +22,6 @@ fn lexer_tests() {
})
}
#[test]
fn reparse_test() {
fn do_check(before: &str, replace_with: &str) {
let (range, before) = extract_range(before);
let after = libsyntax2::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"
fn foo() {
let x = foo + <|>bar<|>
}
", "baz");
do_check(r"
fn foo() {
let x = foo<|> + bar<|>
}
", "baz");
do_check(r"
struct Foo {
f: foo<|><|>
}
", ",\n g: (),");
do_check(r"
fn foo {
let;
1 + 1;
<|>92<|>;
}
", "62");
do_check(r"
mod foo {
fn <|><|>
}
", "bar");
do_check(r"
trait Foo {
type <|>Foo<|>;
}
", "Output");
do_check(r"
trait Foo {
type<|> Foo<|>;
}
", "Output");
do_check(r"
impl IntoIterator<Item=i32> for Foo {
f<|><|>
}
", "n next(");
do_check(r"
use a::b::{foo,<|>,bar<|>};
", "baz");
do_check(r"
pub enum A {
Foo<|><|>
}
", "\nBar;\n");
do_check(r"
foo!{a, b<|><|> d}
", ", c[3]");
do_check(r"
fn foo() {
vec![<|><|>]
}
", "123");
do_check(r"
extern {
fn<|>;<|>
}
", " exit(code: c_int)");
do_check(r"<|><|>
fn foo() -> i32 {
1
}
", "\n\n\n \n");
do_check(r"
fn foo() -> <|><|> {}
", " \n");
do_check(r"
fn <|>foo<|>() -> i32 {
1
}
", "bar");
do_check(r"
fn aa<|><|>bb() {
}
", "foofoo");
do_check(r"
fn aabb /* <|><|> */ () {
}
", "some comment");
do_check(r"
fn aabb <|><|> () {
}
", " \t\t\n\n");
do_check(r"
trait foo {
// comment <|><|>
}
", "\n");
do_check(r"
/// good <|><|>omment
mod {
}
", "c");
do_check(r#"
fn -> &str { "Hello<|><|>" }
"#, ", world");
do_check(r#"
fn -> &str { // "Hello<|><|>"
"#, ", world");
}
#[test]
fn parser_tests() {
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {