Use expect-test for builtin macro/derive tests

This commit is contained in:
Jonas Schievink 2021-03-10 21:05:02 +01:00
parent 6c32e2d8a0
commit bc4ecb199b
4 changed files with 66 additions and 80 deletions

1
Cargo.lock generated
View file

@ -526,6 +526,7 @@ dependencies = [
"base_db", "base_db",
"cfg", "cfg",
"either", "either",
"expect-test",
"la-arena", "la-arena",
"log", "log",
"mbe", "mbe",

View file

@ -25,3 +25,4 @@ mbe = { path = "../mbe", version = "0.0.0" }
[dev-dependencies] [dev-dependencies]
test_utils = { path = "../test_utils" } test_utils = { path = "../test_utils" }
expect-test = "1.1"

View file

@ -267,13 +267,14 @@ fn partial_ord_expand(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use base_db::{fixture::WithFixture, CrateId, SourceDatabase}; use base_db::{fixture::WithFixture, CrateId, SourceDatabase};
use expect_test::{expect, Expect};
use name::{known, Name}; use name::{known, Name};
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc}; use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
use super::*; use super::*;
fn expand_builtin_derive(s: &str, name: Name) -> String { fn expand_builtin_derive(ra_fixture: &str, name: Name) -> String {
let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap(); let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
let fixture = format!( let fixture = format!(
r#"//- /main.rs crate:main deps:core r#"//- /main.rs crate:main deps:core
@ -282,7 +283,7 @@ $0
//- /lib.rs crate:core //- /lib.rs crate:core
// empty // empty
"#, "#,
s ra_fixture
); );
let (db, file_pos) = TestDB::with_position(&fixture); let (db, file_pos) = TestDB::with_position(&fixture);
@ -314,66 +315,57 @@ $0
parsed.text().to_string() parsed.text().to_string()
} }
fn check_derive(ra_fixture: &str, name: Name, expected: Expect) {
let expanded = expand_builtin_derive(ra_fixture, name);
expected.assert_eq(&expanded);
}
#[test] #[test]
fn test_copy_expand_simple() { fn test_copy_expand_simple() {
let expanded = expand_builtin_derive( check_derive(
r#" r#"
#[derive(Copy)] #[derive(Copy)]
struct Foo; struct Foo;
"#, "#,
known::Copy, known::Copy,
expect![["impl< >core::marker::CopyforFoo< >{}"]],
); );
assert_eq!(expanded, "impl< >core::marker::CopyforFoo< >{}");
} }
#[test] #[test]
fn test_copy_expand_with_type_params() { fn test_copy_expand_with_type_params() {
let expanded = expand_builtin_derive( check_derive(
r#" r#"
#[derive(Copy)] #[derive(Copy)]
struct Foo<A, B>; struct Foo<A, B>;
"#, "#,
known::Copy, known::Copy,
); expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
assert_eq!(
expanded,
"impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
); );
} }
#[test] #[test]
fn test_copy_expand_with_lifetimes() { fn test_copy_expand_with_lifetimes() {
let expanded = expand_builtin_derive( check_derive(
r#" r#"
#[derive(Copy)] #[derive(Copy)]
struct Foo<A, B, 'a, 'b>; struct Foo<A, B, 'a, 'b>;
"#, "#,
known::Copy, known::Copy,
); // We currently just ignore lifetimes
expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
// We currently just ignore lifetimes
assert_eq!(
expanded,
"impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
); );
} }
#[test] #[test]
fn test_clone_expand() { fn test_clone_expand() {
let expanded = expand_builtin_derive( check_derive(
r#" r#"
#[derive(Clone)] #[derive(Clone)]
struct Foo<A, B>; struct Foo<A, B>;
"#, "#,
known::Clone, known::Clone,
); expect![["impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"]],
assert_eq!(
expanded,
"impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"
); );
} }
} }

View file

