Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit c042285

Browse files
committed
Add unit tests for Rustfmt::calc_text_edits
1 parent bb76ba0 commit c042285

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

rls/src/actions/format.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,43 @@ fn rustfmt_args(config: &Config, config_path: &Path) -> Vec<String> {
193193

194194
args
195195
}
196+
197+
#[cfg(test)]
198+
mod tests {
199+
use super::*;
200+
use crate::config::FmtConfig;
201+
use lsp_types::{Position, Range, TextEdit};
202+
203+
#[test]
204+
fn calc_text_edits() {
205+
let config = || FmtConfig::default().get_rustfmt_config().clone();
206+
let format = |x: &str| Rustfmt::Internal.calc_text_edits(x.to_string(), config()).unwrap();
207+
let line_range = |start, end| Range {
208+
start: Position { line: start, character: 0 },
209+
end: Position { line: end, character: u64::max_value() },
210+
};
211+
// Handle single-line text wrt. added/removed trailing newline
212+
assert_eq!(
213+
format("fn main() {} "),
214+
vec![TextEdit { range: line_range(0, 0), new_text: "fn main() {}\n".to_owned() }]
215+
);
216+
217+
assert_eq!(
218+
format("fn main() {} \n"),
219+
vec![TextEdit { range: line_range(0, 0), new_text: "fn main() {}".to_owned() }]
220+
);
221+
222+
assert_eq!(
223+
format("\nfn main() {} \n"),
224+
vec![TextEdit { range: line_range(0, 1), new_text: "fn main() {}".to_owned() }]
225+
);
226+
// Check that we send two separate edits
227+
assert_eq!(
228+
format(" struct Upper ;\n\nstruct Lower ;"),
229+
vec![
230+
TextEdit { range: line_range(0, 0), new_text: "struct Upper;".to_owned() },
231+
TextEdit { range: line_range(2, 2), new_text: "struct Lower;\n".to_owned() }
232+
]
233+
);
234+
}
235+
}

0 commit comments

Comments
 (0)