switch to insta for testing

This commit is contained in:
Aleksey Kladov 2019-01-14 16:27:08 +03:00
parent 8caff4e034
commit d79a9b17dc
20 changed files with 495 additions and 195 deletions

View file

@ -21,3 +21,6 @@ ra_text_edit = { path = "../ra_text_edit" }
ra_db = { path = "../ra_db" }
hir = { path = "../ra_hir", package = "ra_hir" }
test_utils = { path = "../test_utils" }
[dev-dependencies]
insta = "0.1.4"

View file

@ -38,8 +38,9 @@ fn find_macro_call(node: &SyntaxNode, range: TextRange) -> Option<&ast::MacroCal
#[cfg(test)]
mod tests {
use ra_syntax::TextRange;
use crate::mock_analysis::single_file_with_range;
use test_utils::assert_eq_dbg;
#[test]
fn extend_selection_inside_macros() {
@ -51,6 +52,6 @@ mod tests {
",
);
let r = analysis.extend_selection(frange);
assert_eq_dbg("[51; 56)", &r);
assert_eq!(r, TextRange::from_to(51.into(), 56.into()));
}
}

View file

@ -92,3 +92,100 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: &ast::Module) -> Opt
kind: RunnableKind::TestMod { path },
})
}
#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot_matches;
use crate::mock_analysis::analysis_and_position;
#[test]
fn test_runnables() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
fn main() {}
#[test]
fn test_foo() {}
#[test]
#[ignore]
fn test_foo() {}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert_debug_snapshot_matches!("runnables", &runnables)
}
#[test]
fn test_runnables_module() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
mod test_mod {
#[test]
fn test_foo1() {}
}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert_debug_snapshot_matches!("runnables_module", &runnables)
}
#[test]
fn test_runnables_one_depth_layer_module() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
mod foo {
mod test_mod {
#[test]
fn test_foo1() {}
}
}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert_debug_snapshot_matches!("runnables_one_depth_layer_module", &runnables)
}
#[test]
fn test_runnables_multiple_depth_module() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
mod foo {
mod bar {
mod test_mod {
#[test]
fn test_foo1() {}
}
}
}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert_debug_snapshot_matches!("runnables_multiple_depth_module", &runnables)
}
#[test]
fn test_runnables_no_test_function_in_module() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
mod test_mod {
fn foo1() {}
}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert!(runnables.is_empty())
}
}

View file

@ -0,0 +1,26 @@
Created: 2019-01-15T11:15:20.732493641+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api/src/syntax_highlighting.rs
[
HighlightedRange {
range: [20; 32),
tag: "macro"
},
HighlightedRange {
range: [13; 18),
tag: "text"
},
HighlightedRange {
range: [51; 54),
tag: "keyword"
},
HighlightedRange {
range: [55; 60),
tag: "keyword"
},
HighlightedRange {
range: [61; 72),
tag: "function"
}
]

View file

@ -0,0 +1,70 @@
Created: 2019-01-15T11:15:20.732523231+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api/src/syntax_highlighting.rs
[
HighlightedRange {
range: [13; 15),
tag: "keyword"
},
HighlightedRange {
range: [16; 20),
tag: "function"
},
HighlightedRange {
range: [41; 46),
tag: "macro"
},
HighlightedRange {
range: [49; 52),
tag: "keyword"
},
HighlightedRange {
range: [57; 59),
tag: "literal"
},
HighlightedRange {
range: [82; 86),
tag: "macro"
},
HighlightedRange {
range: [89; 92),
tag: "keyword"
},
HighlightedRange {
range: [97; 99),
tag: "literal"
},
HighlightedRange {
range: [49; 52),
tag: "keyword"
},
HighlightedRange {
range: [53; 54),
tag: "function"
},
HighlightedRange {
range: [57; 59),
tag: "literal"
},
HighlightedRange {
range: [61; 62),
tag: "text"
},
HighlightedRange {
range: [89; 92),
tag: "keyword"
},
HighlightedRange {
range: [93; 94),
tag: "function"
},
HighlightedRange {
range: [97; 99),
tag: "literal"
},
HighlightedRange {
range: [101; 102),
tag: "text"
}
]

View file

@ -0,0 +1,22 @@
Created: 2019-01-15T11:15:20.732460119+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api/src/runnables.rs
[
Runnable {
range: [1; 21),
kind: Bin
},
Runnable {
range: [22; 46),
kind: Test {
name: "test_foo"
}
},
Runnable {
range: [47; 81),
kind: Test {
name: "test_foo"
}
}
]

