Skip to content

Commit c6d0261

Browse files
committed
some test for import classifier
1 parent 0ecd438 commit c6d0261

3 files changed

Lines changed: 52 additions & 8 deletions

File tree

src/core/ast_parser/ast_parser.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ use std::io;
77
use tree_sitter::{Language};
88
use tree_sitter_python;
99

10-
use crate::core::ast_parser::import_classifier::ImportClassifier;
1110
use crate::core::ast_parser::query::ParseQuery;
1211
use crate::core::ast_parser::syntax_tree::SyntaxTree;
1312
use crate::core::ast_parser::tree_analyzer::TreeAnalyzer;
1413

1514
pub struct AstParser {
1615
file_data: Mutex<HashMap<String, HashMap<String, Vec<String>>>>,
17-
import_classifier: ImportClassifier,
1816
query: ParseQuery,
1917
tree: SyntaxTree,
2018
tree_analyzer: TreeAnalyzer,
@@ -25,7 +23,6 @@ impl AstParser {
2523
pub fn new() -> Self {
2624
Self {
2725
file_data: Mutex::new(HashMap::new()),
28-
import_classifier: ImportClassifier::new(),
2926
query: ParseQuery::new(),
3027
tree: SyntaxTree::new(),
3128
tree_analyzer: TreeAnalyzer::new(),

src/core/ast_parser/import_classifier.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,57 @@ impl ImportClassifier {
2929
) -> bool {
3030

3131
for dir in root_dirs {
32-
if import.starts_with(dir) {
32+
if import.starts_with(dir) || import.starts_with(".") {
3333
return true;
3434
}
3535
}
3636
return false;
3737
}
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use super::*;
43+
44+
#[test]
45+
fn test_import_is_local() {
46+
let root_dirs = vec![
47+
"application".to_string(),
48+
"services".to_string()
49+
];
50+
let import = "services.foo.Bar".to_string();
51+
52+
let classifier = ImportClassifier::new();
53+
let result = classifier.is_eligible(&import, &root_dirs);
54+
55+
assert_eq!(result, true);
56+
}
57+
58+
#[test]
59+
fn test_import_is_not_local() {
60+
let root_dirs = vec![
61+
"application".to_string(),
62+
"services".to_string()
63+
];
64+
let import = "fastapi.ApiRouter".to_string();
65+
66+
let classifier = ImportClassifier::new();
67+
let result = classifier.is_eligible(&import, &root_dirs);
68+
69+
assert_eq!(result, false);
70+
}
71+
72+
#[test]
73+
fn test_import_is_relative() {
74+
let root_dirs = vec![
75+
"application".to_string(),
76+
"services".to_string()
77+
];
78+
let import = "...services.global.GlobalService".to_string();
79+
80+
let classifier = ImportClassifier::new();
81+
let result = classifier.is_eligible(&import, &root_dirs);
82+
83+
assert_eq!(result, true);
84+
}
3885
}

src/core/ast_parser/tree_analyzer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ impl TreeAnalyzer {
5454
}
5555
}
5656
"function" => {
57-
let text = self.dot_name.get(&code, cap.node);
58-
if !text.starts_with("__") & !text.ends_with("__") {
57+
let function = self.dot_name.get(&code, cap.node);
58+
if !function.starts_with("__") & !function.ends_with("__") {
5959
let mut parent = cap.node.parent();
6060
let mut found_class = false;
6161
while let Some(p) = parent {
@@ -64,15 +64,15 @@ impl TreeAnalyzer {
6464
let class = name_node.utf8_text(code.as_bytes())
6565
.unwrap()
6666
.to_string();
67-
let class_method = format!("{class}.{text}");
67+
let class_method = format!("{class}.{function}");
6868
defines.push(class_method);
6969
found_class = true;
7070
}
7171
}
7272
parent = p.parent();
7373
}
7474
if !found_class {
75-
defines.push(text);
75+
defines.push(function);
7676
}
7777
}
7878
}

0 commit comments

Comments
 (0)