Skip to content

Commit 052fe49

Browse files
Merge #7967
7967: Use expect-test for builtin macro/derive tests r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents 6c32e2d + bc4ecb1 commit 052fe49

File tree

4 files changed

+66
-80
lines changed

4 files changed

+66
-80
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir_expand/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ mbe = { path = "../mbe", version = "0.0.0" }
2525

2626
[dev-dependencies]
2727
test_utils = { path = "../test_utils" }
28+
expect-test = "1.1"

crates/hir_expand/src/builtin_derive.rs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,14 @@ fn partial_ord_expand(
267267
#[cfg(test)]
268268
mod tests {
269269
use base_db::{fixture::WithFixture, CrateId, SourceDatabase};
270+
use expect_test::{expect, Expect};
270271
use name::{known, Name};
271272

272273
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
273274

274275
use super::*;
275276

276-
fn expand_builtin_derive(s: &str, name: Name) -> String {
277+
fn expand_builtin_derive(ra_fixture: &str, name: Name) -> String {
277278
let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
278279
let fixture = format!(
279280
r#"//- /main.rs crate:main deps:core
@@ -282,7 +283,7 @@ $0
282283
//- /lib.rs crate:core
283284
// empty
284285
"#,
285-
s
286+
ra_fixture
286287
);
287288

288289
let (db, file_pos) = TestDB::with_position(&fixture);
@@ -314,66 +315,57 @@ $0
314315
parsed.text().to_string()
315316
}
316317

318+
fn check_derive(ra_fixture: &str, name: Name, expected: Expect) {
319+
let expanded = expand_builtin_derive(ra_fixture, name);
320+
expected.assert_eq(&expanded);
321+
}
322+
317323
#[test]
318324
fn test_copy_expand_simple() {
319-
let expanded = expand_builtin_derive(
325+
check_derive(
320326
r#"
321-
#[derive(Copy)]
322-
struct Foo;
323-
"#,
327+
#[derive(Copy)]
328+
struct Foo;
329+
"#,
324330
known::Copy,
331+
expect![["impl< >core::marker::CopyforFoo< >{}"]],
325332
);
326-
327-
assert_eq!(expanded, "impl< >core::marker::CopyforFoo< >{}");
328333
}
329334

330335
#[test]
331336
fn test_copy_expand_with_type_params() {
332-
let expanded = expand_builtin_derive(
337+
check_derive(
333338
r#"
334-
#[derive(Copy)]
335-
struct Foo<A, B>;
336-
"#,
339+
#[derive(Copy)]
340+
struct Foo<A, B>;
341+
"#,
337342
known::Copy,
338-
);
339-
340-
assert_eq!(
341-
expanded,
342-
"impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
343+
expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
343344
);
344345
}
345346

346347
#[test]
347348
fn test_copy_expand_with_lifetimes() {
348-
let expanded = expand_builtin_derive(
349+
check_derive(
349350
r#"
350-
#[derive(Copy)]
351-
struct Foo<A, B, 'a, 'b>;
352-
"#,
351+
#[derive(Copy)]
352+
struct Foo<A, B, 'a, 'b>;
353+
"#,
353354
known::Copy,
354-
);
355-
356-
// We currently just ignore lifetimes
357-
358-
assert_eq!(
359-
expanded,
360-
"impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"
355+
// We currently just ignore lifetimes
356+
expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
361357
);
362358
}
363359

364360
#[test]
365361
fn test_clone_expand() {
366-
let expanded = expand_builtin_derive(
362+
check_derive(
367363
r#"
368-
#[derive(Clone)]
369-
struct Foo<A, B>;
370-
"#,
364+
#[derive(Clone)]
365+
struct Foo<A, B>;
366+
"#,
371367
known::Clone,
372-
);
373-
374-
assert_eq!(
375-
expanded,
376-
"impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"
368+
expect![["impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"]],
377369
);
378370
}
379371
}

crates/hir_expand/src/builtin_macro.rs

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ mod tests {
491491
MacroCallLoc,
492492
};
493493
use base_db::{fixture::WithFixture, SourceDatabase};
494+
use expect_test::{expect, Expect};
494495
use std::sync::Arc;
495496
use syntax::ast::NameOwner;
496497

@@ -574,87 +575,86 @@ mod tests {
574575
db.parse_or_expand(file_id).unwrap().to_string()
575576
}
576577

578+
fn check_expansion(ra_fixture: &str, expect: Expect) {
579+
let expansion = expand_builtin_macro(ra_fixture);
580+
expect.assert_eq(&expansion);
581+
}
582+
577583
#[test]
578584
fn test_column_expand() {
579-
let expanded = expand_builtin_macro(
585+
check_expansion(
580586
r#"
581587
#[rustc_builtin_macro]
582588
macro_rules! column {() => {}}
583589
column!()
584590
"#,
591+
expect![["0"]],
585592
);
586-
587-
assert_eq!(expanded, "0");
588593
}
589594

590595
#[test]
591596
fn test_line_expand() {
592-
let expanded = expand_builtin_macro(
597+
check_expansion(
593598
r#"
594599
#[rustc_builtin_macro]
595600
macro_rules! line {() => {}}
596601
line!()
597602
"#,
603+
expect![["0"]],
598604
);
599-
600-
assert_eq!(expanded, "0");
601605
}
602606

603607
#[test]
604608
fn test_stringify_expand() {
605-
let expanded = expand_builtin_macro(
609+
check_expansion(
606610
r#"
607611
#[rustc_builtin_macro]
608612
macro_rules! stringify {() => {}}
609613
stringify!(a b c)
610614
"#,
615+
expect![["\"a b c\""]],
611616
);
612-
613-
assert_eq!(expanded, "\"a b c\"");
614617
}
615618

616619
#[test]
617620
fn test_env_expand() {
618-
let expanded = expand_builtin_macro(
621+
check_expansion(
619622
r#"
620623
#[rustc_builtin_macro]
621624
macro_rules! env {() => {}}
622625
env!("TEST_ENV_VAR")
623626
"#,
627+
expect![["\"__RA_UNIMPLEMENTED__\""]],
624628
);
625-
626-
assert_eq!(expanded, "\"__RA_UNIMPLEMENTED__\"");
627629
}
628630

629631
#[test]
630632
fn test_option_env_expand() {
631-
let expanded = expand_builtin_macro(
633+
check_expansion(
632634
r#"
633635
#[rustc_builtin_macro]
634636
macro_rules! option_env {() => {}}
635637
option_env!("TEST_ENV_VAR")
636638
"#,
639+
expect![["std::option::Option::None:: < &str>"]],
637640
);
638-
639-
assert_eq!(expanded, "std::option::Option::None:: < &str>");
640641
}
641642

642643
#[test]
643644
fn test_file_expand() {
644-
let expanded = expand_builtin_macro(
645+
check_expansion(
645646
r#"
646647
#[rustc_builtin_macro]
647648
macro_rules! file {() => {}}
648649
file!()
649650
"#,
651+
expect![[r#""""#]],
650652
);
651-
652-
assert_eq!(expanded, "\"\"");
653653
}
654654

655655
#[test]
656656
fn test_assert_expand() {
657-
let expanded = expand_builtin_macro(
657+
check_expansion(
658658
r#"
659659
#[rustc_builtin_macro]
660660
macro_rules! assert {
@@ -663,14 +663,13 @@ mod tests {
663663
}
664664
assert!(true, "{} {:?}", arg1(a, b, c), arg2);
665665
"#,
666+
expect![["{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}"]],
666667
);
667-
668-
assert_eq!(expanded, "{{(&(true), &(\"{} {:?}\"), &(arg1(a,b,c)), &(arg2),);}}");
669668
}
670669

671670
#[test]
672671
fn test_compile_error_expand() {
673-
let expanded = expand_builtin_macro(
672+
check_expansion(
674673
r#"
675674
#[rustc_builtin_macro]
676675
macro_rules! compile_error {
@@ -679,15 +678,14 @@ mod tests {
679678
}
680679
compile_error!("error!");
681680
"#,
681+
// This expands to nothing (since it's in item position), but emits an error.
682+
expect![[""]],
682683
);
683-
684-
// This expands to nothing (since it's in item position), but emits an error.
685-
assert_eq!(expanded, "");
686684
}
687685

688686
#[test]
689687
fn test_format_args_expand() {
690-
let expanded = expand_builtin_macro(
688+
check_expansion(
691689
r#"
692690
#[rustc_builtin_macro]
693691
macro_rules! format_args {
@@ -696,17 +694,15 @@ mod tests {
696694
}
697695
format_args!("{} {:?}", arg1(a, b, c), arg2);
698696
"#,
699-
);
700-
701-
assert_eq!(
702-
expanded,
703-
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),])"#
697+
expect![[
698+
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),])"#
699+
]],
704700
);
705701
}
706702

707703
#[test]
708704
fn test_format_args_expand_with_comma_exprs() {
709-
let expanded = expand_builtin_macro(
705+
check_expansion(
710706
r#"
711707
#[rustc_builtin_macro]
712708
macro_rules! format_args {
@@ -715,17 +711,15 @@ mod tests {
715711
}
716712
format_args!("{} {:?}", a::<A,B>(), b);
717713
"#,
718-
);
719-
720-
assert_eq!(
721-
expanded,
722-
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),])"#
714+
expect![[
715+
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),])"#
716+
]],
723717
);
724718
}
725719

726720
#[test]
727721
fn test_include_bytes_expand() {
728-
let expanded = expand_builtin_macro(
722+
check_expansion(
729723
r#"
730724
#[rustc_builtin_macro]
731725
macro_rules! include_bytes {
@@ -734,21 +728,19 @@ mod tests {
734728
}
735729
include_bytes("foo");
736730
"#,
731+
expect![[r#"b"""#]],
737732
);
738-
739-
assert_eq!(expanded, r#"b"""#);
740733
}
741734

742735
#[test]
743736
fn test_concat_expand() {
744-
let expanded = expand_builtin_macro(
737+
check_expansion(
745738
r##"
746739
#[rustc_builtin_macro]
747740
macro_rules! concat {}
748741
concat!("foo", "r", 0, r#"bar"#, false);
749742
"##,
743+
expect![[r#""foor0barfalse""#]],
750744
);
751-
752-
assert_eq!(expanded, r#""foor0barfalse""#);
753745
}
754746
}

0 commit comments

Comments
 (0)