class RuboCop::Cop::InternalAffairs::ExampleDescription
end
expect_no_offenses(‘…’)
it ‘does not register an offense’ do
end
expect_offense(‘…’)
it ‘registers an offense’ do
# good
end
expect_no_offenses(‘…’)
it ‘registers an offense’ do
end
expect_offense(‘…’)
it ‘does not register an offense’ do
# bad
@example
descriptions.
or ‘expects_no_offenses` do not have conflicting
Checks that RSpec examples that use `expects_offense`
def check_description(current_description, description_map, message)
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
def correct_description(current_description, description_map)
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
def on_send(node)
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
def string_contents(node)
def string_contents(node) node.type?(:str, :dstr) ? node.value : node.source end