Refactor runnable tests

This commit is contained in:
Aleksey Kladov 2020-06-30 14:12:55 +02:00
parent 6a73d544f4
commit de464f5e4a

View file

@ -251,13 +251,21 @@ fn runnable_mod(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use insta::assert_debug_snapshot; use expect::{expect, Expect};
use crate::mock_analysis::analysis_and_position; use crate::mock_analysis::analysis_and_position;
use super::{Runnable, RunnableAction, BENCH, BIN, DOCTEST, TEST}; use super::{RunnableAction, BENCH, BIN, DOCTEST, TEST};
fn assert_actions(runnables: &[Runnable], actions: &[&RunnableAction]) { fn check(
ra_fixture: &str,
// FIXME: fold this into `expect` as well
actions: &[&RunnableAction],
expect: Expect,
) {
let (analysis, position) = analysis_and_position(ra_fixture);
let runnables = analysis.runnables(position.file_id).unwrap();
expect.assert_debug_eq(&runnables);
assert_eq!( assert_eq!(
actions, actions,
runnables.into_iter().map(|it| it.action()).collect::<Vec<_>>().as_slice() runnables.into_iter().map(|it| it.action()).collect::<Vec<_>>().as_slice()
@ -266,10 +274,10 @@ mod tests {
#[test] #[test]
fn test_runnables() { fn test_runnables() {
let (analysis, pos) = analysis_and_position( check(
r#" r#"
//- /lib.rs //- /lib.rs
<|> //empty <|>
fn main() {} fn main() {}
#[test] #[test]
@ -282,21 +290,19 @@ mod tests {
#[bench] #[bench]
fn bench() {} fn bench() {}
"#, "#,
); &[&BIN, &TEST, &TEST, &BENCH],
let runnables = analysis.runnables(pos.file_id).unwrap(); expect![[r#"
assert_debug_snapshot!(&runnables,
@r###"
[ [
Runnable { Runnable {
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 1..21, full_range: 1..13,
name: "main", name: "main",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
12..16, 4..8,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -310,11 +316,11 @@ mod tests {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 23..47, full_range: 15..39,
name: "test_foo", name: "test_foo",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
34..42, 26..34,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -335,11 +341,11 @@ mod tests {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 49..83, full_range: 41..75,
name: "test_foo", name: "test_foo",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
70..78, 62..70,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -360,11 +366,11 @@ mod tests {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 85..107, full_range: 77..99,
name: "bench", name: "bench",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
97..102, 89..94,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -378,17 +384,16 @@ mod tests {
cfg_exprs: [], cfg_exprs: [],
}, },
] ]
"### "#]],
); );
assert_actions(&runnables, &[&BIN, &TEST, &TEST, &BENCH]);
} }
#[test] #[test]
fn test_runnables_doc_test() { fn test_runnables_doc_test() {
let (analysis, pos) = analysis_and_position( check(
r#" r#"
//- /lib.rs //- /lib.rs
<|> //empty <|>
fn main() {} fn main() {}
/// ``` /// ```
@ -396,21 +401,19 @@ mod tests {
/// ``` /// ```
fn foo() {} fn foo() {}
"#, "#,
); &[&BIN, &DOCTEST],
let runnables = analysis.runnables(pos.file_id).unwrap(); expect![[r#"
assert_debug_snapshot!(&runnables,
@r###"
[ [
Runnable { Runnable {
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 1..21, full_range: 1..13,
name: "main", name: "main",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
12..16, 4..8,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -424,7 +427,7 @@ mod tests {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 23..65, full_range: 15..57,
name: "foo", name: "foo",
kind: FN_DEF, kind: FN_DEF,
focus_range: None, focus_range: None,
@ -440,17 +443,16 @@ mod tests {
cfg_exprs: [], cfg_exprs: [],
}, },
] ]
"### "#]],
); );
assert_actions(&runnables, &[&BIN, &DOCTEST]);
} }
#[test] #[test]
fn test_runnables_doc_test_in_impl() { fn test_runnables_doc_test_in_impl() {
let (analysis, pos) = analysis_and_position( check(
r#" r#"
//- /lib.rs //- /lib.rs
<|> //empty <|>
fn main() {} fn main() {}
struct Data; struct Data;
@ -461,21 +463,19 @@ mod tests {
fn foo() {} fn foo() {}
} }
"#, "#,
); &[&BIN, &DOCTEST],
let runnables = analysis.runnables(pos.file_id).unwrap(); expect![[r#"
assert_debug_snapshot!(&runnables,
@r###"
[ [
Runnable { Runnable {
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 1..21, full_range: 1..13,
name: "main", name: "main",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
12..16, 4..8,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -489,7 +489,7 @@ mod tests {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 52..106, full_range: 44..98,
name: "foo", name: "foo",
kind: FN_DEF, kind: FN_DEF,
focus_range: None, focus_range: None,
@ -505,37 +505,34 @@ mod tests {
cfg_exprs: [], cfg_exprs: [],
}, },
] ]
"### "#]],
); );
assert_actions(&runnables, &[&BIN, &DOCTEST]);
} }
#[test] #[test]
fn test_runnables_module() { fn test_runnables_module() {
let (analysis, pos) = analysis_and_position( check(
r#" r#"
//- /lib.rs //- /lib.rs
<|> //empty <|>
mod test_mod { mod test_mod {
#[test] #[test]
fn test_foo1() {} fn test_foo1() {}
} }
"#, "#,
); &[&TEST, &TEST],
let runnables = analysis.runnables(pos.file_id).unwrap(); expect![[r#"
assert_debug_snapshot!(&runnables,
@r###"
[ [
Runnable { Runnable {
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 1..59, full_range: 1..51,
name: "test_mod", name: "test_mod",
kind: MODULE, kind: MODULE,
focus_range: Some( focus_range: Some(
13..21, 5..13,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -551,11 +548,11 @@ mod tests {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 28..57, full_range: 20..49,
name: "test_foo1", name: "test_foo1",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
43..52, 35..44,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -572,17 +569,16 @@ mod tests {
cfg_exprs: [], cfg_exprs: [],
}, },
] ]
"### "#]],
); );
assert_actions(&runnables, &[&TEST, &TEST]);
} }
#[test] #[test]
fn test_runnables_one_depth_layer_module() { fn test_runnables_one_depth_layer_module() {
let (analysis, pos) = analysis_and_position( check(
r#" r#"
//- /lib.rs //- /lib.rs
<|> //empty <|>
mod foo { mod foo {
mod test_mod { mod test_mod {
#[test] #[test]
@ -590,21 +586,19 @@ mod tests {
} }
} }
"#, "#,
); &[&TEST, &TEST],
let runnables = analysis.runnables(pos.file_id).unwrap(); expect![[r#"
assert_debug_snapshot!(&runnables,
@r###"
[ [
Runnable { Runnable {
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 23..85, full_range: 15..77,
name: "test_mod", name: "test_mod",
kind: MODULE, kind: MODULE,
focus_range: Some( focus_range: Some(
27..35, 19..27,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -620,11 +614,11 @@ mod tests {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 46..79, full_range: 38..71,
name: "test_foo1", name: "test_foo1",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
65..74, 57..66,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -641,17 +635,16 @@ mod tests {
cfg_exprs: [], cfg_exprs: [],
}, },
] ]
"### "#]],
); );
assert_actions(&runnables, &[&TEST, &TEST]);
} }
#[test] #[test]
fn test_runnables_multiple_depth_module() { fn test_runnables_multiple_depth_module() {
let (analysis, pos) = analysis_and_position( check(
r#" r#"
//- /lib.rs //- /lib.rs
<|> //empty <|>
mod foo { mod foo {
mod bar { mod bar {
mod test_mod { mod test_mod {
@ -661,21 +654,19 @@ mod tests {
} }
} }
"#, "#,
); &[&TEST, &TEST],
let runnables = analysis.runnables(pos.file_id).unwrap(); expect![[r#"
assert_debug_snapshot!(&runnables,
@r###"
[ [
Runnable { Runnable {
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 41..115, full_range: 33..107,
name: "test_mod", name: "test_mod",
kind: MODULE, kind: MODULE,
focus_range: Some( focus_range: Some(
45..53, 37..45,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -691,11 +682,11 @@ mod tests {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 68..105, full_range: 60..97,
name: "test_foo1", name: "test_foo1",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
91..100, 83..92,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -712,36 +703,33 @@ mod tests {
cfg_exprs: [], cfg_exprs: [],
}, },
] ]
"### "#]],
); );
assert_actions(&runnables, &[&TEST, &TEST]);
} }
#[test] #[test]
fn test_runnables_with_feature() { fn test_runnables_with_feature() {
let (analysis, pos) = analysis_and_position( check(
r#" r#"
//- /lib.rs crate:foo cfg:feature=foo //- /lib.rs crate:foo cfg:feature=foo
<|> //empty <|>
#[test] #[test]
#[cfg(feature = "foo")] #[cfg(feature = "foo")]
fn test_foo1() {} fn test_foo1() {}
"#, "#,
); &[&TEST],
let runnables = analysis.runnables(pos.file_id).unwrap(); expect![[r#"
assert_debug_snapshot!(&runnables,
@r###"
[ [
Runnable { Runnable {
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 1..58, full_range: 1..50,
name: "test_foo1", name: "test_foo1",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
44..53, 36..45,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -763,36 +751,33 @@ mod tests {
], ],
}, },
] ]
"### "#]],
); );
assert_actions(&runnables, &[&TEST]);
} }
#[test] #[test]
fn test_runnables_with_features() { fn test_runnables_with_features() {
let (analysis, pos) = analysis_and_position( check(
r#" r#"
//- /lib.rs crate:foo cfg:feature=foo,feature=bar //- /lib.rs crate:foo cfg:feature=foo,feature=bar
<|> //empty <|>
#[test] #[test]
#[cfg(all(feature = "foo", feature = "bar"))] #[cfg(all(feature = "foo", feature = "bar"))]
fn test_foo1() {} fn test_foo1() {}
"#, "#,
); &[&TEST],
let runnables = analysis.runnables(pos.file_id).unwrap(); expect![[r#"
assert_debug_snapshot!(&runnables,
@r###"
[ [
Runnable { Runnable {
nav: NavigationTarget { nav: NavigationTarget {
file_id: FileId( file_id: FileId(
1, 1,
), ),
full_range: 1..80, full_range: 1..72,
name: "test_foo1", name: "test_foo1",
kind: FN_DEF, kind: FN_DEF,
focus_range: Some( focus_range: Some(
66..75, 58..67,
), ),
container_name: None, container_name: None,
description: None, description: None,
@ -822,23 +807,24 @@ mod tests {
], ],
}, },
] ]
"### "#]],
); );
assert_actions(&runnables, &[&TEST]);
} }
#[test] #[test]
fn test_runnables_no_test_function_in_module() { fn test_runnables_no_test_function_in_module() {
let (analysis, pos) = analysis_and_position( check(
r#" r#"
//- /lib.rs //- /lib.rs
<|> //empty <|>
mod test_mod { mod test_mod {
fn foo1() {} fn foo1() {}
} }
"#, "#,
&[],
expect![[r#"
[]
"#]],
); );
let runnables = analysis.runnables(pos.file_id).unwrap();
assert!(runnables.is_empty())
} }
} }