mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Update RegexMatcher to use MatchData
This commit is contained in:
parent
f29aba42d2
commit
fbee1d9461
2 changed files with 151 additions and 97 deletions
|
@ -1,22 +1,25 @@
|
||||||
require "../spec_helper"
|
require "../spec_helper"
|
||||||
|
|
||||||
describe Spectator::Matchers::RegexMatcher do
|
describe Spectator::Matchers::RegexMatcher do
|
||||||
describe "#match?" do
|
describe "#match" do
|
||||||
it "compares using #=~" do
|
it "compares using #=~" do
|
||||||
spy = SpySUT.new
|
spy = SpySUT.new
|
||||||
partial = new_partial(spy)
|
partial = new_partial(spy)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(/foobar/)
|
matcher = Spectator::Matchers::RegexMatcher.new(/foobar/)
|
||||||
matcher.match?(partial).should be_true
|
matcher.match(partial)
|
||||||
spy.match_call_count.should be > 0
|
spy.match_call_count.should be > 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "returned MatchData" do
|
||||||
|
describe "#matched?" do
|
||||||
context "with a matching pattern" do
|
context "with a matching pattern" do
|
||||||
it "is true" do
|
it "is true" do
|
||||||
value = "foobar"
|
value = "foobar"
|
||||||
pattern = /foo/
|
pattern = /foo/
|
||||||
partial = new_partial(value)
|
partial = new_partial(value)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
matcher.match?(partial).should be_true
|
match_data = matcher.match(partial)
|
||||||
|
match_data.matched?.should be_true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -26,7 +29,32 @@ describe Spectator::Matchers::RegexMatcher do
|
||||||
pattern = /bar/
|
pattern = /bar/
|
||||||
partial = new_partial(value)
|
partial = new_partial(value)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
matcher.match?(partial).should be_false
|
match_data = matcher.match(partial)
|
||||||
|
match_data.matched?.should be_false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#values" do
|
||||||
|
context "expected" do
|
||||||
|
it "is the expected value" do
|
||||||
|
value = "foo"
|
||||||
|
pattern = /bar/
|
||||||
|
partial = new_partial(value)
|
||||||
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.values[:expected].should eq(pattern)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "actual" do
|
||||||
|
it "is the actual value" do
|
||||||
|
value = "foo"
|
||||||
|
pattern = /bar/
|
||||||
|
partial = new_partial(value)
|
||||||
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
|
match_data = matcher.match(partial)
|
||||||
|
match_data.values[:actual].should eq(value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -37,7 +65,8 @@ describe Spectator::Matchers::RegexMatcher do
|
||||||
pattern = /foo/
|
pattern = /foo/
|
||||||
partial = new_partial(value)
|
partial = new_partial(value)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
matcher.message(partial).should contain("=~")
|
match_data = matcher.match(partial)
|
||||||
|
match_data.message.should contain("=~")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "contains the actual label" do
|
it "contains the actual label" do
|
||||||
|
@ -46,7 +75,8 @@ describe Spectator::Matchers::RegexMatcher do
|
||||||
pattern = /foo/
|
pattern = /foo/
|
||||||
partial = new_partial(value, label)
|
partial = new_partial(value, label)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
matcher.message(partial).should contain(label)
|
match_data = matcher.match(partial)
|
||||||
|
match_data.message.should contain(label)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "contains the expected label" do
|
it "contains the expected label" do
|
||||||
|
@ -55,7 +85,8 @@ describe Spectator::Matchers::RegexMatcher do
|
||||||
pattern = /foo/
|
pattern = /foo/
|
||||||
partial = new_partial(value)
|
partial = new_partial(value)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern, label)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern, label)
|
||||||
matcher.message(partial).should contain(label)
|
match_data = matcher.match(partial)
|
||||||
|
match_data.message.should contain(label)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when expected label is omitted" do
|
context "when expected label is omitted" do
|
||||||
|
@ -64,7 +95,8 @@ describe Spectator::Matchers::RegexMatcher do
|
||||||
pattern = /foo/
|
pattern = /foo/
|
||||||
partial = new_partial(value)
|
partial = new_partial(value)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
matcher.message(partial).should contain(pattern.to_s)
|
match_data = matcher.match(partial)
|
||||||
|
match_data.message.should contain(pattern.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -75,7 +107,8 @@ describe Spectator::Matchers::RegexMatcher do
|
||||||
pattern = /foo/
|
pattern = /foo/
|
||||||
partial = new_partial(value)
|
partial = new_partial(value)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
matcher.negated_message(partial).should contain("=~")
|
match_data = matcher.match(partial)
|
||||||
|
match_data.negated_message.should contain("=~")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "contains the actual label" do
|
it "contains the actual label" do
|
||||||
|
@ -84,7 +117,8 @@ describe Spectator::Matchers::RegexMatcher do
|
||||||
pattern = /foo/
|
pattern = /foo/
|
||||||
partial = new_partial(value, label)
|
partial = new_partial(value, label)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
matcher.negated_message(partial).should contain(label)
|
match_data = matcher.match(partial)
|
||||||
|
match_data.negated_message.should contain(label)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "contains the expected label" do
|
it "contains the expected label" do
|
||||||
|
@ -93,7 +127,8 @@ describe Spectator::Matchers::RegexMatcher do
|
||||||
pattern = /foo/
|
pattern = /foo/
|
||||||
partial = new_partial(value)
|
partial = new_partial(value)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern, label)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern, label)
|
||||||
matcher.negated_message(partial).should contain(label)
|
match_data = matcher.match(partial)
|
||||||
|
match_data.negated_message.should contain(label)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when expected label is omitted" do
|
context "when expected label is omitted" do
|
||||||
|
@ -102,7 +137,10 @@ describe Spectator::Matchers::RegexMatcher do
|
||||||
pattern = /foo/
|
pattern = /foo/
|
||||||
partial = new_partial(value)
|
partial = new_partial(value)
|
||||||
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
|
||||||
matcher.negated_message(partial).should contain(pattern.to_s)
|
match_data = matcher.match(partial)
|
||||||
|
match_data.negated_message.should contain(pattern.to_s)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,27 +5,43 @@ module Spectator::Matchers
|
||||||
# The value is compared with the =~ operator.
|
# The value is compared with the =~ operator.
|
||||||
struct RegexMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
struct RegexMatcher(ExpectedType) < ValueMatcher(ExpectedType)
|
||||||
# Determines whether the matcher is satisfied with the value given to it.
|
# Determines whether the matcher is satisfied with the value given to it.
|
||||||
# True is returned if the match was successful, false otherwise.
|
private def match?(actual)
|
||||||
def match?(partial)
|
!!(actual =~ expected)
|
||||||
!!(partial.actual =~ expected)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determines whether the matcher is satisfied with the partial given to it.
|
# Determines whether the matcher is satisfied with the partial given to it.
|
||||||
# `MatchData` is returned that contains information about the match.
|
# `MatchData` is returned that contains information about the match.
|
||||||
def match(partial) : MatchData
|
def match(partial)
|
||||||
raise NotImplementedError.new("#match")
|
values = ExpectedActual.new(partial, self)
|
||||||
|
MatchData.new(match?(values.actual), values)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Match data specific to this matcher.
|
||||||
|
private struct MatchData(ExpectedType, ActualType) < MatchData
|
||||||
|
# Creates the match data.
|
||||||
|
def initialize(matched, @values : ExpectedActual(ExpectedType, ActualType))
|
||||||
|
super(matched)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Information about the match.
|
||||||
|
def values
|
||||||
|
{
|
||||||
|
expected: @values.expected,
|
||||||
|
actual: @values.actual,
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Describes the condition that satisfies the matcher.
|
# Describes the condition that satisfies the matcher.
|
||||||
# This is informational and displayed to the end-user.
|
# This is informational and displayed to the end-user.
|
||||||
def message(partial)
|
def message
|
||||||
"Expected #{partial.label} to match #{label} (using =~)"
|
"#{@values.actual_label} matches #{@values.expected_label} (using =~)"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Describes the condition that won't satsify the matcher.
|
# Describes the condition that won't satsify the matcher.
|
||||||
# This is informational and displayed to the end-user.
|
# This is informational and displayed to the end-user.
|
||||||
def negated_message(partial)
|
def negated_message
|
||||||
"Expected #{partial.label} to not match #{label} (using =~)"
|
"#{@values.actual_label} does not match #{@values.expected_label} (using =~)"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue