Workaround for mashup of compile-time types

This commit is contained in:
Michael Miller 2019-03-06 18:40:57 -07:00
parent 018e3232cd
commit a9d1f1aabc
2 changed files with 31 additions and 8 deletions

View file

@ -0,0 +1,23 @@
# 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)
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
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.values[:lower].value.should eq(range.begin)
match_data_prefix(match_data, :lower, Int32)[: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.values[:lower].to_s.should start_with(">=")
match_data_prefix(match_data, :lower, Int32)[: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.values[:upper].value.should eq(range.end)
match_data_prefix(match_data, :upper, Int32)[: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.values[:upper].to_s.should start_with("<=")
match_data_prefix(match_data, :upper, Int32)[: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.values[:upper].to_s.should start_with("<")
match_data_prefix(match_data, :upper, Int32)[: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.values[:actual].should eq(value)
match_data_value(match_data, :actual, Int32).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.values[:set].should eq(array)
match_data_value(match_data, :set, typeof(array)).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.values[:actual].should eq(value)
match_data_value(match_data, :actual, typeof(value)).should eq(value)
end
end
end