diff options
author | Aleksey Kladov <[email protected]> | 2020-06-23 17:46:56 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-06-23 17:46:56 +0100 |
commit | fdf86aee18e396d393d50df7df27b02111838507 (patch) | |
tree | 88cd35c526a926eb8c8a8645243209759a7bb7f9 /crates | |
parent | 3486b47e5c4f71479cc3c876da1fd1dcbfcab257 (diff) |
Nicer API
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_db/src/fixture.rs | 10 | ||||
-rw-r--r-- | crates/ra_ide/src/mock_analysis.rs | 16 | ||||
-rw-r--r-- | crates/test_utils/src/fixture.rs | 120 | ||||
-rw-r--r-- | crates/test_utils/src/lib.rs | 2 |
4 files changed, 75 insertions, 73 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index f786fb87f..6c13e62bb 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs | |||
@@ -61,7 +61,7 @@ use std::{str::FromStr, sync::Arc}; | |||
61 | 61 | ||
62 | use ra_cfg::CfgOptions; | 62 | use ra_cfg::CfgOptions; |
63 | use rustc_hash::FxHashMap; | 63 | use rustc_hash::FxHashMap; |
64 | use test_utils::{extract_offset, parse_fixture, FixtureEntry, CURSOR_MARKER}; | 64 | use test_utils::{extract_offset, Fixture, CURSOR_MARKER}; |
65 | use vfs::{file_set::FileSet, VfsPath}; | 65 | use vfs::{file_set::FileSet, VfsPath}; |
66 | 66 | ||
67 | use crate::{ | 67 | use crate::{ |
@@ -107,9 +107,9 @@ fn with_files( | |||
107 | db: &mut dyn SourceDatabaseExt, | 107 | db: &mut dyn SourceDatabaseExt, |
108 | fixture: &str, | 108 | fixture: &str, |
109 | ) -> (Option<FilePosition>, Vec<FileId>) { | 109 | ) -> (Option<FilePosition>, Vec<FileId>) { |
110 | let mut files = Vec::new(); | 110 | let fixture = Fixture::parse(fixture); |
111 | let fixture = parse_fixture(fixture); | ||
112 | 111 | ||
112 | let mut files = Vec::new(); | ||
113 | let mut crate_graph = CrateGraph::default(); | 113 | let mut crate_graph = CrateGraph::default(); |
114 | let mut crates = FxHashMap::default(); | 114 | let mut crates = FxHashMap::default(); |
115 | let mut crate_deps = Vec::new(); | 115 | let mut crate_deps = Vec::new(); |
@@ -201,8 +201,8 @@ struct FileMeta { | |||
201 | env: Env, | 201 | env: Env, |
202 | } | 202 | } |
203 | 203 | ||
204 | impl From<&FixtureEntry> for ParsedMeta { | 204 | impl From<&Fixture> for ParsedMeta { |
205 | fn from(f: &FixtureEntry) -> Self { | 205 | fn from(f: &Fixture) -> Self { |
206 | Self::File(FileMeta { | 206 | Self::File(FileMeta { |
207 | path: f.path.to_owned(), | 207 | path: f.path.to_owned(), |
208 | krate: f.crate_name.to_owned(), | 208 | krate: f.crate_name.to_owned(), |
diff --git a/crates/ra_ide/src/mock_analysis.rs b/crates/ra_ide/src/mock_analysis.rs index d480fcf62..f15990158 100644 --- a/crates/ra_ide/src/mock_analysis.rs +++ b/crates/ra_ide/src/mock_analysis.rs | |||
@@ -3,7 +3,7 @@ use std::{str::FromStr, sync::Arc}; | |||
3 | 3 | ||
4 | use ra_cfg::CfgOptions; | 4 | use ra_cfg::CfgOptions; |
5 | use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath}; | 5 | use ra_db::{CrateName, Env, FileSet, SourceRoot, VfsPath}; |
6 | use test_utils::{extract_offset, extract_range, parse_fixture, FixtureEntry, CURSOR_MARKER}; | 6 | use test_utils::{extract_offset, extract_range, Fixture, CURSOR_MARKER}; |
7 | 7 | ||
8 | use crate::{ | 8 | use crate::{ |
9 | Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange, | 9 | Analysis, AnalysisChange, AnalysisHost, CrateGraph, Edition, FileId, FilePosition, FileRange, |
@@ -12,7 +12,7 @@ use crate::{ | |||
12 | #[derive(Debug)] | 12 | #[derive(Debug)] |
13 | enum MockFileData { | 13 | enum MockFileData { |
14 | Plain { path: String, content: String }, | 14 | Plain { path: String, content: String }, |
15 | Fixture(FixtureEntry), | 15 | Fixture(Fixture), |
16 | } | 16 | } |
17 | 17 | ||
18 | impl MockFileData { | 18 | impl MockFileData { |
@@ -60,8 +60,8 @@ impl MockFileData { | |||
60 | } | 60 | } |
61 | } | 61 | } |
62 | 62 | ||
63 | impl From<FixtureEntry> for MockFileData { | 63 | impl From<Fixture> for MockFileData { |
64 | fn from(fixture: FixtureEntry) -> Self { | 64 | fn from(fixture: Fixture) -> Self { |
65 | Self::Fixture(fixture) | 65 | Self::Fixture(fixture) |
66 | } | 66 | } |
67 | } | 67 | } |
@@ -89,7 +89,7 @@ impl MockAnalysis { | |||
89 | /// ``` | 89 | /// ``` |
90 | pub fn with_files(fixture: &str) -> MockAnalysis { | 90 | pub fn with_files(fixture: &str) -> MockAnalysis { |
91 | let mut res = MockAnalysis::new(); | 91 | let mut res = MockAnalysis::new(); |
92 | for entry in parse_fixture(fixture) { | 92 | for entry in Fixture::parse(fixture) { |
93 | res.add_file_fixture(entry); | 93 | res.add_file_fixture(entry); |
94 | } | 94 | } |
95 | res | 95 | res |
@@ -100,7 +100,7 @@ impl MockAnalysis { | |||
100 | pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) { | 100 | pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) { |
101 | let mut position = None; | 101 | let mut position = None; |
102 | let mut res = MockAnalysis::new(); | 102 | let mut res = MockAnalysis::new(); |
103 | for entry in parse_fixture(fixture) { | 103 | for entry in Fixture::parse(fixture) { |
104 | if entry.text.contains(CURSOR_MARKER) { | 104 | if entry.text.contains(CURSOR_MARKER) { |
105 | assert!(position.is_none(), "only one marker (<|>) per fixture is allowed"); | 105 | assert!(position.is_none(), "only one marker (<|>) per fixture is allowed"); |
106 | position = Some(res.add_file_fixture_with_position(entry)); | 106 | position = Some(res.add_file_fixture_with_position(entry)); |
@@ -112,13 +112,13 @@ impl MockAnalysis { | |||
112 | (res, position) | 112 | (res, position) |
113 | } | 113 | } |
114 | 114 | ||
115 | pub fn add_file_fixture(&mut self, fixture: FixtureEntry) -> FileId { | 115 | pub fn add_file_fixture(&mut self, fixture: Fixture) -> FileId { |
116 | let file_id = self.next_id(); | 116 | let file_id = self.next_id(); |
117 | self.files.push(MockFileData::from(fixture)); | 117 | self.files.push(MockFileData::from(fixture)); |
118 | file_id | 118 | file_id |
119 | } | 119 | } |
120 | 120 | ||
121 | pub fn add_file_fixture_with_position(&mut self, mut fixture: FixtureEntry) -> FilePosition { | 121 | pub fn add_file_fixture_with_position(&mut self, mut fixture: Fixture) -> FilePosition { |
122 | let (offset, text) = extract_offset(&fixture.text); | 122 | let (offset, text) = extract_offset(&fixture.text); |
123 | fixture.text = text; | 123 | fixture.text = text; |
124 | let file_id = self.next_id(); | 124 | let file_id = self.next_id(); |
diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index 25d80806b..2a51bb559 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs | |||
@@ -3,7 +3,7 @@ use rustc_hash::FxHashMap; | |||
3 | use stdx::split1; | 3 | use stdx::split1; |
4 | 4 | ||
5 | #[derive(Debug, Eq, PartialEq)] | 5 | #[derive(Debug, Eq, PartialEq)] |
6 | pub struct FixtureEntry { | 6 | pub struct Fixture { |
7 | pub path: String, | 7 | pub path: String, |
8 | pub text: String, | 8 | pub text: String, |
9 | pub crate_name: Option<String>, | 9 | pub crate_name: Option<String>, |
@@ -13,19 +13,20 @@ pub struct FixtureEntry { | |||
13 | pub env: FxHashMap<String, String>, | 13 | pub env: FxHashMap<String, String>, |
14 | } | 14 | } |
15 | 15 | ||
16 | /// Parses text which looks like this: | 16 | impl Fixture { |
17 | /// | 17 | /// Parses text which looks like this: |
18 | /// ```not_rust | 18 | /// |
19 | /// //- some meta | 19 | /// ```not_rust |
20 | /// line 1 | 20 | /// //- some meta |
21 | /// line 2 | 21 | /// line 1 |
22 | /// // - other meta | 22 | /// line 2 |
23 | /// ``` | 23 | /// // - other meta |
24 | pub fn parse_fixture(ra_fixture: &str) -> Vec<FixtureEntry> { | 24 | /// ``` |
25 | let fixture = indent_first_line(ra_fixture); | 25 | pub fn parse(ra_fixture: &str) -> Vec<Fixture> { |
26 | let margin = fixture_margin(&fixture); | 26 | let fixture = indent_first_line(ra_fixture); |
27 | 27 | let margin = fixture_margin(&fixture); | |
28 | let mut lines = fixture | 28 | |
29 | let mut lines = fixture | ||
29 | .split('\n') // don't use `.lines` to not drop `\r\n` | 30 | .split('\n') // don't use `.lines` to not drop `\r\n` |
30 | .enumerate() | 31 | .enumerate() |
31 | .filter_map(|(ix, line)| { | 32 | .filter_map(|(ix, line)| { |
@@ -48,58 +49,59 @@ The offending line: {:?}"#, | |||
48 | } | 49 | } |
49 | }); | 50 | }); |
50 | 51 | ||
51 | let mut res: Vec<FixtureEntry> = Vec::new(); | 52 | let mut res: Vec<Fixture> = Vec::new(); |
52 | for line in lines.by_ref() { | 53 | for line in lines.by_ref() { |
53 | if line.starts_with("//-") { | 54 | if line.starts_with("//-") { |
54 | let meta = line["//-".len()..].trim().to_string(); | 55 | let meta = line["//-".len()..].trim().to_string(); |
55 | let meta = parse_meta(&meta); | 56 | let meta = Fixture::parse_single(&meta); |
56 | res.push(meta) | 57 | res.push(meta) |
57 | } else if let Some(entry) = res.last_mut() { | 58 | } else if let Some(entry) = res.last_mut() { |
58 | entry.text.push_str(line); | 59 | entry.text.push_str(line); |
59 | entry.text.push('\n'); | 60 | entry.text.push('\n'); |
61 | } | ||
60 | } | 62 | } |
63 | res | ||
61 | } | 64 | } |
62 | res | ||
63 | } | ||
64 | 65 | ||
65 | //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo | 66 | //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo |
66 | fn parse_meta(meta: &str) -> FixtureEntry { | 67 | fn parse_single(meta: &str) -> Fixture { |
67 | let components = meta.split_ascii_whitespace().collect::<Vec<_>>(); | 68 | let components = meta.split_ascii_whitespace().collect::<Vec<_>>(); |
68 | 69 | ||
69 | let path = components[0].to_string(); | 70 | let path = components[0].to_string(); |
70 | assert!(path.starts_with("/")); | 71 | assert!(path.starts_with("/")); |
71 | 72 | ||
72 | let mut krate = None; | 73 | let mut krate = None; |
73 | let mut deps = Vec::new(); | 74 | let mut deps = Vec::new(); |
74 | let mut edition = None; | 75 | let mut edition = None; |
75 | let mut cfg = CfgOptions::default(); | 76 | let mut cfg = CfgOptions::default(); |
76 | let mut env = FxHashMap::default(); | 77 | let mut env = FxHashMap::default(); |
77 | for component in components[1..].iter() { | 78 | for component in components[1..].iter() { |
78 | let (key, value) = split1(component, ':').unwrap(); | 79 | let (key, value) = split1(component, ':').unwrap(); |
79 | match key { | 80 | match key { |
80 | "crate" => krate = Some(value.to_string()), | 81 | "crate" => krate = Some(value.to_string()), |
81 | "deps" => deps = value.split(',').map(|it| it.to_string()).collect(), | 82 | "deps" => deps = value.split(',').map(|it| it.to_string()).collect(), |
82 | "edition" => edition = Some(value.to_string()), | 83 | "edition" => edition = Some(value.to_string()), |
83 | "cfg" => { | 84 | "cfg" => { |
84 | for key in value.split(',') { | 85 | for key in value.split(',') { |
85 | match split1(key, '=') { | 86 | match split1(key, '=') { |
86 | None => cfg.insert_atom(key.into()), | 87 | None => cfg.insert_atom(key.into()), |
87 | Some((k, v)) => cfg.insert_key_value(k.into(), v.into()), | 88 | Some((k, v)) => cfg.insert_key_value(k.into(), v.into()), |
89 | } | ||
88 | } | 90 | } |
89 | } | 91 | } |
90 | } | 92 | "env" => { |
91 | "env" => { | 93 | for key in value.split(',') { |
92 | for key in value.split(',') { | 94 | if let Some((k, v)) = split1(key, '=') { |
93 | if let Some((k, v)) = split1(key, '=') { | 95 | env.insert(k.into(), v.into()); |
94 | env.insert(k.into(), v.into()); | 96 | } |
95 | } | 97 | } |
96 | } | 98 | } |
99 | _ => panic!("bad component: {:?}", component), | ||
97 | } | 100 | } |
98 | _ => panic!("bad component: {:?}", component), | ||
99 | } | 101 | } |
100 | } | ||
101 | 102 | ||
102 | FixtureEntry { path, text: String::new(), crate_name: krate, deps, edition, cfg, env } | 103 | Fixture { path, text: String::new(), crate_name: krate, deps, edition, cfg, env } |
104 | } | ||
103 | } | 105 | } |
104 | 106 | ||
105 | /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. | 107 | /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. |
@@ -170,8 +172,8 @@ fn parse_fixture_can_handle_dedented_first_line() { | |||
170 | struct Bar; | 172 | struct Bar; |
171 | "; | 173 | "; |
172 | assert_eq!( | 174 | assert_eq!( |
173 | parse_fixture(fixture), | 175 | Fixture::parse(fixture), |
174 | parse_fixture( | 176 | Fixture::parse( |
175 | "//- /lib.rs | 177 | "//- /lib.rs |
176 | mod foo; | 178 | mod foo; |
177 | //- /foo.rs | 179 | //- /foo.rs |
@@ -183,7 +185,7 @@ struct Bar; | |||
183 | 185 | ||
184 | #[test] | 186 | #[test] |
185 | fn parse_fixture_gets_full_meta() { | 187 | fn parse_fixture_gets_full_meta() { |
186 | let parsed = parse_fixture( | 188 | let parsed = Fixture::parse( |
187 | r" | 189 | r" |
188 | //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo | 190 | //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo |
189 | mod m; | 191 | mod m; |
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index d44b2f9ab..316f3d501 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs | |||
@@ -22,7 +22,7 @@ pub use difference::Changeset as __Changeset; | |||
22 | pub use ra_cfg::CfgOptions; | 22 | pub use ra_cfg::CfgOptions; |
23 | pub use rustc_hash::FxHashMap; | 23 | pub use rustc_hash::FxHashMap; |
24 | 24 | ||
25 | pub use crate::fixture::{parse_fixture, FixtureEntry}; | 25 | pub use crate::fixture::Fixture; |
26 | 26 | ||
27 | pub const CURSOR_MARKER: &str = "<|>"; | 27 | pub const CURSOR_MARKER: &str = "<|>"; |
28 | 28 | ||