Fix tests to account for negation wrappers

This commit is contained in:
Michael Miller 2019-03-07 16:12:23 -07:00
parent 37f2ce99a3
commit 0ca0ba0a55
19 changed files with 53 additions and 45 deletions

View file

@ -46,7 +46,22 @@ describe Spectator::Expectations::Expectation do
value = 42
match_data = new_matcher(value).match(new_partial(value))
expectation = Spectator::Expectations::Expectation.new(match_data, false)
expectation.values.should eq(match_data.values)
expectation_values = expectation.values
match_data.values.each do |k, v|
expectation_values.has_key?(k).should be_true
expectation_values[k].to_s.should eq(v.to_s)
end
end
context "when negated" do
it "negates all negatable values" do
value = 42
match_data = new_matcher(value).match(new_partial(value))
expectation = Spectator::Expectations::Expectation.new(match_data, true)
expectation.values.each_value do |value|
value.to_s.should start_with(/not/i) if value.responds_to?(:negate)
end
end
end
end

View file

@ -1,23 +1,16 @@
# Retrieves a value from the `NamedTuple` returned by `Spectator::Matchers::MatchData#values`.
def match_data_value(match_data, key, t : T.class) forall T
match_data.values.fetch(key) { raise "#{key} is missing" }.as(T)
def match_data_value(match_data, key)
match_data.values.fetch(key) { raise "#{key} is missing" }
end
# Retrieves the string representation and base value
# from a `Spectator::Matchers::PrefixedValue`
# in a `NamedTuple` returned by `Spectator::Matchers::MatchData#values`.
def match_data_prefix(match_data, key, t : T.class) forall T
prefix = match_data_value(match_data, key, Spectator::Matchers::PrefixGrabber.get(t))
{to_s: prefix.to_s, value: prefix.value}
end
# Dirty cheat to get around visibility restriction.
module Spectator::Matchers
module PrefixGrabber
extend self
def get(t : T.class) forall T
PrefixedValue(T)
end
def match_data_prefix(match_data, key)
prefix = match_data.values.fetch(key) { raise "#{key} is missing" }
if prefix.responds_to?(:value)
{to_s: prefix.to_s, value: prefix.value}
else
{to_s: prefix.to_s, value: prefix}
end
end

View file

@ -274,9 +274,9 @@ describe Spectator::Matchers::AttributesMatcher do
matcher = Spectator::Matchers::AttributesMatcher.new(attributes)
match_data = matcher.match(partial)
values = match_data.values
values[:"expected first"].should eq(attributes[:first])
values[:"expected last"].should eq(attributes[:last])
values[:"expected size"].should eq(attributes[:size])
values[:"expected first"].value.should eq(attributes[:first])
values[:"expected last"].value.should eq(attributes[:last])
values[:"expected size"].value.should eq(attributes[:size])
end
it "has the actual values" do

View file

@ -99,7 +99,7 @@ describe Spectator::Matchers::CaseMatcher do
partial = new_partial(actual)
matcher = Spectator::Matchers::CaseMatcher.new(expected)
match_data = matcher.match(partial)
match_data.values[:expected].should eq(expected)
match_data.values[:expected].value.should eq(expected)
end
end

View file

@ -300,7 +300,7 @@ describe Spectator::Matchers::ContainMatcher do
partial = new_partial(array)
matcher = Spectator::Matchers::ContainMatcher.new(search)
match_data = matcher.match(partial)
match_data.values[:subset].should eq(search)
match_data.values[:subset].value.should eq(search)
end
end

View file

@ -32,7 +32,7 @@ describe Spectator::Matchers::EmptyMatcher do
partial = new_partial(array)
matcher = Spectator::Matchers::EmptyMatcher.new
match_data = matcher.match(partial)
match_data.values[:expected].size.should eq(0)
match_data.values[:expected].value.size.should eq(0)
end
end

View file

@ -213,7 +213,7 @@ describe Spectator::Matchers::EndWithMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::EndWithMatcher.new(last)
match_data = matcher.match(partial)
match_data.values[:expected].should eq(last)
match_data.values[:expected].value.should eq(last)
end
end

View file

@ -76,7 +76,7 @@ describe Spectator::Matchers::EqualityMatcher do
partial = new_partial(actual)
matcher = Spectator::Matchers::EqualityMatcher.new(expected)
match_data = matcher.match(partial)
match_data.values[:expected].should eq(expected)
match_data.values[:expected].value.should eq(expected)
end
end

View file

@ -67,7 +67,7 @@ describe Spectator::Matchers::HaveKeyMatcher do
partial = new_partial(tuple)
matcher = Spectator::Matchers::HaveKeyMatcher.new(key)
match_data = matcher.match(partial)
match_data.values[:key].should eq(key)
match_data.values[:key].value.should eq(key)
end
end

View file

@ -518,7 +518,7 @@ describe Spectator::Matchers::HaveMatcher do
partial = new_partial(array)
matcher = Spectator::Matchers::HaveMatcher.new(search)
match_data = matcher.match(partial)
match_data.values[:subset].should eq(search)
match_data.values[:subset].value.should eq(search)
end
end

View file

