Sha256: 4b7640e077496a7749acdfdbd2325d62e2988547c850ba662c63ea4d8ace6a9f

Contents?: true

Size: 1.14 KB

Versions: 30

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for `AssertInDelta` and `RefuteInDelta` cops.
    # @api private
    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

30 entries across 30 versions & 1 rubygems

Version Path
rubocop-minitest-0.36.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.35.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.35.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.34.5 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.33.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.32.2 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.32.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.32.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.31.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.29.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.28.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.27.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.26.1 lib/rubocop/cop/mixin/in_delta_mixin.rb