Fix tests

This commit is contained in:
Aleksey Kladov 2020-02-06 17:42:17 +01:00
parent ad204f7562
commit d1e8b8d134
2 changed files with 24 additions and 13 deletions

View File

@ -168,16 +168,27 @@ pub(crate) fn all() -> &'static [fn(AssistCtx) -> Option<Assist>] {
#[cfg(test)]
mod helpers {
use ra_db::{fixture::WithFixture, FileRange};
use ra_ide_db::RootDatabase;
use std::sync::Arc;
use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase};
use ra_syntax::TextRange;
use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
use crate::{Assist, AssistCtx};
pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
let (mut db, file_id) = RootDatabase::with_single_file(text);
// FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`,
// but it looks like this might need specialization? :(
let local_roots = vec![db.file_source_root(file_id)];
db.set_local_roots(Arc::new(local_roots));
(db, file_id)
}
pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, file_id) = RootDatabase::with_single_file(&before);
let (db, file_id) = with_single_file(&before);
let frange =
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
let assist =
@ -205,7 +216,7 @@ pub(crate) fn check_assist_range(
after: &str,
) {
let (range, before) = extract_range(before);
let (db, file_id) = RootDatabase::with_single_file(&before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range };
let assist =
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@ -227,7 +238,7 @@ pub(crate) fn check_assist_target(
target: &str,
) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, file_id) = RootDatabase::with_single_file(&before);
let (db, file_id) = with_single_file(&before);
let frange =
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
let assist =
@ -247,7 +258,7 @@ pub(crate) fn check_assist_range_target(
target: &str,
) {
let (range, before) = extract_range(before);
let (db, file_id) = RootDatabase::with_single_file(&before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range };
let assist =
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@ -265,7 +276,7 @@ pub(crate) fn check_assist_not_applicable(
before: &str,
) {
let (before_cursor_pos, before) = extract_offset(before);
let (db, file_id) = RootDatabase::with_single_file(&before);
let (db, file_id) = with_single_file(&before);
let frange =
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
let assist = AssistCtx::with_ctx(&db, frange, true, assist);
@ -277,7 +288,7 @@ pub(crate) fn check_assist_range_not_applicable(
before: &str,
) {
let (range, before) = extract_range(before);
let (db, file_id) = RootDatabase::with_single_file(&before);
let (db, file_id) = with_single_file(&before);
let frange = FileRange { file_id, range };
let assist = AssistCtx::with_ctx(&db, frange, true, assist);
assert!(assist.is_none());
@ -286,17 +297,17 @@ pub(crate) fn check_assist_range_not_applicable(
#[cfg(test)]
mod tests {
use ra_db::{fixture::WithFixture, FileRange};
use ra_db::FileRange;
use ra_syntax::TextRange;
use test_utils::{extract_offset, extract_range};
use ra_ide_db::RootDatabase;
use crate::helpers;
#[test]
fn assist_order_field_struct() {
let before = "struct Foo { <|>bar: u32 }";
let (before_cursor_pos, before) = extract_offset(before);
let (db, file_id) = RootDatabase::with_single_file(&before);
let (db, file_id) = helpers::with_single_file(&before);
let frange =
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
let assists = super::assists(&db, frange);
@ -320,7 +331,7 @@ pub fn test_some_range(a: int) -> bool {
}
}";
let (range, before) = extract_range(before);
let (db, file_id) = RootDatabase::with_single_file(&before);
let (db, file_id) = helpers::with_single_file(&before);
let frange = FileRange { file_id, range };
let assists = super::assists(&db, frange);
let mut assists = assists.iter();

View File

@ -92,7 +92,7 @@ pub fn classify_name(
ast::FnDef(it) => {
let src = name.with_value(it);
let def: hir::Function = sb.to_def(src)?;
if parent.parent().and_then(ast::ItemList::cast).is_some() {
if parent.parent().and_then(ast::ItemList::cast).map_or(false, |it| it.syntax().parent().and_then(ast::Module::cast).is_none()) {
Some(from_assoc_item(sb.db, def.into()))
} else {
Some(from_module_def(sb.db, def.into(), None))