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