Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support expect_offense template arguments #993

Merged
merged 2 commits into from Aug 9, 2020

Conversation

biinari
Copy link
Contributor

@biinari biinari commented Aug 9, 2020

Pass keyword arguments through to RuboCop::RSpec::ExpectOffense#expect_offense. These are used to set template replacements like %{name} added in rubocop #8069.

Allow code offense templates in specs to use %{name}. Style/FormatStringToken prefers %<name>s to %{name} but expect_offense does not use Kernel#format or String#%.

Example usage (from work in progress for #992):

[include_examples it_behaves_like it_should_behave_like].each do |include_method|
  it "registers an offense with #{include_method}" do
    expect_offense(<<~RUBY, include_method: include_method)
      describe 'foo' do
        %{include_method} 'an x'
        ^{include_method}^^^^^^^ Repeated include of shared_examples 'an x' on line(s) [3]
        %{include_method} 'an x'
        ^{include_method}^^^^^^^ Repeated include of shared_examples 'an x' on line(s) [2]
      end
    RUBY
  end
end

Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

Pass keyword arguments to `expect_offense` to format offense templates.
Allow using `%{...}` syntax to substitute values for `expect_offense`.
@pirj
Copy link
Member

pirj commented Aug 9, 2020

Thanks!

Can you please add an example usage of this feature?

I'm not sure I completely understand why FormatStringToken should be disabled for cops.

The feature was released as part of RuboCop 0.85, and we depend on 0.87+, so 👍

@biinari
Copy link
Contributor Author

biinari commented Aug 9, 2020

No problem @pirj. I've added an example and a bit more explanation for the disabling of the Style/FormatStringToken cop. I'm making use of this in the specs for #992, which I'll finish off later.

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@pirj pirj merged commit 5ac67ea into rubocop:master Aug 9, 2020
@biinari biinari deleted the fix/expect_offense_kwargs branch August 9, 2020 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants