From d9519791590170fd77aa45cc9a802539eaf16efc Mon Sep 17 00:00:00 2001 From: Bernardo Date: Fri, 14 Dec 2018 18:00:35 +0100 Subject: test translate_offset_with_edit against simple impl for single edits --- crates/ra_text_edit/Cargo.toml | 2 +- crates/ra_text_edit/src/lib.rs | 1 - crates/ra_text_edit/src/test_utils.rs | 70 +++++++++++++++++++---------------- 3 files changed, 39 insertions(+), 34 deletions(-) (limited to 'crates/ra_text_edit') diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml index 67dfd0924..e0db49688 100644 --- a/crates/ra_text_edit/Cargo.toml +++ b/crates/ra_text_edit/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] text_unit = "0.1.5" +proptest = "0.8.7" [dev-dependencies] test_utils = { path = "../test_utils" } -proptest = "0.8.7" diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index 0a407795e..89600413a 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs @@ -1,6 +1,5 @@ mod text_edit; pub mod text_utils; -#[cfg(test)] pub mod test_utils; pub use crate::text_edit::{TextEdit, TextEditBuilder}; diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs index 29301bff3..4a0ebc08e 100644 --- a/crates/ra_text_edit/src/test_utils.rs +++ b/crates/ra_text_edit/src/test_utils.rs @@ -1,4 +1,4 @@ -use proptest::{prelude::*, proptest, proptest_helper}; +use proptest::prelude::*; use text_unit::{TextUnit, TextRange}; use crate::AtomTextEdit; @@ -66,42 +66,48 @@ pub fn arb_edits(text: &str) -> BoxedStrategy> { .boxed() } -fn arb_text_with_edits() -> BoxedStrategy<(String, Vec)> { - let text = arb_text(); - text.prop_flat_map(|s| { - let edits = arb_edits(&s); - (Just(s), edits) - }) - .boxed() -} +#[cfg(test)] +mod tests { + use super::*; + use proptest::{proptest, proptest_helper}; -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 + fn arb_text_with_edits() -> BoxedStrategy<(String, Vec)> { + let text = arb_text(); + text.prop_flat_map(|s| { + let edits = arb_edits(&s); + (Just(s), edits) + }) + .boxed() } -} -proptest! { - #[test] - fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { - proptest_atom_text_edits_are_valid(text, edits) + 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 + } } -} - -fn proptest_atom_text_edits_are_valid(text: String, edits: Vec) { - // slicing doesn't panic - for e in &edits { - let _ = &text[e.delete]; + proptest! { + #[test] + fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { + proptest_atom_text_edits_are_valid(text, edits) + } } - // ranges do not overlap - for (i1, e1) in edits.iter().skip(1).enumerate() { - for e2 in &edits[0..i1] { - if intersect(e1.delete, e2.delete).is_some() { - assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); + + 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); + } } } } -- cgit v1.2.3