View file

@ -0,0 +1,18 @@
Created: 2019-01-15T11:15:20.732460109+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api/src/runnables.rs
[
Runnable {
range: [1; 59),
kind: TestMod {
path: "test_mod"
}
},
Runnable {
range: [28; 57),
kind: Test {
name: "test_foo1"
}
}
]

View file

@ -0,0 +1,18 @@
Created: 2019-01-15T11:15:20.732522773+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api/src/runnables.rs
[
Runnable {
range: [41; 115),
kind: TestMod {
path: "foo::bar::test_mod"
}
},
Runnable {
range: [68; 105),
kind: Test {
name: "test_foo1"
}
}
]

View file

@ -0,0 +1,18 @@
Created: 2019-01-15T11:15:20.732480089+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api/src/runnables.rs
[
Runnable {
range: [23; 85),
kind: TestMod {
path: "foo::test_mod"
}
},
Runnable {
range: [46; 79),
kind: Test {
name: "test_foo1"
}
}
]

View file

@ -34,7 +34,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<Hi
#[cfg(test)]
mod tests {
use crate::mock_analysis::single_file;
use test_utils::assert_eq_dbg;
use insta::assert_debug_snapshot_matches;
#[test]
fn highlights_code_inside_macros() {
@ -47,25 +48,7 @@ mod tests {
",
);
let highlights = analysis.highlight(file_id).unwrap();
assert_eq_dbg(
r#"[HighlightedRange { range: [13; 15), tag: "keyword" },
HighlightedRange { range: [16; 20), tag: "function" },
HighlightedRange { range: [41; 46), tag: "macro" },
HighlightedRange { range: [49; 52), tag: "keyword" },
HighlightedRange { range: [57; 59), tag: "literal" },
HighlightedRange { range: [82; 86), tag: "macro" },
HighlightedRange { range: [89; 92), tag: "keyword" },
HighlightedRange { range: [97; 99), tag: "literal" },
HighlightedRange { range: [49; 52), tag: "keyword" },
HighlightedRange { range: [53; 54), tag: "function" },
HighlightedRange { range: [57; 59), tag: "literal" },
HighlightedRange { range: [61; 62), tag: "text" },
HighlightedRange { range: [89; 92), tag: "keyword" },
HighlightedRange { range: [93; 94), tag: "function" },
HighlightedRange { range: [97; 99), tag: "literal" },
HighlightedRange { range: [101; 102), tag: "text" }]"#,
&highlights,
)
assert_debug_snapshot_matches!("highlights_code_inside_macros", &highlights);
}
// FIXME: this test is not really necessary: artifact of the inital hacky
@ -80,13 +63,6 @@ mod tests {
",
);
let highlights = analysis.highlight(file_id).unwrap();
assert_eq_dbg(
r#"[HighlightedRange { range: [20; 32), tag: "macro" },
HighlightedRange { range: [13; 18), tag: "text" },
HighlightedRange { range: [51; 54), tag: "keyword" },
HighlightedRange { range: [55; 60), tag: "keyword" },
HighlightedRange { range: [61; 72), tag: "function" }]"#,
&highlights,
)
assert_debug_snapshot_matches!("highlight_query_group_macro", &highlights);
}
}

View file

