diff options
author | Aleksey Kladov <[email protected]> | 2021-06-15 19:02:40 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-06-15 19:03:08 +0100 |
commit | f521e4185323699cd5d063b2704367a319583982 (patch) | |
tree | 249c358780bebe049e01f0003caf8e15eb3c4a26 /crates/test_utils/src/fixture.rs | |
parent | 3f5eead9e3b32f7e88562337c95f76ea75c82486 (diff) |
internal: introduce minicore -- a subset of libcore for testing
Diffstat (limited to 'crates/test_utils/src/fixture.rs')
-rw-r--r-- | crates/test_utils/src/fixture.rs | 157 |
1 files changed, 148 insertions, 9 deletions
diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index d0bddf7d8..535892f3f 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs | |||
@@ -77,6 +77,11 @@ pub struct Fixture { | |||
77 | pub introduce_new_source_root: bool, | 77 | pub introduce_new_source_root: bool, |
78 | } | 78 | } |
79 | 79 | ||
80 | pub struct MiniCore { | ||
81 | activated_flags: Vec<String>, | ||
82 | valid_flags: Vec<String>, | ||
83 | } | ||
84 | |||
80 | impl Fixture { | 85 | impl Fixture { |
81 | /// Parses text which looks like this: | 86 | /// Parses text which looks like this: |
82 | /// | 87 | /// |
@@ -86,12 +91,28 @@ impl Fixture { | |||
86 | /// line 2 | 91 | /// line 2 |
87 | /// //- other meta | 92 | /// //- other meta |
88 | /// ``` | 93 | /// ``` |
89 | pub fn parse(ra_fixture: &str) -> Vec<Fixture> { | 94 | /// |
95 | /// Fixture can also start with a minicore declaration: | ||
96 | /// | ||
97 | /// ``` | ||
98 | /// //- minicore: sized | ||
99 | /// ``` | ||
100 | /// | ||
101 | /// That will include a subset of `libcore` into the fixture, see | ||
102 | /// `minicore.rs` for what's available. | ||
103 | pub fn parse(ra_fixture: &str) -> (Option<MiniCore>, Vec<Fixture>) { | ||
90 | let fixture = trim_indent(ra_fixture); | 104 | let fixture = trim_indent(ra_fixture); |
91 | 105 | let mut fixture = fixture.as_str(); | |
106 | let mut mini_core = None; | ||
92 | let mut res: Vec<Fixture> = Vec::new(); | 107 | let mut res: Vec<Fixture> = Vec::new(); |
93 | 108 | ||
94 | let default = if ra_fixture.contains("//-") { None } else { Some("//- /main.rs") }; | 109 | if fixture.starts_with("//- minicore:") { |
110 | let first_line = fixture.split_inclusive('\n').next().unwrap(); | ||
111 | mini_core = Some(MiniCore::parse(first_line)); | ||
112 | fixture = &fixture[first_line.len()..]; | ||
113 | } | ||
114 | |||
115 | let default = if fixture.contains("//-") { None } else { Some("//- /main.rs") }; | ||
95 | 116 | ||
96 | for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() { | 117 | for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() { |
97 | if line.contains("//-") { | 118 | if line.contains("//-") { |
@@ -113,7 +134,7 @@ impl Fixture { | |||
113 | } | 134 | } |
114 | } | 135 | } |
115 | 136 | ||
116 | res | 137 | (mini_core, res) |
117 | } | 138 | } |
118 | 139 | ||
119 | //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo | 140 | //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo |
@@ -172,6 +193,122 @@ impl Fixture { | |||
172 | } | 193 | } |
173 | } | 194 | } |
174 | 195 | ||
196 | impl MiniCore { | ||
197 | fn has_flag(&self, flag: &str) -> bool { | ||
198 | self.activated_flags.iter().any(|it| it == flag) | ||
199 | } | ||
200 | |||
201 | fn assert_valid_flag(&self, flag: &str) { | ||
202 | if !self.valid_flags.iter().any(|it| it == flag) { | ||
203 | panic!("invalid flag: {:?}, valid flags: {:?}", flag, self.valid_flags); | ||
204 | } | ||
205 | } | ||
206 | |||
207 | fn parse(line: &str) -> MiniCore { | ||
208 | let mut res = MiniCore { activated_flags: Vec::new(), valid_flags: Vec::new() }; | ||
209 | |||
210 | let line = line.strip_prefix("//- minicore:").unwrap().trim(); | ||
211 | for entry in line.split(", ") { | ||
212 | if res.has_flag(entry) { | ||
213 | panic!("duplicate minicore flag: {:?}", entry) | ||
214 | } | ||
215 | res.activated_flags.push(entry.to_string()) | ||
216 | } | ||
217 | |||
218 | res | ||
219 | } | ||
220 | |||
221 | /// Strips parts of minicore.rs which are flagged by inactive flags. | ||
222 | /// | ||
223 | /// This is probably over-engineered to support flags dependencies. | ||
224 | pub fn source_code(mut self) -> String { | ||
225 | let mut buf = String::new(); | ||
226 | let raw_mini_core = include_str!("./minicore.rs"); | ||
227 | let mut lines = raw_mini_core.split_inclusive('\n'); | ||
228 | |||
229 | let mut parsing_flags = false; | ||
230 | let mut implications = Vec::new(); | ||
231 | |||
232 | // Parse `//!` preamble and extract flags and dependencies. | ||
233 | for line in lines.by_ref() { | ||
234 | let line = match line.strip_prefix("//!") { | ||
235 | Some(it) => it, | ||
236 | None => { | ||
237 | assert!(line.trim().is_empty()); | ||
238 | break; | ||
239 | } | ||
240 | }; | ||
241 | |||
242 | if parsing_flags { | ||
243 | let (flag, deps) = line.split_once(':').unwrap(); | ||
244 | let flag = flag.trim(); | ||
245 | self.valid_flags.push(flag.to_string()); | ||
246 | for dep in deps.split(", ") { | ||
247 | let dep = dep.trim(); | ||
248 | if !dep.is_empty() { | ||
249 | self.assert_valid_flag(dep); | ||
250 | implications.push((flag, dep)); | ||
251 | } | ||
252 | } | ||
253 | } | ||
254 | |||
255 | if line.contains("Available flags:") { | ||
256 | parsing_flags = true; | ||
257 | } | ||
258 | } | ||
259 | |||
260 | for flag in &self.activated_flags { | ||
261 | self.assert_valid_flag(flag); | ||
262 | } | ||
263 | |||
264 | // Fixed point loop to compute transitive closure of flags. | ||
265 | loop { | ||
266 | let mut changed = false; | ||
267 | for &(u, v) in implications.iter() { | ||
268 | if self.has_flag(u) && !self.has_flag(v) { | ||
269 | self.activated_flags.push(v.to_string()); | ||
270 | changed = true; | ||
271 | } | ||
272 | } | ||
273 | if !changed { | ||
274 | break; | ||
275 | } | ||
276 | } | ||
277 | |||
278 | let mut curr_region = ""; | ||
279 | for line in lines { | ||
280 | let trimmed = line.trim(); | ||
281 | if let Some(region) = trimmed.strip_prefix("// region:") { | ||
282 | assert_eq!(curr_region, ""); | ||
283 | curr_region = region; | ||
284 | continue; | ||
285 | } | ||
286 | if let Some(region) = trimmed.strip_prefix("// endregion:") { | ||
287 | assert_eq!(curr_region, region); | ||
288 | curr_region = ""; | ||
289 | continue; | ||
290 | } | ||
291 | |||
292 | let mut flag = curr_region; | ||
293 | if let Some(idx) = trimmed.find("// :") { | ||
294 | flag = &trimmed[idx + "// :".len()..]; | ||
295 | } | ||
296 | |||
297 | let skip = if flag == "" { | ||
298 | false | ||
299 | } else { | ||
300 | self.assert_valid_flag(flag); | ||
301 | !self.has_flag(flag) | ||
302 | }; | ||
303 | |||
304 | if !skip { | ||
305 | buf.push_str(line) | ||
306 | } | ||
307 | } | ||
308 | buf | ||
309 | } | ||
310 | } | ||
311 | |||
175 | #[test] | 312 | #[test] |
176 | #[should_panic] | 313 | #[should_panic] |
177 | fn parse_fixture_checks_further_indented_metadata() { | 314 | fn parse_fixture_checks_further_indented_metadata() { |
@@ -189,12 +326,14 @@ fn parse_fixture_checks_further_indented_metadata() { | |||
189 | 326 | ||
190 | #[test] | 327 | #[test] |
191 | fn parse_fixture_gets_full_meta() { | 328 | fn parse_fixture_gets_full_meta() { |
192 | let parsed = Fixture::parse( | 329 | let (mini_core, parsed) = Fixture::parse( |
193 | r" | 330 | r#" |
194 | //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo | 331 | //- minicore: coerce_unsized |
195 | mod m; | 332 | //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo |
196 | ", | 333 | mod m; |
334 | "#, | ||
197 | ); | 335 | ); |
336 | assert_eq!(mini_core.unwrap().activated_flags, vec!["coerce_unsized".to_string()]); | ||
198 | assert_eq!(1, parsed.len()); | 337 | assert_eq!(1, parsed.len()); |
199 | 338 | ||
200 | let meta = &parsed[0]; | 339 | let meta = &parsed[0]; |