Move example filters from === to their own types

This commit is contained in:
Michael Miller 2019-03-25 10:51:50 -06:00
parent b2ab579d8a
commit a3c1892465
11 changed files with 113 additions and 160 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"

View file

@ -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

View file

@ -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

View file

@ -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