//! 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, pub deps: Vec, pub cfg_atoms: Vec, pub cfg_key_values: Vec<(String, String)>, pub edition: Option, pub env: FxHashMap, 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 { let fixture = trim_indent(ra_fixture); let mut res: Vec = 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::>(); 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()); }