@ -1,7 +1,6 @@
mod runnables;
use ra_syntax::TextRange;
use test_utils::{assert_eq_dbg, assert_eq_text};
use test_utils::assert_eq_text;
use insta::assert_debug_snapshot_matches;
use ra_ide_api::{
mock_analysis::{single_file, single_file_with_position, MockAnalysis},
@ -12,18 +11,7 @@ use ra_ide_api::{
fn test_unresolved_module_diagnostic() {
let (analysis, file_id) = single_file("mod foo;");
let diagnostics = analysis.diagnostics(file_id).unwrap();
assert_eq_dbg(
r#"[Diagnostic {
message: "unresolved module",
range: [4; 7),
fix: Some(SourceChange {
label: "create module",
source_file_edits: [],
file_system_edits: [CreateFile { source_root: SourceRootId(0), path: "foo.rs" }],
cursor_position: None }),
severity: Error }]"#,
&diagnostics,
);
assert_debug_snapshot_matches!("unresolved_module_diagnostic", &diagnostics);
}
// FIXME: move this test to hir
@ -31,7 +19,7 @@ fn test_unresolved_module_diagnostic() {
fn test_unresolved_module_diagnostic_no_diag_for_inline_mode() {
let (analysis, file_id) = single_file("mod foo {}");
let diagnostics = analysis.diagnostics(file_id).unwrap();
assert_eq_dbg(r#"[]"#, &diagnostics);
assert!(diagnostics.is_empty());
}
#[test]

View file

@ -1,109 +0,0 @@
use test_utils::assert_eq_dbg;
use ra_ide_api::mock_analysis::analysis_and_position;
#[test]
fn test_runnables() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
fn main() {}
#[test]
fn test_foo() {}
#[test]
#[ignore]
fn test_foo() {}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert_eq_dbg(
r#"[Runnable { range: [1; 21), kind: Bin },
Runnable { range: [22; 46), kind: Test { name: "test_foo" } },
Runnable { range: [47; 81), kind: Test { name: "test_foo" } }]"#,
&runnables,
)
}
#[test]
fn test_runnables_module() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
mod test_mod {
#[test]
fn test_foo1() {}
}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert_eq_dbg(
r#"[Runnable { range: [1; 59), kind: TestMod { path: "test_mod" } },
Runnable { range: [28; 57), kind: Test { name: "test_foo1" } }]"#,
&runnables,
)
}
#[test]
fn test_runnables_one_depth_layer_module() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
mod foo {
mod test_mod {
#[test]
fn test_foo1() {}
}
}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert_eq_dbg(
r#"[Runnable { range: [23; 85), kind: TestMod { path: "foo::test_mod" } },
Runnable { range: [46; 79), kind: Test { name: "test_foo1" } }]"#,
&runnables,
)
}
#[test]
fn test_runnables_multiple_depth_module() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
mod foo {
mod bar {
mod test_mod {
#[test]
fn test_foo1() {}
}
}
}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert_eq_dbg(
r#"[Runnable { range: [41; 115), kind: TestMod { path: "foo::bar::test_mod" } },
Runnable { range: [68; 105), kind: Test { name: "test_foo1" } }]"#,
&runnables,
)
}
#[test]
fn test_runnables_no_test_function_in_module() {
let (analysis, pos) = analysis_and_position(
r#"
//- /lib.rs
<|> //empty
mod test_mod {
fn foo1() {}
}
"#,
);
let runnables = analysis.runnables(pos.file_id).unwrap();
assert_eq_dbg(r#"[]"#, &runnables)
}

View file

@ -0,0 +1,26 @@
Created: 2019-01-15T11:15:20.891129945+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api/tests/test/main.rs
[
Diagnostic {
message: "unresolved module",
range: [4; 7),
fix: Some(
SourceChange {
label: "create module",
source_file_edits: [],
file_system_edits: [
CreateFile {
source_root: SourceRootId(
0
),
path: "foo.rs"
}
],
cursor_position: None
}
),
severity: Error
}
]

View file

@ -17,3 +17,4 @@ ra_text_edit = { path = "../ra_text_edit" }
[dev-dependencies]
test_utils = { path = "../test_utils" }
proptest = "0.8.7"
insta = "0.1.4"

View file

@ -132,8 +132,9 @@ pub fn syntax_tree(file: &SourceFile) -> String {
#[cfg(test)]
mod tests {
use ra_syntax::AstNode;
use insta::assert_debug_snapshot_matches;
use crate::test_utils::{add_cursor, assert_eq_dbg, assert_eq_text, extract_offset};
use crate::test_utils::{add_cursor, assert_eq_text, extract_offset};
use super::*;
@ -147,15 +148,7 @@ fn main() {}
"#,
);
let hls = highlight(file.syntax());
assert_eq_dbg(
r#"[HighlightedRange { range: [1; 11), tag: "comment" },
HighlightedRange { range: [12; 14), tag: "keyword" },
HighlightedRange { range: [15; 19), tag: "function" },
HighlightedRange { range: [29; 37), tag: "macro" },
HighlightedRange { range: [38; 50), tag: "string" },
HighlightedRange { range: [52; 54), tag: "literal" }]"#,
&hls,
);
assert_debug_snapshot_matches!("highlighting", hls);
}
#[test]

View file

