From 863ed19946d6f707ce09dd77bf26b26be73e097c Mon Sep 17 00:00:00 2001 From: Bernardo Date: Mon, 24 Dec 2018 17:01:25 +0100 Subject: remove benchmark and simplify tests --- crates/ra_text_edit/src/test_utils.rs | 72 +++++++++-------------------------- crates/ra_text_edit/src/text_edit.rs | 15 +++++--- 2 files changed, 28 insertions(+), 59 deletions(-) (limited to 'crates/ra_text_edit') diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs index f150288f6..f0b8dfde1 100644 --- a/crates/ra_text_edit/src/test_utils.rs +++ b/crates/ra_text_edit/src/test_utils.rs @@ -1,6 +1,6 @@ use proptest::prelude::*; use text_unit::{TextUnit, TextRange}; -use crate::AtomTextEdit; +use crate::{AtomTextEdit, TextEdit}; pub fn arb_text() -> proptest::string::RegexGeneratorStrategy { // generate multiple newlines @@ -23,11 +23,7 @@ pub fn arb_offset(text: &str) -> BoxedStrategy { } } -pub fn arb_edits(text: &str) -> BoxedStrategy> { - arb_edits_custom(&text, 0, 7) -} - -pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy> { +pub fn arb_text_edit(text: &str) -> BoxedStrategy { if text.is_empty() { // only valid edits return Just(vec![]) @@ -37,14 +33,14 @@ pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy = cuts .chunks(2) @@ -68,52 +64,22 @@ pub fn arb_edits_custom(text: &str, min: usize, max: usize) -> BoxedStrategy BoxedStrategy<(String, Vec)> { - let text = arb_text(); - text.prop_flat_map(|s| { - let edits = arb_edits(&s); - (Just(s), edits) - }) - .boxed() - } - - fn intersect(r1: TextRange, r2: TextRange) -> Option { - let start = r1.start().max(r2.start()); - let end = r1.end().min(r2.end()); - if start <= end { - Some(TextRange::from_to(start, end)) - } else { - None - } - } - proptest! { - #[test] - fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { - proptest_atom_text_edits_are_valid(text, edits) - } - } +#[derive(Debug, Clone)] +pub struct ArbTextWithEdits { + pub text: String, + pub edits: TextEdit, +} - fn proptest_atom_text_edits_are_valid(text: String, edits: Vec) { - // slicing doesn't panic - for e in &edits { - let _ = &text[e.delete]; - } - // ranges do not overlap - for i in 1..edits.len() { - let e1 = &edits[i]; - for e2 in &edits[0..i] { - if intersect(e1.delete, e2.delete).is_some() { - assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); - } - } - } - } +pub fn arb_text_with_edits() -> BoxedStrategy { + let text = arb_text(); + text.prop_flat_map(|s| { + let edits = arb_text_edit(&s); + (Just(s), edits) + }) + .prop_map(|(text, edits)| ArbTextWithEdits { text, edits }) + .boxed() } diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs index 392968d63..0881f3e1c 100644 --- a/crates/ra_text_edit/src/text_edit.rs +++ b/crates/ra_text_edit/src/text_edit.rs @@ -26,12 +26,7 @@ impl TextEditBuilder { self.atoms.push(AtomTextEdit::insert(offset, text)) } pub fn finish(self) -> TextEdit { - let mut atoms = self.atoms; - atoms.sort_by_key(|a| (a.delete.start(), a.delete.end())); - for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) { - assert!(a1.delete.end() <= a2.delete.start()) - } - TextEdit { atoms } + TextEdit::from_atoms(self.atoms) } pub fn invalidates_offset(&self, offset: TextUnit) -> bool { self.atoms @@ -41,6 +36,14 @@ impl TextEditBuilder { } impl TextEdit { + pub(crate) fn from_atoms(mut atoms: Vec) -> TextEdit { + atoms.sort_by_key(|a| (a.delete.start(), a.delete.end())); + for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) { + assert!(a1.delete.end() <= a2.delete.start()) + } + TextEdit { atoms } + } + pub fn as_atoms(&self) -> &[AtomTextEdit] { &self.atoms } -- cgit v1.2.3