Skip to content

Commit

Permalink
Make test discovery patterns configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdeviant committed Apr 21, 2024
1 parent ad0df8c commit 4e9f748
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/startest.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/option.{None}
import gleam/regex
import startest/cli
import startest/config.{type Config, Config}
import startest/reporters/default as default_reporter
Expand Down Expand Up @@ -37,5 +38,13 @@ pub fn run(config: Config) {

/// Returns the default Startest config.
pub fn default_config() -> Config {
Config(reporters: [default_reporter.new()], test_name_pattern: None)
let assert Ok(discover_describe_tests_pattern) = regex.from_string("_tests$")
let assert Ok(discover_standalone_tests_pattern) = regex.from_string("_test$")

Config(
reporters: [default_reporter.new()],
discover_describe_tests_pattern: discover_describe_tests_pattern,
discover_standalone_tests_pattern: discover_standalone_tests_pattern,
test_name_pattern: None,
)
}
33 changes: 33 additions & 0 deletions src/startest/config.gleam
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
import gleam/option.{type Option}
import gleam/regex.{type Regex}
import startest/reporters.{type Reporter}

pub type Config {
Config(
/// The list of reporters to use to report test results.
reporters: List(Reporter),
/// The pattern to use when discovering tests using the `describe` API.
///
/// If a function's name matches the pattern it will be evaluated and expected
/// to return a `TestTree` (like the ones returned by `describe` or `it`).
discover_describe_tests_pattern: Regex,
/// The pattern to use when discovering tests defined using standalone functions.
///
/// If a function's name matches the pattern it will be run as a test.
discover_standalone_tests_pattern: Regex,
/// The pattern to use to filter test names.
test_name_pattern: Option(String),
)
}

/// Updates the given `Config` with the specified `discover_describe_tests_pattern`.
pub fn with_discover_describe_tests_pattern(
config: Config,
discover_describe_tests_pattern: Regex,
) -> Config {
Config(
..config,
discover_describe_tests_pattern: discover_describe_tests_pattern,
)
}

/// Updates the given `Config` with the specified `discover_standalone_tests_pattern`.
pub fn with_discover_standalone_tests_pattern(
config: Config,
discover_standalone_tests_pattern: Regex,
) -> Config {
Config(
..config,
discover_standalone_tests_pattern: discover_standalone_tests_pattern,
)
}

/// Updates the given `Config` with the specified `test_name_pattern`.
pub fn with_test_name_pattern(
config: Config,
test_name_pattern: Option(String),
Expand Down
13 changes: 7 additions & 6 deletions src/startest/locator.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gleam/dynamic.{type Dynamic}
import gleam/list
import gleam/regex
import gleam/result.{try}
import gleam/string
import simplifile
Expand Down Expand Up @@ -31,7 +32,7 @@ pub fn identify_tests(
) -> List(TestTree) {
let #(standalone_tests, test_functions) =
test_functions
|> list.partition(is_standalone_test)
|> list.partition(is_standalone_test(_, ctx))
let standalone_tests =
standalone_tests
|> list.map(fn(named_fn) {
Expand All @@ -48,7 +49,7 @@ pub fn identify_tests(

let #(test_suites, _test_functions) =
test_functions
|> list.partition(is_test_suite)
|> list.partition(is_test_suite(_, ctx))
let test_suites =
test_suites
|> list.filter_map(fn(named_fn) {
Expand All @@ -65,16 +66,16 @@ pub fn identify_tests(
list.concat([test_suites, standalone_tests])
}

fn is_standalone_test(named_fn: NamedFunction) -> Bool {
fn is_standalone_test(named_fn: NamedFunction, ctx: Context) -> Bool {
let #(function_name, _) = named_fn

function_name
|> string.ends_with("_test")
|> regex.check(with: ctx.config.discover_standalone_tests_pattern)
}

fn is_test_suite(named_fn: NamedFunction) -> Bool {
fn is_test_suite(named_fn: NamedFunction, ctx: Context) -> Bool {
let #(function_name, _) = named_fn

function_name
|> string.ends_with("_tests")
|> regex.check(with: ctx.config.discover_describe_tests_pattern)
}

0 comments on commit 4e9f748

Please sign in to comment.