aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-06-23 17:46:56 +0100
committerAleksey Kladov <[email protected]>2020-06-23 17:46:56 +0100
commitfdf86aee18e396d393d50df7df27b02111838507 (patch)
tree88cd35c526a926eb8c8a8645243209759a7bb7f9 /crates/test_utils
parent3486b47e5c4f71479cc3c876da1fd1dcbfcab257 (diff)
Nicer API
Diffstat (limited to 'crates/test_utils')
-rw-r--r--crates/test_utils/src/fixture.rs120
-rw-r--r--crates/test_utils/src/lib.rs2
2 files changed, 62 insertions, 60 deletions
diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs
index 25d80806b..2a51bb559 100644
--- a/crates/test_utils/src/fixture.rs
+++ b/crates/test_utils/src/fixture.rs
@@ -3,7 +3,7 @@ use rustc_hash::FxHashMap;
3use stdx::split1; 3use stdx::split1;
4 4
5#[derive(Debug, Eq, PartialEq)] 5#[derive(Debug, Eq, PartialEq)]
6pub struct FixtureEntry { 6pub struct Fixture {
7 pub path: String, 7 pub path: String,
8 pub text: String, 8 pub text: String,
9 pub crate_name: Option<String>, 9 pub crate_name: Option<String>,
@@ -13,19 +13,20 @@ pub struct FixtureEntry {
13 pub env: FxHashMap<String, String>, 13 pub env: FxHashMap<String, String>,
14} 14}
15 15
16/// Parses text which looks like this: 16impl Fixture {
17/// 17 /// Parses text which looks like this:
18/// ```not_rust 18 ///
19/// //- some meta 19 /// ```not_rust
20/// line 1 20 /// //- some meta
21/// line 2 21 /// line 1
22/// // - other meta 22 /// line 2
23/// ``` 23 /// // - other meta
24pub fn parse_fixture(ra_fixture: &str) -> Vec<FixtureEntry> { 24 /// ```
25 let fixture = indent_first_line(ra_fixture); 25 pub fn parse(ra_fixture: &str) -> Vec<Fixture> {
26 let margin = fixture_margin(&fixture); 26 let fixture = indent_first_line(ra_fixture);
27 27 let margin = fixture_margin(&fixture);
28 let mut lines = fixture 28
29 let mut lines = fixture
29 .split('\n') // don't use `.lines` to not drop `\r\n` 30 .split('\n') // don't use `.lines` to not drop `\r\n`
30 .enumerate() 31 .enumerate()
31 .filter_map(|(ix, line)| { 32 .filter_map(|(ix, line)| {
@@ -48,58 +49,59 @@ The offending line: {:?}"#,
48 } 49 }
49 }); 50 });
50 51
51 let mut res: Vec<FixtureEntry> = Vec::new(); 52 let mut res: Vec<Fixture> = Vec::new();
52 for line in lines.by_ref() { 53 for line in lines.by_ref() {
53 if line.starts_with("//-") { 54 if line.starts_with("//-") {
54 let meta = line["//-".len()..].trim().to_string(); 55 let meta = line["//-".len()..].trim().to_string();
55 let meta = parse_meta(&meta); 56 let meta = Fixture::parse_single(&meta);
56 res.push(meta) 57 res.push(meta)
57 } else if let Some(entry) = res.last_mut() { 58 } else if let Some(entry) = res.last_mut() {
58 entry.text.push_str(line); 59 entry.text.push_str(line);
59 entry.text.push('\n'); 60 entry.text.push('\n');
61 }
60 } 62 }
63 res
61 } 64 }
62 res
63}
64 65
65//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo 66 //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
66fn parse_meta(meta: &str) -> FixtureEntry { 67 fn parse_single(meta: &str) -> Fixture {
67 let components = meta.split_ascii_whitespace().collect::<Vec<_>>(); 68 let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
68 69
69 let path = components[0].to_string(); 70 let path = components[0].to_string();
70 assert!(path.starts_with("/")); 71 assert!(path.starts_with("/"));
71 72
72 let mut krate = None; 73 let mut krate = None;
73 let mut deps = Vec::new(); 74 let mut deps = Vec::new();
74 let mut edition = None; 75 let mut edition = None;
75 let mut cfg = CfgOptions::default(); 76 let mut cfg = CfgOptions::default();
76 let mut env = FxHashMap::default(); 77 let mut env = FxHashMap::default();
77 for component in components[1..].iter() { 78 for component in components[1..].iter() {
78 let (key, value) = split1(component, ':').unwrap(); 79 let (key, value) = split1(component, ':').unwrap();
79 match key { 80 match key {
80 "crate" => krate = Some(value.to_string()), 81 "crate" => krate = Some(value.to_string()),
81 "deps" => deps = value.split(',').map(|it| it.to_string()).collect(), 82 "deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
82 "edition" => edition = Some(value.to_string()), 83 "edition" => edition = Some(value.to_string()),
83 "cfg" => { 84 "cfg" => {
84 for key in value.split(',') { 85 for key in value.split(',') {
85 match split1(key, '=') { 86 match split1(key, '=') {
86 None => cfg.insert_atom(key.into()), 87 None => cfg.insert_atom(key.into()),
87 Some((k, v)) => cfg.insert_key_value(k.into(), v.into()), 88 Some((k, v)) => cfg.insert_key_value(k.into(), v.into()),
89 }
88 } 90 }
89 } 91 }
90 } 92 "env" => {
91 "env" => { 93 for key in value.split(',') {
92 for key in value.split(',') { 94 if let Some((k, v)) = split1(key, '=') {
93 if let Some((k, v)) = split1(key, '=') { 95 env.insert(k.into(), v.into());
94 env.insert(k.into(), v.into()); 96 }
95 } 97 }
96 } 98 }
99 _ => panic!("bad component: {:?}", component),
97 } 100 }
98 _ => panic!("bad component: {:?}", component),
99 } 101 }
100 }
101 102
102 FixtureEntry { path, text: String::new(), crate_name: krate, deps, edition, cfg, env } 103 Fixture { path, text: String::new(), crate_name: krate, deps, edition, cfg, env }
104 }
103} 105}
104 106
105/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. 107/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines.
@@ -170,8 +172,8 @@ fn parse_fixture_can_handle_dedented_first_line() {
170 struct Bar; 172 struct Bar;
171"; 173";
172 assert_eq!( 174 assert_eq!(
173 parse_fixture(fixture), 175 Fixture::parse(fixture),
174 parse_fixture( 176 Fixture::parse(
175 "//- /lib.rs 177 "//- /lib.rs
176mod foo; 178mod foo;
177//- /foo.rs 179//- /foo.rs
@@ -183,7 +185,7 @@ struct Bar;
183 185
184#[test] 186#[test]
185fn parse_fixture_gets_full_meta() { 187fn parse_fixture_gets_full_meta() {
186 let parsed = parse_fixture( 188 let parsed = Fixture::parse(
187 r" 189 r"
188 //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo 190 //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
189 mod m; 191 mod m;
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index d44b2f9ab..316f3d501 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -22,7 +22,7 @@ pub use difference::Changeset as __Changeset;
22pub use ra_cfg::CfgOptions; 22pub use ra_cfg::CfgOptions;
23pub use rustc_hash::FxHashMap; 23pub use rustc_hash::FxHashMap;
24 24
25pub use crate::fixture::{parse_fixture, FixtureEntry}; 25pub use crate::fixture::Fixture;
26 26
27pub const CURSOR_MARKER: &str = "<|>"; 27pub const CURSOR_MARKER: &str = "<|>";
28 28