From aff0124b37e34910190498ad519deb1aea0d8451 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sun, 23 Dec 2018 14:01:36 +0100 Subject: add line_index proptest --- crates/ra_editor/src/line_index.rs | 234 +++++++++++++++++-------------- crates/ra_editor/src/line_index_utils.rs | 4 +- 2 files changed, 132 insertions(+), 106 deletions(-) (limited to 'crates/ra_editor/src') diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index b01760313..5304fbcf6 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs @@ -128,7 +128,8 @@ impl LineIndex { } } -// for bench and test +/// Simple reference implementation to use in proptests +/// and benchmarks as baseline pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { let mut res = LineCol { line: 0, @@ -150,111 +151,135 @@ pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { res } -#[test] -fn test_line_index() { - let text = "hello\nworld"; - let index = LineIndex::new(text); - assert_eq!( - index.line_col(0.into()), - LineCol { - line: 0, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(1.into()), - LineCol { - line: 0, - col_utf16: 1 - } - ); - assert_eq!( - index.line_col(5.into()), - LineCol { - line: 0, - col_utf16: 5 - } - ); - assert_eq!( - index.line_col(6.into()), - LineCol { - line: 1, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(7.into()), - LineCol { - line: 1, - col_utf16: 1 - } - ); - assert_eq!( - index.line_col(8.into()), - LineCol { - line: 1, - col_utf16: 2 - } - ); - assert_eq!( - index.line_col(10.into()), - LineCol { - line: 1, - col_utf16: 4 - } - ); - assert_eq!( - index.line_col(11.into()), - LineCol { - line: 1, - col_utf16: 5 - } - ); - assert_eq!( - index.line_col(12.into()), - LineCol { - line: 1, - col_utf16: 6 - } - ); +#[cfg(test)] +mod test_line_index { + use super::*; + use proptest::{prelude::*, proptest, proptest_helper}; + use ra_text_edit::test_utils::{arb_text, arb_offset}; - let text = "\nhello\nworld"; - let index = LineIndex::new(text); - assert_eq!( - index.line_col(0.into()), - LineCol { - line: 0, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(1.into()), - LineCol { - line: 1, - col_utf16: 0 - } - ); - assert_eq!( - index.line_col(2.into()), - LineCol { - line: 1, - col_utf16: 1 - } - ); - assert_eq!( - index.line_col(6.into()), - LineCol { - line: 1, - col_utf16: 5 - } - ); - assert_eq!( - index.line_col(7.into()), - LineCol { - line: 2, - col_utf16: 0 + #[test] + fn test_line_index() { + let text = "hello\nworld"; + let index = LineIndex::new(text); + assert_eq!( + index.line_col(0.into()), + LineCol { + line: 0, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(1.into()), + LineCol { + line: 0, + col_utf16: 1 + } + ); + assert_eq!( + index.line_col(5.into()), + LineCol { + line: 0, + col_utf16: 5 + } + ); + assert_eq!( + index.line_col(6.into()), + LineCol { + line: 1, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(7.into()), + LineCol { + line: 1, + col_utf16: 1 + } + ); + assert_eq!( + index.line_col(8.into()), + LineCol { + line: 1, + col_utf16: 2 + } + ); + assert_eq!( + index.line_col(10.into()), + LineCol { + line: 1, + col_utf16: 4 + } + ); + assert_eq!( + index.line_col(11.into()), + LineCol { + line: 1, + col_utf16: 5 + } + ); + assert_eq!( + index.line_col(12.into()), + LineCol { + line: 1, + col_utf16: 6 + } + ); + + let text = "\nhello\nworld"; + let index = LineIndex::new(text); + assert_eq!( + index.line_col(0.into()), + LineCol { + line: 0, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(1.into()), + LineCol { + line: 1, + col_utf16: 0 + } + ); + assert_eq!( + index.line_col(2.into()), + LineCol { + line: 1, + col_utf16: 1 + } + ); + assert_eq!( + index.line_col(6.into()), + LineCol { + line: 1, + col_utf16: 5 + } + ); + assert_eq!( + index.line_col(7.into()), + LineCol { + line: 2, + col_utf16: 0 + } + ); + } + + fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> { + arb_text() + .prop_flat_map(|text| (arb_offset(&text), Just(text))) + .boxed() + } + + proptest! { + #[test] + fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { + let expected = to_line_col(&text, offset); + let line_index = LineIndex::new(&text); + let actual = line_index.line_col(offset); + + assert_eq!(actual, expected); } - ); + } } #[cfg(test)] @@ -349,4 +374,5 @@ const C: char = \"メ メ\"; assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); } + } diff --git a/crates/ra_editor/src/line_index_utils.rs b/crates/ra_editor/src/line_index_utils.rs index 632c962cc..a0bb9a6dd 100644 --- a/crates/ra_editor/src/line_index_utils.rs +++ b/crates/ra_editor/src/line_index_utils.rs @@ -325,7 +325,7 @@ pub fn translate_offset_with_edit( res.to_line_col(offset) } -// for bench +/// Simplest implementation to use as reference in proptest and benchmarks pub fn translate_after_edit( pre_edit_text: &str, offset: TextUnit, @@ -352,8 +352,8 @@ fn edit_text(pre_edit_text: &str, mut edits: Vec) -> String { #[cfg(test)] mod test { - use proptest::{prelude::*, proptest, proptest_helper}; use super::*; + use proptest::{prelude::*, proptest, proptest_helper}; use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; #[derive(Debug)] -- cgit v1.2.3