Make Example comparable against names and sources

This will be needed for filtering examples.
This commit is contained in:
Michael Miller 2019-03-23 21:40:13 -06:00
parent 4f3ca20741
commit 2f8b4761de
3 changed files with 140 additions and 0 deletions

View file

@ -119,4 +119,66 @@ 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,4 +1565,66 @@ 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

@ -59,5 +59,21 @@ 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