rspec / rspec-expectations

Provides a readable API to express expected outcomes of a code example
https://rspec.info
MIT License
1.26k stars 396 forks source link

Having BigDecimal comparisons print decimal amount rather than scientific #459

Closed justin808 closed 10 years ago

justin808 commented 10 years ago

I can monkey patch BigDecimal to get clear error messages, but maybe we can just change rspec/matchers/built_in/eq.rb to support special inspect?

I'm supposing we'd want some generalized way of checking if there's a helper for the value of expected to do a better to_s than inspect.

If there's interest, I'll submit a PR.

# Monkey patch BigDecimal#inspect
# Otherwises test errors print like:
# expected: #<BigDecimal:108482700,'0.11E3',9(27)>
#     got: #<BigDecimal:108468080,'0.12E3',9(27)>
# After get:
# expected: 110.0
#      got: 120.0
class BigDecimal
  def inspect
    to_s
  end
end
module RSpec
  module Matchers
    module BuiltIn
      class Eq < BaseMatcher
        def match(expected, actual)
          actual == expected
        end

        def failure_message_for_should
          "\nexpected: #{expected.inspect}\n     got: #{actual.inspect}\n\n(compared using ==)\n"
        end

        def failure_message_for_should_not
          "\nexpected: value != #{expected.inspect}\n     got: #{actual.inspect}\n\n(compared using ==)\n"
        end

        def diffable?; true; end
      end
    end
  end
end
JonRowe commented 10 years ago

Sure, that seems reasonable, we already special case Time and DateTime. (Your example is out of date). Please note you'll need a defined?(BigDecimal) check, as we won't require it for people.

justin808 commented 10 years ago

@JonRowe What is the correct branch to base this on? I'll implement similar to Time and DateTime.

JonRowe commented 10 years ago

Always master.

justin808 commented 10 years ago

And then will we merge this to 2-14-maintenance? Or should I update the rspec my rails app is using?

JonRowe commented 10 years ago

No, this is a new feature, it will only go into RSpec 3.x

danielfone commented 10 years ago

@JonRowe this can be closed.