From d1767ebb7a8bf993d3ad1b01932bb5bf1f66a385 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 25 Mar 2019 11:10:27 -0600 Subject: [PATCH] Add CompositeExampleFilter --- spec/composite_example_filter_spec.cr | 33 +++++++++ spec/example_filter_spec.cr | 85 ----------------------- src/spectator/composite_example_filter.cr | 13 ++++ src/spectator/includes.cr | 1 + 4 files changed, 47 insertions(+), 85 deletions(-) create mode 100644 spec/composite_example_filter_spec.cr delete mode 100644 spec/example_filter_spec.cr create mode 100644 src/spectator/composite_example_filter.cr diff --git a/spec/composite_example_filter_spec.cr b/spec/composite_example_filter_spec.cr new file mode 100644 index 0000000..0319712 --- /dev/null +++ b/spec/composite_example_filter_spec.cr @@ -0,0 +1,33 @@ +require "./spec_helper" + +describe Spectator::CompositeExampleFilter do + describe "#includes?" do + context "with a matching filter" do + it "is true" do + example = PassingExample.create + filters = [Spectator::NullExampleFilter.new.as(Spectator::ExampleFilter)] + filter = Spectator::CompositeExampleFilter.new(filters) + filter.includes?(example).should be_true + end + end + + context "with a non-matching filter" do + it "is false" do + example = PassingExample.create + source = Spectator::Source.new(__FILE__, __LINE__) + filters = [Spectator::SourceExampleFilter.new(source).as(Spectator::ExampleFilter)] + filter = Spectator::CompositeExampleFilter.new(filters) + filter.includes?(example).should be_false + end + end + + context "with no filters" do + it "is false" do + example = PassingExample.create + filters = [] of Spectator::ExampleFilter + filter = Spectator::CompositeExampleFilter.new(filters) + filter.includes?(example).should be_false + end + end + end +end diff --git a/spec/example_filter_spec.cr b/spec/example_filter_spec.cr deleted file mode 100644 index a4a991d..0000000 --- a/spec/example_filter_spec.cr +++ /dev/null @@ -1,85 +0,0 @@ -require "./spec_helper" - -def new_filter(criterion) - criteria = ["BOGUS", -1, criterion, /^$/, Spectator::Source.new(__FILE__, __LINE__)] - Spectator::ExampleFilter.new(criteria) -end - -describe Spectator::ExampleFilter do - describe "#includes?" do - context "with a matching Regex" do - it "is true" do - example = new_runnable_example - regex = Regex.new(Regex.escape(example.what)) - filter = new_filter(regex) - filter.includes?(example).should be_true - end - end - - context "with a non-matching Regex" do - it "is false" do - example = new_runnable_example - regex = /BOGUS/ - filter = new_filter(regex) - filter.includes?(example).should be_false - end - end - - context "with a String equal to the name" do - it "is true" do - example = new_runnable_example - filter = new_filter(example.to_s) - filter.includes?(example).should be_true - end - end - - context "with a String different than the name" do - it "is false" do - example = new_runnable_example - filter = new_filter("FAKE") - filter.includes?(example).should be_false - end - end - - context "with a matching source location" do - it "is true" do - example = new_runnable_example - filter = new_filter(example.source) - filter.includes?(example).should be_true - end - end - - context "with a non-matching source location" do - it "is false" do - example = new_runnable_example - source = Spectator::Source.new(__FILE__, __LINE__) - filter = new_filter(source) - filter.includes?(example).should be_false - end - end - - context "with a matching source line" do - it "is true" do - example = new_runnable_example - filter = new_filter(example.source.line) - filter.includes?(example).should be_true - end - end - - context "with a non-matching source line" do - it "is false" do - example = new_runnable_example - line = example.source.line + 5 - filter = new_filter(line) - filter.includes?(example).should be_false - end - end - - context "with an empty criteria" do - it "is true" do - filter = Spectator::ExampleFilter.new - filter.includes?(new_runnable_example).should be_true - end - end - end -end diff --git a/src/spectator/composite_example_filter.cr b/src/spectator/composite_example_filter.cr new file mode 100644 index 0000000..0b1f9d3 --- /dev/null +++ b/src/spectator/composite_example_filter.cr @@ -0,0 +1,13 @@ +module Spectator + # Filter that combines multiple other filters. + class CompositeExampleFilter < ExampleFilter + # Creates the example filter. + def initialize(@filters : Array(ExampleFilter)) + end + + # Checks whether the example satisfies the filter. + def includes?(example) + @filters.any?(&.includes?(example)) + end + end +end diff --git a/src/spectator/includes.cr b/src/spectator/includes.cr index 19412a2..915732f 100644 --- a/src/spectator/includes.cr +++ b/src/spectator/includes.cr @@ -34,6 +34,7 @@ require "./source_example_filter" require "./line_example_filter" require "./name_example_filter" require "./null_example_filter" +require "./composite_example_filter" require "./example_failed" require "./expectation_failed"