1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
//! Defines `Fixture` -- a convenient way to describe the initial state of
//! rust-analyzer database from a single string.
//!
//! Fixtures are strings containing rust source code with optional metadata.
//! A fixture without metadata is parsed into a single source file.
//! Use this to test functionality local to one file.
//!
//! Simple Example:
//! ```
//! r#"
//! fn main() {
//! println!("Hello World")
//! }
//! "#
//! ```
//!
//! Metadata can be added to a fixture after a `//-` comment.
//! The basic form is specifying filenames,
//! which is also how to define multiple files in a single test fixture
//!
//! Example using two files in the same crate:
//! ```
//! "
//! //- /main.rs
//! mod foo;
//! fn main() {
//! foo::bar();
//! }
//!
//! //- /foo.rs
//! pub fn bar() {}
//! "
//! ```
//!
//! Example using two crates with one file each, with one crate depending on the other:
//! ```
//! r#"
//! //- /main.rs crate:a deps:b
//! fn main() {
//! b::foo();
//! }
//! //- /lib.rs crate:b
//! pub fn b() {
//! println!("Hello World")
//! }
//! "#
//! ```
//!
//! Metadata allows specifying all settings and variables
//! that are available in a real rust project:
//! - crate names via `crate:cratename`
//! - dependencies via `deps:dep1,dep2`
//! - configuration settings via `cfg:dbg=false,opt_level=2`
//! - environment variables via `env:PATH=/bin,RUST_LOG=debug`
//!
//! Example using all available metadata:
//! ```
//! "
//! //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
//! fn insert_source_code_here() {}
//! "
//! ```
use rustc_hash::FxHashMap;
use stdx::trim_indent;
#[derive(Debug, Eq, PartialEq)]
pub struct Fixture {
pub path: String,
pub text: String,
pub krate: Option<String>,
pub deps: Vec<String>,
pub cfg_atoms: Vec<String>,
pub cfg_key_values: Vec<(String, String)>,
pub edition: Option<String>,
pub env: FxHashMap<String, String>,
pub introduce_new_source_root: bool,
}
impl Fixture {
/// Parses text which looks like this:
///
/// ```not_rust
/// //- some meta
/// line 1
/// line 2
/// //- other meta
/// ```
pub fn parse(ra_fixture: &str) -> Vec<Fixture> {
let fixture = trim_indent(ra_fixture);
let mut res: Vec<Fixture> = Vec::new();
let default = if ra_fixture.contains("//-") { None } else { Some("//- /main.rs") };
for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() {
if line.contains("//-") {
assert!(
line.starts_with("//-"),
"Metadata line {} has invalid indentation. \
All metadata lines need to have the same indentation.\n\
The offending line: {:?}",
ix,
line
);
}
if line.starts_with("//-") {
let meta = Fixture::parse_meta_line(line);
res.push(meta)
} else if let Some(entry) = res.last_mut() {
entry.text.push_str(line);
}
}
res
}
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
fn parse_meta_line(meta: &str) -> Fixture {
assert!(meta.starts_with("//-"));
let meta = meta["//-".len()..].trim();
let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
let path = components[0].to_string();
assert!(path.starts_with('/'));
let mut krate = None;
let mut deps = Vec::new();
let mut edition = None;
let mut cfg_atoms = Vec::new();
let mut cfg_key_values = Vec::new();
let mut env = FxHashMap::default();
let mut introduce_new_source_root = false;
for component in components[1..].iter() {
let (key, value) = component.split_once(':').unwrap();
match key {
"crate" => krate = Some(value.to_string()),
"deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
"edition" => edition = Some(value.to_string()),
"cfg" => {
for entry in value.split(',') {
match entry.split_once('=') {
Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())),
None => cfg_atoms.push(entry.to_string()),
}
}
}
"env" => {
for key in value.split(',') {
if let Some((k, v)) = key.split_once('=') {
env.insert(k.into(), v.into());
}
}
}
"new_source_root" => introduce_new_source_root = true,
_ => panic!("bad component: {:?}", component),
}
}
Fixture {
path,
text: String::new(),
krate,
deps,
cfg_atoms,
cfg_key_values,
edition,
env,
introduce_new_source_root,
}
}
}
#[test]
#[should_panic]
fn parse_fixture_checks_further_indented_metadata() {
Fixture::parse(
r"
//- /lib.rs
mod bar;
fn foo() {}
//- /bar.rs
pub fn baz() {}
",
);
}
#[test]
fn parse_fixture_gets_full_meta() {
let parsed = Fixture::parse(
r"
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
mod m;
",
);
assert_eq!(1, parsed.len());
let meta = &parsed[0];
assert_eq!("mod m;\n", meta.text);
assert_eq!("foo", meta.krate.as_ref().unwrap());
assert_eq!("/lib.rs", meta.path);
assert_eq!(2, meta.env.len());
}
|