internal: move derived tests to the unified macro infra

This commit is contained in:
Aleksey Kladov 2021-10-10 16:08:01 +03:00
parent 55d8be5a24
commit 5af80d8372
4 changed files with 98 additions and 127 deletions

View file

@ -258,128 +258,3 @@ fn partial_ord_expand(
let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd })
}
#[cfg(test)]
mod tests {
use base_db::{fixture::WithFixture, CrateId, SourceDatabase};
use expect_test::{expect, Expect};
use name::AsName;
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
use super::*;
fn expand_builtin_derive(ra_fixture: &str) -> String {
let fixture = format!(
r#"//- /main.rs crate:main deps:core
$0
{}
//- /lib.rs crate:core
// empty
"#,
ra_fixture
);
let (db, file_pos) = TestDB::with_position(&fixture);
let file_id = file_pos.file_id;
let ast_id_map = db.ast_id_map(file_id.into());
let parsed = db.parse(file_id);
let macros: Vec<_> =
parsed.syntax_node().descendants().filter_map(ast::Macro::cast).collect();
let items: Vec<_> = parsed
.syntax_node()
.descendants()
.filter(|node| !ast::Macro::can_cast(node.kind()))
.filter_map(ast::Item::cast)
.collect();
assert_eq!(macros.len(), 1, "test must contain exactly 1 macro definition");
assert_eq!(items.len(), 1, "test must contain exactly 1 item");
let macro_ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macros[0]));
let name = match &macros[0] {
ast::Macro::MacroRules(rules) => rules.name().unwrap().as_name(),
ast::Macro::MacroDef(def) => def.name().unwrap().as_name(),
};
let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
let ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]));
let loc = MacroCallLoc {
def: MacroDefId {
krate: CrateId(0),
kind: MacroDefKind::BuiltInDerive(expander, macro_ast_id),
local_inner: false,
},
krate: CrateId(0),
eager: None,
kind: MacroCallKind::Derive {
ast_id,
derive_name: name.to_string(),
derive_attr_index: 0,
},
};
let id: MacroCallId = db.intern_macro(loc);
let parsed = db.parse_or_expand(id.as_file()).unwrap();
// FIXME text() for syntax nodes parsed from token tree looks weird
// because there's no whitespace, see below
parsed.text().to_string()
}
fn check_derive(ra_fixture: &str, expected: Expect) {
let expanded = expand_builtin_derive(ra_fixture);
expected.assert_eq(&expanded);
}
#[test]
fn test_copy_expand_simple() {
check_derive(
r#"
macro Copy {}
#[derive(Copy)]
struct Foo;
"#,
expect![["impl< >core::marker::CopyforFoo< >{}"]],
);
}
#[test]
fn test_copy_expand_with_type_params() {
check_derive(
r#"
macro Copy {}
#[derive(Copy)]
struct Foo<A, B>;
"#,
expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
);
}
#[test]
fn test_copy_expand_with_lifetimes() {
check_derive(
r#"
macro Copy {}
#[derive(Copy)]
struct Foo<A, B, 'a, 'b>;
"#,
// We currently just ignore lifetimes
expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
);
}
#[test]
fn test_clone_expand() {
check_derive(
r#"
macro Clone {}
#[derive(Clone)]
struct Foo<A, B>;
"#,
expect![["impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"]],
);
}
}