Skip to content

Commit d016cb4

Browse files
committed
Use only one code-path for parsing fixtures
This removes leading newlines everywhere, shifting all ranges in tests by one
1 parent e5101ae commit d016cb4

File tree

11 files changed

+2751
-2790
lines changed

11 files changed

+2751
-2790
lines changed

crates/ra_assists/src/tests.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hir::Semantics;
44
use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
55
use ra_ide_db::RootDatabase;
66
use ra_syntax::TextRange;
7-
use test_utils::{assert_eq_text, extract_offset, extract_range, extract_range_or_offset};
7+
use test_utils::{assert_eq_text, extract_offset, extract_range};
88

99
use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, Assists};
1010
use stdx::trim_indent;
@@ -30,8 +30,9 @@ pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) {
3030
}
3131

3232
fn check_doc_test(assist_id: &str, before: &str, after: &str) {
33-
let (selection, before) = extract_range_or_offset(before);
34-
let (db, file_id) = crate::tests::with_single_file(&before);
33+
let after = trim_indent(after);
34+
let (db, file_id, selection) = RootDatabase::with_range_or_offset(&before);
35+
let before = db.file_text(file_id).to_string();
3536
let frange = FileRange { file_id, range: selection.into() };
3637

3738
let mut assist = Assist::resolved(&db, &AssistConfig::default(), frange)
@@ -51,11 +52,11 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
5152

5253
let actual = {
5354
let change = assist.source_change.source_file_edits.pop().unwrap();
54-
let mut actual = before.clone();
55+
let mut actual = before;
5556
change.edit.apply(&mut actual);
5657
actual
5758
};
58-
assert_eq_text!(after, &actual);
59+
assert_eq_text!(&after, &actual);
5960
}
6061

6162
enum ExpectedResult<'a> {
@@ -66,7 +67,7 @@ enum ExpectedResult<'a> {
6667

6768
fn check(handler: Handler, before: &str, expected: ExpectedResult) {
6869
let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before);
69-
let text_without_caret = db.file_text(file_with_caret_id).as_ref().to_owned();
70+
let text_without_caret = db.file_text(file_with_caret_id).to_string();
7071

7172
let frange = FileRange { file_id: file_with_caret_id, range: range_or_offset.into() };
7273

crates/ra_db/src/fixture.rs

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0);
7474
pub trait WithFixture: Default + SourceDatabaseExt + 'static {
7575
fn with_single_file(text: &str) -> (Self, FileId) {
7676
let mut db = Self::default();
77-
let file_id = with_single_file(&mut db, text);
78-
(db, file_id)
77+
let (_, files) = with_files(&mut db, text);
78+
assert_eq!(files.len(), 1);
79+
(db, files[0])
7980
}
8081

