From d79a9b17dc4fb132443aa4ec1ca0ab278d2a217c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 14 Jan 2019 16:27:08 +0300 Subject: switch to insta for testing --- crates/ra_ide_api/Cargo.toml | 3 + crates/ra_ide_api/src/extend_selection.rs | 5 +- crates/ra_ide_api/src/runnables.rs | 97 ++++++++++++++++++ .../tests__highlight_query_group_macro.snap | 26 +++++ .../tests__highlights_code_inside_macros.snap | 70 +++++++++++++ .../ra_ide_api/src/snapshots/tests__runnables.snap | 22 +++++ .../src/snapshots/tests__runnables_module.snap | 18 ++++ .../tests__runnables_multiple_depth_module.snap | 18 ++++ .../tests__runnables_one_depth_layer_module.snap | 18 ++++ crates/ra_ide_api/src/syntax_highlighting.rs | 32 +----- crates/ra_ide_api/tests/test/main.rs | 20 +--- crates/ra_ide_api/tests/test/runnables.rs | 109 --------------------- .../test__unresolved_module_diagnostic.snap | 26 +++++ crates/ra_ide_api_light/Cargo.toml | 1 + crates/ra_ide_api_light/src/lib.rs | 13 +-- .../src/snapshots/tests__file_structure.snap | 80 +++++++++++++++ .../src/snapshots/tests__highlighting.snap | 30 ++++++ crates/ra_ide_api_light/src/structure.rs | 16 +-- crates/test_utils/src/lib.rs | 8 -- 19 files changed, 425 insertions(+), 187 deletions(-) create mode 100644 crates/ra_ide_api/src/snapshots/tests__highlight_query_group_macro.snap create mode 100644 crates/ra_ide_api/src/snapshots/tests__highlights_code_inside_macros.snap create mode 100644 crates/ra_ide_api/src/snapshots/tests__runnables.snap create mode 100644 crates/ra_ide_api/src/snapshots/tests__runnables_module.snap create mode 100644 crates/ra_ide_api/src/snapshots/tests__runnables_multiple_depth_module.snap create mode 100644 crates/ra_ide_api/src/snapshots/tests__runnables_one_depth_layer_module.snap delete mode 100644 crates/ra_ide_api/tests/test/runnables.rs create mode 100644 crates/ra_ide_api/tests/test/snapshots/test__unresolved_module_diagnostic.snap create mode 100644 crates/ra_ide_api_light/src/snapshots/tests__file_structure.snap create mode 100644 crates/ra_ide_api_light/src/snapshots/tests__highlighting.snap (limited to 'crates') diff --git a/crates/ra_ide_api/Cargo.toml b/crates/ra_ide_api/Cargo.toml index d42a664b6..f7013c3c1 100644 --- a/crates/ra_ide_api/Cargo.toml +++ b/crates/ra_ide_api/Cargo.toml @@ -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" diff --git a/crates/ra_ide_api/src/extend_selection.rs b/crates/ra_ide_api/src/extend_selection.rs index c3c809c9f..9f0ab2f1c 100644 --- a/crates/ra_ide_api/src/extend_selection.rs +++ b/crates/ra_ide_api/src/extend_selection.rs @@ -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())); } } diff --git a/crates/ra_ide_api/src/runnables.rs b/crates/ra_ide_api/src/runnables.rs index 53e49da5b..f1de28094 100644 --- a/crates/ra_ide_api/src/runnables.rs +++ b/crates/ra_ide_api/src/runnables.rs @@ -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()) + } + +} diff --git a/crates/ra_ide_api/src/snapshots/tests__highlight_query_group_macro.snap b/crates/ra_ide_api/src/snapshots/tests__highlight_query_group_macro.snap new file mode 100644 index 000000000..b84aa9c78 --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__highlight_query_group_macro.snap @@ -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" + } +] diff --git a/crates/ra_ide_api/src/snapshots/tests__highlights_code_inside_macros.snap b/crates/ra_ide_api/src/snapshots/tests__highlights_code_inside_macros.snap new file mode 100644 index 000000000..14c6e5a4e --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__highlights_code_inside_macros.snap @@ -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" + } +] diff --git a/crates/ra_ide_api/src/snapshots/tests__runnables.snap b/crates/ra_ide_api/src/snapshots/tests__runnables.snap new file mode 100644 index 000000000..ba6cba0ab --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__runnables.snap @@ -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" + } + } +] diff --git a/crates/ra_ide_api/src/snapshots/tests__runnables_module.snap b/crates/ra_ide_api/src/snapshots/tests__runnables_module.snap new file mode 100644 index 000000000..b3f2d4d6e --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__runnables_module.snap @@ -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" + } + } +] diff --git a/crates/ra_ide_api/src/snapshots/tests__runnables_multiple_depth_module.snap b/crates/ra_ide_api/src/snapshots/tests__runnables_multiple_depth_module.snap new file mode 100644 index 000000000..6eba482e7 --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__runnables_multiple_depth_module.snap @@ -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" + } + } +] diff --git a/crates/ra_ide_api/src/snapshots/tests__runnables_one_depth_layer_module.snap b/crates/ra_ide_api/src/snapshots/tests__runnables_one_depth_layer_module.snap new file mode 100644 index 000000000..f40c762f3 --- /dev/null +++ b/crates/ra_ide_api/src/snapshots/tests__runnables_one_depth_layer_module.snap @@ -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" + } + } +] diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index cb19e9515..480b78dce 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs @@ -34,7 +34,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Cancelable //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) -} diff --git a/crates/ra_ide_api/tests/test/snapshots/test__unresolved_module_diagnostic.snap b/crates/ra_ide_api/tests/test/snapshots/test__unresolved_module_diagnostic.snap new file mode 100644 index 000000000..1b41e2b00 --- /dev/null +++ b/crates/ra_ide_api/tests/test/snapshots/test__unresolved_module_diagnostic.snap @@ -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 + } +] diff --git a/crates/ra_ide_api_light/Cargo.toml b/crates/ra_ide_api_light/Cargo.toml index 8c192fca6..16cf228e7 100644 --- a/crates/ra_ide_api_light/Cargo.toml +++ b/crates/ra_ide_api_light/Cargo.toml @@ -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" diff --git a/crates/ra_ide_api_light/src/lib.rs b/crates/ra_ide_api_light/src/lib.rs index 72fba9402..9dd72701d 100644 --- a/crates/ra_ide_api_light/src/lib.rs +++ b/crates/ra_ide_api_light/src/lib.rs @@ -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] diff --git a/crates/ra_ide_api_light/src/snapshots/tests__file_structure.snap b/crates/ra_ide_api_light/src/snapshots/tests__file_structure.snap new file mode 100644 index 000000000..10eb5c724 --- /dev/null +++ b/crates/ra_ide_api_light/src/snapshots/tests__file_structure.snap @@ -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 + } +] diff --git a/crates/ra_ide_api_light/src/snapshots/tests__highlighting.snap b/crates/ra_ide_api_light/src/snapshots/tests__highlighting.snap new file mode 100644 index 000000000..d8f44a713 --- /dev/null +++ b/crates/ra_ide_api_light/src/snapshots/tests__highlighting.snap @@ -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" + } +] diff --git a/crates/ra_ide_api_light/src/structure.rs b/crates/ra_ide_api_light/src/structure.rs index 8bd57555f..3c6f39e16 100644 --- a/crates/ra_ide_api_light/src/structure.rs +++ b/crates/ra_ide_api_light/src/structure.rs @@ -87,7 +87,7 @@ fn structure_node(node: &SyntaxNode) -> Option { #[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); } } diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 9b1c9c9a0..6489033dd 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -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"), -- cgit v1.2.3