2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2022-03-29 18:59:27 -04:00
|
|
|
|
|
|
|
use super::lsp_custom;
|
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
use crate::lsp::analysis::source_range_to_lsp_range;
|
2022-03-29 18:59:27 -04:00
|
|
|
use crate::lsp::client::TestingNotification;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::checksum;
|
2022-03-29 18:59:27 -04:00
|
|
|
|
2022-05-20 16:40:55 -04:00
|
|
|
use deno_ast::SourceRange;
|
2022-03-29 18:59:27 -04:00
|
|
|
use deno_ast::SourceTextInfo;
|
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use std::collections::HashMap;
|
2022-04-03 00:17:30 -04:00
|
|
|
use tower_lsp::lsp_types as lsp;
|
2022-03-29 18:59:27 -04:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct TestDefinition {
|
|
|
|
pub id: String,
|
|
|
|
pub name: String,
|
2022-05-20 16:40:55 -04:00
|
|
|
pub range: SourceRange,
|
2022-07-15 13:09:22 -04:00
|
|
|
pub steps: Vec<TestDefinition>,
|
2022-03-29 18:59:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestDefinition {
|
|
|
|
pub fn new(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
name: String,
|
2022-05-20 16:40:55 -04:00
|
|
|
range: SourceRange,
|
2023-08-27 05:16:09 -04:00
|
|
|
mut steps: Vec<TestDefinition>,
|
2022-03-29 18:59:27 -04:00
|
|
|
) -> Self {
|
2023-08-27 05:16:09 -04:00
|
|
|
let mut id_components = Vec::with_capacity(7);
|
|
|
|
id_components.push(specifier.as_str().as_bytes());
|
|
|
|
id_components.push(name.as_bytes());
|
|
|
|
let id = checksum::gen(&id_components);
|
|
|
|
Self::fix_ids(&mut steps, &mut id_components);
|
2022-03-29 18:59:27 -04:00
|
|
|
Self {
|
|
|
|
id,
|
|
|
|
name,
|
2022-05-20 16:40:55 -04:00
|
|
|
range,
|
2022-03-29 18:59:27 -04:00
|
|
|
steps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 05:16:09 -04:00
|
|
|
fn fix_ids<'a>(
|
|
|
|
steps: &'a mut Vec<TestDefinition>,
|
|
|
|
id_components: &mut Vec<&'a [u8]>,
|
|
|
|
) {
|
|
|
|
for step in steps {
|
|
|
|
id_components.push(step.name.as_bytes());
|
|
|
|
step.id = checksum::gen(id_components);
|
|
|
|
Self::fix_ids(&mut step.steps, id_components);
|
|
|
|
id_components.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:59:27 -04:00
|
|
|
pub fn new_step(
|
|
|
|
name: String,
|
2022-05-20 16:40:55 -04:00
|
|
|
range: SourceRange,
|
2022-07-15 13:09:22 -04:00
|
|
|
steps: Vec<TestDefinition>,
|
2022-03-29 18:59:27 -04:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2023-08-27 05:16:09 -04:00
|
|
|
// ID will be fixed later when the entire ancestry is available.
|
|
|
|
id: "".to_string(),
|
2022-03-29 18:59:27 -04:00
|
|
|
name,
|
2022-05-20 16:40:55 -04:00
|
|
|
range,
|
2022-03-29 18:59:27 -04:00
|
|
|
steps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_test_data(
|
|
|
|
&self,
|
|
|
|
source_text_info: &SourceTextInfo,
|
|
|
|
) -> lsp_custom::TestData {
|
|
|
|
lsp_custom::TestData {
|
|
|
|
id: self.id.clone(),
|
|
|
|
label: self.name.clone(),
|
2022-07-15 13:09:22 -04:00
|
|
|
steps: self
|
|
|
|
.steps
|
|
|
|
.iter()
|
|
|
|
.map(|step| step.as_test_data(source_text_info))
|
|
|
|
.collect(),
|
2022-05-20 16:40:55 -04:00
|
|
|
range: Some(source_range_to_lsp_range(&self.range, source_text_info)),
|
2022-03-29 18:59:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
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))
|
2022-03-29 18:59:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct TestDefinitions {
|
|
|
|
/// definitions of tests and their steps which were statically discovered from
|
|
|
|
/// the source document.
|
|
|
|
pub discovered: Vec<TestDefinition>,
|
|
|
|
/// Tests and steps which the test runner notified us of, which were
|
|
|
|
/// dynamically added
|
|
|
|
pub injected: Vec<lsp_custom::TestData>,
|
|
|
|
/// The version of the document that the discovered tests relate to.
|
|
|
|
pub script_version: String,
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
impl Default for TestDefinitions {
|
|
|
|
fn default() -> Self {
|
|
|
|
TestDefinitions {
|
|
|
|
script_version: "1".to_string(),
|
|
|
|
discovered: vec![],
|
|
|
|
injected: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:59:27 -04:00
|
|
|
impl TestDefinitions {
|
|
|
|
/// Return the test definitions as a testing module notification.
|
|
|
|
pub fn as_notification(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
maybe_root: Option<&ModuleSpecifier>,
|
|
|
|
source_text_info: &SourceTextInfo,
|
|
|
|
) -> TestingNotification {
|
|
|
|
let label = if let Some(root) = maybe_root {
|
|
|
|
specifier.as_str().replace(root.as_str(), "")
|
|
|
|
} else {
|
|
|
|
specifier
|
|
|
|
.path_segments()
|
|
|
|
.and_then(|s| s.last().map(|s| s.to_string()))
|
|
|
|
.unwrap_or_else(|| "<unknown>".to_string())
|
|
|
|
};
|
|
|
|
let mut tests_map: HashMap<String, lsp_custom::TestData> = self
|
|
|
|
.injected
|
|
|
|
.iter()
|
|
|
|
.map(|td| (td.id.clone(), td.clone()))
|
|
|
|
.collect();
|
|
|
|
tests_map.extend(self.discovered.iter().map(|td| {
|
|
|
|
let test_data = td.as_test_data(source_text_info);
|
|
|
|
(test_data.id.clone(), test_data)
|
|
|
|
}));
|
|
|
|
TestingNotification::Module(lsp_custom::TestModuleNotificationParams {
|
|
|
|
text_document: lsp::TextDocumentIdentifier {
|
|
|
|
uri: specifier.clone(),
|
|
|
|
},
|
|
|
|
kind: lsp_custom::TestModuleNotificationKind::Replace,
|
|
|
|
label,
|
|
|
|
tests: tests_map.into_values().collect(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:59:27 -04:00
|
|
|
/// Return a test definition identified by the test ID.
|
|
|
|
pub fn get_by_id<S: AsRef<str>>(&self, id: S) -> Option<&TestDefinition> {
|
|
|
|
self
|
|
|
|
.discovered
|
|
|
|
.iter()
|
|
|
|
.find(|td| td.id.as_str() == id.as_ref())
|
|
|
|
}
|
2023-08-25 05:12:11 -04:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.discovered.is_empty() && self.injected.is_empty()
|
|
|
|
}
|
2022-03-29 18:59:27 -04:00
|
|
|
}
|