8182
fn with_files(ra_fixture: &str) -> Self {
@@ -112,52 +113,6 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
112113

113114
impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
114115

115-
fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId {
116-
let file_id = FileId(0);
117-
let mut file_set = vfs::file_set::FileSet::default();
118-
file_set.insert(file_id, vfs::VfsPath::new_virtual_path("/main.rs".to_string()));
119-
120-
let source_root = SourceRoot::new_local(file_set);
121-
122-
let crate_graph = if let Some(meta) = ra_fixture.lines().find(|it| it.contains("//-")) {
123-
let entry = Fixture::parse_meta_line(meta.trim());
124-
let meta = match ParsedMeta::from(&entry) {
125-
ParsedMeta::File(it) => it,
126-
};
127-
128-
let mut crate_graph = CrateGraph::default();
129-
crate_graph.add_crate_root(
130-
file_id,
131-
meta.edition,
132-
meta.krate.map(|name| {
133-
CrateName::new(&name).expect("Fixture crate name should not contain dashes")
134-
}),
135-
meta.cfg,
136-
meta.env,
137-
Default::default(),
138-
);
139-
crate_graph
140-
} else {
141-
let mut crate_graph = CrateGraph::default();
142-
crate_graph.add_crate_root(
143-
file_id,
144-
Edition::Edition2018,
145-
None,
146-
CfgOptions::default(),
147-
Env::default(),
148-
Default::default(),
149-
);
150-
crate_graph
151-
};
152-
153-
db.set_file_text(file_id, Arc::new(ra_fixture.to_string()));
154-
db.set_file_source_root(file_id, WORKSPACE);
155-
db.set_source_root(WORKSPACE, Arc::new(source_root));
156-
db.set_crate_graph(Arc::new(crate_graph));
157-
158-
file_id
159-
}
160-
161116
fn with_files(
162117
db: &mut dyn SourceDatabaseExt,
163118
fixture: &str,

crates/ra_hir_def/src/body/scope.rs

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,22 @@ mod tests {
189189
}
190190

191191
fn do_check(ra_fixture: &str, expected: &[&str]) {
192-
let (off, code) = extract_offset(ra_fixture);
192+
let (offset, code) = extract_offset(ra_fixture);
193193
let code = {
194194
let mut buf = String::new();
195-
let off: usize = off.into();
195+
let off: usize = offset.into();
196196
buf.push_str(&code[..off]);
197-
buf.push_str("marker");
197+
buf.push_str("<|>marker");
198198
buf.push_str(&code[off..]);
199199
buf
200200
};
201201

202-
let (db, file_id) = TestDB::with_single_file(&code);
202+
let (db, position) = TestDB::with_position(&code);
203+
let file_id = position.file_id;
204+
let offset = position.offset;
203205

204206
let file_syntax = db.parse(file_id).syntax_node();
205-
let marker: ast::PathExpr = find_node_at_offset(&file_syntax, off).unwrap();
207+
let marker: ast::PathExpr = find_node_at_offset(&file_syntax, offset).unwrap();
206208
let function = find_function(&db, file_id);
207209

208210
let scopes = db.expr_scopes(function.into());
@@ -302,27 +304,28 @@ mod tests {
302304
fn test_bindings_after_at() {
303305
do_check(
304306
r"
305-
fn foo() {
306-
match Some(()) {
307-
opt @ Some(unit) => {
308-
<|>
309-
}
310-
_ => {}
311-
}
312-
}",
307+
fn foo() {
308+
match Some(()) {
309+
opt @ Some(unit) => {
310+
<|>
311+
}
312+
_ => {}
313+
}
314+
}
315+
",
313316
&["opt", "unit"],
314317
);
315318
}
316319

317-
fn do_check_local_name(code: &str, expected_offset: u32) {
318-
let (off, code) = extract_offset(code);
319-
320-
let (db, file_id) = TestDB::with_single_file(&code);
320+
fn do_check_local_name(ra_fixture: &str, expected_offset: u32) {
321+
let (db, position) = TestDB::with_position(ra_fixture);
322+
let file_id = position.file_id;
323+
let offset = position.offset;
321324

322325
let file = db.parse(file_id).ok().unwrap();
323326
let expected_name = find_node_at_offset::<ast::Name>(file.syntax(), expected_offset.into())
324327
.expect("failed to find a name at the target offset");
325-
let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap();
328+
let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), offset).unwrap();
326329

327330
let function = find_function(&db, file_id);
328331

@@ -350,53 +353,55 @@ mod tests {
350353
fn test_resolve_local_name() {
351354
do_check_local_name(
352355
r#"
353-
fn foo(x: i32, y: u32) {
354-
{
355-
let z = x * 2;
356-
}
357-
{
358-
let t = x<|> * 3;
359-
}
360-
}"#,
361-
21,
356+
fn foo(x: i32, y: u32) {
357+
{
358+
let z = x * 2;
359+
}
360+
{
361+
let t = x<|> * 3;
362+
}
363+
}
364+
"#,
365+
7,
362366
);
363367
}
364368

365369
#[test]
366370
fn test_resolve_local_name_declaration() {
367371
do_check_local_name(
368372
r#"
369-
fn foo(x: String) {
370-
let x : &str = &x<|>;
371-
}"#,
372-
21,
373+
fn foo(x: String) {
374+
let x : &str = &x<|>;
375+
}
376+
"#,
377+
7,
373378
);
374379
}
375380

376381
#[test]
377382
fn test_resolve_local_name_shadow() {
378383
do_check_local_name(
379384
r"
380-
fn foo(x: String) {
381-
let x : &str = &x;
382-
x<|>
383-
}
384-
",
385-
53,
385+
fn foo(x: String) {
386+
let x : &str = &x;
387+
x<|>
388+
}
389+
",
390+
28,
386391
);
387392
}
388393

389394
#[test]
390395
fn ref_patterns_contribute_bindings() {
391396
do_check_local_name(
392397
r"
393-
fn foo() {
394-
if let Some(&from) = bar() {
395-
from<|>;
396-
}
397-
}
398-
",
399-
53,
398+
fn foo() {
399+
if let Some(&from) = bar() {
400+
from<|>;
401+
}
402+
}
403+
",
404+
28,
400405
);
401406
}
402407

0 commit comments

Comments
 (0)