diff --git a/lib/pact/matchers/matchers.rb b/lib/pact/matchers/matchers.rb index 8f5bb88..0455425 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 #{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 " end end diff --git a/lib/pact/term.rb b/lib/pact/term.rb index cf83c69..69f8ce9 100644 --- a/lib/pact/term.rb +++ b/lib/pact/term.rb @@ -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 diff --git a/spec/lib/pact/matchers/matchers_messages_regexp_spec.rb b/spec/lib/pact/matchers/matchers_messages_regexp_spec.rb index 4eadeda..596255a 100644 --- a/spec/lib/pact/matchers/matchers_messages_regexp_spec.rb +++ b/spec/lib/pact/matchers/matchers_messages_regexp_spec.rb @@ -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 " + expect(difference[:thing].message).to eq "Expected a Integer matching /foo/ (like \"food\") but got #{a_numeric} (1) at " + 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 " end end @@ -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 " + expect(difference[:thing].message).to eq "Expected a Integer matching /foo/ (like \"food\") but got #{a_numeric} (1) at " + 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 " end end diff --git a/spec/lib/pact/term_spec.rb b/spec/lib/pact/term_spec.rb index 4c17fbe..414ca09 100644 --- a/spec/lib/pact/term_spec.rb +++ b/spec/lib/pact/term_spec.rb @@ -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 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