@ -0,0 +1,80 @@
Created: 2019-01-15T11:15:21.073862814+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api_light/src/structure.rs
[
StructureNode {
parent: None,
label: "Foo",
navigation_range: [8; 11),
node_range: [1; 26),
kind: STRUCT_DEF
},
StructureNode {
parent: Some(
0
),
label: "x",
navigation_range: [18; 19),
node_range: [18; 24),
kind: NAMED_FIELD_DEF
},
StructureNode {
parent: None,
label: "m",
navigation_range: [32; 33),
node_range: [28; 53),
kind: MODULE
},
StructureNode {
parent: Some(
2
),
label: "bar",
navigation_range: [43; 46),
node_range: [40; 51),
kind: FN_DEF
},
StructureNode {
parent: None,
label: "E",
navigation_range: [60; 61),
node_range: [55; 75),
kind: ENUM_DEF
},
StructureNode {
parent: None,
label: "T",
navigation_range: [81; 82),
node_range: [76; 88),
kind: TYPE_DEF
},
StructureNode {
parent: None,
label: "S",
navigation_range: [96; 97),
node_range: [89; 108),
kind: STATIC_DEF
},
StructureNode {
parent: None,
label: "C",
navigation_range: [115; 116),
node_range: [109; 127),
kind: CONST_DEF
},
StructureNode {
parent: None,
label: "impl E",
navigation_range: [134; 135),
node_range: [129; 138),
kind: IMPL_BLOCK
},
StructureNode {
parent: None,
label: "impl fmt::Debug for E",
navigation_range: [160; 161),
node_range: [140; 164),
kind: IMPL_BLOCK
}
]

View file

@ -0,0 +1,30 @@
Created: 2019-01-15T11:15:21.073858657+00:00
Creator: insta@0.1.4
Source: crates/ra_ide_api_light/src/lib.rs
[
HighlightedRange {
range: [1; 11),
tag: "comment"
},
HighlightedRange {
range: [12; 14),
tag: "keyword"
},
HighlightedRange {
range: [15; 19),
tag: "function"
},
HighlightedRange {
range: [29; 37),
tag: "macro"
},
HighlightedRange {
range: [38; 50),
tag: "string"
},
HighlightedRange {
range: [52; 54),
tag: "literal"
}
]

View file

@ -87,7 +87,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
#[cfg(test)]
mod tests {
use super::*;
use test_utils::assert_eq_dbg;
use insta::assert_debug_snapshot_matches;
#[test]
fn test_file_structure() {
@ -112,18 +112,6 @@ impl fmt::Debug for E {}
"#,
);
let structure = file_structure(&file);
assert_eq_dbg(
r#"[StructureNode { parent: None, label: "Foo", navigation_range: [8; 11), node_range: [1; 26), kind: STRUCT_DEF },
StructureNode { parent: Some(0), label: "x", navigation_range: [18; 19), node_range: [18; 24), kind: NAMED_FIELD_DEF },
StructureNode { parent: None, label: "m", navigation_range: [32; 33), node_range: [28; 53), kind: MODULE },
StructureNode { parent: Some(2), label: "bar", navigation_range: [43; 46), node_range: [40; 51), kind: FN_DEF },
StructureNode { parent: None, label: "E", navigation_range: [60; 61), node_range: [55; 75), kind: ENUM_DEF },
StructureNode { parent: None, label: "T", navigation_range: [81; 82), node_range: [76; 88), kind: TYPE_DEF },
StructureNode { parent: None, label: "S", navigation_range: [96; 97), node_range: [89; 108), kind: STATIC_DEF },
StructureNode { parent: None, label: "C", navigation_range: [115; 116), node_range: [109; 127), kind: CONST_DEF },
StructureNode { parent: None, label: "impl E", navigation_range: [134; 135), node_range: [129; 138), kind: IMPL_BLOCK },
StructureNode { parent: None, label: "impl fmt::Debug for E", navigation_range: [160; 161), node_range: [140; 164), kind: IMPL_BLOCK }]"#,
&structure,
)
assert_debug_snapshot_matches!("file_structure", structure);
}
}

View file

@ -1,8 +1,6 @@
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use itertools::Itertools;
use text_unit::{TextRange, TextUnit};
use serde_json::Value;
@ -31,12 +29,6 @@ macro_rules! assert_eq_text {
}};
}
pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) {
let actual = format!("{:?}", actual);
let expected = expected.lines().map(|l| l.trim()).join(" ");
assert_eq!(expected, actual);
}
pub fn extract_offset(text: &str) -> (TextUnit, String) {
match try_extract_offset(text) {
None => panic!("text should contain cursor marker"),