From bc2550b196fbf341ce0168f7dda5498e4d7aaf63 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 28 May 2019 16:59:22 +0300 Subject: [PATCH] update tests --- crates/ra_syntax/src/lib.rs | 16 +++++++++++- crates/ra_syntax/src/parsing/reparsing.rs | 12 +++++---- crates/ra_syntax/src/syntax_node.rs | 30 +++-------------------- crates/ra_syntax/tests/test.rs | 25 ++++++++----------- 4 files changed, 36 insertions(+), 47 deletions(-) diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index f765f621bf0..37320e1ba3e 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -31,7 +31,7 @@ #[doc(hidden)] pub mod fuzz; -use std::sync::Arc; +use std::{sync::Arc, fmt::Write}; use ra_text_edit::AtomTextEdit; @@ -68,6 +68,14 @@ pub fn ok(self) -> Result, Arc>> { Err(self.errors) } } + + pub fn debug_dump(&self) -> String { + let mut buf = self.tree.syntax().debug_dump(); + for err in self.errors.iter() { + writeln!(buf, "err: `{}`", err).unwrap(); + } + buf + } } /// `SourceFile` represents a parse tree for a single Rust file. @@ -83,6 +91,12 @@ fn new(green: GreenNode) -> TreeArc { TreeArc::cast(root) } + pub fn parse2(text: &str) -> Parse { + let (green, errors) = parsing::parse_text(text); + let tree = SourceFile::new(green); + Parse { tree, errors: Arc::new(errors) } + } + pub fn parse(text: &str) -> TreeArc { let (green, _errors) = parsing::parse_text(text); SourceFile::new(green) diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index 3b6687f61b4..dc913cf2b2e 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -166,9 +166,11 @@ fn merge_errors( #[cfg(test)] mod tests { + use std::sync::Arc; + use test_utils::{extract_range, assert_eq_text}; - use crate::{SourceFile, AstNode}; + use crate::{SourceFile, AstNode, Parse}; use super::*; fn do_check(before: &str, replace_with: &str, reparsed_len: u32) { @@ -176,19 +178,19 @@ fn do_check(before: &str, replace_with: &str, reparsed_len: u32) { let edit = AtomTextEdit::replace(range, replace_with.to_owned()); let after = edit.apply(before.clone()); - let fully_reparsed = SourceFile::parse(&after); + let fully_reparsed = SourceFile::parse2(&after); let incrementally_reparsed = { let f = SourceFile::parse(&before); let edit = AtomTextEdit { delete: range, insert: replace_with.to_string() }; let (green, new_errors, range) = incremental_reparse(f.syntax(), &edit, f.errors()).unwrap(); assert_eq!(range.len(), reparsed_len.into(), "reparsed fragment has wrong length"); - SourceFile::new(green, new_errors) + Parse { tree: SourceFile::new(green), errors: Arc::new(new_errors) } }; assert_eq_text!( - &fully_reparsed.syntax().debug_dump(), - &incrementally_reparsed.syntax().debug_dump(), + &fully_reparsed.tree.syntax().debug_dump(), + &incrementally_reparsed.tree.syntax().debug_dump(), ); } diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index ef7a51686ee..769125d11bb 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -256,37 +256,18 @@ pub fn memory_size_of_subtree(&self) -> usize { } pub fn debug_dump(&self) -> String { - let mut errors: Vec<_> = match self.ancestors().find_map(SourceFile::cast) { - Some(file) => file.errors(), - None => self.root_data().to_vec(), - }; - errors.sort_by_key(|e| e.offset()); - let mut err_pos = 0; let mut level = 0; let mut buf = String::new(); - macro_rules! indent { - () => { - for _ in 0..level { - buf.push_str(" "); - } - }; - } for event in self.preorder_with_tokens() { match event { WalkEvent::Enter(element) => { - indent!(); + for _ in 0..level { + buf.push_str(" "); + } match element { SyntaxElement::Node(node) => writeln!(buf, "{:?}", node).unwrap(), - SyntaxElement::Token(token) => { - writeln!(buf, "{:?}", token).unwrap(); - let off = token.range().end(); - while err_pos < errors.len() && errors[err_pos].offset() <= off { - indent!(); - writeln!(buf, "err: `{}`", errors[err_pos]).unwrap(); - err_pos += 1; - } - } + SyntaxElement::Token(token) => writeln!(buf, "{:?}", token).unwrap(), } level += 1; } @@ -295,9 +276,6 @@ macro_rules! indent { } assert_eq!(level, 0); - for err in errors[err_pos..].iter() { - writeln!(buf, "err: `{}`", err).unwrap(); - } buf } diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 91799f8b55a..4b711f27165 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -8,7 +8,7 @@ }; use test_utils::{project_dir, dir_tests, read_text, collect_tests}; -use ra_syntax::{SourceFile, AstNode, fuzz}; +use ra_syntax::{SourceFile, fuzz}; #[test] fn lexer_tests() { @@ -21,26 +21,21 @@ fn lexer_tests() { #[test] fn parser_tests() { dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], |text, path| { - let file = SourceFile::parse(text); - let errors = file.errors(); + let parse = SourceFile::parse2(text); + let errors = parse.errors.as_slice(); assert_eq!( - &*errors, + errors, &[] as &[ra_syntax::SyntaxError], "There should be no errors in the file {:?}", - path.display() + path.display(), ); - file.syntax().debug_dump() + parse.debug_dump() }); dir_tests(&test_data_dir(), &["parser/err", "parser/inline/err"], |text, path| { - let file = SourceFile::parse(text); - let errors = file.errors(); - assert_ne!( - &*errors, - &[] as &[ra_syntax::SyntaxError], - "There should be errors in the file {:?}", - path.display() - ); - file.syntax().debug_dump() + let parse = SourceFile::parse2(text); + let errors = parse.errors.as_slice(); + assert!(!errors.is_empty(), "There should be errors in the file {:?}", path.display()); + parse.debug_dump() }); }