From f5992964edb0e6dba7970c344b46bfd75f57e9a0 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Thu, 3 Jan 2019 12:01:52 +0900 Subject: Add Analysis#teype_of test --- crates/ra_analysis/src/mock_analysis.rs | 28 +++++++++++++++++++++++++++- crates/ra_analysis/tests/tests.rs | 18 +++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/mock_analysis.rs b/crates/ra_analysis/src/mock_analysis.rs index 960529404..b37371499 100644 --- a/crates/ra_analysis/src/mock_analysis.rs +++ b/crates/ra_analysis/src/mock_analysis.rs @@ -55,6 +55,26 @@ impl MockAnalysis { (res, position) } + /// Same as `with_files`, but requires that a single file contains two `<|>` marker, + /// whose range is also returned. + pub fn with_files_and_range(fixture: &str) -> (MockAnalysis, FileRange) { + let mut range = None; + let mut res = MockAnalysis::new(); + for entry in parse_fixture(fixture) { + if entry.text.contains(CURSOR_MARKER) { + assert!( + range.is_none(), + "only two marker (<|>) per fixture is allowed" + ); + range = Some(res.add_file_with_range(&entry.meta, &entry.text)); + } else { + res.add_file(&entry.meta, &entry.text); + } + } + let range = range.expect("expected two marker (<|>)"); + (res, range) + } + pub fn add_file(&mut self, path: &str, text: &str) -> FileId { let file_id = FileId((self.files.len() + 1) as u32); self.files.push((path.to_string(), text.to_string())); @@ -102,12 +122,18 @@ impl MockAnalysis { } } -/// Creates analysis from a multi-file fixture, returns positions marked with <|>. +/// Creates analysis from a multi-file fixture, returns positions marked with a <|>. pub fn analysis_and_position(fixture: &str) -> (Analysis, FilePosition) { let (mock, position) = MockAnalysis::with_files_and_position(fixture); (mock.analysis(), position) } +/// Creates analysis from a multi-file fixture, returns ranges marked with two <|>. +pub fn analysis_and_range(fixture: &str) -> (Analysis, FileRange) { + let (mock, range) = MockAnalysis::with_files_and_range(fixture); + (mock.analysis(), range) +} + /// Creates analysis for a single file. pub fn single_file(code: &str) -> (Analysis, FileId) { let mut mock = MockAnalysis::new(); diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 3045c2e78..ce6f6f3fa 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -2,7 +2,7 @@ use ra_syntax::TextRange; use test_utils::{assert_eq_dbg, assert_eq_text}; use ra_analysis::{ - mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, + mock_analysis::{analysis_and_position, analysis_and_range, single_file, single_file_with_position, MockAnalysis}, AnalysisChange, CrateGraph, FileId, FnSignatureInfo, }; @@ -10,6 +10,22 @@ fn get_signature(text: &str) -> (FnSignatureInfo, Option) { let (analysis, position) = single_file_with_position(text); analysis.resolve_callable(position).unwrap().unwrap() } +#[test] +fn test_type_of() { + let (analysis, range) = analysis_and_range( + " + //- /lib.rs + pub fn foo() -> u32 { + 1 + }; + + let <|>foo_test<|> = foo(); + ", + ); + + let type_name = analysis.type_of(range).unwrap().unwrap(); + assert_eq_dbg("u32", &type_name); +} #[test] fn approximate_resolve_works_in_items() { -- cgit v1.2.3 From f99e96698c81a731842ef21ae564470a7d391046 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Thu, 3 Jan 2019 18:27:02 +0900 Subject: Remove unnecessary mock functions --- crates/ra_analysis/src/mock_analysis.rs | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/mock_analysis.rs b/crates/ra_analysis/src/mock_analysis.rs index b37371499..960529404 100644 --- a/crates/ra_analysis/src/mock_analysis.rs +++ b/crates/ra_analysis/src/mock_analysis.rs @@ -55,26 +55,6 @@ impl MockAnalysis { (res, position) } - /// Same as `with_files`, but requires that a single file contains two `<|>` marker, - /// whose range is also returned. - pub fn with_files_and_range(fixture: &str) -> (MockAnalysis, FileRange) { - let mut range = None; - let mut res = MockAnalysis::new(); - for entry in parse_fixture(fixture) { - if entry.text.contains(CURSOR_MARKER) { - assert!( - range.is_none(), - "only two marker (<|>) per fixture is allowed" - ); - range = Some(res.add_file_with_range(&entry.meta, &entry.text)); - } else { - res.add_file(&entry.meta, &entry.text); - } - } - let range = range.expect("expected two marker (<|>)"); - (res, range) - } - pub fn add_file(&mut self, path: &str, text: &str) -> FileId { let file_id = FileId((self.files.len() + 1) as u32); self.files.push((path.to_string(), text.to_string())); @@ -122,18 +102,12 @@ impl MockAnalysis { } } -/// Creates analysis from a multi-file fixture, returns positions marked with a <|>. +/// Creates analysis from a multi-file fixture, returns positions marked with <|>. pub fn analysis_and_position(fixture: &str) -> (Analysis, FilePosition) { let (mock, position) = MockAnalysis::with_files_and_position(fixture); (mock.analysis(), position) } -/// Creates analysis from a multi-file fixture, returns ranges marked with two <|>. -pub fn analysis_and_range(fixture: &str) -> (Analysis, FileRange) { - let (mock, range) = MockAnalysis::with_files_and_range(fixture); - (mock.analysis(), range) -} - /// Creates analysis for a single file. pub fn single_file(code: &str) -> (Analysis, FileId) { let mut mock = MockAnalysis::new(); -- cgit v1.2.3 From 4363e7b9b2de715b0108b53d40eb33ed2fcc8532 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Thu, 3 Jan 2019 18:48:03 +0900 Subject: Deive type_of test from tests --- crates/ra_analysis/tests/tests.rs | 18 +-------- crates/ra_analysis/tests/type_of.rs | 79 +++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 crates/ra_analysis/tests/type_of.rs (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index ce6f6f3fa..3045c2e78 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -2,7 +2,7 @@ use ra_syntax::TextRange; use test_utils::{assert_eq_dbg, assert_eq_text}; use ra_analysis::{ - mock_analysis::{analysis_and_position, analysis_and_range, single_file, single_file_with_position, MockAnalysis}, + mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, AnalysisChange, CrateGraph, FileId, FnSignatureInfo, }; @@ -10,22 +10,6 @@ fn get_signature(text: &str) -> (FnSignatureInfo, Option) { let (analysis, position) = single_file_with_position(text); analysis.resolve_callable(position).unwrap().unwrap() } -#[test] -fn test_type_of() { - let (analysis, range) = analysis_and_range( - " - //- /lib.rs - pub fn foo() -> u32 { - 1 - }; - - let <|>foo_test<|> = foo(); - ", - ); - - let type_name = analysis.type_of(range).unwrap().unwrap(); - assert_eq_dbg("u32", &type_name); -} #[test] fn approximate_resolve_works_in_items() { diff --git a/crates/ra_analysis/tests/type_of.rs b/crates/ra_analysis/tests/type_of.rs new file mode 100644 index 000000000..375f808bb --- /dev/null +++ b/crates/ra_analysis/tests/type_of.rs @@ -0,0 +1,79 @@ +use ra_analysis::{ + mock_analysis::{single_file_with_range}, +}; + +#[test] +fn test_type_of_for_function() { + let (analysis, range) = single_file_with_range( + " + pub fn foo() -> u32 { 1 }; + + fn main() { + let foo_test = <|>foo()<|>; + } + ", + ); + + let type_name = analysis.type_of(range).unwrap().unwrap(); + assert_eq!("u32", &type_name); +} + +// FIXME: improve type_of to make this work +#[test] +fn test_type_of_for_num() { + let (analysis, range) = single_file_with_range( + r#" + fn main() { + let foo_test = <|>"foo"<|>; + } + "#, + ); + + assert!(analysis.type_of(range).unwrap().is_none()); +} +// FIXME: improve type_of to make this work +#[test] +fn test_type_of_for_binding() { + let (analysis, range) = single_file_with_range( + " + pub fn foo() -> u32 { 1 }; + + fn main() { + let <|>foo_test<|> = foo(); + } + ", + ); + + assert!(analysis.type_of(range).unwrap().is_none()); +} + +// FIXME: improve type_of to make this work +#[test] +fn test_type_of_for_expr_1() { + let (analysis, range) = single_file_with_range( + " + fn main() { + let foo = <|>1 + foo_test<|>; + } + ", + ); + + let type_name = analysis.type_of(range).unwrap().unwrap(); + assert_eq!("[unknown]", &type_name); +} + +// FIXME: improve type_of to make this work +#[test] +fn test_type_of_for_expr_2() { + let (analysis, range) = single_file_with_range( + " + fn main() { + let foo: usize = 1; + let bar = <|>1 + foo_test<|>; + } + ", + ); + + let type_name = analysis.type_of(range).unwrap().unwrap(); + assert_eq!("[unknown]", &type_name); +} -- cgit v1.2.3