fix(adapter): support nested namespace definition

This commit is contained in:
kbwo 2024-10-19 16:50:11 +09:00
parent 7ce2e97df6
commit cf926f56a2
4 changed files with 53 additions and 49 deletions

View file

@ -259,7 +259,7 @@ mod tests {
fn test_discover() {
let file_path = "../../demo/node-test/index.test.js";
let test_items = discover(file_path).unwrap();
assert_eq!(test_items.len(), 27);
assert_eq!(test_items.len(), 26);
assert_eq!(
test_items,
[
@ -672,8 +672,8 @@ mod tests {
}
},
TestItem {
id: "should be ok".to_string(),
name: "should be ok".to_string(),
id: "A thing::should be ok".to_string(),
name: "A thing::should be ok".to_string(),
start_position: Range {
start: Position {
line: 109,
@ -696,8 +696,8 @@ mod tests {
}
},
TestItem {
id: "a nested thing::should work".to_string(),
name: "a nested thing::should work".to_string(),
id: "A thing::a nested thing::should work".to_string(),
name: "A thing::a nested thing::should work".to_string(),
start_position: Range {
start: Position {
line: 114,
@ -720,8 +720,8 @@ mod tests {
}
},
TestItem {
id: "this test is run".to_string(),
name: "this test is run".to_string(),
id: "only: this test is run".to_string(),
name: "only: this test is run".to_string(),
start_position: Range {
start: Position {
line: 123,
@ -744,8 +744,8 @@ mod tests {
}
},
TestItem {
id: "this test is not run".to_string(),
name: "this test is not run".to_string(),
id: "only: this test is not run".to_string(),
name: "only: this test is not run".to_string(),
start_position: Range {
start: Position {
line: 142,
@ -792,32 +792,8 @@ mod tests {
}
},
TestItem {
id: "this test is run A ".to_string(),
name: "this test is run A ".to_string(),
start_position: Range {
start: Position {
line: 149,
character: 2
},
end: Position {
line: 149,
character: 10000
}
},
end_position: Range {
start: Position {
line: 151,
character: 0
},
end: Position {
line: 151,
character: 4
}
}
},
TestItem {
id: "this test is not run B".to_string(),
name: "this test is not run B".to_string(),
id: "A suite::this test is not run B".to_string(),
name: "A suite::this test is not run B".to_string(),
start_position: Range {
start: Position {
line: 153,
@ -840,8 +816,8 @@ mod tests {
}
},
TestItem {
id: "B suite::this test is run A".to_string(),
name: "B suite::this test is run A".to_string(),
id: "this test is run C".to_string(),
name: "this test is run C".to_string(),
start_position: Range {
start: Position {
line: 161,
@ -864,8 +840,8 @@ mod tests {
}
},
TestItem {
id: "this test is run B".to_string(),
name: "this test is run B".to_string(),
id: "this test is run D".to_string(),
name: "this test is run D".to_string(),
start_position: Range {
start: Position {
line: 165,

View file

@ -12,6 +12,8 @@ use testing_language_server::spec::{RunFileTestResultItem, TestItem};
use testing_language_server::{error::LSError, spec::RunFileTestResult};
use tree_sitter::{Language, Point, Query, QueryCursor};
pub struct DiscoverWithTSOption {}
pub static LOG_LOCATION: LazyLock<PathBuf> = LazyLock::new(|| {
let home_dir = dirs::home_dir().unwrap();
home_dir.join(".config/testing_language_server/adapter/")
@ -178,7 +180,9 @@ pub fn discover_with_treesitter(
cursor.set_byte_range(tree.root_node().byte_range());
let source = source_code.as_bytes();
let matches = cursor.matches(&query, tree.root_node(), source);
let mut namespace = "";
let mut namespace_name = String::new();
let mut namespace_position_stack: Vec<(Point, Point)> = vec![];
let mut test_id_set = HashSet::new();
for m in matches {
let mut test_start_position = Point::default();
@ -188,25 +192,50 @@ pub fn discover_with_treesitter(
let value = capture.node.utf8_text(source)?;
let start_position = capture.node.start_position();
let end_position = capture.node.end_position();
match capture_name {
"namespace.definition" => {
namespace_position_stack.push((start_position, end_position));
}
"namespace.name" => {
namespace = value;
let current_namespace = namespace_position_stack.first();
if let Some((ns_start, ns_end)) = current_namespace {
// In namespace definition
if start_position.row >= ns_start.row
&& end_position.row <= ns_end.row
&& !namespace_name.is_empty()
{
namespace_name = format!("{}::{}", namespace_name, value);
} else {
namespace_name = value.to_string();
}
} else {
namespace_name = value.to_string();
}
}
"test.definition" => {
if let Some((ns_start, ns_end)) = namespace_position_stack.first() {
if start_position.row < ns_start.row || end_position.row > ns_end.row {
namespace_position_stack.remove(0);
namespace_name = String::new();
}
}
test_start_position = start_position;
test_end_position = end_position;
}
"test.name" => {
let test_id = if namespace.is_empty() {
let test_id = if namespace_name.is_empty() {
value.to_string()
} else {
[namespace, value].join("::")
format!("{}::{}", namespace_name, value)
};
if test_id_set.contains(&test_id) {
continue;
} else {
test_id_set.insert(test_id.clone());
}
let test_item = TestItem {
id: test_id.clone(),
name: test_id,
@ -234,7 +263,6 @@ pub fn discover_with_treesitter(
test_items.push(test_item);
test_start_position = Point::default();
test_end_position = Point::default();
namespace = "";
}
_ => {}
}

View file

@ -208,7 +208,7 @@ mod tests {
assert_eq!(test_items.len(), 2);
assert_eq!(
test_items,
vec![
[
TestItem {
id: "describe text::pass".to_string(),
name: "describe text::pass".to_string(),