Sha256: e46325bc9a8842ad9d2a01aef5b309db6019628ffb95869a27ed9d0291b497e1

Contents?: true

Size: 1.31 KB

Versions: 18

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Check that the `output` matcher is not called with an empty string.
      #
      # @example
      #   # bad
      #   expect { foo }.to output('').to_stdout
      #   expect { bar }.not_to output('').to_stderr
      #
      #   # good
      #   expect { foo }.not_to output.to_stdout
      #   expect { bar }.to output.to_stderr
      #
      class EmptyOutput < Base
        extend AutoCorrector

        MSG = 'Use `%<runner>s` instead of matching on an empty output.'
        RESTRICT_ON_SEND = Runners.all

        # @!method matching_empty_output(node)
        def_node_matcher :matching_empty_output, <<~PATTERN
          (send
            (block
              (send nil? :expect) ...
            )
            #Runners.all
            (send $(send nil? :output (str empty?)) ...)
          )
        PATTERN

        def on_send(send_node)
          matching_empty_output(send_node) do |node|
            runner = send_node.method?(:to) ? 'not_to' : 'to'
            message = format(MSG, runner: runner)
            add_offense(node, message: message) do |corrector|
              corrector.replace(send_node.loc.selector, runner)
              corrector.replace(node, 'output')
            end
          end
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.3/lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.3.0 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.2.0 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.1.0 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.0.5 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.0.4 lib/rubocop/cop/rspec/empty_output.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.0.3 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.0.2 lib/rubocop/cop/rspec/empty_output.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rspec-3.0.1/lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.0.1 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.0.0 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-3.0.0.pre lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-2.31.0 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-2.30.0 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-2.29.2 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-2.29.1 lib/rubocop/cop/rspec/empty_output.rb
rubocop-rspec-2.29.0 lib/rubocop/cop/rspec/empty_output.rb