-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #272 from baszalmstra/feature/fixtures
feature: adds fixtures to support multiple files from string
- Loading branch information
Showing
11 changed files
with
297 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#![cfg(test)] | ||
|
||
use crate::{FileId, SourceDatabase, SourceRoot, SourceRootId}; | ||
pub use mun_test::Fixture; | ||
use std::convert::TryInto; | ||
use std::sync::Arc; | ||
|
||
impl<DB: SourceDatabase + Default + 'static> WithFixture for DB {} | ||
|
||
/// Enables the creation of an instance from a [`Fixture`] | ||
pub trait WithFixture: Default + SourceDatabase + 'static { | ||
/// Constructs an instance from a fixture | ||
fn with_files(fixture: impl AsRef<str>) -> Self { | ||
let mut db = Self::default(); | ||
with_files(&mut db, fixture.as_ref()); | ||
db | ||
} | ||
|
||
/// Constructs an instance from a fixture | ||
fn with_single_file(text: impl AsRef<str>) -> (Self, FileId) { | ||
let mut db = Self::default(); | ||
let files = with_files(&mut db, text.as_ref()); | ||
assert_eq!(files.len(), 1); | ||
(db, files[0]) | ||
} | ||
} | ||
|
||
/// Fills the specified database with all the files from the specified `fixture` | ||
fn with_files(db: &mut dyn SourceDatabase, fixture: &str) -> Vec<FileId> { | ||
let fixture = Fixture::parse(fixture); | ||
|
||
let mut source_root = SourceRoot::default(); | ||
let source_root_id = SourceRootId(0); | ||
let mut files = Vec::new(); | ||
|
||
for (idx, entry) in fixture.into_iter().enumerate() { | ||
let file_id = FileId(idx.try_into().expect("too many files")); | ||
db.set_file_relative_path(file_id, entry.relative_path); | ||
db.set_file_text(file_id, Arc::new(entry.text)); | ||
db.set_file_source_root(file_id, source_root_id); | ||
source_root.insert_file(file_id); | ||
files.push(file_id); | ||
} | ||
|
||
db.set_source_root(source_root_id, Arc::new(source_root)); | ||
|
||
return files; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ mod db; | |
pub mod diagnostics; | ||
mod display; | ||
mod expr; | ||
mod fixture; | ||
mod ids; | ||
mod in_file; | ||
mod input; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
use compiler::RelativePathBuf; | ||
use itertools::Itertools; | ||
|
||
const DEFAULT_FILE_NAME: &str = "main.mun"; | ||
const META_LINE: &str = "//-"; | ||
|
||
/// A `Fixture` describes an single file in a project workspace. `Fixture`s can be parsed from a | ||
/// single string with the `parse` function. Using that function enables users to conveniently | ||
/// describe an entire workspace in a single string. | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub struct Fixture { | ||
/// The relative path of this file | ||
pub relative_path: RelativePathBuf, | ||
|
||
/// The text of the file | ||
pub text: String, | ||
} | ||
|
||
impl Fixture { | ||
/// Parses text which looks like this: | ||
/// | ||
/// ```not_rust | ||
/// //- /foo.mun | ||
/// fn hello_world() { | ||
/// } | ||
/// | ||
/// //- /bar.mun | ||
/// fn baz() { | ||
/// } | ||
/// ``` | ||
/// | ||
/// into two separate `Fixture`s one with `relative_path` 'foo.mun' and one with 'bar.mun'. | ||
pub fn parse(text: impl AsRef<str>) -> Vec<Fixture> { | ||
let text = trim_raw_string_literal(text); | ||
let mut result: Vec<Fixture> = Vec::new(); | ||
|
||
// If the text does not contain any meta tags, insert a default meta tag at the start. | ||
let default_start = if text.contains(META_LINE) { | ||
None | ||
} else { | ||
Some(format!("{} /{}", META_LINE, DEFAULT_FILE_NAME)) | ||
}; | ||
|
||
for (idx, line) in default_start | ||
.as_deref() | ||
.into_iter() | ||
.chain(text.lines()) | ||
.enumerate() | ||
{ | ||
if line.contains(META_LINE) { | ||
assert!( | ||
line.starts_with(META_LINE), | ||
"Metadata line {} has invalid indentation. \ | ||
All metadata lines need to have the same indentation \n\ | ||
The offending line: {:?}", | ||
idx, | ||
line | ||
); | ||
} | ||
|
||
if line.starts_with(META_LINE) { | ||
let meta = Fixture::parse_meta_line(line); | ||
result.push(meta); | ||
} else if let Some(entry) = result.last_mut() { | ||
entry.text.push_str(line); | ||
entry.text.push_str("\n"); | ||
} | ||
} | ||
|
||
result | ||
} | ||
|
||
/// Parses a fixture meta line like: | ||
/// ``` | ||
/// //- /main.mun | ||
/// ``` | ||
fn parse_meta_line(line: impl AsRef<str>) -> Fixture { | ||
let line = line.as_ref(); | ||
assert!(line.starts_with(META_LINE)); | ||
|
||
let line = line[META_LINE.len()..].trim(); | ||
let components = line.split_ascii_whitespace().collect::<Vec<_>>(); | ||
|
||
let path = components[0].to_string(); | ||
assert!(path.starts_with('/')); | ||
let relative_path = RelativePathBuf::from(&path[1..]); | ||
|
||
Fixture { | ||
relative_path, | ||
text: String::new(), | ||
} | ||
} | ||
} | ||
|
||
/// Turns a string that is likely to come from a raw string literal into something that is | ||
/// probably intended. | ||
/// | ||
/// * Strips the first newline if there is one | ||
/// * Removes any initial indentation | ||
/// | ||
/// Example usecase: | ||
/// | ||
/// ``` | ||
/// # fn do_something(s: &str) {} | ||
/// do_something(r#" | ||
/// fn func() { | ||
/// // code | ||
/// } | ||
/// "#) | ||
/// ``` | ||
/// | ||
/// Results in the string (with no leading newline): | ||
/// ```not_rust | ||
/// fn func() { | ||
/// // code | ||
/// } | ||
/// ``` | ||
pub fn trim_raw_string_literal(text: impl AsRef<str>) -> String { | ||
let mut text = text.as_ref(); | ||
if text.starts_with('\n') { | ||
text = &text[1..]; | ||
} | ||
|
||
let minimum_indentation = text | ||
.lines() | ||
.filter(|it| !it.trim().is_empty()) | ||
.map(|it| it.len() - it.trim_start().len()) | ||
.min() | ||
.unwrap_or(0); | ||
|
||
text.lines() | ||
.map(|line| { | ||
if line.len() <= minimum_indentation { | ||
line.trim_start_matches(' ') | ||
} else { | ||
&line[minimum_indentation..] | ||
} | ||
}) | ||
.join("\n") | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn trim_raw_string_literal() { | ||
assert_eq!( | ||
&super::trim_raw_string_literal( | ||
r#" | ||
fn hello_world() { | ||
// code | ||
} | ||
"# | ||
), | ||
"fn hello_world() {\n // code\n}\n" | ||
); | ||
} | ||
|
||
#[test] | ||
fn empty_fixture() { | ||
assert_eq!( | ||
Fixture::parse(""), | ||
vec![Fixture { | ||
relative_path: RelativePathBuf::from(DEFAULT_FILE_NAME), | ||
text: "".to_owned() | ||
}] | ||
); | ||
} | ||
|
||
#[test] | ||
fn single_fixture() { | ||
assert_eq!( | ||
Fixture::parse(format!("{} /foo.mun\nfn hello_world() {{}}", META_LINE)), | ||
vec![Fixture { | ||
relative_path: RelativePathBuf::from("foo.mun"), | ||
text: "fn hello_world() {}\n".to_owned() | ||
}] | ||
); | ||
} | ||
|
||
#[test] | ||
fn multiple_fixtures() { | ||
assert_eq!( | ||
Fixture::parse( | ||
r#" | ||
//- /foo.mun | ||
fn hello_world() { | ||
} | ||
//- /bar.mun | ||
fn baz() { | ||
} | ||
"# | ||
), | ||
vec![ | ||
Fixture { | ||
relative_path: RelativePathBuf::from("foo.mun"), | ||
text: "fn hello_world() {\n}\n\n".to_owned() | ||
}, | ||
Fixture { | ||
relative_path: RelativePathBuf::from("bar.mun"), | ||
text: "fn baz() {\n}\n".to_owned() | ||
} | ||
] | ||
); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn incorrectly_indented_fixture() { | ||
Fixture::parse( | ||
r" | ||
//- /foo.mun | ||
fn foo() {} | ||
//- /bar.mun | ||
pub fn baz() {} | ||
", | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters