class RuboCop::Cop::InternalAffairs::ExampleDescription
Checks that RSpec
examples that use ‘expects_offense` or `expects_no_offenses` do not have conflicting descriptions.
@example
# bad it 'does not register an offense' do expect_offense('...') end it 'registers an offense' do expect_no_offenses('...') end # good it 'registers an offense' do expect_offense('...') end it 'does not register an offense' do expect_no_offenses('...') end
Constants
- EXAMPLE_DESCRIPTION_MAPPING
- EXPECT_CORRECTION_DESCRIPTION_MAPPING
- EXPECT_NO_CORRECTIONS_DESCRIPTION_MAPPING
- EXPECT_NO_OFFENSES_DESCRIPTION_MAPPING
- EXPECT_OFFENSE_DESCRIPTION_MAPPING
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
Source
# File lib/rubocop/cop/internal_affairs/example_description.rb, line 78 def on_send(node) parent = node.each_ancestor(:block).first return unless parent && (current_description = offense_example(parent)&.first) method_name = node.method_name message = format(MSG, method_name: method_name) description_map = EXAMPLE_DESCRIPTION_MAPPING[method_name] check_description(current_description, description_map, message) end
Private Instance Methods
Source
# File lib/rubocop/cop/internal_affairs/example_description.rb, line 91 def check_description(current_description, description_map, message) description_text = string_contents(current_description) return unless (new_description = correct_description(description_text, description_map)) quote = current_description.dstr_type? ? '"' : "'" add_offense(current_description, message: message) do |corrector| corrector.replace(current_description, "#{quote}#{new_description}#{quote}") end end
Source
# File lib/rubocop/cop/internal_affairs/example_description.rb, line 102 def correct_description(current_description, description_map) description_map.each do |incorrect_description_pattern, preferred_description| if incorrect_description_pattern.match?(current_description) return current_description.gsub(incorrect_description_pattern, preferred_description) end end nil end
Source
# File lib/rubocop/cop/internal_affairs/example_description.rb, line 112 def string_contents(node) node.type?(:str, :dstr) ? node.value : node.source end