Sha256: d7969bed5d9bd0f92505600499e0aba27a06dbcf81365b6f324525c34fb82663

Contents?: true

Size: 1004 Bytes

Versions: 157

Compression:

Stored size: 1004 Bytes

Contents

#
# courtesy of sander 6
# - http://github.com/sander6/custom_matchers/blob/master/lib/matchers/tolerance_matchers.rb
#
module Sander6
  module CustomMatchers

    class ToleranceMatcher
      def initialize(tolerance)
        @tolerance = tolerance
        @target = 0
      end

      def of(target)
        @target = target
        self
      end

      def matches?(value)
        @value = value
        ((@target - @tolerance)..(@target + @tolerance)).include?(@value)
      end

      def failure_message
        "Expected #{@value.inspect} to be within #{@tolerance.inspect} of #{@target.inspect}, but it wasn't.\n" +
          "Difference: #{@value - @target}"
          end

      def negative_failure_message
        "Expected #{@value.inspect} not to be within #{@tolerance.inspect} of #{@target.inspect}, but it was.\n" +
          "Difference: #{@value - @target}"
          end
    end

    def be_within(value)
      Sander6::CustomMatchers::ToleranceMatcher.new(value)
    end

  end
end

Version data entries

157 entries across 157 versions & 2 rubygems

Version Path
protobuffy-3.6.0 spec/support/tolerance_matcher.rb
protobuffy-3.5.1 spec/support/tolerance_matcher.rb
protobuffy-3.4.0 spec/support/tolerance_matcher.rb
protobuffy-3.3.0 spec/support/tolerance_matcher.rb
protobuffy-3.2.0 spec/support/tolerance_matcher.rb
protobuffy-3.1.0 spec/support/tolerance_matcher.rb
protobuf-3.2.0 spec/support/tolerance_matcher.rb
protobuf-3.1.0 spec/support/tolerance_matcher.rb
protobuf-3.0.5 spec/support/tolerance_matcher.rb
protobuf-3.0.4 spec/support/tolerance_matcher.rb
protobuf-2.8.13 spec/support/tolerance_matcher.rb
protobuf-3.0.3 spec/support/tolerance_matcher.rb
protobuf-3.0.2 spec/support/tolerance_matcher.rb
protobuf-3.0.1 spec/support/tolerance_matcher.rb
protobuf-3.0.0 spec/support/tolerance_matcher.rb
protobuf-3.0.0.rc2 spec/support/tolerance_matcher.rb
protobuf-3.0.0.rc1 spec/support/tolerance_matcher.rb
protobuf-2.8.12 spec/support/tolerance_matcher.rb
protobuf-2.8.11 spec/support/tolerance_matcher.rb
protobuf-2.8.10 spec/support/tolerance_matcher.rb