Skip to content

Commit d938414

Browse files
committed
optimize snapshot-testing macro detection and add tests
1 parent c8f29aa commit d938414

2 files changed

Lines changed: 137 additions & 45 deletions

File tree

‎src/tools/rust-analyzer/crates/ide/src/hover/tests.rs‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9926,3 +9926,99 @@ fn bar() {
99269926
"#]],
99279927
);
99289928
}
9929+
9930+
#[test]
9931+
fntest_runnables_with_snapshot_tests(){
9932+
check_actions(
9933+
r#"
9934+
//- /lib.rs crate:foo deps:expect_test,insta,snapbox
9935+
use expect_test::expect;
9936+
use insta::assert_debug_snapshot;
9937+
use snapbox::Assert;
9938+
9939+
#[test]
9940+
fn test$0() {
9941+
let actual = "new25";
9942+
expect!["new25"].assert_eq(&actual);
9943+
Assert::new()
9944+
.action_env("SNAPSHOTS")
9945+
.eq(actual, snapbox::str!["new25"]);
9946+
assert_debug_snapshot!(actual);
9947+
}
9948+
9949+
//- /lib.rs crate:expect_test
9950+
struct Expect;
9951+
9952+
impl Expect {
9953+
fn assert_eq(&self, actual: &str) {}
9954+
}
9955+
9956+
#[macro_export]
9957+
macro_rules! expect {
9958+
($e:expr) => Expect; // dummy
9959+
}
9960+
9961+
//- /lib.rs crate:insta
9962+
#[macro_export]
9963+
macro_rules! assert_debug_snapshot {
9964+
($e:expr) => {}; // dummy
9965+
}
9966+
9967+
//- /lib.rs crate:snapbox
9968+
pub struct Assert;
9969+
9970+
impl Assert {
9971+
pub fn new() -> Self { Assert }
9972+
9973+
pub fn action_env(&self, env: &str) -> &Self { self }
9974+
9975+
pub fn eq(&self, actual: &str, expected: &str) {}
9976+
}
9977+
9978+
#[macro_export]
9979+
macro_rules! str {
9980+
($e:expr) => ""; // dummy
9981+
}
9982+
"#,
9983+
expect![[r#"
9984+
[
9985+
Reference(
9986+
FilePositionWrapper {
9987+
file_id: FileId(
9988+
0,
9989+
),
9990+
offset: 92,
9991+
},
9992+
),
9993+
Runnable(
9994+
Runnable {
9995+
use_name_in_title: false,
9996+
nav: NavigationTarget {
9997+
file_id: FileId(
9998+
0,
9999+
),
10000+
full_range: 81..301,
10001+
focus_range: 92..96,
10002+
name: "test",
10003+
kind: Function,
10004+
},
10005+
kind: Test {
10006+
test_id: Path(
10007+
"test",
10008+
),
10009+
attr: TestAttr {
10010+
ignore: false,
10011+
},
10012+
},
10013+
cfg: None,
10014+
update_test: UpdateTest {
10015+
expect_test: true,
10016+
insta: true,
10017+
snapbox: true,
10018+
},
10019+
},
10020+
),
10021+
]
10022+
"#]],
10023+
);
10024+
}

‎src/tools/rust-analyzer/crates/ide/src/runnables.rs‎

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -657,62 +657,58 @@ impl<'a, 'b> TestDefs<'a, 'b> {
657657
}
658658

659659
fnexpect_test(&self) -> bool{
660-
self.find_macro("expect_test:expect") || self.find_macro("expect_test::expect_file")
660+
self.find_macro("expect_test",&["expect","expect_file"])
661661
}
662662

663663
fninsta(&self) -> bool{
664-
self.find_macro("insta:assert_snapshot")
665-
|| self.find_macro("insta:assert_debug_snapshot")
666-
|| self.find_macro("insta:assert_display_snapshot")
667-
|| self.find_macro("insta:assert_json_snapshot")
668-
|| self.find_macro("insta:assert_yaml_snapshot")
669-
|| self.find_macro("insta:assert_ron_snapshot")
670-
|| self.find_macro("insta:assert_toml_snapshot")
671-
|| self.find_macro("insta:assert_csv_snapshot")
672-
|| self.find_macro("insta:assert_compact_json_snapshot")
673-
|| self.find_macro("insta:assert_compact_debug_snapshot")
674-
|| self.find_macro("insta:assert_binary_snapshot")
664+
self.find_macro(
665+
"insta",
666+
&[
667+
"assert_snapshot",
668+
"assert_debug_snapshot",
669+
"assert_display_snapshot",
670+
"assert_json_snapshot",
671+
"assert_yaml_snapshot",
672+
"assert_ron_snapshot",
673+
"assert_toml_snapshot",
674+
"assert_csv_snapshot",
675+
"assert_compact_json_snapshot",
676+
"assert_compact_debug_snapshot",
677+
"assert_binary_snapshot",
678+
],
679+
)
675680
}
676681

677682
fnsnapbox(&self) -> bool{
678-
self.find_macro("snapbox:assert_data_eq")
679-
|| self.find_macro("snapbox:file")
680-
|| self.find_macro("snapbox:str")
683+
self.find_macro("snapbox",&["assert_data_eq","file","str"])
681684
}
682685

683-
fnfind_macro(&self,path:&str) -> bool{
684-
letSome(hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(it))) = self.find_def(path)else{
685-
returnfalse;
686-
};
687-
688-
Definition::Macro(it)
689-
.usages(self.0)
690-
.in_scope(&SearchScope::file_range(self.2))
691-
.at_least_one()
692-
}
693-
694-
fnfind_def(&self,path:&str) -> Option<hir::ScopeDef>{
686+
fnfind_macro(&self,crate_name:&str,paths:&[&str]) -> bool{
695687
let db = self.0.db;
696688

697-
letmut path = path.split(':');
698-
let item = path.next_back()?;
699-
let krate = path.next()?;
700-
let dep = self.1.dependencies(db).into_iter().find(|dep| dep.name.eq_ident(krate))?;
701-
702-
letmut module = dep.krate.root_module();
703-
for segment in path {
704-
module = module.children(db).find_map(|child| {
705-
let name = child.name(db)?;
706-
if name.eq_ident(segment){
707-
Some(child)
708-
}else{
709-
None
689+
letSome(dep) =
690+
self.1.dependencies(db).into_iter().find(|dep| dep.name.eq_ident(crate_name))
691+
else{
692+
returnfalse;
693+
};
694+
let module = dep.krate.root_module();
695+
let scope = module.scope(db,None);
696+
697+
paths
698+
.iter()
699+
.filter_map(|path| {
700+
let(_, def) = scope.iter().find(|(name, _)| name.eq_ident(path))?;
701+
match def {
702+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(it)) => Some(it),
703+
_ => None,
710704
}
711-
})?;
712-
}
713-
714-
let(_, def) = module.scope(db,None).into_iter().find(|(name, _)| name.eq_ident(item))?;
715-
Some(def)
705+
})
706+
.any(|makro| {
707+
Definition::Macro(*makro)
708+
.usages(self.0)
709+
.in_scope(&SearchScope::file_range(self.2))
710+
.at_least_one()
711+
})
716712
}
717713
}
718714

0 commit comments

Comments
 (0)