diff options
author | Bernardo <[email protected]> | 2018-12-14 17:00:35 +0000 |
---|---|---|
committer | Bernardo <[email protected]> | 2018-12-25 18:49:55 +0000 |
commit | d9519791590170fd77aa45cc9a802539eaf16efc (patch) | |
tree | ebd711bfab97eea59dff07623f7b3b55482b6cb5 /crates/ra_text_edit | |
parent | dee426e1b120cd12fc608df77f977d4800b6655d (diff) |
test translate_offset_with_edit against simple impl for single edits
Diffstat (limited to 'crates/ra_text_edit')
-rw-r--r-- | crates/ra_text_edit/Cargo.toml | 2 | ||||
-rw-r--r-- | crates/ra_text_edit/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/ra_text_edit/src/test_utils.rs | 70 |
3 files changed, 39 insertions, 34 deletions
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 | |||
7 | 7 | ||
8 | [dependencies] | 8 | [dependencies] |
9 | text_unit = "0.1.5" | 9 | text_unit = "0.1.5" |
10 | proptest = "0.8.7" | ||
10 | 11 | ||
11 | [dev-dependencies] | 12 | [dev-dependencies] |
12 | test_utils = { path = "../test_utils" } | 13 | test_utils = { path = "../test_utils" } |
13 | 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 @@ | |||
1 | mod text_edit; | 1 | mod text_edit; |
2 | pub mod text_utils; | 2 | pub mod text_utils; |
3 | #[cfg(test)] | ||
4 | pub mod test_utils; | 3 | pub mod test_utils; |
5 | 4 | ||
6 | pub use crate::text_edit::{TextEdit, TextEditBuilder}; | 5 | 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 @@ | |||
1 | use proptest::{prelude::*, proptest, proptest_helper}; | 1 | use proptest::prelude::*; |
2 | use text_unit::{TextUnit, TextRange}; | 2 | use text_unit::{TextUnit, TextRange}; |
3 | use crate::AtomTextEdit; | 3 | use crate::AtomTextEdit; |
4 | 4 | ||
@@ -66,42 +66,48 @@ pub fn arb_edits(text: &str) -> BoxedStrategy<Vec<AtomTextEdit>> { | |||
66 | .boxed() | 66 | .boxed() |
67 | } | 67 | } |
68 | 68 | ||
69 | fn arb_text_with_edits() -> BoxedStrategy<(String, Vec<AtomTextEdit>)> { | 69 | #[cfg(test)] |
70 | let text = arb_text(); | 70 | mod tests { |
71 | text.prop_flat_map(|s| { | 71 | use super::*; |
72 | let edits = arb_edits(&s); | 72 | use proptest::{proptest, proptest_helper}; |
73 | (Just(s), edits) | ||
74 | }) | ||
75 | .boxed() | ||
76 | } | ||
77 | 73 | ||
78 | fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> { | 74 | fn arb_text_with_edits() -> BoxedStrategy<(String, Vec<AtomTextEdit>)> { |
79 | let start = r1.start().max(r2.start()); | 75 | let text = arb_text(); |
80 | let end = r1.end().min(r2.end()); | 76 | text.prop_flat_map(|s| { |
81 | if start <= end { | 77 | let edits = arb_edits(&s); |
82 | Some(TextRange::from_to(start, end)) | 78 | (Just(s), edits) |
83 | } else { | 79 | }) |
84 | None | 80 | .boxed() |
85 | } | 81 | } |
86 | } | ||
87 | 82 | ||
88 | proptest! { | 83 | fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> { |
89 | #[test] | 84 | let start = r1.start().max(r2.start()); |
90 | fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { | 85 | let end = r1.end().min(r2.end()); |
91 | proptest_atom_text_edits_are_valid(text, edits) | 86 | if start <= end { |
87 | Some(TextRange::from_to(start, end)) | ||
88 | } else { | ||
89 | None | ||
90 | } | ||
92 | } | 91 | } |
93 | } | 92 | proptest! { |
94 | 93 | #[test] | |
95 | fn proptest_atom_text_edits_are_valid(text: String, edits: Vec<AtomTextEdit>) { | 94 | fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) { |
96 | // slicing doesn't panic | 95 | proptest_atom_text_edits_are_valid(text, edits) |
97 | for e in &edits { | 96 | } |
98 | let _ = &text[e.delete]; | ||
99 | } | 97 | } |
100 | // ranges do not overlap | 98 | |
101 | for (i1, e1) in edits.iter().skip(1).enumerate() { | 99 | fn proptest_atom_text_edits_are_valid(text: String, edits: Vec<AtomTextEdit>) { |
102 | for e2 in &edits[0..i1] { | 100 | // slicing doesn't panic |
103 | if intersect(e1.delete, e2.delete).is_some() { | 101 | for e in &edits { |
104 | assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); | 102 | let _ = &text[e.delete]; |
103 | } | ||
104 | // ranges do not overlap | ||
105 | for i in 1..edits.len() { | ||
106 | let e1 = &edits[i]; | ||
107 | for e2 in &edits[0..i] { | ||
108 | if intersect(e1.delete, e2.delete).is_some() { | ||
109 | assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete); | ||
110 | } | ||
105 | } | 111 | } |
106 | } | 112 | } |
107 | } | 113 | } |