Class: RuboCop::Cop::RSpec::ExampleWording
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/rspec/example_wording.rb
Overview
Checks for common mistakes in example descriptions.
This cop will correct docstrings that begin with ‘should’ and ‘it’. This cop will also look for insufficient examples and call them out.
The autocorrect is experimental - use with care! It can be configured with CustomTransform (e.g. have => has) and IgnoredWords (e.g. only).
Use the DisallowedExamples setting to prevent unclear or insufficient descriptions. Please note that this config will not be treated as case sensitive.
Constant Summary collapse
- MSG_SHOULD =
'Do not use should when describing your tests.'
- MSG_WILL =
'Do not use the future tense when describing your tests.'
- MSG_IT =
"Do not repeat 'it' when describing your tests."
- MSG_INSUFFICIENT_DESCRIPTION =
'Your example description is ' \ 'insufficient.'
- SHOULD_PREFIX =
/\Ashould(?:n't|n’t)?\b/i.freeze
- WILL_PREFIX =
/\A(?:will|won't|won’t)\b/i.freeze
- IT_PREFIX =
/\Ait /i.freeze
Instance Method Summary collapse
- #it_description(node) ⇒ Object
-
#on_block(node) ⇒ Object
rubocop:disable Metrics/MethodLength.
Methods inherited from Base
inherited, #on_new_investigation
Methods included from RSpec::Language
#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Instance Method Details
#it_description(node) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/rubocop/cop/rspec/example_wording.rb', line 63 def_node_matcher :it_description, <<~PATTERN (block (send _ :it ${ (str $_) (dstr (str $_ ) ...) } ...) ...) PATTERN |
#on_block(node) ⇒ Object
rubocop:disable Metrics/MethodLength
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rubocop/cop/rspec/example_wording.rb', line 71 def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler it_description(node) do |description_node, | if .match?(SHOULD_PREFIX) add_wording_offense(description_node, MSG_SHOULD) elsif .match?(WILL_PREFIX) add_wording_offense(description_node, MSG_WILL) elsif .match?(IT_PREFIX) add_wording_offense(description_node, MSG_IT) elsif insufficient_docstring?(description_node) add_offense(docstring(description_node), message: MSG_INSUFFICIENT_DESCRIPTION) end end end |