From a3c18924657310f4d2457b50a26a7c6f48d71283 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 25 Mar 2019 10:51:50 -0600 Subject: [PATCH] Move example filters from === to their own types --- spec/line_example_filter_spec.cr | 21 +++++++++ spec/name_example_filter_spec.cr | 21 +++++++++ spec/pending_example_spec.cr | 62 -------------------------- spec/runnable_example_spec.cr | 62 -------------------------- spec/source_example_filter_spec.cr | 22 +++++++++ src/spectator/example.cr | 16 ------- src/spectator/example_filter.cr | 25 +++-------- src/spectator/includes.cr | 4 ++ src/spectator/line_example_filter.cr | 13 ++++++ src/spectator/name_example_filter.cr | 13 ++++++ src/spectator/source_example_filter.cr | 14 ++++++ 11 files changed, 113 insertions(+), 160 deletions(-) create mode 100644 spec/line_example_filter_spec.cr create mode 100644 spec/name_example_filter_spec.cr create mode 100644 spec/source_example_filter_spec.cr create mode 100644 src/spectator/line_example_filter.cr create mode 100644 src/spectator/name_example_filter.cr create mode 100644 src/spectator/source_example_filter.cr diff --git a/spec/line_example_filter_spec.cr b/spec/line_example_filter_spec.cr new file mode 100644 index 0000000..ca2b1df --- /dev/null +++ b/spec/line_example_filter_spec.cr @@ -0,0 +1,21 @@ +require "./spec_helper" + +describe Spectator::LineExampleFilter do + describe "#includes?" do + context "with a matching example" do + it "is true" do + example = PassingExample.create + filter = Spectator::LineExampleFilter.new(example.source.line) + filter.includes?(example).should be_true + end + end + + context "with a non-matching example" do + it "is false" do + example = PassingExample.create + filter = Spectator::LineExampleFilter.new(example.source.line + 5) + filter.includes?(example).should be_false + end + end + end +end diff --git a/spec/name_example_filter_spec.cr b/spec/name_example_filter_spec.cr new file mode 100644 index 0000000..0fdeefb --- /dev/null +++ b/spec/name_example_filter_spec.cr @@ -0,0 +1,21 @@ +require "./spec_helper" + +describe Spectator::NameExampleFilter do + describe "#includes?" do + context "with a matching example" do + it "is true" do + example = PassingExample.create + filter = Spectator::NameExampleFilter.new(example.to_s) + filter.includes?(example).should be_true + end + end + + context "with a non-matching example" do + it "is false" do + example = PassingExample.create + filter = Spectator::NameExampleFilter.new("BOGUS") + filter.includes?(example).should be_false + end + end + end +end diff --git a/spec/pending_example_spec.cr b/spec/pending_example_spec.cr index 845ed76..6809816 100644 --- a/spec/pending_example_spec.cr +++ b/spec/pending_example_spec.cr @@ -119,66 +119,4 @@ describe Spectator::PendingExample do example.to_s.should contain(group.what.to_s) end end - - describe "#===" do - context "with a matching Regex" do - it "is true" do - example = new_pending_example - regex = Regex.new(Regex.escape(example.what)) - (example === regex).should be_true - end - end - - context "with a non-matching Regex" do - it "is false" do - example = new_pending_example - regex = /BOGUS/ - (example === regex).should be_false - end - end - - context "with a String equal to the name" do - it "is true" do - example = new_pending_example - (example === example.to_s).should be_true - end - end - - context "with a String different than the name" do - it "is false" do - example = new_pending_example - (example === "BOGUS").should be_false - end - end - - context "with a matching source location" do - it "is true" do - example = new_pending_example - (example === example.source).should be_true - end - end - - context "with a non-matching source location" do - it "is false" do - example = new_pending_example - source = Spectator::Source.new(__FILE__, __LINE__) - (example === source).should be_false - end - end - - context "with a matching source line" do - it "is true" do - example = new_pending_example - (example === example.source.line).should be_true - end - end - - context "with a non-matching source line" do - it "is false" do - example = new_pending_example - line = example.source.line + 5 - (example === line).should be_false - end - end - end end diff --git a/spec/runnable_example_spec.cr b/spec/runnable_example_spec.cr index 8fb7b1f..602dc38 100644 --- a/spec/runnable_example_spec.cr +++ b/spec/runnable_example_spec.cr @@ -1565,66 +1565,4 @@ describe Spectator::RunnableExample do end end end - - describe "#===" do - context "with a matching Regex" do - it "is true" do - example = new_runnable_example - regex = Regex.new(Regex.escape(example.what)) - (example === regex).should be_true - end - end - - context "with a non-matching Regex" do - it "is false" do - example = new_runnable_example - regex = /BOGUS/ - (example === regex).should be_false - end - end - - context "with a String equal to the name" do - it "is true" do - example = new_runnable_example - (example === example.to_s).should be_true - end - end - - context "with a String different than the name" do - it "is false" do - example = new_runnable_example - (example === "BOGUS").should be_false - end - end - - context "with a matching source location" do - it "is true" do - example = new_runnable_example - (example === example.source).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__) - (example === source).should be_false - end - end - - context "with a matching source line" do - it "is true" do - example = new_runnable_example - (example === example.source.line).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 - (example === line).should be_false - end - end - end end diff --git a/spec/source_example_filter_spec.cr b/spec/source_example_filter_spec.cr new file mode 100644 index 0000000..f8e5e0a --- /dev/null +++ b/spec/source_example_filter_spec.cr @@ -0,0 +1,22 @@ +require "./spec_helper" + +describe Spectator::SourceExampleFilter do + describe "#includes?" do + context "with a matching example" do + it "is true" do + example = PassingExample.create + filter = Spectator::SourceExampleFilter.new(example.source) + filter.includes?(example).should be_true + end + end + + context "with a non-matching example" do + it "is false" do + example = PassingExample.create + source = Spectator::Source.new(__FILE__, __LINE__) + filter = Spectator::SourceExampleFilter.new(source) + filter.includes?(example).should be_false + end + end + end +end diff --git a/src/spectator/example.cr b/src/spectator/example.cr index faa5673..d12363e 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -59,21 +59,5 @@ module Spectator def to_json(json : ::JSON::Builder) json.string(to_s) end - - # Checks if this example matches some criteria. - # This is used to filter examples. - def ===(other) - other === to_s - end - - # Checks if this example is at the specified source. - def ===(other : Source) - source == other - end - - # Checks if this example is at the specified line number. - def ===(other : Int32) - source.line === other - end end end diff --git a/src/spectator/example_filter.cr b/src/spectator/example_filter.cr index f4de2b9..0aa3ab2 100644 --- a/src/spectator/example_filter.cr +++ b/src/spectator/example_filter.cr @@ -1,24 +1,9 @@ module Spectator - # Checks examples to determine which should be run. - class ExampleFilter - # The criteria for filtering examples can be multiple types. - # A `String` will exact-match an example's name. - # A `Regex` will perform a regular expression match on the example's name. - # A `Source` will check if the example is in the specified file on a given line. - # An `Int32` will check if an example is on a given line. - alias Type = String | Regex | Source | Int32 - - # Creates the example filter. - # The *criteria* should be a list of what to filter on. - def initialize(@criteria = [] of Type) - end - + # Base class for all example filters. + # Checks whether an example should be run. + # Sub-classes must implement the `#includes?` method. + abstract class ExampleFilter # Checks if an example is in the filter, and should be run. - def includes?(example) - return true if @criteria.empty? - @criteria.any? do |criterion| - example === criterion - end - end + abstract def includes?(example : Example) : Bool end end diff --git a/src/spectator/includes.cr b/src/spectator/includes.cr index 1e6be59..c214b8f 100644 --- a/src/spectator/includes.cr +++ b/src/spectator/includes.cr @@ -28,7 +28,11 @@ require "./config" require "./config_builder" require "./config_source" require "./command_line_arguments_config_source" + require "./example_filter" +require "./source_example_filter" +require "./line_example_filter" +require "./name_example_filter" require "./example_failed" require "./expectation_failed" diff --git a/src/spectator/line_example_filter.cr b/src/spectator/line_example_filter.cr new file mode 100644 index 0000000..112e086 --- /dev/null +++ b/src/spectator/line_example_filter.cr @@ -0,0 +1,13 @@ +module Spectator + # Filter that matches examples on a given line. + class LineExampleFilter < ExampleFilter + # Creates the example filter. + def initialize(@line : Int32) + end + + # Checks whether the example satisfies the filter. + def includes?(example) + @line == example.source.line + end + end +end diff --git a/src/spectator/name_example_filter.cr b/src/spectator/name_example_filter.cr new file mode 100644 index 0000000..8ec6409 --- /dev/null +++ b/src/spectator/name_example_filter.cr @@ -0,0 +1,13 @@ +module Spectator + # Filter that matches examples based on their name. + class NameExampleFilter < ExampleFilter + # Creates the example filter. + def initialize(@name : String) + end + + # Checks whether the example satisfies the filter. + def includes?(example) + @name == example.to_s + end + end +end diff --git a/src/spectator/source_example_filter.cr b/src/spectator/source_example_filter.cr new file mode 100644 index 0000000..6eaec5f --- /dev/null +++ b/src/spectator/source_example_filter.cr @@ -0,0 +1,14 @@ +module Spectator + # Filter that matches examples in a given file and line. + class SourceExampleFilter < ExampleFilter + # Creates the filter. + # The *source* indicates which file and line the example must be on. + def initialize(@source : Source) + end + + # Checks whether the example satisfies the filter. + def includes?(example) + @source === example.source + end + end +end