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;
64 use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER};
65 use vfs::{file_set::FileSet, VfsPath};
68 input::CrateName, Change, CrateGraph, CrateId, Edition, Env, FileId, FilePosition,
69 SourceDatabaseExt, SourceRoot, SourceRootId,
72 pub const WORKSPACE: SourceRootId = SourceRootId(0);
74 pub trait WithFixture: Default + SourceDatabaseExt + 'static {
75 fn with_single_file(text: &str) -> (Self, FileId) {
76 let fixture = ChangeFixture::parse(text);
77 let mut db = Self::default();
78 fixture.change.apply(&mut db);
79 assert_eq!(fixture.files.len(), 1);
80 (db, fixture.files[0])
83 fn with_files(ra_fixture: &str) -> Self {
84 let fixture = ChangeFixture::parse(ra_fixture);
85 let mut db = Self::default();
86 fixture.change.apply(&mut db);
87 assert!(fixture.file_position.is_none());
91 fn with_position(ra_fixture: &str) -> (Self, FilePosition) {
92 let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture);
93 let offset = match range_or_offset {
94 RangeOrOffset::Range(_) => panic!(),
95 RangeOrOffset::Offset(it) => it,
97 (db, FilePosition { file_id, offset })
100 fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) {
101 let fixture = ChangeFixture::parse(ra_fixture);
102 let mut db = Self::default();
103 fixture.change.apply(&mut db);
104 let (file_id, range_or_offset) = fixture.file_position.unwrap();
105 (db, file_id, range_or_offset)
108 fn test_crate(&self) -> CrateId {
109 let crate_graph = self.crate_graph();
110 let mut it = crate_graph.iter();
111 let res = it.next().unwrap();
112 assert!(it.next().is_none());
117 impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
119 pub struct ChangeFixture {
120 pub file_position: Option<(FileId, RangeOrOffset)>,
121 pub files: Vec<FileId>,
126 pub fn parse(ra_fixture: &str) -> ChangeFixture {
127 let fixture = Fixture::parse(ra_fixture);
128 let mut change = Change::new();
130 let mut files = Vec::new();
131 let mut crate_graph = CrateGraph::default();
132 let mut crates = FxHashMap::default();
133 let mut crate_deps = Vec::new();
134 let mut default_crate_root: Option<FileId> = None;
135 let mut default_cfg = CfgOptions::default();
137 let mut file_set = FileSet::default();
138 let source_root_prefix = "/".to_string();
139 let mut file_id = FileId(0);
141 let mut file_position = None;
143 for entry in fixture {
144 let text = if entry.text.contains(CURSOR_MARKER) {
145 let (range_or_offset, text) = extract_range_or_offset(&entry.text);
146 assert!(file_position.is_none());
147 file_position = Some((file_id, range_or_offset));
153 let meta = FileMeta::from(entry);
154 assert!(meta.path.starts_with(&source_root_prefix));
156 if let Some(krate) = meta.krate {
157 let crate_name = CrateName::normalize_dashes(&krate);
158 let crate_id = crate_graph.add_crate_root(
161 Some(crate_name.clone().into()),
166 let prev = crates.insert(crate_name.clone(), crate_id);
167 assert!(prev.is_none());
168 for dep in meta.deps {
169 let dep = CrateName::normalize_dashes(&dep);
170 crate_deps.push((crate_name.clone(), dep))
172 } else if meta.path == "/main.rs" || meta.path == "/lib.rs" {
173 assert!(default_crate_root.is_none());
174 default_crate_root = Some(file_id);
175 default_cfg = meta.cfg;
178 change.change_file(file_id, Some(Arc::new(text)));
179 let path = VfsPath::new_virtual_path(meta.path);
180 file_set.insert(file_id, path.into());
185 if crates.is_empty() {
186 let crate_root = default_crate_root.unwrap();
187 crate_graph.add_crate_root(
189 Edition::Edition2018,
190 Some(CrateName::new("test").unwrap().into()),
196 for (from, to) in crate_deps {
197 let from_id = crates[&from];
198 let to_id = crates[&to];
199 crate_graph.add_dep(from_id, CrateName::new(&to).unwrap(), to_id).unwrap();
203 change.set_roots(vec![SourceRoot::new_local(file_set)]);
204 change.set_crate_graph(crate_graph);
206 ChangeFixture { file_position, files, change }
212 krate: Option<String>,
219 impl From<Fixture> for FileMeta {
220 fn from(f: Fixture) -> FileMeta {
221 let mut cfg = CfgOptions::default();
222 f.cfg_atoms.iter().for_each(|it| cfg.insert_atom(it.into()));
223 f.cfg_key_values.iter().for_each(|(k, v)| cfg.insert_key_value(k.into(), v.into()));
233 .map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
234 env: f.env.into_iter().collect(),