1 //! Fixtures are strings containing rust source code with optional metadata.
2 //! A fixture without metadata is parsed into a single source file.
3 //! Use this to test functionality local to one file.
9 //! println!("Hello World")
14 //! Metadata can be added to a fixture after a `//-` comment.
15 //! The basic form is specifying filenames,
16 //! which is also how to define multiple files in a single test fixture
18 //! Example using two files in the same crate:
32 //! Example using two crates with one file each, with one crate depending on the other:
35 //! //- /main.rs crate:a deps:b
39 //! //- /lib.rs crate:b
41 //! println!("Hello World")
46 //! Metadata allows specifying all settings and variables
47 //! that are available in a real rust project:
48 //! - crate names via `crate:cratename`
49 //! - dependencies via `deps:dep1,dep2`
50 //! - configuration settings via `cfg:dbg=false,opt_level=2`
51 //! - environment variables via `env:PATH=/bin,RUST_LOG=debug`
53 //! Example using all available metadata:
56 //! //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
57 //! fn insert_source_code_here() {}
60 use std::{str::FromStr, sync::Arc};
63 use rustc_hash::FxHashMap;
65 extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER, ESCAPED_CURSOR_MARKER,
67 use vfs::{file_set::FileSet, VfsPath};
70 input::CrateName, Change, CrateGraph, CrateId, Edition, Env, FileId, FilePosition,
71 SourceDatabaseExt, SourceRoot, SourceRootId,
74 pub const WORKSPACE: SourceRootId = SourceRootId(0);
76 pub trait WithFixture: Default + SourceDatabaseExt + 'static {
77 fn with_single_file(text: &str) -> (Self, FileId) {
78 let fixture = ChangeFixture::parse(text);
79 let mut db = Self::default();
80 fixture.change.apply(&mut db);
81 assert_eq!(fixture.files.len(), 1);
82 (db, fixture.files[0])
85 fn with_files(ra_fixture: &str) -> Self {
86 let fixture = ChangeFixture::parse(ra_fixture);
87 let mut db = Self::default();
88 fixture.change.apply(&mut db);
89 assert!(fixture.file_position.is_none());
93 fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
94 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
95 let offset = match range_or_offset {
96 RangeOrOffset::Range(_) => panic!(),
97 RangeOrOffset::Offset(it) => it,
99 (db, FilePosition { file_id, offset })
102 fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) {
103 let fixture = ChangeFixture::parse(ra_fixture);
104 let mut db = Self::default();
105 fixture.change.apply(&mut db);
106 let (file_id, range_or_offset) = fixture.file_position.unwrap();
107 (db, file_id, range_or_offset)
110 fn test_crate(&self) -> CrateId {
111 let crate_graph = self.crate_graph();
112 let mut it = crate_graph.iter();
113 let res = it.next().unwrap();
114 assert!(it.next().is_none());
119 impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
121 pub struct ChangeFixture {
122 pub file_position: Option<(FileId, RangeOrOffset)>,
123 pub files: Vec<FileId>,
128 pub fn parse(ra_fixture: &str) -> ChangeFixture {
129 let fixture = Fixture::parse(ra_fixture);
130 let mut change = Change::new();
132 let mut files = Vec::new();
133 let mut crate_graph = CrateGraph::default();
134 let mut crates = FxHashMap::default();
135 let mut crate_deps = Vec::new();
136 let mut default_crate_root: Option<FileId> = None;
137 let mut default_cfg = CfgOptions::default();
139 let mut file_set = FileSet::default();
140 let source_root_prefix = "/".to_string();
141 let mut file_id = FileId(0);
143 let mut file_position = None;
145 for entry in fixture {
146 let text = if entry.text.contains(CURSOR_MARKER) {
147 if entry.text.contains(ESCAPED_CURSOR_MARKER) {
148 entry.text.replace(ESCAPED_CURSOR_MARKER, CURSOR_MARKER)
150 let (range_or_offset, text) = extract_range_or_offset(&entry.text);
151 assert!(file_position.is_none());
152 file_position = Some((file_id, range_or_offset));
159 let meta = FileMeta::from(entry);
160 assert!(meta.path.starts_with(&source_root_prefix));
162 if let Some(krate) = meta.krate {
163 let crate_name = CrateName::normalize_dashes(&krate);
164 let crate_id = crate_graph.add_crate_root(
167 Some(crate_name.clone().into()),
172 let prev = crates.insert(crate_name.clone(), crate_id);
173 assert!(prev.is_none());
174 for dep in meta.deps {
175 let dep = CrateName::normalize_dashes(&dep);
176 crate_deps.push((crate_name.clone(), dep))
178 } else if meta.path == "/main.rs" || meta.path == "/lib.rs" {
179 assert!(default_crate_root.is_none());
180 default_crate_root = Some(file_id);
181 default_cfg = meta.cfg;
184 change.change_file(file_id, Some(Arc::new(text)));
185 let path = VfsPath::new_virtual_path(meta.path);
186 file_set.insert(file_id, path.into());
191 if crates.is_empty() {
192 let crate_root = default_crate_root.unwrap();
193 crate_graph.add_crate_root(
195 Edition::Edition2018,
196 Some(CrateName::new("test").unwrap().into()),
202 for (from, to) in crate_deps {
203 let from_id = crates[&from];
204 let to_id = crates[&to];
205 crate_graph.add_dep(from_id, CrateName::new(&to).unwrap(), to_id).unwrap();
209 change.set_roots(vec![SourceRoot::new_local(file_set)]);
210 change.set_crate_graph(crate_graph);
212 ChangeFixture { file_position, files, change }
218 krate: Option<String>,
225 impl From<Fixture> for FileMeta {
226 fn from(f: Fixture) -> FileMeta {
227 let mut cfg = CfgOptions::default();
228 f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
229 f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
239 .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
240 env: f.env.into_iter().collect(),