Sha256: 722d8b4327d55aa1a1cc7a2cfe6ee7d1d5779f749a0a7f4711b602ef8a3aa4b8

Contents?: true

Size: 1.14 KB

Versions: 8

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Discourse
      # Use `eq_time` matcher with timestamps in specs.
      #
      # @example
      #   # bad
      #   expect(user.created_at).to eq(Time.zone.now)
      #
      #   # good
      #   expect(user.created_at).to eq_time(Time.zone.now)
      class TimeEqMatcher < Base
        MSG = "Use eq_time when testing timestamps"

        def_node_matcher :using_eq_matcher_with_timestamp?, <<-MATCHER
          (send
            (send nil? :expect
              (send ... #timestamp_suffix?))
            {:to :not_to :to_not}
            (send nil? :eq #not_nil?))
        MATCHER

        def on_send(node)
          return unless using_eq_matcher_with_timestamp?(node)

          add_offense(node, message: MSG)
        end

        def autocorrect(node)
          lambda { |corrector| corrector.replace(node.children.last.loc.selector, "eq_time") }
        end

        private

        def timestamp_suffix?(property)
          property.is_a?(Symbol) && property =~ /_at$/
        end

        def not_nil?(expression)
          !expression.nil_type?
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-discourse-3.9.2 lib/rubocop/cop/discourse/time_eq_matcher.rb
rubocop-discourse-3.9.1 lib/rubocop/cop/discourse/time_eq_matcher.rb
rubocop-discourse-3.9.0 lib/rubocop/cop/discourse/time_eq_matcher.rb
rubocop-discourse-3.8.6 lib/rubocop/cop/discourse/time_eq_matcher.rb
rubocop-discourse-3.8.5 lib/rubocop/cop/discourse/time_eq_matcher.rb
rubocop-discourse-3.8.4 lib/rubocop/cop/discourse/time_eq_matcher.rb
rubocop-discourse-3.8.3 lib/rubocop/cop/discourse/time_eq_matcher.rb
rubocop-discourse-3.8.2 lib/rubocop/cop/discourse/time_eq_matcher.rb