1 //! A set of high-level utility fixture methods to use in tests.
2 use std::{mem, str::FromStr, sync::Arc};
5 use rustc_hash::FxHashMap;
7 extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER, ESCAPED_CURSOR_MARKER,
9 use vfs::{file_set::FileSet, VfsPath};
12 input::CrateName, Change, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, FileRange,
13 SourceDatabaseExt, SourceRoot, SourceRootId,
16 pub const WORKSPACE: SourceRootId = SourceRootId(0);
18 pub trait WithFixture: Default + SourceDatabaseExt + 'static {
19 fn with_single_file(text: &str) -> (Self, FileId) {
20 let fixture = ChangeFixture::parse(text);
21 let mut db = Self::default();
22 fixture.change.apply(&mut db);
23 assert_eq!(fixture.files.len(), 1);
24 (db, fixture.files[0])
27 fn with_files(ra_fixture: &str) -> Self {
28 let fixture = ChangeFixture::parse(ra_fixture);
29 let mut db = Self::default();
30 fixture.change.apply(&mut db);
31 assert!(fixture.file_position.is_none());
35 fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
36 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
37 let offset = range_or_offset.expect_offset();
38 (db, FilePosition { file_id, offset })
41 fn with_range(ra_fixture: &str) -> (Self, FileRange) {
42 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
43 let range = range_or_offset.expect_range();
44 (db, FileRange { file_id, range })
47 fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) {
48 let fixture = ChangeFixture::parse(ra_fixture);
49 let mut db = Self::default();
50 fixture.change.apply(&mut db);
51 let (file_id, range_or_offset) = fixture
53 .expect("Could not find file position in fixture. Did you forget to add an `$0`?");
54 (db, file_id, range_or_offset)
57 fn test_crate(&self) -> CrateId {
58 let crate_graph = self.crate_graph();
59 let mut it = crate_graph.iter();
60 let res = it.next().unwrap();
61 assert!(it.next().is_none());
66 impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
68 pub struct ChangeFixture {
69 pub file_position: Option<(FileId, RangeOrOffset)>,
70 pub files: Vec<FileId>,
75 pub fn parse(ra_fixture: &str) -> ChangeFixture {
76 let fixture = Fixture::parse(ra_fixture);
77 let mut change = Change::new();
79 let mut files = Vec::new();
80 let mut crate_graph = CrateGraph::default();
81 let mut crates = FxHashMap::default();
82 let mut crate_deps = Vec::new();
83 let mut default_crate_root: Option<FileId> = None;
84 let mut default_cfg = CfgOptions::default();
86 let mut file_set = FileSet::default();
87 let source_root_prefix = "/".to_string();
88 let mut file_id = FileId(0);
89 let mut roots = Vec::new();
91 let mut file_position = None;
93 for entry in fixture {
94 let text = if entry.text.contains(CURSOR_MARKER) {
95 if entry.text.contains(ESCAPED_CURSOR_MARKER) {
96 entry.text.replace(ESCAPED_CURSOR_MARKER, CURSOR_MARKER)
98 let (range_or_offset, text) = extract_range_or_offset(&entry.text);
99 assert!(file_position.is_none());
100 file_position = Some((file_id, range_or_offset));
107 let meta = FileMeta::from(entry);
108 assert!(meta.path.starts_with(&source_root_prefix));
110 if meta.introduce_new_source_root {
111 roots.push(SourceRoot::new_local(mem::take(&mut file_set)));
114 if let Some(krate) = meta.krate {
115 let crate_name = CrateName::normalize_dashes(&krate);
116 let crate_id = crate_graph.add_crate_root(
119 Some(crate_name.clone().into()),
124 let prev = crates.insert(crate_name.clone(), crate_id);
125 assert!(prev.is_none());
126 for dep in meta.deps {
127 let dep = CrateName::normalize_dashes(&dep);
128 crate_deps.push((crate_name.clone(), dep))
130 } else if meta.path == "/main.rs" || meta.path == "/lib.rs" {
131 assert!(default_crate_root.is_none());
132 default_crate_root = Some(file_id);
133 default_cfg = meta.cfg;
136 change.change_file(file_id, Some(Arc::new(text)));
137 let path = VfsPath::new_virtual_path(meta.path);
138 file_set.insert(file_id, path);
143 if crates.is_empty() {
144 let crate_root = default_crate_root.unwrap();
145 crate_graph.add_crate_root(
147 Edition::Edition2018,
148 Some(CrateName::new("test").unwrap().into()),
154 for (from, to) in crate_deps {
155 let from_id = crates[&from];
156 let to_id = crates[&to];
157 crate_graph.add_dep(from_id, CrateName::new(&to).unwrap(), to_id).unwrap();
161 roots.push(SourceRoot::new_local(mem::take(&mut file_set)));
162 change.set_roots(roots);
163 change.set_crate_graph(crate_graph);
165 ChangeFixture { file_position, files, change }
171 krate: Option<String>,
176 introduce_new_source_root: bool,
179 impl From<Fixture> for FileMeta {
180 fn from(f: Fixture) -> FileMeta {
181 let mut cfg = CfgOptions::default();
182 f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
183 f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
193 .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
194 env: f.env.into_iter().collect(),
195 introduce_new_source_root: f.introduce_new_source_root,