Skip to content

Commit

Permalink
Use filters instead of select/reject
Browse files Browse the repository at this point in the history
  • Loading branch information
icy-arctic-fox committed Dec 24, 2024
1 parent 85d6a9a commit 6618939
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
21 changes: 15 additions & 6 deletions src/spectator/core/filters.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ require "./example"
require "./location"

module Spectator::Core
abstract struct Filter
abstract class Filter
abstract def matches?(example : Example)
end

struct ExampleFilter < Filter
class ExampleFilter < Filter
def initialize(@substring : String)
end

Expand All @@ -15,7 +15,7 @@ module Spectator::Core
end
end

struct LineFilter < Filter
class LineFilter < Filter
def initialize(@line : Int32)
end

Expand All @@ -24,7 +24,7 @@ module Spectator::Core
end
end

struct LocationFilter < Filter
class LocationFilter < Filter
def initialize(@location : Location)
end

Expand All @@ -33,7 +33,7 @@ module Spectator::Core
end
end

struct TagFilter < Filter
class TagFilter < Filter
@tags = [] of {String, String?}

def matches?(example : Example)
Expand All @@ -48,7 +48,16 @@ module Spectator::Core
end
end

struct CompoundFilter < Filter
class NegatedFilter < Filter
def initialize(@filter : Filter)
end

def matches?(example : Example)
!@filter.matches?(example)
end
end

class CompoundFilter < Filter
def initialize(@filters : Array(Filter))
end

Expand Down
9 changes: 5 additions & 4 deletions src/spectator/core/runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require "./example"
require "./example_group"
require "./execution_result"
require "./fail_reason"
require "./filters"
require "./sandbox"

module Spectator::Core
Expand Down Expand Up @@ -60,14 +61,14 @@ module Spectator::Core
end

private def examples_to_run(group : ExampleGroup) : Array(Example)
examples = group.select(Example)
if filter = @configuration.inclusion_filter
examples.select! { |example| filter.matches?(example) }
group.filter(filter)
end
if filter = @configuration.exclusion_filter
examples.reject! { |example| filter.matches?(example) }
filter = NegatedFilter.new(filter)
group.filter(filter)
end
examples
group.select(Example)
end

private def run_example(example : Example) : ExecutionResult
Expand Down

0 comments on commit 6618939

Please sign in to comment.