@ -41,7 +41,7 @@ describe Spectator::Matchers::HaveValueMatcher do
partial = new_partial(hash)
matcher = Spectator::Matchers::HaveValueMatcher.new(value)
match_data = matcher.match(partial)
match_data.values[:value].should eq(value)
match_data.values[:value].value.should eq(value)
end
end

View file

@ -31,7 +31,7 @@ describe Spectator::Matchers::NilMatcher do
partial = new_partial(42)
matcher = Spectator::Matchers::NilMatcher.new
match_data = matcher.match(partial)
match_data.values[:expected].should eq(nil)
match_data.values[:expected].value.should eq(nil)
end
end

View file

@ -157,7 +157,7 @@ describe Spectator::Matchers::RangeMatcher do
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data_prefix(match_data, :lower, Int32)[:value].should eq(range.begin)
match_data_prefix(match_data, :lower)[:value].should eq(range.begin)
end
it "is prefixed with >=" do
@ -165,7 +165,7 @@ describe Spectator::Matchers::RangeMatcher do
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data_prefix(match_data, :lower, Int32)[:to_s].should start_with(">=")
match_data_prefix(match_data, :lower)[:to_s].should start_with(">=")
end
end
@ -175,7 +175,7 @@ describe Spectator::Matchers::RangeMatcher do
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data_prefix(match_data, :upper, Int32)[:value].should eq(range.end)
match_data_prefix(match_data, :upper)[:value].should eq(range.end)
end
context "when inclusive" do
@ -184,7 +184,7 @@ describe Spectator::Matchers::RangeMatcher do
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data_prefix(match_data, :upper, Int32)[:to_s].should start_with("<=")
match_data_prefix(match_data, :upper)[:to_s].should start_with("<=")
end
end
@ -194,7 +194,7 @@ describe Spectator::Matchers::RangeMatcher do
partial = new_partial(5)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data_prefix(match_data, :upper, Int32)[:to_s].should start_with("<")
match_data_prefix(match_data, :upper)[:to_s].should start_with("<")
end
end
end
@ -206,7 +206,7 @@ describe Spectator::Matchers::RangeMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(range)
match_data = matcher.match(partial)
match_data_value(match_data, :actual, Int32).should eq(value)
match_data_value(match_data, :actual).should eq(value)
end
end
end
@ -219,7 +219,7 @@ describe Spectator::Matchers::RangeMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(array)
match_data = matcher.match(partial)
match_data_value(match_data, :set, typeof(array)).should eq(array)
match_data_prefix(match_data, :set)[:value].should eq(array)
end
end
@ -230,7 +230,7 @@ describe Spectator::Matchers::RangeMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::RangeMatcher.new(array)
match_data = matcher.match(partial)
match_data_value(match_data, :actual, typeof(value)).should eq(value)
match_data_value(match_data, :actual).should eq(value)
end
end
end

View file

@ -43,7 +43,7 @@ describe Spectator::Matchers::RegexMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::RegexMatcher.new(pattern)
match_data = matcher.match(partial)
match_data.values[:expected].should eq(pattern)
match_data.values[:expected].value.should eq(pattern)
end
end

View file

@ -213,7 +213,7 @@ describe Spectator::Matchers::StartWithMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::StartWithMatcher.new(first)
match_data = matcher.match(partial)
match_data.values[:expected].should eq(first)
match_data.values[:expected].value.should eq(first)
end
end

View file

@ -58,7 +58,7 @@ describe Spectator::Matchers::TruthyMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::TruthyMatcher.new(true)
match_data = matcher.match(partial)
match_data.values[:expected].should match(/false or nil/i)
match_data.values[:expected].to_s.should match(/false or nil/i)
end
it "is prefixed with \"Not\"" do
@ -66,7 +66,7 @@ describe Spectator::Matchers::TruthyMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::TruthyMatcher.new(true)
match_data = matcher.match(partial)
match_data.values[:expected].should start_with(/not/i)
match_data.values[:expected].to_s.should start_with(/not/i)
end
end
@ -194,7 +194,7 @@ describe Spectator::Matchers::TruthyMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::TruthyMatcher.new(false)
match_data = matcher.match(partial)
match_data.values[:expected].should match(/false or nil/i)
match_data.values[:expected].to_s.should match(/false or nil/i)
end
it "is not prefixed with \"Not\"" do
@ -202,7 +202,7 @@ describe Spectator::Matchers::TruthyMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::TruthyMatcher.new(false)
match_data = matcher.match(partial)
match_data.values[:expected].should_not start_with(/not/i)
match_data.values[:expected].to_s.should_not start_with(/not/i)
end
end

View file

@ -62,7 +62,7 @@ describe Spectator::Matchers::TypeMatcher do
partial = new_partial(value)
matcher = Spectator::Matchers::TypeMatcher(String).new
match_data = matcher.match(partial)
match_data.values[:expected].should eq(String)
match_data.values[:expected].value.should eq(String)
end
end

View file

@ -14,7 +14,7 @@ module Spectator::Matchers
# Returns the correct value based on the negated status.
def value
@negated ? @value1 : @value2
@negated ? @value2 : @value1
end
# Produces a stringified value.

View file

@ -4,7 +4,7 @@ module Spectator::Matchers
private class NegatablePrefixedValue(T)
# Negatable value.
getter value
# Creates the wrapper.
def initialize(@positive_prefix : String, @negative_prefix : String, @value : T)
@negated = false