diff --git a/lib/pact/helpers.rb b/lib/pact/helpers.rb index c6c8228..3059dc0 100644 --- a/lib/pact/helpers.rb +++ b/lib/pact/helpers.rb @@ -47,6 +47,16 @@ def like_date date Pact::Term.new(generate: date, matcher: /^\d{4}-[01]\d-[0-3]\d$/) end + # regex matched with pact-jvm + # https://github.com/pact-foundation/pact-jvm/blob/00442e6df51e5be906ed470b19859246312e5c83/core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/MatcherExecutor.kt#L56-L59 + def like_integer int + Pact::Term.new(generate: int, matcher: /^-?\d+$/) + end + + def like_decimal float + Pact::Term.new(generate: float, matcher: /^0|-?\d+\.\d*$/) + end + def like_datetime_rfc822 datetime Pact::Term.new( generate: datetime, diff --git a/lib/pact/matchers/matchers.rb b/lib/pact/matchers/matchers.rb index 8f5bb88..c6c5395 100644 --- a/lib/pact/matchers/matchers.rb +++ b/lib/pact/matchers/matchers.rb @@ -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 @@ -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 " + RegexpDifference.new term.matcher, options[:original] ||= actual, "Expected a Value 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 " end end diff --git a/lib/pact/term.rb b/lib/pact/term.rb index 3e7647b..69f8ce9 100644 --- a/lib/pact/term.rb +++ b/lib/pact/term.rb @@ -29,7 +29,11 @@ def initialize(attributes = {}) @matcher = attributes[:matcher] raise Pact::Error.new("Please specify a matcher for the Term") unless @matcher != nil raise Pact::Error.new("Please specify a value to generate for the Term") unless @generate != nil - raise Pact::Error.new("Value to generate '#{@generate}' does not match regular expression #{@matcher.inspect}") unless @generate =~ @matcher + 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 diff --git a/spec/lib/pact/helpers_spec.rb b/spec/lib/pact/helpers_spec.rb index 86a6eff..001a257 100644 --- a/spec/lib/pact/helpers_spec.rb +++ b/spec/lib/pact/helpers_spec.rb @@ -139,5 +139,26 @@ module Pact end end + + describe "#like_integer" do + let(:integer) { 1 } + + it "creates a Pact::Term with regex matcher for integers" do + expect(like_integer(integer)).to eq Pact::Term.new( + generate: integer, + matcher: /^-?\d+$/ + ) + end + end + describe "#like_decimal" do + let(:float) { 10.2 } + + it "creates a Pact::Term with regex matcher for decimals" do + expect(like_decimal(10.2)).to eq Pact::Term.new( + generate: float, + matcher: /^0|-?\d+\.\d*$/ + ) + end + end end end diff --git a/spec/lib/pact/matchers/matchers_messages_regexp_spec.rb b/spec/lib/pact/matchers/matchers_messages_regexp_spec.rb index 4eadeda..8be9204 100644 --- a/spec/lib/pact/matchers/matchers_messages_regexp_spec.rb +++ b/spec/lib/pact/matchers/matchers_messages_regexp_spec.rb @@ -27,28 +27,28 @@ module Pact::Matchers context "when the Pact::Term does not match" do it "returns a message" do - expect(difference[:thing].message).to eq "Expected a String matching /foo/ (like \"food\") but got \"drink\" at " + expect(difference[:thing].message).to eq "Expected a Value matching /foo/ (like \"food\") but got \"drink\" at " end end 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 " + expect(difference[:thing].message).to eq "Expected a Value matching /foo/ (like \"food\") but got #{a_numeric} (1) at " end end - context "when the actual is Hash" do - let(:actual) { HASH } + context "when the actual is a float" do + let(:actual) { FLOAT } it "returns a message" do - expect(difference[:thing].message).to eq "Expected a String matching /foo/ (like \"food\") but got a Hash at " + expect(difference[:thing].message).to eq "Expected a Value matching /foo/ (like \"food\") but got #{a_float} (1.0) at " end end - context "when the actual is a numeric" do - let(:actual) { INT } + context "when the actual is Hash" do + let(:actual) { HASH } it "returns a message" do - expect(difference[:thing].message).to eq "Expected a String matching /foo/ (like \"food\") but got #{a_numeric} (1) at " + expect(difference[:thing].message).to eq "Expected a String matching /foo/ (like \"food\") but got a Hash at " end end diff --git a/spec/lib/pact/term_spec.rb b/spec/lib/pact/term_spec.rb index d2ced7d..0b4dd0f 100644 --- a/spec/lib/pact/term_spec.rb +++ b/spec/lib/pact/term_spec.rb @@ -14,6 +14,22 @@ module Pact end end + context "when the generate is a integer" do + let(:term) { Term.new(generate: 10, matcher: /^-?\d+$/)} + + it 'does not raise an exception' do + term + end + end + + context "when the generate is a float" do + let(:term) { Term.new(generate: 50.51, matcher: /^0|-?\d+\.\d*$/)} + + it 'does not raise an exception' do + term + end + end + context "when the matcher does not match the generated value" do let(:generate) { 'banana' } it 'raises an exception' do diff --git a/spec/support/ruby_version_helpers.rb b/spec/support/ruby_version_helpers.rb index b7602a0..6abcc05 100644 --- a/spec/support/ruby_version_helpers.rb +++ b/spec/support/ruby_version_helpers.rb @@ -20,4 +20,8 @@ def a_numeric end end module_function :a_numeric + def a_float + "a #{Float}" + end + module_function :a_float end