Skip to content

Commit

Permalink
refactor: allocate IDs for tests (denoland#14729)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Jul 15, 2022
1 parent 635eed9 commit 22a4998
Show file tree
Hide file tree
Showing 12 changed files with 793 additions and 843 deletions.
54 changes: 22 additions & 32 deletions cli/lsp/testing/collectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn arrow_to_steps(
parent: &str,
level: usize,
arrow_expr: &ast::ArrowExpr,
) -> Option<Vec<TestDefinition>> {
) -> Vec<TestDefinition> {
if let Some((maybe_test_context, maybe_step_var)) =
parse_test_context_param(arrow_expr.params.get(0))
{
Expand All @@ -26,14 +26,9 @@ fn arrow_to_steps(
maybe_step_var,
);
arrow_expr.body.visit_with(&mut collector);
let steps = collector.take();
if !steps.is_empty() {
Some(steps)
} else {
None
}
collector.take()
} else {
None
vec![]
}
}

Expand All @@ -42,7 +37,7 @@ fn fn_to_steps(
parent: &str,
level: usize,
function: &ast::Function,
) -> Option<Vec<TestDefinition>> {
) -> Vec<TestDefinition> {
if let Some((maybe_test_context, maybe_step_var)) =
parse_test_context_param(function.params.get(0).map(|p| &p.pat))
{
Expand All @@ -53,14 +48,9 @@ fn fn_to_steps(
maybe_step_var,
);
function.body.visit_with(&mut collector);
let steps = collector.take();
if !steps.is_empty() {
Some(steps)
} else {
None
}
collector.take()
} else {
None
vec![]
}
}

Expand Down Expand Up @@ -139,12 +129,12 @@ fn check_call_expr(
parent: &str,
node: &ast::CallExpr,
level: usize,
) -> Option<(String, Option<Vec<TestDefinition>>)> {
) -> Option<(String, Vec<TestDefinition>)> {
if let Some(expr) = node.args.get(0).map(|es| es.expr.as_ref()) {
match expr {
ast::Expr::Object(obj_lit) => {
let mut maybe_name = None;
let mut steps = None;
let mut steps = vec![];
for prop in &obj_lit.props {
if let ast::PropOrSpread::Prop(prop) = prop {
match prop.as_ref() {
Expand Down Expand Up @@ -203,7 +193,7 @@ fn check_call_expr(
}
ast::Expr::Lit(ast::Lit::Str(lit_str)) => {
let name = lit_str.value.to_string();
let mut steps = None;
let mut steps = vec![];
match node.args.get(1).map(|es| es.expr.as_ref()) {
Some(ast::Expr::Fn(fn_expr)) => {
steps = fn_to_steps(parent, level, &fn_expr.function);
Expand Down Expand Up @@ -256,7 +246,7 @@ impl TestStepCollector {
&mut self,
name: N,
range: SourceRange,
steps: Option<Vec<TestDefinition>>,
steps: Vec<TestDefinition>,
) {
let step = TestDefinition::new_step(
name.as_ref().to_string(),
Expand Down Expand Up @@ -388,7 +378,7 @@ impl TestCollector {
&mut self,
name: N,
range: SourceRange,
steps: Option<Vec<TestDefinition>>,
steps: Vec<TestDefinition>,
) {
let definition = TestDefinition::new(
&self.specifier,
Expand Down Expand Up @@ -553,59 +543,59 @@ pub mod tests {
level: 0,
name: "test a".to_string(),
range: new_range(12, 16),
steps: Some(vec![
steps: vec![
TestDefinition {
id: "4c7333a1e47721631224408c467f32751fe34b876cab5ec1f6ac71980ff15ad3".to_string(),
level: 1,
name: "a step".to_string(),
range: new_range(83, 87),
steps: Some(vec![
steps: vec![
TestDefinition {
id: "abf356f59139b77574089615f896a6f501c010985d95b8a93abeb0069ccb2201".to_string(),
level: 2,
name: "sub step".to_string(),
range: new_range(132, 136),
steps: None,
steps: vec![],
}
])
]
}
]),
],
},
TestDefinition {
id: "86b4c821900e38fc89f24bceb0e45193608ab3f9d2a6019c7b6a5aceff5d7df2".to_string(),
level: 0,
name: "useFnName".to_string(),
range: new_range(254, 258),
steps: Some(vec![
steps: vec![
TestDefinition {
id: "67a390d0084ae5fb88f3510c470a72a553581f1d0d5ba5fa89aee7a754f3953a".to_string(),
level: 1,
name: "step c".to_string(),
range: new_range(313, 314),
steps: None,
steps: vec![],
}
])
]
},
TestDefinition {
id: "580eda89d7f5e619774c20e13b7d07a8e77c39cba101d60565144d48faa837cb".to_string(),
level: 0,
name: "test b".to_string(),
range: new_range(358, 362),
steps: None,
steps: vec![],
},
TestDefinition {
id: "0b7c6bf3cd617018d33a1bf982a08fe088c5bb54fcd5eb9e802e7c137ec1af94".to_string(),
level: 0,
name: "test c".to_string(),
range: new_range(420, 424),
steps: None,
steps: vec![],
},
TestDefinition {
id: "69d9fe87f64f5b66cb8b631d4fd2064e8224b8715a049be54276c42189ff8f9f".to_string(),
level: 0,
name: "test d".to_string(),
range: new_range(480, 481),
steps: None,
steps: vec![],
}
]
);
Expand Down
70 changes: 34 additions & 36 deletions cli/lsp/testing/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ pub struct TestDefinition {
pub level: usize,
pub name: String,
pub range: SourceRange,
pub steps: Option<Vec<TestDefinition>>,
pub steps: Vec<TestDefinition>,
}

impl TestDefinition {
pub fn new(
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 {
Expand All @@ -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(),
Expand All @@ -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))
}
}

Expand All @@ -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(
Expand Down Expand Up @@ -137,27 +138,24 @@ 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
.discovered
.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))
}
}
Loading

0 comments on commit 22a4998

Please sign in to comment.