aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_text_edit/src
diff options
context:
space:
mode:
authorBernardo <[email protected]>2018-12-14 17:00:35 +0000
committerBernardo <[email protected]>2018-12-25 18:49:55 +0000
commitd9519791590170fd77aa45cc9a802539eaf16efc (patch)
treeebd711bfab97eea59dff07623f7b3b55482b6cb5 /crates/ra_text_edit/src
parentdee426e1b120cd12fc608df77f977d4800b6655d (diff)
test translate_offset_with_edit against simple impl for single edits
Diffstat (limited to 'crates/ra_text_edit/src')
-rw-r--r--crates/ra_text_edit/src/lib.rs1
-rw-r--r--crates/ra_text_edit/src/test_utils.rs70
2 files changed, 38 insertions, 33 deletions
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 @@
1mod text_edit; 1mod text_edit;
2pub mod text_utils; 2pub mod text_utils;
3#[cfg(test)]
4pub mod test_utils; 3pub mod test_utils;
5 4
6pub use crate::text_edit::{TextEdit, TextEditBuilder}; 5pub 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 @@
1use proptest::{prelude::*, proptest, proptest_helper}; 1use proptest::prelude::*;
2use text_unit::{TextUnit, TextRange}; 2use text_unit::{TextUnit, TextRange};
3use crate::AtomTextEdit; 3use 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
69fn arb_text_with_edits() -> BoxedStrategy<(String, Vec<AtomTextEdit>)> { 69#[cfg(test)]
70 let text = arb_text(); 70mod 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
78fn 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
88proptest! { 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]
95fn 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 }