aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_db/src/fixture.rs70
-rw-r--r--crates/test_utils/src/fixture.rs13
-rw-r--r--crates/test_utils/src/lib.rs2
3 files changed, 14 insertions, 71 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs
index d65536bbc..f786fb87f 100644
--- a/crates/ra_db/src/fixture.rs
+++ b/crates/ra_db/src/fixture.rs
@@ -61,9 +61,7 @@ use std::{str::FromStr, sync::Arc};
61 61
62use ra_cfg::CfgOptions; 62use ra_cfg::CfgOptions;
63use rustc_hash::FxHashMap; 63use rustc_hash::FxHashMap;
64use test_utils::{ 64use test_utils::{extract_offset, parse_fixture, FixtureEntry, CURSOR_MARKER};
65 extract_offset, parse_fixture, parse_single_fixture, FixtureEntry, CURSOR_MARKER,
66};
67use vfs::{file_set::FileSet, VfsPath}; 65use vfs::{file_set::FileSet, VfsPath};
68 66
69use crate::{ 67use crate::{
@@ -76,20 +74,21 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0);
76pub trait WithFixture: Default + SourceDatabaseExt + 'static { 74pub trait WithFixture: Default + SourceDatabaseExt + 'static {
77 fn with_single_file(text: &str) -> (Self, FileId) { 75 fn with_single_file(text: &str) -> (Self, FileId) {
78 let mut db = Self::default(); 76 let mut db = Self::default();
79 let file_id = with_single_file(&mut db, text); 77 let (_, files) = with_files(&mut db, text);
80 (db, file_id) 78 assert!(files.len() == 1);
79 (db, files[0])
81 } 80 }
82 81
83 fn with_files(ra_fixture: &str) -> Self { 82 fn with_files(ra_fixture: &str) -> Self {
84 let mut db = Self::default(); 83 let mut db = Self::default();
85 let pos = with_files(&mut db, ra_fixture); 84 let (pos, _) = with_files(&mut db, ra_fixture);
86 assert!(pos.is_none()); 85 assert!(pos.is_none());
87 db 86 db
88 } 87 }
89 88
90 fn with_position(ra_fixture: &str) -> (Self, FilePosition) { 89 fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
91 let mut db = Self::default(); 90 let mut db = Self::default();
92 let pos = with_files(&mut db, ra_fixture); 91 let (pos, _) = with_files(&mut db, ra_fixture);
93 (db, pos.unwrap()) 92 (db, pos.unwrap())
94 } 93 }
95 94
@@ -104,54 +103,11 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
104 103
105impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} 104impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
106 105
107fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId { 106fn with_files(
108 let file_id = FileId(0); 107 db: &mut dyn SourceDatabaseExt,
109 let mut file_set = vfs::file_set::FileSet::default(); 108 fixture: &str,
110 file_set.insert(file_id, vfs::VfsPath::new_virtual_path("/main.rs".to_string())); 109) -> (Option<FilePosition>, Vec<FileId>) {
111 110 let mut files = Vec::new();
112 let source_root = SourceRoot::new_local(file_set);
113
114 let fixture = parse_single_fixture(ra_fixture);
115
116 let crate_graph = if let Some(entry) = fixture {
117 let meta = match ParsedMeta::from(&entry) {
118 ParsedMeta::File(it) => it,
119 };
120
121 let mut crate_graph = CrateGraph::default();
122 crate_graph.add_crate_root(
123 file_id,
124 meta.edition,
125 meta.krate.map(|name| {
126 CrateName::new(&name).expect("Fixture crate name should not contain dashes")
127 }),
128 meta.cfg,
129 meta.env,
130 Default::default(),
131 );
132 crate_graph
133 } else {
134 let mut crate_graph = CrateGraph::default();
135 crate_graph.add_crate_root(
136 file_id,
137 Edition::Edition2018,
138 None,
139 CfgOptions::default(),
140 Env::default(),
141 Default::default(),
142 );
143 crate_graph
144 };
145
146 db.set_file_text(file_id, Arc::new(ra_fixture.to_string()));
147 db.set_file_source_root(file_id, WORKSPACE);
148 db.set_source_root(WORKSPACE, Arc::new(source_root));
149 db.set_crate_graph(Arc::new(crate_graph));
150
151 file_id
152}
153
154fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosition> {
155 let fixture = parse_fixture(fixture); 111 let fixture = parse_fixture(fixture);
156 112
157 let mut crate_graph = CrateGraph::default(); 113 let mut crate_graph = CrateGraph::default();
@@ -204,7 +160,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
204 db.set_file_source_root(file_id, source_root_id); 160 db.set_file_source_root(file_id, source_root_id);
205 let path = VfsPath::new_virtual_path(meta.path); 161 let path = VfsPath::new_virtual_path(meta.path);
206 file_set.insert(file_id, path.into()); 162 file_set.insert(file_id, path.into());
207 163 files.push(file_id);
208 file_id.0 += 1; 164 file_id.0 += 1;
209 } 165 }
210 166
@@ -229,7 +185,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
229 db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set))); 185 db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set)));
230 db.set_crate_graph(Arc::new(crate_graph)); 186 db.set_crate_graph(Arc::new(crate_graph));
231 187
232 file_position 188 (file_position, files)
233} 189}
234 190
235enum ParsedMeta { 191enum ParsedMeta {
diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs
index bda826d50..25d80806b 100644
--- a/crates/test_utils/src/fixture.rs
+++ b/crates/test_utils/src/fixture.rs
@@ -13,19 +13,6 @@ pub struct FixtureEntry {
13 pub env: FxHashMap<String, String>, 13 pub env: FxHashMap<String, String>,
14} 14}
15 15
16/// Same as `parse_fixture`, except it allow empty fixture
17pub fn parse_single_fixture(ra_fixture: &str) -> Option<FixtureEntry> {
18 if !ra_fixture.lines().any(|it| it.trim_start().starts_with("//-")) {
19 return None;
20 }
21
22 let fixtures = parse_fixture(ra_fixture);
23 if fixtures.len() > 1 {
24 panic!("too many fixtures");
25 }
26 fixtures.into_iter().nth(0)
27}
28
29/// Parses text which looks like this: 16/// Parses text which looks like this:
30/// 17///
31/// ```not_rust 18/// ```not_rust
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index 0fdd1a36b..d44b2f9ab 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;
22pub use ra_cfg::CfgOptions; 22pub use ra_cfg::CfgOptions;
23pub use rustc_hash::FxHashMap; 23pub use rustc_hash::FxHashMap;
24 24
25pub use crate::fixture::{parse_fixture, parse_single_fixture, FixtureEntry}; 25pub use crate::fixture::{parse_fixture, FixtureEntry};
26 26
27pub const CURSOR_MARKER: &str = "<|>"; 27pub const CURSOR_MARKER: &str = "<|>";
28 28