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_lsp_server/Cargo.toml | 4 +- crates/ra_lsp_server/src/conv.rs | 61 ++++++++++++++++++++++++++++++ crates/ra_text_edit/Cargo.toml | 2 +- crates/ra_text_edit/src/lib.rs | 1 - crates/ra_text_edit/src/test_utils.rs | 70 +++++++++++++++++++---------------- 5 files changed, 103 insertions(+), 35 deletions(-) (limited to 'crates') diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index 3c8c240cd..d73ff158f 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -34,6 +34,8 @@ ra_analysis = { path = "../ra_analysis" } gen_lsp_server = { path = "../gen_lsp_server" } ra_vfs = { path = "../ra_vfs" } +proptest = "0.8.7" + [dev-dependencies] tempdir = "0.3.7" -test_utils = { path = "../test_utils" } +test_utils = { path = "../test_utils" } \ No newline at end of file diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 051f1f995..1db499175 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -386,3 +386,64 @@ where self.iter.next().map(|item| item.conv_with(self.ctx)) } } + +#[cfg(test)] +mod test { + use proptest::{prelude::*, proptest, proptest_helper}; + use super::*; + use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; + + #[derive(Debug)] + struct ArbTextWithOffsetAndEdits { + text: String, + offset: TextUnit, + edits: Vec, + } + + fn arb_text_with_offset_and_edits() -> BoxedStrategy { + arb_text() + .prop_flat_map(|text| { + (arb_offset(&text), arb_edits(&text), Just(text)).prop_map( + |(offset, edits, text)| ArbTextWithOffsetAndEdits { + text, + offset, + edits, + }, + ) + }) + .boxed() + } + + fn translate_offset_with_edit_naive( + pre_edit_text: String, + offset: TextUnit, + edits: &[AtomTextEdit], + ) -> LineCol { + // apply edits ordered from last to first + // since they should not overlap we can just use start() + let mut edits: Vec = edits.to_vec(); + edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize)); + + let mut text = pre_edit_text; + + for edit in &edits { + let range = edit.delete.start().to_usize()..edit.delete.end().to_usize(); + text.replace_range(range, &edit.insert); + } + + let line_index = LineIndex::new(&text); + + line_index.line_col(offset) + } + + proptest! { + #[test] + fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) { + if x.edits.len() <= 1 { + let expected = translate_offset_with_edit_naive(x.text.clone(), x.offset, &x.edits); + let actual = translate_offset_with_edit(&LineIndex::new(&x.text), x.offset, &x.edits); + assert_eq!(actual, expected); + } + } + } +} 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