Skip to content

Commit

Permalink
chore: ensure int/floats stored in pact as such, and processed at ver…
Browse files Browse the repository at this point in the history
…ification
  • Loading branch information
YOU54F committed Aug 14, 2024
1 parent 9229b11 commit f2c448c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
8 changes: 7 additions & 1 deletion lib/pact/matchers/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def calculate_diff expected, actual, opts = {}
alias_method :structure_diff, :type_diff # Backwards compatibility

def term_diff term, actual, options
if actual.is_a?(Float) || actual.is_a?(Integer)
options[:original] = actual
options[:was_float] = actual.is_a?(Float)
options[:was_int] = actual.is_a?(Integer)
actual = actual.to_s
end
if actual.is_a?(String)
actual_term_diff term, actual, options
else
Expand All @@ -72,7 +78,7 @@ def actual_term_diff term, actual, options
if term.matcher.match(actual)
NO_DIFF
else
RegexpDifference.new term.matcher, actual, "Expected a String matching #{term.matcher.inspect} (like #{term.generate.inspect}) but got #{actual.inspect} at <path>"
RegexpDifference.new term.matcher, options[:original] ||= actual, "Expected a #{options[:was_float] ? "Float" : options[:was_int] ? "Integer" : "String" } matching #{term.matcher.inspect} (like #{term.generate.inspect}) but got #{options[:was_float] || options[:was_int] ? class_name_with_value_in_brackets(options[:original]) : actual.inspect} at <path>"
end
end

Expand Down
10 changes: 6 additions & 4 deletions lib/pact/term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ def self.unpack_regexps source

def initialize(attributes = {})
@generate = attributes[:generate]
raise Pact::Error.new("Please specify a value to generate for the Term") unless @generate != nil

@generate = @generate.to_s
@matcher = attributes[:matcher]
raise Pact::Error.new("Please specify a matcher for the Term") unless @matcher != nil
raise Pact::Error.new("Value to generate '#{@generate}' does not match regular expression #{@matcher.inspect}") unless @generate =~ @matcher
raise Pact::Error.new("Please specify a value to generate for the Term") unless @generate != nil
if @generate.is_a?(Float) || @generate.is_a?(Integer)
raise Pact::Error.new("#{@generate.is_a?(Float) ? "Float" : "Integer"} Value to generate '#{@generate}' does not match regular expression #{@matcher.inspect} when converted to string") unless @generate.to_s =~ @matcher
else
raise Pact::Error.new("Value to generate '#{@generate}' does not match regular expression #{@matcher.inspect}") unless @generate =~ @matcher
end
end

def to_hash
Expand Down
18 changes: 16 additions & 2 deletions spec/lib/pact/matchers/matchers_messages_regexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ module Pact::Matchers
context "when the actual is a numeric" do
let(:actual) { INT }
it "returns a message" do
expect(difference[:thing].message).to eq "Expected a String matching /foo/ (like \"food\") but got #{a_numeric} (1) at <path>"
expect(difference[:thing].message).to eq "Expected a Integer matching /foo/ (like \"food\") but got #{a_numeric} (1) at <path>"
end
end

context "when the actual is a float" do
let(:actual) { FLOAT }
it "returns a message" do
expect(difference[:thing].message).to eq "Expected a Float matching /foo/ (like \"food\") but got #{a_float} (1.0) at <path>"
end
end

Expand All @@ -48,7 +55,14 @@ module Pact::Matchers
context "when the actual is a numeric" do
let(:actual) { INT }
it "returns a message" do
expect(difference[:thing].message).to eq "Expected a String matching /foo/ (like \"food\") but got #{a_numeric} (1) at <path>"
expect(difference[:thing].message).to eq "Expected a Integer matching /foo/ (like \"food\") but got #{a_numeric} (1) at <path>"
end
end

context "when the actual is a float" do
let(:actual) { FLOAT }
it "returns a message" do
expect(difference[:thing].message).to eq "Expected a Float matching /foo/ (like \"food\") but got #{a_float} (1.0) at <path>"
end
end

Expand Down
10 changes: 5 additions & 5 deletions spec/lib/pact/term_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ module Pact
end

context "when the generate is a integer" do
let(:term) { Term.new(generate: 10, matcher: /[0-9]/)}
let(:term) { Term.new(generate: 10, matcher: /[0-9]/)}

it 'does not raise an exception' do
term
end
end
end

context "when the generate is a float" do
let(:term) { Term.new(generate: 50.51, matcher: /\d(\.\d{1,2})/)}
let(:term) { Term.new(generate: 50.51, matcher: /\d(\.\d{1,2})/)}

it 'does not raise an exception' do
term
end
end
end

context "when the matcher does not match the generated value" do
Expand Down
4 changes: 4 additions & 0 deletions spec/support/ruby_version_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ def a_numeric
end
end
module_function :a_numeric
def a_float
"a #{Float}"
end
module_function :a_float
end

0 comments on commit f2c448c

Please sign in to comment.