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/src/conv.rs | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'crates/ra_lsp_server/src') 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); + } + } + } +} -- cgit v1.2.3