Sha256: 36eaa760cc240fa4a24f207db5078a23bce45fc9b21ea658391c5e836172e501

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 KB

Contents

module RuboCop
  module Cop
    module Ezcater
      # Enforce use of `mock_ezcater_app`, `mock_chrome_browser` & `mock_custom_browser` helpers
      # instead of mocking `Browser` or `EzBrowser` directly.
      #
      # @example
      #
      #   # good
      #   mock_ezcater_app
      #   mock_chrome_browser
      #   mock_custom_browser
      #   mock_ezcater_app(device: "My Device", version: "1.2.3", language: "en=US,en")
      #   mock_chrome_browser(device: "My Device", version: "1.2.3", language: "en=US,en")
      #   mock_custom_browser(user_agent: "My Custom Agent", language: "en=US,en")
      #
      #   # bad
      #   allow(Browser).to receive...
      #   allow(EzBrowser).to receive...
      class RspecRequireBrowserMock < Cop
        MSG = "Use the mocks provided by `BrowserHelpers` instead of mocking `%s`".freeze

        def_node_matcher :browser_const?, <<~PATTERN
          (const _ {:Browser :EzBrowser})
        PATTERN

        def on_const(node)
          return unless browser_const?(node) && node.descendants.empty?

          # Walk to send node where method = :allow
          match_node = node
          match_node = match_node.parent while not_allow_send_node?(match_node.parent)
          match_node = match_node.parent

          # Validate send node, method = :allow; could have never been found
          return unless allow_send_node?(match_node)

          # Finish tree navigation to full line for highlighting
          match_node = match_node.parent while match_node.parent
          add_offense(match_node, location: :expression, message: MSG % node.source)
        end

        private

        def not_allow_send_node?(node)
          node && !allow_send_node?(node)
        end

        def allow_send_node?(node)
          node&.send_type? && node.method_name == :allow
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ezcater_rubocop-0.51.8 lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb
ezcater_rubocop-0.51.7 lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb
ezcater_rubocop-0.51.6 lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb
ezcater_rubocop-0.51.5 lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb
ezcater_rubocop-0.51.4 lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb
ezcater_rubocop-0.51.3 lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb
ezcater_rubocop-0.51.2 lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb
ezcater_rubocop-0.51.1 lib/rubocop/cop/ezcater/rspec_require_browser_mock.rb