summaryrefslogtreecommitdiff
path: root/cli/lsp/testing/definitions.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/lsp/testing/definitions.rs')
-rw-r--r--cli/lsp/testing/definitions.rs70
1 files changed, 34 insertions, 36 deletions
diff --git a/cli/lsp/testing/definitions.rs b/cli/lsp/testing/definitions.rs
index c810b6a25..14ac165fd 100644
--- a/cli/lsp/testing/definitions.rs
+++ b/cli/lsp/testing/definitions.rs
@@ -18,7 +18,7 @@ pub struct TestDefinition {
pub level: usize,
pub name: String,
pub range: SourceRange,
- pub steps: Option<Vec<TestDefinition>>,
+ pub steps: Vec<TestDefinition>,
}
impl TestDefinition {
@@ -26,7 +26,7 @@ impl TestDefinition {
specifier: &ModuleSpecifier,
name: String,
range: SourceRange,
- steps: Option<Vec<TestDefinition>>,
+ steps: Vec<TestDefinition>,
) -> Self {
let id = checksum::gen(&[specifier.as_str().as_bytes(), name.as_bytes()]);
Self {
@@ -43,7 +43,7 @@ impl TestDefinition {
range: SourceRange,
parent: String,
level: usize,
- steps: Option<Vec<TestDefinition>>,
+ steps: Vec<TestDefinition>,
) -> Self {
let id = checksum::gen(&[
parent.as_bytes(),
@@ -66,27 +66,18 @@ impl TestDefinition {
lsp_custom::TestData {
id: self.id.clone(),
label: self.name.clone(),
- steps: self.steps.as_ref().map(|steps| {
- steps
- .iter()
- .map(|step| step.as_test_data(source_text_info))
- .collect()
- }),
+ steps: self
+ .steps
+ .iter()
+ .map(|step| step.as_test_data(source_text_info))
+ .collect(),
range: Some(source_range_to_lsp_range(&self.range, source_text_info)),
}
}
- fn find_step(&self, name: &str, level: usize) -> Option<&TestDefinition> {
- if let Some(steps) = &self.steps {
- for step in steps {
- if step.name == name && step.level == level {
- return Some(step);
- } else if let Some(step) = step.find_step(name, level) {
- return Some(step);
- }
- }
- }
- None
+ fn contains_id<S: AsRef<str>>(&self, id: S) -> bool {
+ let id = id.as_ref();
+ self.id == id || self.steps.iter().any(|td| td.contains_id(id))
}
}
@@ -102,6 +93,16 @@ pub struct TestDefinitions {
pub script_version: String,
}
+impl Default for TestDefinitions {
+ fn default() -> Self {
+ TestDefinitions {
+ script_version: "1".to_string(),
+ discovered: vec![],
+ injected: vec![],
+ }
+ }
+}
+
impl TestDefinitions {
/// Return the test definitions as a testing module notification.
pub fn as_notification(
@@ -137,6 +138,19 @@ impl TestDefinitions {
})
}
+ /// Register a dynamically-detected test. Returns false if a test with the
+ /// same static id was already registered statically or dynamically. Otherwise
+ /// returns true.
+ pub fn inject(&mut self, data: lsp_custom::TestData) -> bool {
+ if self.discovered.iter().any(|td| td.contains_id(&data.id))
+ || self.injected.iter().any(|td| td.id == data.id)
+ {
+ return false;
+ }
+ self.injected.push(data);
+ true
+ }
+
/// Return a test definition identified by the test ID.
pub fn get_by_id<S: AsRef<str>>(&self, id: S) -> Option<&TestDefinition> {
self
@@ -144,20 +158,4 @@ impl TestDefinitions {
.iter()
.find(|td| td.id.as_str() == id.as_ref())
}
-
- /// Return a test definition by the test name.
- pub fn get_by_name(&self, name: &str) -> Option<&TestDefinition> {
- self.discovered.iter().find(|td| td.name.as_str() == name)
- }
-
- pub fn get_step_by_name(
- &self,
- test_name: &str,
- level: usize,
- name: &str,
- ) -> Option<&TestDefinition> {
- self
- .get_by_name(test_name)
- .and_then(|td| td.find_step(name, level))
- }
}