aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r--crates/ra_hir/src/ty/tests.rs62
1 files changed, 39 insertions, 23 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index f2466dd51..98eedaa3f 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -1,8 +1,11 @@
1use std::fmt::Write;
1use std::sync::Arc; 2use std::sync::Arc;
3use std::path::{Path, PathBuf};
2 4
3use salsa::Database; 5use salsa::Database;
4use ra_db::{FilesDatabase, CrateGraph, SyntaxDatabase}; 6use ra_db::{FilesDatabase, CrateGraph, SyntaxDatabase};
5use ra_syntax::{SmolStr, algo::visit::{visitor, Visitor}, ast::{self, AstNode}}; 7use ra_syntax::{SmolStr, algo::visit::{visitor, Visitor}, ast::{self, AstNode}};
8use test_utils::{project_dir, dir_tests};
6use relative_path::RelativePath; 9use relative_path::RelativePath;
7 10
8use crate::{source_binder, mock::WORKSPACE, module::ModuleSourceNode}; 11use crate::{source_binder, mock::WORKSPACE, module::ModuleSourceNode};
@@ -13,33 +16,46 @@ use crate::{
13 mock::MockDatabase, 16 mock::MockDatabase,
14}; 17};
15 18
16fn infer_all_fns(fixture: &str) -> () { 19fn infer_file(content: &str) -> String {
17 let (db, source_root) = MockDatabase::with_files(fixture); 20 let (db, source_root, file_id) = MockDatabase::with_single_file(content);
18 for &file_id in source_root.files.values() { 21 let source_file = db.source_file(file_id);
19 let source_file = db.source_file(file_id); 22 let mut acc = String::new();
20 for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { 23 for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) {
21 let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap().unwrap(); 24 let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap().unwrap();
22 let inference_result = func.infer(&db); 25 let inference_result = func.infer(&db);
23 for (syntax_ptr, ty) in &inference_result.type_for { 26 for (syntax_ptr, ty) in &inference_result.type_for {
24 let node = syntax_ptr.resolve(&source_file); 27 let node = syntax_ptr.resolve(&source_file);
25 eprintln!("{} '{}': {:?}", syntax_ptr.range(), node.text(), ty); 28 write!(acc, "{} '{}': {}\n", syntax_ptr.range(), ellipsize(node.text().to_string().replace("\n", " "), 15), ty);
26 }
27 } 29 }
28 } 30 }
31 acc
32}
33
34fn ellipsize(mut text: String, max_len: usize) -> String {
35 if text.len() <= max_len {
36 return text;
37 }
38 let ellipsis = "...";
39 let e_len = ellipsis.len();
40 let mut prefix_len = (max_len - e_len) / 2;
41 while !text.is_char_boundary(prefix_len) {
42 prefix_len += 1;
43 }
44 let mut suffix_len = max_len - e_len - prefix_len;
45 while !text.is_char_boundary(text.len() - suffix_len) {
46 suffix_len += 1;
47 }
48 text.replace_range(prefix_len..text.len() - suffix_len, ellipsis);
49 text
29} 50}
30 51
31#[test] 52#[test]
32fn infer_smoke_test() { 53pub fn infer_tests() {
33 let text = " 54 dir_tests(&test_data_dir(), &["."], |text, _path| {
34 //- /lib.rs 55 infer_file(text)
35 fn foo(x: u32, y: !) -> i128 { 56 });
36 x; 57}
37 y;
38 return 1;
39 \"hello\";
40 0
41 }
42 ";
43 58
44 infer_all_fns(text); 59fn test_data_dir() -> PathBuf {
60 project_dir().join("crates/ra_hir/src/ty/tests/data")
45} 61}