diff options
author | Bernardo <[email protected]> | 2018-12-23 13:01:36 +0000 |
---|---|---|
committer | Bernardo <[email protected]> | 2018-12-25 19:03:14 +0000 |
commit | aff0124b37e34910190498ad519deb1aea0d8451 (patch) | |
tree | 89a98d0f4212684c076d2c535e74d9683dd03e2c /crates/ra_editor/src | |
parent | c886b72dab918d6f4d3be025135f769bc429cc79 (diff) |
add line_index proptest
Diffstat (limited to 'crates/ra_editor/src')
-rw-r--r-- | crates/ra_editor/src/line_index.rs | 234 | ||||
-rw-r--r-- | crates/ra_editor/src/line_index_utils.rs | 4 |
2 files changed, 132 insertions, 106 deletions
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 { | |||
128 | } | 128 | } |
129 | } | 129 | } |
130 | 130 | ||
131 | // for bench and test | 131 | /// Simple reference implementation to use in proptests |
132 | /// and benchmarks as baseline | ||
132 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | 133 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { |
133 | let mut res = LineCol { | 134 | let mut res = LineCol { |
134 | line: 0, | 135 | line: 0, |
@@ -150,111 +151,135 @@ pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | |||
150 | res | 151 | res |
151 | } | 152 | } |
152 | 153 | ||
153 | #[test] | 154 | #[cfg(test)] |
154 | fn test_line_index() { | 155 | mod test_line_index { |
155 | let text = "hello\nworld"; | 156 | use super::*; |
156 | let index = LineIndex::new(text); | 157 | use proptest::{prelude::*, proptest, proptest_helper}; |
157 | assert_eq!( | 158 | use ra_text_edit::test_utils::{arb_text, arb_offset}; |
158 | index.line_col(0.into()), | ||
159 | LineCol { | ||
160 | line: 0, | ||
161 | col_utf16: 0 | ||
162 | } | ||
163 | ); | ||
164 | assert_eq!( | ||
165 | index.line_col(1.into()), | ||
166 | LineCol { | ||
167 | line: 0, | ||
168 | col_utf16: 1 | ||
169 | } | ||
170 | ); | ||
171 | assert_eq!( | ||
172 | index.line_col(5.into()), | ||
173 | LineCol { | ||
174 | line: 0, | ||
175 | col_utf16: 5 | ||
176 | } | ||
177 | ); | ||
178 | assert_eq!( | ||
179 | index.line_col(6.into()), | ||
180 | LineCol { | ||
181 | line: 1, | ||
182 | col_utf16: 0 | ||
183 | } | ||
184 | ); | ||
185 | assert_eq!( | ||
186 | index.line_col(7.into()), | ||
187 | LineCol { | ||
188 | line: 1, | ||
189 | col_utf16: 1 | ||
190 | } | ||
191 | ); | ||
192 | assert_eq!( | ||
193 | index.line_col(8.into()), | ||
194 | LineCol { | ||
195 | line: 1, | ||
196 | col_utf16: 2 | ||
197 | } | ||
198 | ); | ||
199 | assert_eq!( | ||
200 | index.line_col(10.into()), | ||
201 | LineCol { | ||
202 | line: 1, | ||
203 | col_utf16: 4 | ||
204 | } | ||
205 | ); | ||
206 | assert_eq!( | ||
207 | index.line_col(11.into()), | ||
208 | LineCol { | ||
209 | line: 1, | ||
210 | col_utf16: 5 | ||
211 | } | ||
212 | ); | ||
213 | assert_eq!( | ||
214 | index.line_col(12.into()), | ||
215 | LineCol { | ||
216 | line: 1, | ||
217 | col_utf16: 6 | ||
218 | } | ||
219 | ); | ||
220 | 159 | ||
221 | let text = "\nhello\nworld"; | 160 | #[test] |
222 | let index = LineIndex::new(text); | 161 | fn test_line_index() { |
223 | assert_eq!( | 162 | let text = "hello\nworld"; |
224 | index.line_col(0.into()), | 163 | let index = LineIndex::new(text); |
225 | LineCol { | 164 | assert_eq!( |
226 | line: 0, | 165 | index.line_col(0.into()), |
227 | col_utf16: 0 | 166 | LineCol { |
228 | } | 167 | line: 0, |
229 | ); | 168 | col_utf16: 0 |
230 | assert_eq!( | 169 | } |
231 | index.line_col(1.into()), | 170 | ); |
232 | LineCol { | 171 | assert_eq!( |
233 | line: 1, | 172 | index.line_col(1.into()), |
234 | col_utf16: 0 | 173 | LineCol { |
235 | } | 174 | line: 0, |
236 | ); | 175 | col_utf16: 1 |
237 | assert_eq!( | 176 | } |
238 | index.line_col(2.into()), | 177 | ); |
239 | LineCol { | 178 | assert_eq!( |
240 | line: 1, | 179 | index.line_col(5.into()), |
241 | col_utf16: 1 | 180 | LineCol { |
242 | } | 181 | line: 0, |
243 | ); | 182 | col_utf16: 5 |
244 | assert_eq!( | 183 | } |
245 | index.line_col(6.into()), | 184 | ); |
246 | LineCol { | 185 | assert_eq!( |
247 | line: 1, | 186 | index.line_col(6.into()), |
248 | col_utf16: 5 | 187 | LineCol { |
249 | } | 188 | line: 1, |
250 | ); | 189 | col_utf16: 0 |
251 | assert_eq!( | 190 | } |
252 | index.line_col(7.into()), | 191 | ); |
253 | LineCol { | 192 | assert_eq!( |
254 | line: 2, | 193 | index.line_col(7.into()), |
255 | col_utf16: 0 | 194 | LineCol { |
195 | line: 1, | ||
196 | col_utf16: 1 | ||
197 | } | ||
198 | ); | ||
199 | assert_eq!( | ||
200 | index.line_col(8.into()), | ||
201 | LineCol { | ||
202 | line: 1, | ||
203 | col_utf16: 2 | ||
204 | } | ||
205 | ); | ||
206 | assert_eq!( | ||
207 | index.line_col(10.into()), | ||
208 | LineCol { | ||
209 | line: 1, | ||
210 | col_utf16: 4 | ||
211 | } | ||
212 | ); | ||
213 | assert_eq!( | ||
214 | index.line_col(11.into()), | ||
215 | LineCol { | ||
216 | line: 1, | ||
217 | col_utf16: 5 | ||
218 | } | ||
219 | ); | ||
220 | assert_eq!( | ||
221 | index.line_col(12.into()), | ||
222 | LineCol { | ||
223 | line: 1, | ||
224 | col_utf16: 6 | ||
225 | } | ||
226 | ); | ||
227 | |||
228 | let text = "\nhello\nworld"; | ||
229 | let index = LineIndex::new(text); | ||
230 | assert_eq!( | ||
231 | index.line_col(0.into()), | ||
232 | LineCol { | ||
233 | line: 0, | ||
234 | col_utf16: 0 | ||
235 | } | ||
236 | ); | ||
237 | assert_eq!( | ||
238 | index.line_col(1.into()), | ||
239 | LineCol { | ||
240 | line: 1, | ||
241 | col_utf16: 0 | ||
242 | } | ||
243 | ); | ||
244 | assert_eq!( | ||
245 | index.line_col(2.into()), | ||
246 | LineCol { | ||
247 | line: 1, | ||
248 | col_utf16: 1 | ||
249 | } | ||
250 | ); | ||
251 | assert_eq!( | ||
252 | index.line_col(6.into()), | ||
253 | LineCol { | ||
254 | line: 1, | ||
255 | col_utf16: 5 | ||
256 | } | ||
257 | ); | ||
258 | assert_eq!( | ||
259 | index.line_col(7.into()), | ||
260 | LineCol { | ||
261 | line: 2, | ||
262 | col_utf16: 0 | ||
263 | } | ||
264 | ); | ||
265 | } | ||
266 | |||
267 | fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> { | ||
268 | arb_text() | ||
269 | .prop_flat_map(|text| (arb_offset(&text), Just(text))) | ||
270 | .boxed() | ||
271 | } | ||
272 | |||
273 | proptest! { | ||
274 | #[test] | ||
275 | fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { | ||
276 | let expected = to_line_col(&text, offset); | ||
277 | let line_index = LineIndex::new(&text); | ||
278 | let actual = line_index.line_col(offset); | ||
279 | |||
280 | assert_eq!(actual, expected); | ||
256 | } | 281 | } |
257 | ); | 282 | } |
258 | } | 283 | } |
259 | 284 | ||
260 | #[cfg(test)] | 285 | #[cfg(test)] |
@@ -349,4 +374,5 @@ const C: char = \"メ メ\"; | |||
349 | 374 | ||
350 | assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); | 375 | assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); |
351 | } | 376 | } |
377 | |||
352 | } | 378 | } |
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( | |||
325 | res.to_line_col(offset) | 325 | res.to_line_col(offset) |
326 | } | 326 | } |
327 | 327 | ||
328 | // for bench | 328 | /// Simplest implementation to use as reference in proptest and benchmarks |
329 | pub fn translate_after_edit( | 329 | pub fn translate_after_edit( |
330 | pre_edit_text: &str, | 330 | pre_edit_text: &str, |
331 | offset: TextUnit, | 331 | offset: TextUnit, |
@@ -352,8 +352,8 @@ fn edit_text(pre_edit_text: &str, mut edits: Vec<AtomTextEdit>) -> String { | |||
352 | 352 | ||
353 | #[cfg(test)] | 353 | #[cfg(test)] |
354 | mod test { | 354 | mod test { |
355 | use proptest::{prelude::*, proptest, proptest_helper}; | ||
356 | use super::*; | 355 | use super::*; |
356 | use proptest::{prelude::*, proptest, proptest_helper}; | ||
357 | use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; | 357 | use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits}; |
358 | 358 | ||
359 | #[derive(Debug)] | 359 | #[derive(Debug)] |