Sha256: 728c1630d2a940634b2282f6b9e606678b180c90b89ae7ecc0c13564ff0cef6d

Contents?: true

Size: 1.13 KB

Versions: 7

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for `AssertInDelta` and `RefuteInDelta` cops.
    module InDeltaMixin
      MSG = 'Prefer using `%<good_method>s`.'

      def on_send(node)
        equal_floats_call(node) do |expected, actual, message|
          message = message.first
          good_method = build_good_method(expected, actual, message)

          if expected.float_type? || actual.float_type?
            message = format(MSG, good_method: good_method)

            add_offense(node, message: message) do |corrector|
              corrector.replace(node, good_method)
            end
          end
        end
      end

      private

      def build_good_method(expected, actual, message)
        if message
          "#{assertion_method}_in_delta(#{expected.source}, #{actual.source}, 0.001, #{message.source})"
        else
          "#{assertion_method}_in_delta(#{expected.source}, #{actual.source})"
        end
      end

      def assertion_method
        class_name = self.class.name.split('::').last
        class_name[/\A[[:upper:]][[:lower:]]+/].downcase
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rubocop-minitest-0.21.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.21.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.20.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.20.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.19.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.19.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.18.0 lib/rubocop/cop/mixin/in_delta_mixin.rb