Sha256: c8223b5ef1d707c2d9d4daff3a18dc3f5014b9c0217bdf38713961c4f50744dd

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks if an example contains any expectation.
      #
      # All RSpec's example and expectation methods are covered by default.
      # If you are using your own custom methods,
      # add the following configuration:
      #
      #   RSpec:
      #     Language:
      #       Examples:
      #         Regular:
      #           - custom_it
      #       Expectations:
      #         - custom_expect
      #
      # @example
      #   # bad
      #   it do
      #     a?
      #   end
      #
      #   # good
      #   it do
      #     expect(a?).to be(true)
      #   end
      #
      class NoExpectationExample < Base
        MSG = 'No expectation found in this example.'

        # @!method regular_or_focused_example?(node)
        # @param [RuboCop::AST::Node] node
        # @return [Boolean]
        def_node_matcher :regular_or_focused_example?, <<~PATTERN
          {
            #{block_pattern('{#Examples.regular | #Examples.focused}')}
            #{numblock_pattern('{#Examples.regular | #Examples.focused}')}
          }
        PATTERN

        # @!method including_any_expectation?(node)
        # @param [RuboCop::AST::Node] node
        # @return [Boolean]
        def_node_search(
          :including_any_expectation?,
          send_pattern('#Expectations.all')
        )

        # @!method including_any_skip_example?(node)
        # @param [RuboCop::AST::Node] node
        # @return [Boolean]
        def_node_search :including_any_skip_example?, <<~PATTERN
          (send nil? {:pending :skip} ...)
        PATTERN

        # @param [RuboCop::AST::BlockNode] node
        def on_block(node)
          return unless regular_or_focused_example?(node)
          return if including_any_expectation?(node)
          return if including_any_skip_example?(node)

          add_offense(node)
        end

        alias on_numblock on_block
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rspec-2.13.2 lib/rubocop/cop/rspec/no_expectation_example.rb