Skip to content

Commit

Permalink
Update runnable scheme for subtests
Browse files Browse the repository at this point in the history
  • Loading branch information
anilsenay committed May 24, 2024
1 parent 4c14ac3 commit 1db2f07
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
30 changes: 11 additions & 19 deletions crates/languages/src/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,9 @@ impl ContextProvider for GoContextProvider {
(GO_PACKAGE_TASK_VARIABLE.clone(), package_name.to_string())
});

let snapshot = location.buffer.read(cx).snapshot();
let point_range = location.range.to_point(&snapshot);
let start = Point::new(point_range.start.row, 0);
let end = Point::new(point_range.start.row + 1, 0);
let line = snapshot.text_for_range(start..end).peek();
let _subtest_name = variables.get(&VariableName::Custom(Cow::Borrowed("_subtest_name")));

let go_subtest_variable = extract_subtest_name(line.unwrap_or(""))
let go_subtest_variable = extract_subtest_name(_subtest_name.unwrap_or(""))
.map(|subtest_name| (GO_SUBTEST_NAME_TASK_VARIABLE.clone(), subtest_name));

Ok(TaskVariables::from_iter(
Expand Down Expand Up @@ -582,19 +578,15 @@ impl ContextProvider for GoContextProvider {
}

fn extract_subtest_name(input: &str) -> Option<String> {
GO_EXTRACT_SUBTEST_NAME_REGEX
.captures(input)
.map(|captures| {
let subtest_name = captures
.get(1)
.map(|matched| matched.as_str().replace(' ', "_"))
.unwrap_or_default();
GO_ESCAPE_SUBTEST_NAME_REGEX
.replace_all(&subtest_name, |caps: &regex::Captures| {
format!("\\{}", &caps[0])
})
.to_string()
})
let replaced_spaces = input.trim_matches('"').replace(" ", "_");

Some(
GO_ESCAPE_SUBTEST_NAME_REGEX
.replace_all(&replaced_spaces, |caps: &regex::Captures| {
format!("\\{}", &caps[0])
})
.to_string(),
)
}

#[cfg(test)]
Expand Down
41 changes: 34 additions & 7 deletions crates/languages/src/go/runnables.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,52 @@
(function_declaration name: (_) @run @_name
(#match? @_name "^Test.+"))
(#set! tag go-test)
)
) @go-test

; `t.Run`
(
(call_expression function: (_) @run @_name
(#match? @_name "^t.Run.*"))
(#set! tag go-subtest)
)
(call_expression
function: (
selector_expression
field: _ @run @_name
(#eq? @_name "Run")
)
arguments: (
argument_list
.
(interpreted_string_literal) @_subtest_name
.
(func_literal
parameters: (
parameter_list
(parameter_declaration
name: (identifier) @_param_name
type: (pointer_type
(qualified_type
package: (package_identifier) @_pkg
name: (type_identifier) @_type
(#eq? @_pkg "testing")
(#eq? @_type "T")
)
)
)
)
) @second_argument
)
)
(#set! tag go-subtest)
) @go-subtest

; Functions names start with `Benchmark`
(
(function_declaration name: (_) @run @_name
(#match? @_name "^Benchmark.+"))
(#set! tag go-benchmark)
)
) @go-benchmark

; go run
(
(function_declaration name: (_) @run @_name
(#eq? @_name "main"))
(#set! tag go-run)
)
) @go-run

0 comments on commit 1db2f07

Please sign in to comment.