aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide/Cargo.toml6
-rw-r--r--crates/ra_ide_db/Cargo.toml6
-rw-r--r--crates/ra_ide_db/src/line_index.rs60
-rw-r--r--crates/ra_ide_db/src/line_index_utils.rs42
-rw-r--r--crates/ra_text_edit/Cargo.toml6
-rw-r--r--crates/ra_text_edit/src/lib.rs5
-rw-r--r--crates/ra_text_edit/src/test_utils.rs83
7 files changed, 2 insertions, 206 deletions
diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml
index 9ace35229..d50cf1d20 100644
--- a/crates/ra_ide/Cargo.toml
+++ b/crates/ra_ide/Cargo.toml
@@ -41,9 +41,3 @@ hir = { path = "../ra_hir", package = "ra_hir" }
41 41
42[dev-dependencies] 42[dev-dependencies]
43insta = "0.13.0" 43insta = "0.13.0"
44
45[dev-dependencies.proptest]
46version = "0.9.0"
47# Disable `fork` feature to allow compiling on webassembly
48default-features = false
49features = ["std", "bit-set", "break-dead-code"]
diff --git a/crates/ra_ide_db/Cargo.toml b/crates/ra_ide_db/Cargo.toml
index 495fffb5a..ad3acce59 100644
--- a/crates/ra_ide_db/Cargo.toml
+++ b/crates/ra_ide_db/Cargo.toml
@@ -38,9 +38,3 @@ hir = { path = "../ra_hir", package = "ra_hir" }
38 38
39[dev-dependencies] 39[dev-dependencies]
40insta = "0.13.0" 40insta = "0.13.0"
41
42[dev-dependencies.proptest]
43version = "0.9.0"
44# Disable `fork` feature to allow compiling on webassembly
45default-features = false
46features = ["std", "bit-set", "break-dead-code"]
diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs
index 452c87ac5..af7b759e5 100644
--- a/crates/ra_ide_db/src/line_index.rs
+++ b/crates/ra_ide_db/src/line_index.rs
@@ -125,30 +125,8 @@ impl LineIndex {
125} 125}
126 126
127#[cfg(test)] 127#[cfg(test)]
128/// Simple reference implementation to use in proptests
129pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
130 let mut res = LineCol { line: 0, col_utf16: 0 };
131 for (i, c) in text.char_indices() {
132 if i + c.len_utf8() > offset.to_usize() {
133 // if it's an invalid offset, inside a multibyte char
134 // return as if it was at the start of the char
135 break;
136 }
137 if c == '\n' {
138 res.line += 1;
139 res.col_utf16 = 0;
140 } else {
141 res.col_utf16 += 1;
142 }
143 }
144 res
145}
146
147#[cfg(test)]
148mod test_line_index { 128mod test_line_index {
149 use super::*; 129 use super::*;
150 use proptest::{prelude::*, proptest};
151 use ra_text_edit::test_utils::{arb_offset, arb_text};
152 130
153 #[test] 131 #[test]
154 fn test_line_index() { 132 fn test_line_index() {
@@ -173,44 +151,6 @@ mod test_line_index {
173 assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 }); 151 assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 });
174 } 152 }
175 153
176 fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> {
177 arb_text().prop_flat_map(|text| (arb_offset(&text), Just(text))).boxed()
178 }
179
180 fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
181 let mut res = LineCol { line: 0, col_utf16: 0 };
182 for (i, c) in text.char_indices() {
183 if i + c.len_utf8() > offset.to_usize() {
184 // if it's an invalid offset, inside a multibyte char
185 // return as if it was at the start of the char
186 break;
187 }
188 if c == '\n' {
189 res.line += 1;
190 res.col_utf16 = 0;
191 } else {
192 res.col_utf16 += 1;
193 }
194 }
195 res
196 }
197
198 proptest! {
199 #[test]
200 fn test_line_index_proptest((offset, text) in arb_text_with_offset()) {
201 let expected = to_line_col(&text, offset);
202 let line_index = LineIndex::new(&text);
203 let actual = line_index.line_col(offset);
204
205 assert_eq!(actual, expected);
206 }
207 }
208}
209
210#[cfg(test)]
211mod test_utf8_utf16_conv {
212 use super::*;
213
214 #[test] 154 #[test]
215 fn test_char_len() { 155 fn test_char_len() {
216 assert_eq!('メ'.len_utf8(), 3); 156 assert_eq!('メ'.len_utf8(), 3);
diff --git a/crates/ra_ide_db/src/line_index_utils.rs b/crates/ra_ide_db/src/line_index_utils.rs
index 435b06511..75a498151 100644
--- a/crates/ra_ide_db/src/line_index_utils.rs
+++ b/crates/ra_ide_db/src/line_index_utils.rs
@@ -297,45 +297,3 @@ impl RunningLineCol {
297 self.col_adjust += range.len() - TextUnit::from(1); 297 self.col_adjust += range.len() - TextUnit::from(1);
298 } 298 }
299} 299}
300
301#[cfg(test)]
302mod test {
303 use proptest::{prelude::*, proptest};
304 use ra_text_edit::test_utils::{arb_offset, arb_text_with_edit};
305 use ra_text_edit::TextEdit;
306
307 use crate::line_index;
308
309 use super::*;
310
311 #[derive(Debug)]
312 struct ArbTextWithEditAndOffset {
313 text: String,
314 edit: TextEdit,
315 edited_text: String,
316 offset: TextUnit,
317 }
318
319 fn arb_text_with_edit_and_offset() -> BoxedStrategy<ArbTextWithEditAndOffset> {
320 arb_text_with_edit()
321 .prop_flat_map(|x| {
322 let edited_text = x.edit.apply(&x.text);
323 let arb_offset = arb_offset(&edited_text);
324 (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| {
325 ArbTextWithEditAndOffset { text: x.text, edit: x.edit, edited_text, offset }
326 })
327 })
328 .boxed()
329 }
330
331 proptest! {
332 #[test]
333 fn test_translate_offset_with_edit(x in arb_text_with_edit_and_offset()) {
334 let expected = line_index::to_line_col(&x.edited_text, x.offset);
335 let line_index = LineIndex::new(&x.text);
336 let actual = translate_offset_with_edit(&line_index, x.offset, &x.edit);
337
338 assert_eq!(actual, expected);
339 }
340 }
341}
diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml
index 8573c521a..4490ae43b 100644
--- a/crates/ra_text_edit/Cargo.toml
+++ b/crates/ra_text_edit/Cargo.toml
@@ -11,11 +11,5 @@ doctest = false
11[dependencies] 11[dependencies]
12text_unit = "0.1.6" 12text_unit = "0.1.6"
13 13
14[dependencies.proptest]
15version = "0.9.0"
16# Disable `fork` feature to allow compiling on webassembly
17default-features = false
18features = ["std", "bit-set", "break-dead-code"]
19
20[dev-dependencies] 14[dev-dependencies]
21test_utils = { path = "../test_utils" } 15test_utils = { path = "../test_utils" }
diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs
index 37f23d043..f6769e6a6 100644
--- a/crates/ra_text_edit/src/lib.rs
+++ b/crates/ra_text_edit/src/lib.rs
@@ -1,12 +1,11 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3mod text_edit; 3mod text_edit;
4pub mod test_utils;
5
6pub use crate::text_edit::{TextEdit, TextEditBuilder};
7 4
8use text_unit::{TextRange, TextUnit}; 5use text_unit::{TextRange, TextUnit};
9 6
7pub use crate::text_edit::{TextEdit, TextEditBuilder};
8
10/// Must not overlap with other `AtomTextEdit`s 9/// Must not overlap with other `AtomTextEdit`s
11#[derive(Debug, Clone)] 10#[derive(Debug, Clone)]
12pub struct AtomTextEdit { 11pub struct AtomTextEdit {
diff --git a/crates/ra_text_edit/src/test_utils.rs b/crates/ra_text_edit/src/test_utils.rs
deleted file mode 100644
index d4c7840ff..000000000
--- a/crates/ra_text_edit/src/test_utils.rs
+++ /dev/null
@@ -1,83 +0,0 @@
1//! FIXME: write short doc here
2
3use crate::{AtomTextEdit, TextEdit};
4use proptest::prelude::*;
5use text_unit::{TextRange, TextUnit};
6
7pub fn arb_text() -> proptest::string::RegexGeneratorStrategy<String> {
8 // generate multiple newlines
9 proptest::string::string_regex("(.*\n?)*").unwrap()
10}
11
12fn text_offsets(text: &str) -> Vec<TextUnit> {
13 text.char_indices().map(|(i, _)| TextUnit::from_usize(i)).collect()
14}
15
16pub fn arb_offset(text: &str) -> BoxedStrategy<TextUnit> {
17 let offsets = text_offsets(text);
18 // this is necessary to avoid "Uniform::new called with `low >= high`" panic
19 if offsets.is_empty() {
20 Just(TextUnit::from(0)).boxed()
21 } else {
22 prop::sample::select(offsets).boxed()
23 }
24}
25
26pub fn arb_text_edit(text: &str) -> BoxedStrategy<TextEdit> {
27 if text.is_empty() {
28 // only valid edits
29 return Just(vec![])
30 .boxed()
31 .prop_union(
32 arb_text()
33 .prop_map(|text| vec![AtomTextEdit::insert(TextUnit::from(0), text)])
34 .boxed(),
35 )
36 .prop_map(TextEdit::from_atoms)
37 .boxed();
38 }
39
40 let offsets = text_offsets(text);
41 let max_cuts = 7.min(offsets.len());
42
43 proptest::sample::subsequence(offsets, 0..max_cuts)
44 .prop_flat_map(|cuts| {
45 let strategies: Vec<_> = cuts
46 .chunks(2)
47 .map(|chunk| match *chunk {
48 [from, to] => {
49 let range = TextRange::from_to(from, to);
50 Just(AtomTextEdit::delete(range))
51 .boxed()
52 .prop_union(
53 arb_text()
54 .prop_map(move |text| AtomTextEdit::replace(range, text))
55 .boxed(),
56 )
57 .boxed()
58 }
59 [x] => arb_text().prop_map(move |text| AtomTextEdit::insert(x, text)).boxed(),
60 _ => unreachable!(),
61 })
62 .collect();
63 strategies
64 })
65 .prop_map(TextEdit::from_atoms)
66 .boxed()
67}
68
69#[derive(Debug, Clone)]
70pub struct ArbTextWithEdit {
71 pub text: String,
72 pub edit: TextEdit,
73}
74
75pub fn arb_text_with_edit() -> BoxedStrategy<ArbTextWithEdit> {
76 let text = arb_text();
77 text.prop_flat_map(|s| {
78 let edit = arb_text_edit(&s);
79 (Just(s), edit)
80 })
81 .prop_map(|(text, edit)| ArbTextWithEdit { text, edit })
82 .boxed()
83}