diff --git a/src/spectator/core/filters.cr b/src/spectator/core/filters.cr index 7873970a..6600a64f 100644 --- a/src/spectator/core/filters.cr +++ b/src/spectator/core/filters.cr @@ -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 @@ -15,7 +15,7 @@ module Spectator::Core end end - struct LineFilter < Filter + class LineFilter < Filter def initialize(@line : Int32) end @@ -24,7 +24,7 @@ module Spectator::Core end end - struct LocationFilter < Filter + class LocationFilter < Filter def initialize(@location : Location) end @@ -33,7 +33,7 @@ module Spectator::Core end end - struct TagFilter < Filter + class TagFilter < Filter @tags = [] of {String, String?} def matches?(example : Example) @@ -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 diff --git a/src/spectator/core/runner.cr b/src/spectator/core/runner.cr index 6c35898e..4b950010 100644 --- a/src/spectator/core/runner.cr +++ b/src/spectator/core/runner.cr @@ -5,6 +5,7 @@ require "./example" require "./example_group" require "./execution_result" require "./fail_reason" +require "./filters" require "./sandbox" module Spectator::Core @@ -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