mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 07:04:22 +00:00
test translate_offset_with_edit against simple impl for single edits
This commit is contained in:
parent
dee426e1b1
commit
d951979159
6 changed files with 104 additions and 35 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -763,6 +763,7 @@ dependencies = [
|
|||
"languageserver-types 0.53.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"parking_lot 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proptest 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"ra_analysis 0.1.0",
|
||||
"ra_editor 0.1.0",
|
||||
"ra_syntax 0.1.0",
|
||||
|
|
|
@ -34,6 +34,8 @@ ra_analysis = { path = "../ra_analysis" }
|
|||
gen_lsp_server = { path = "../gen_lsp_server" }
|
||||
ra_vfs = { path = "../ra_vfs" }
|
||||
|
||||
proptest = "0.8.7"
|
||||
|
||||
[dev-dependencies]
|
||||
tempdir = "0.3.7"
|
||||
test_utils = { path = "../test_utils" }
|
||||
test_utils = { path = "../test_utils" }
|
|
@ -386,3 +386,64 @@ where
|
|||
self.iter.next().map(|item| item.conv_with(self.ctx))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use proptest::{prelude::*, proptest, proptest_helper};
|
||||
use super::*;
|
||||
use ra_text_edit::test_utils::{arb_text, arb_offset, arb_edits};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct ArbTextWithOffsetAndEdits {
|
||||
text: String,
|
||||
offset: TextUnit,
|
||||
edits: Vec<AtomTextEdit>,
|
||||
}
|
||||
|
||||
fn arb_text_with_offset_and_edits() -> BoxedStrategy<ArbTextWithOffsetAndEdits> {
|
||||
arb_text()
|
||||
.prop_flat_map(|text| {
|
||||
(arb_offset(&text), arb_edits(&text), Just(text)).prop_map(
|
||||
|(offset, edits, text)| ArbTextWithOffsetAndEdits {
|
||||
text,
|
||||
offset,
|
||||
edits,
|
||||
},
|
||||
)
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
|
||||
fn translate_offset_with_edit_naive(
|
||||
pre_edit_text: String,
|
||||
offset: TextUnit,
|
||||
edits: &[AtomTextEdit],
|
||||
) -> LineCol {
|
||||
// apply edits ordered from last to first
|
||||
// since they should not overlap we can just use start()
|
||||
let mut edits: Vec<AtomTextEdit> = edits.to_vec();
|
||||
edits.sort_by_key(|x| -(x.delete.start().to_usize() as isize));
|
||||
|
||||
let mut text = pre_edit_text;
|
||||
|
||||
for edit in &edits {
|
||||
let range = edit.delete.start().to_usize()..edit.delete.end().to_usize();
|
||||
text.replace_range(range, &edit.insert);
|
||||
}
|
||||
|
||||
let line_index = LineIndex::new(&text);
|
||||
|
||||
line_index.line_col(offset)
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn test_translate_offset_with_edit(x in arb_text_with_offset_and_edits()) {
|
||||
if x.edits.len() <= 1 {
|
||||
let expected = translate_offset_with_edit_naive(x.text.clone(), x.offset, &x.edits);
|
||||
let actual = translate_offset_with_edit(&LineIndex::new(&x.text), x.offset, &x.edits);
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ publish = false
|
|||
|
||||
[dependencies]
|
||||
text_unit = "0.1.5"
|
||||
proptest = "0.8.7"
|
||||
|
||||
[dev-dependencies]
|
||||
test_utils = { path = "../test_utils" }
|
||||
proptest = "0.8.7"
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
mod text_edit;
|
||||
pub mod text_utils;
|
||||
#[cfg(test)]
|
||||
pub mod test_utils;
|
||||
|
||||
pub use crate::text_edit::{TextEdit, TextEditBuilder};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use proptest::{prelude::*, proptest, proptest_helper};
|
||||
use proptest::prelude::*;
|
||||
use text_unit::{TextUnit, TextRange};
|
||||
use crate::AtomTextEdit;
|
||||
|
||||
|
@ -66,42 +66,48 @@ pub fn arb_edits(text: &str) -> BoxedStrategy<Vec<AtomTextEdit>> {
|
|||
.boxed()
|
||||
}
|
||||
|
||||
fn arb_text_with_edits() -> BoxedStrategy<(String, Vec<AtomTextEdit>)> {
|
||||
let text = arb_text();
|
||||
text.prop_flat_map(|s| {
|
||||
let edits = arb_edits(&s);
|
||||
(Just(s), edits)
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use proptest::{proptest, proptest_helper};
|
||||
|
||||
fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> {
|
||||
let start = r1.start().max(r2.start());
|
||||
let end = r1.end().min(r2.end());
|
||||
if start <= end {
|
||||
Some(TextRange::from_to(start, end))
|
||||
} else {
|
||||
None
|
||||
fn arb_text_with_edits() -> BoxedStrategy<(String, Vec<AtomTextEdit>)> {
|
||||
let text = arb_text();
|
||||
text.prop_flat_map(|s| {
|
||||
let edits = arb_edits(&s);
|
||||
(Just(s), edits)
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) {
|
||||
proptest_atom_text_edits_are_valid(text, edits)
|
||||
fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> {
|
||||
let start = r1.start().max(r2.start());
|
||||
let end = r1.end().min(r2.end());
|
||||
if start <= end {
|
||||
Some(TextRange::from_to(start, end))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
proptest! {
|
||||
#[test]
|
||||
fn atom_text_edits_are_valid((text, edits) in arb_text_with_edits()) {
|
||||
proptest_atom_text_edits_are_valid(text, edits)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn proptest_atom_text_edits_are_valid(text: String, edits: Vec<AtomTextEdit>) {
|
||||
// slicing doesn't panic
|
||||
for e in &edits {
|
||||
let _ = &text[e.delete];
|
||||
}
|
||||
// ranges do not overlap
|
||||
for (i1, e1) in edits.iter().skip(1).enumerate() {
|
||||
for e2 in &edits[0..i1] {
|
||||
if intersect(e1.delete, e2.delete).is_some() {
|
||||
assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete);
|
||||
fn proptest_atom_text_edits_are_valid(text: String, edits: Vec<AtomTextEdit>) {
|
||||
// slicing doesn't panic
|
||||
for e in &edits {
|
||||
let _ = &text[e.delete];
|
||||
}
|
||||
// ranges do not overlap
|
||||
for i in 1..edits.len() {
|
||||
let e1 = &edits[i];
|
||||
for e2 in &edits[0..i] {
|
||||
if intersect(e1.delete, e2.delete).is_some() {
|
||||
assert!(false, "Overlapping ranges {} {}", e1.delete, e2.delete);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue