diff options
Diffstat (limited to 'crates/ra_ssr/src/tests.rs')
-rw-r--r-- | crates/ra_ssr/src/tests.rs | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs index 1b03b7f4b..c7c37af2f 100644 --- a/crates/ra_ssr/src/tests.rs +++ b/crates/ra_ssr/src/tests.rs | |||
@@ -1,6 +1,8 @@ | |||
1 | use crate::{MatchFinder, SsrRule}; | 1 | use crate::{MatchFinder, SsrRule}; |
2 | use expect::{expect, Expect}; | 2 | use expect::{expect, Expect}; |
3 | use ra_db::{FileId, SourceDatabaseExt}; | 3 | use ra_db::{salsa::Durability, FileId, SourceDatabaseExt}; |
4 | use rustc_hash::FxHashSet; | ||
5 | use std::sync::Arc; | ||
4 | use test_utils::mark; | 6 | use test_utils::mark; |
5 | 7 | ||
6 | fn parse_error_text(query: &str) -> String { | 8 | fn parse_error_text(query: &str) -> String { |
@@ -57,9 +59,15 @@ fn parser_undefined_placeholder_in_replacement() { | |||
57 | ); | 59 | ); |
58 | } | 60 | } |
59 | 61 | ||
60 | fn single_file(code: &str) -> (ra_ide_db::RootDatabase, FileId) { | 62 | pub(crate) fn single_file(code: &str) -> (ra_ide_db::RootDatabase, FileId) { |
61 | use ra_db::fixture::WithFixture; | 63 | use ra_db::fixture::WithFixture; |
62 | ra_ide_db::RootDatabase::with_single_file(code) | 64 | use ra_ide_db::symbol_index::SymbolsDatabase; |
65 | let (db, file_id) = ra_ide_db::RootDatabase::with_single_file(code); | ||
66 | let mut db = db; | ||
67 | let mut local_roots = FxHashSet::default(); | ||
68 | local_roots.insert(ra_db::fixture::WORKSPACE); | ||
69 | db.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH); | ||
70 | (db, file_id) | ||
63 | } | 71 | } |
64 | 72 | ||
65 | fn assert_ssr_transform(rule: &str, input: &str, expected: Expect) { | 73 | fn assert_ssr_transform(rule: &str, input: &str, expected: Expect) { |
@@ -73,15 +81,16 @@ fn assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect) { | |||
73 | let rule: SsrRule = rule.parse().unwrap(); | 81 | let rule: SsrRule = rule.parse().unwrap(); |
74 | match_finder.add_rule(rule); | 82 | match_finder.add_rule(rule); |
75 | } | 83 | } |
76 | if let Some(edits) = match_finder.edits_for_file(file_id) { | 84 | let edits = match_finder.edits(); |
77 | // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters | 85 | if edits.is_empty() { |
78 | // stuff. | ||
79 | let mut actual = db.file_text(file_id).to_string(); | ||
80 | edits.apply(&mut actual); | ||
81 | expected.assert_eq(&actual); | ||
82 | } else { | ||
83 | panic!("No edits were made"); | 86 | panic!("No edits were made"); |
84 | } | 87 | } |
88 | assert_eq!(edits[0].file_id, file_id); | ||
89 | // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters | ||
90 | // stuff. | ||
91 | let mut actual = db.file_text(file_id).to_string(); | ||
92 | edits[0].edit.apply(&mut actual); | ||
93 | expected.assert_eq(&actual); | ||
85 | } | 94 | } |
86 | 95 | ||
87 | fn print_match_debug_info(match_finder: &MatchFinder, file_id: FileId, snippet: &str) { | 96 | fn print_match_debug_info(match_finder: &MatchFinder, file_id: FileId, snippet: &str) { |
@@ -100,13 +109,8 @@ fn assert_matches(pattern: &str, code: &str, expected: &[&str]) { | |||
100 | let (db, file_id) = single_file(code); | 109 | let (db, file_id) = single_file(code); |
101 | let mut match_finder = MatchFinder::new(&db); | 110 | let mut match_finder = MatchFinder::new(&db); |
102 | match_finder.add_search_pattern(pattern.parse().unwrap()); | 111 | match_finder.add_search_pattern(pattern.parse().unwrap()); |
103 | let matched_strings: Vec<String> = match_finder | 112 | let matched_strings: Vec<String> = |
104 | .find_matches_in_file(file_id) | 113 | match_finder.matches().flattened().matches.iter().map(|m| m.matched_text()).collect(); |
105 | .flattened() | ||
106 | .matches | ||
107 | .iter() | ||
108 | .map(|m| m.matched_text()) | ||
109 | .collect(); | ||
110 | if matched_strings != expected && !expected.is_empty() { | 114 | if matched_strings != expected && !expected.is_empty() { |
111 | print_match_debug_info(&match_finder, file_id, &expected[0]); | 115 | print_match_debug_info(&match_finder, file_id, &expected[0]); |
112 | } | 116 | } |
@@ -117,7 +121,7 @@ fn assert_no_match(pattern: &str, code: &str) { | |||
117 | let (db, file_id) = single_file(code); | 121 | let (db, file_id) = single_file(code); |
118 | let mut match_finder = MatchFinder::new(&db); | 122 | let mut match_finder = MatchFinder::new(&db); |
119 | match_finder.add_search_pattern(pattern.parse().unwrap()); | 123 | match_finder.add_search_pattern(pattern.parse().unwrap()); |
120 | let matches = match_finder.find_matches_in_file(file_id).flattened().matches; | 124 | let matches = match_finder.matches().flattened().matches; |
121 | if !matches.is_empty() { | 125 | if !matches.is_empty() { |
122 | print_match_debug_info(&match_finder, file_id, &matches[0].matched_text()); | 126 | print_match_debug_info(&match_finder, file_id, &matches[0].matched_text()); |
123 | panic!("Got {} matches when we expected none: {:#?}", matches.len(), matches); | 127 | panic!("Got {} matches when we expected none: {:#?}", matches.len(), matches); |