@ -491,6 +491,7 @@ mod tests {
MacroCallLoc, MacroCallLoc,
}; };
use base_db::{fixture::WithFixture, SourceDatabase}; use base_db::{fixture::WithFixture, SourceDatabase};
use expect_test::{expect, Expect};
use std::sync::Arc; use std::sync::Arc;
use syntax::ast::NameOwner; use syntax::ast::NameOwner;
@ -574,87 +575,86 @@ mod tests {
db.parse_or_expand(file_id).unwrap().to_string() db.parse_or_expand(file_id).unwrap().to_string()
} }
fn check_expansion(ra_fixture: &str, expect: Expect) {
let expansion = expand_builtin_macro(ra_fixture);
expect.assert_eq(&expansion);
}
#[test] #[test]
fn test_column_expand() { fn test_column_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! column {() => {}} macro_rules! column {() => {}}
column!() column!()
"#, "#,
expect![["0"]],
); );
assert_eq!(expanded, "0");
} }
#[test] #[test]
fn test_line_expand() { fn test_line_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! line {() => {}} macro_rules! line {() => {}}
line!() line!()
"#, "#,
expect![["0"]],
); );
assert_eq!(expanded, "0");
} }
#[test] #[test]
fn test_stringify_expand() { fn test_stringify_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! stringify {() => {}} macro_rules! stringify {() => {}}
stringify!(a b c) stringify!(a b c)
"#, "#,
expect![["\"a b c\""]],
); );
assert_eq!(expanded, "\"a b c\"");
} }
#[test] #[test]
fn test_env_expand() { fn test_env_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! env {() => {}} macro_rules! env {() => {}}
env!("TEST_ENV_VAR") env!("TEST_ENV_VAR")
"#, "#,
expect![["\"__RA_UNIMPLEMENTED__\""]],
); );
assert_eq!(expanded, "\"__RA_UNIMPLEMENTED__\"");
} }
#[test] #[test]
fn test_option_env_expand() { fn test_option_env_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! option_env {() => {}} macro_rules! option_env {() => {}}
option_env!("TEST_ENV_VAR") option_env!("TEST_ENV_VAR")
"#, "#,
expect![["std::option::Option::None:: < &str>"]],
); );
assert_eq!(expanded, "std::option::Option::None:: < &str>");
} }
#[test] #[test]
fn test_file_expand() { fn test_file_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! file {() => {}} macro_rules! file {() => {}}
file!() file!()
"#, "#,
expect![[r#""""#]],
); );
assert_eq!(expanded, "\"\"");
} }
#[test] #[test]
fn test_assert_expand() { fn test_assert_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! assert { macro_rules! assert {
@ -663,14 +663,13 @@ mod tests {
} }
assert!(true, "{} {:?}", arg1(a, b, c), arg2); assert!(true, "{} {:?}", arg1(a, b, c), arg2);
"#, "#,
expect![["{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}"]],
); );
assert_eq!(expanded, "{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}");
} }
#[test] #[test]
fn test_compile_error_expand() { fn test_compile_error_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! compile_error { macro_rules! compile_error {
@ -679,15 +678,14 @@ mod tests {
} }
compile_error!("error!"); compile_error!("error!");
"#, "#,
// This expands to nothing (since it's in item position), but emits an error.
expect![[""]],
); );
// This expands to nothing (since it's in item position), but emits an error.
assert_eq!(expanded, "");
} }
#[test] #[test]
fn test_format_args_expand() { fn test_format_args_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! format_args { macro_rules! format_args {
@ -696,17 +694,15 @@ mod tests {
} }
format_args!("{} {:?}", arg1(a, b, c), arg2); format_args!("{} {:?}", arg1(a, b, c), arg2);
"#, "#,
); expect![[
r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#
assert_eq!( ]],
expanded,
r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#
); );
} }
#[test] #[test]
fn test_format_args_expand_with_comma_exprs() { fn test_format_args_expand_with_comma_exprs() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! format_args { macro_rules! format_args {
@ -715,17 +711,15 @@ mod tests {
} }
format_args!("{} {:?}", a::<A,B>(), b); format_args!("{} {:?}", a::<A,B>(), b);
"#, "#,
); expect![[
r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(a::<A,B>()),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(b),std::fmt::Display::fmt),])"#
assert_eq!( ]],
expanded,
r#"std::fmt::Arguments::new_v1(&[], &[std::fmt::ArgumentV1::new(&(a::<A,B>()),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(b),std::fmt::Display::fmt),])"#
); );
} }
#[test] #[test]
fn test_include_bytes_expand() { fn test_include_bytes_expand() {
let expanded = expand_builtin_macro( check_expansion(
r#" r#"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! include_bytes { macro_rules! include_bytes {
@ -734,21 +728,19 @@ mod tests {
} }
include_bytes("foo"); include_bytes("foo");
"#, "#,
expect![[r#"b"""#]],
); );
assert_eq!(expanded, r#"b"""#);
} }
#[test] #[test]
fn test_concat_expand() { fn test_concat_expand() {
let expanded = expand_builtin_macro( check_expansion(
r##" r##"
#[rustc_builtin_macro] #[rustc_builtin_macro]
macro_rules! concat {} macro_rules! concat {}
concat!("foo", "r", 0, r#"bar"#, false); concat!("foo", "r", 0, r#"bar"#, false);
"##, "##,
expect![[r#""foor0barfalse""#]],
); );
assert_eq!(expanded, r#""foor0barfalse""#);
} }
} }