mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Move example filters from === to their own types
This commit is contained in:
parent
b2ab579d8a
commit
a3c1892465
11 changed files with 113 additions and 160 deletions
21
spec/line_example_filter_spec.cr
Normal file
21
spec/line_example_filter_spec.cr
Normal 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
|
21
spec/name_example_filter_spec.cr
Normal file
21
spec/name_example_filter_spec.cr
Normal 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
|
|
@ -119,66 +119,4 @@ describe Spectator::PendingExample do
|
||||||
example.to_s.should contain(group.what.to_s)
|
example.to_s.should contain(group.what.to_s)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -1565,66 +1565,4 @@ describe Spectator::RunnableExample do
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
22
spec/source_example_filter_spec.cr
Normal file
22
spec/source_example_filter_spec.cr
Normal 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
|
|
@ -59,21 +59,5 @@ module Spectator
|
||||||
def to_json(json : ::JSON::Builder)
|
def to_json(json : ::JSON::Builder)
|
||||||
json.string(to_s)
|
json.string(to_s)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,24 +1,9 @@
|
||||||
module Spectator
|
module Spectator
|
||||||
# Checks examples to determine which should be run.
|
# Base class for all example filters.
|
||||||
class ExampleFilter
|
# Checks whether an example should be run.
|
||||||
# The criteria for filtering examples can be multiple types.
|
# Sub-classes must implement the `#includes?` method.
|
||||||
# A `String` will exact-match an example's name.
|
abstract class ExampleFilter
|
||||||
# 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
|
|
||||||
|
|
||||||
# Checks if an example is in the filter, and should be run.
|
# Checks if an example is in the filter, and should be run.
|
||||||
def includes?(example)
|
abstract def includes?(example : Example) : Bool
|
||||||
return true if @criteria.empty?
|
|
||||||
@criteria.any? do |criterion|
|
|
||||||
example === criterion
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -28,7 +28,11 @@ require "./config"
|
||||||
require "./config_builder"
|
require "./config_builder"
|
||||||
require "./config_source"
|
require "./config_source"
|
||||||
require "./command_line_arguments_config_source"
|
require "./command_line_arguments_config_source"
|
||||||
|
|
||||||
require "./example_filter"
|
require "./example_filter"
|
||||||
|
require "./source_example_filter"
|
||||||
|
require "./line_example_filter"
|
||||||
|
require "./name_example_filter"
|
||||||
|
|
||||||
require "./example_failed"
|
require "./example_failed"
|
||||||
require "./expectation_failed"
|
require "./expectation_failed"
|
||||||
|
|
13
src/spectator/line_example_filter.cr
Normal file
13
src/spectator/line_example_filter.cr
Normal 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
|
13
src/spectator/name_example_filter.cr
Normal file
13
src/spectator/name_example_filter.cr
Normal 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
|
14
src/spectator/source_example_filter.cr
Normal file
14
src/spectator/source_example_filter.cr
Normal 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
|
Loading…
Reference in a new issue