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).
biinari added 2 commits Aug 9, 2020
Pass keyword arguments to `expect_offense` to format offense templates.
Allow using `%{...}` syntax to substitute values for `expect_offense`.
@biinari biinari force-pushed the biinari:fix/expect_offense_kwargs branch from 9ebdf91 to 49e729d Aug 9, 2020
@pirj
Copy link
Member

@pirj 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 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.

@pirj
pirj approved these changes Aug 9, 2020
Copy link
Member

@pirj pirj left a comment

Thank you!

@pirj pirj merged commit 5ac67ea into rubocop-hq:master Aug 9, 2020
13 checks passed
13 checks passed
ci/circleci: code-climate Your tests passed on CircleCI!
Details
ci/circleci: confirm_config_and_documentation Your tests passed on CircleCI!
Details
ci/circleci: edge-rubocop Your tests passed on CircleCI!
Details
ci/circleci: jruby Your tests passed on CircleCI!
Details
ci/circleci: rspec-2.4 Your tests passed on CircleCI!
Details
ci/circleci: rspec-2.5 Your tests passed on CircleCI!
Details
ci/circleci: rspec-2.6 Your tests passed on CircleCI!
Details
ci/circleci: rspec-2.7 Your tests passed on CircleCI!
Details
ci/circleci: rubocop-2.4 Your tests passed on CircleCI!
Details
ci/circleci: rubocop-2.5 Your tests passed on CircleCI!
Details
ci/circleci: rubocop-2.6 Your tests passed on CircleCI!
Details
ci/circleci: rubocop-2.7 Your tests passed on CircleCI!
Details
codeclimate All good!
Details
@biinari biinari deleted the biinari:fix/expect_offense_kwargs branch Aug 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.