Sha256: 19ae23c7faca26b812bb7c8ddf95af0c57cf139419620362c8ac06650be6d978

Contents?: true

Size: 1.17 KB

Versions: 11

Compression:

Stored size: 1.17 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` over `%<bad_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, bad_method: node.source)

            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

11 entries across 11 versions & 1 rubygems

Version Path
rubocop-minitest-0.17.2 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.17.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.17.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.16.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.15.2 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.15.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.15.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.14.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.13.0 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.12.1 lib/rubocop/cop/mixin/in_delta_mixin.rb
rubocop-minitest-0.12.0 lib/rubocop/cop/mixin/in_delta_mixin.rb