Skip to content

Commit

Permalink
Tell SimpleCov to ignore unreachable code
Browse files Browse the repository at this point in the history
In some cases we know that a case statement is covering all possible
branches, e.g. `case [foo?, bar?]` has four branches (if the predicate
methods return booleans, of course), and `case style` has a know number
of branches, depending on `SupportedStyles` in config/default.yml.

So, when to use `else ... raise ArgumentError`, and when to change the
last `elsif` or `when` into an `else`? I don't know.
  • Loading branch information
bquorning committed Feb 9, 2025
1 parent 837c7d7 commit d9a0658
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SimpleCov.start do
enable_coverage :branch
minimum_coverage line: 100, branch: 98.44
minimum_coverage line: 100, branch: 99.6
add_filter '/spec/'
add_filter '/vendor/bundle/'
end
4 changes: 4 additions & 0 deletions lib/rubocop/cop/rspec/be_nil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def on_send(node)
check_be_style(node)
when :be_nil
check_be_nil_style(node)
else
# :nocov:
raise ArgumentError("Unsupported style :#{style}")
# :nocov:
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/example_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def text(node)
node.node_parts.map { |child_node| text(child_node) }.join
when :str
node.value
when :begin
else
node.source
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/excessive_docstring_spacing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def text(node)
node.node_parts.map { |child_node| text(child_node) }.join
when :str, :sym
node.value
when :begin
else
node.source
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/rubocop/cop/rspec/implicit_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def on_send(node)

private

# rubocop:disable Metrics/MethodLength
def autocorrect(corrector, node)
case node.method_name
when :expect
Expand All @@ -107,8 +108,13 @@ def autocorrect(corrector, node)
corrector.replace(node.location.selector, 'expect(subject).to')
when :should_not
corrector.replace(node.location.selector, 'expect(subject).not_to')
else
# :nocov:
raise ArgumentError("Unexpected method name #{node.method_name}")
# :nocov:
end
end
# rubocop:enable Metrics/MethodLength

def message(_node)
case style
Expand All @@ -119,6 +125,7 @@ def message(_node)
end
end

# rubocop:disable Metrics/MethodLength
def invalid?(node)
case style
when :require_implicit
Expand All @@ -129,8 +136,13 @@ def invalid?(node)
implicit_subject_in_non_its_and_non_single_line?(node)
when :single_statement_only
implicit_subject_in_non_its_and_non_single_statement?(node)
else
# :nocov:
raise ArgumentError("Unsupported style :#{style}")
# :nocov:
end
end
# rubocop:enable Metrics/MethodLength

def implicit_subject_in_non_its?(node)
implicit_subject?(node) && !its?(node)
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/cop/rspec/message_spies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def error_message(receiver)
MSG_RECEIVE
when :have_received
format(MSG_HAVE_RECEIVED, source: receiver.source)
else
# :nocov:
raise ArgumentError("Unsupported style :#{style}")
# :nocov:
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/rubocop/cop/rspec/predicate_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def true?(to_symbol, matcher)
matcher.first_argument.true_type?
when :be_truthy, :a_truthy_value
true
when :be_falsey, :be_falsy, :a_falsey_value, :a_falsy_value
else # :be_falsey, :be_falsy, :a_falsey_value, :a_falsy_value
false
end
to_symbol == :to ? result : !result
Expand Down Expand Up @@ -260,7 +260,7 @@ def replacement_matcher(node)
'be(false)'
when [false, true]
'be_truthy'
when [false, false]
else
'be_falsey'
end
end
Expand Down Expand Up @@ -329,6 +329,10 @@ def on_send(node)
check_inflected(node)
when :explicit
check_explicit(node)
else
# :nocov:
raise ArgumentError("Unsupported style :#{style}")
# :nocov:
end
end

Expand Down

0 comments on commit d9a0658

Please sign in to comment.