rust/crates/ra_assists/src/doc_tests.rs

40 lines
1.3 KiB
Rust
Raw Normal View History

2019-10-25 06:16:46 -05:00
//! Each assist definition has a special comment, which specifies docs and
//! example.
//!
//! We collect all the example and write the as tests in this module.
mod generated;
use ra_db::{fixture::WithFixture, FileRange};
2019-10-26 11:58:18 -05:00
use test_utils::{assert_eq_text, extract_range_or_offset};
2019-10-25 06:16:46 -05:00
use ra_ide_db::RootDatabase;
2019-11-04 13:28:47 -06:00
2019-10-25 06:16:46 -05:00
fn check(assist_id: &str, before: &str, after: &str) {
// FIXME we cannot get the imports search functionality here yet, but still need to generate a test and a doc for an assist
if assist_id == "auto_import" {
return;
}
2019-10-26 11:58:18 -05:00
let (selection, before) = extract_range_or_offset(before);
let (db, file_id) = RootDatabase::with_single_file(&before);
2019-10-26 11:58:18 -05:00
let frange = FileRange { file_id, range: selection.into() };
2019-10-25 06:16:46 -05:00
2020-01-11 16:40:36 -06:00
let assist = crate::assists(&db, frange)
2019-10-25 15:38:15 -05:00
.into_iter()
2020-01-11 16:40:36 -06:00
.find(|assist| assist.label.id.0 == assist_id)
.unwrap_or_else(|| {
panic!(
"\n\nAssist is not applicable: {}\nAvailable assists: {}",
assist_id,
crate::assists(&db, frange)
.into_iter()
2020-01-11 16:40:36 -06:00
.map(|assist| assist.label.id.0)
.collect::<Vec<_>>()
.join(", ")
)
});
2019-10-25 06:16:46 -05:00
2020-01-11 16:40:36 -06:00
let actual = assist.get_first_action().edit.apply(&before);
2019-10-25 06:16:46 -05:00
assert_eq_text!(after, &actual);
}