Class: RuboCop::Cop::RSpecParity::SufficientContexts::BranchTally

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/cop/rspec_parity/sufficient_contexts.rb

Overview

Collection of Branch descriptors. Exposes the guard/regular/total/+ interface the rest of the cop (and PrivateMethodCallGraph) relies on —a sequence of guard clauses (‘return/raise/next … if/unless`) shares a single “all guards pass” fall-through counted once per method (see #branches_from) rather than once per guard. `with_origin` tags inlined branches as the call graph descends into single-use helpers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(branches = []) ⇒ BranchTally

Returns a new instance of BranchTally.



235
236
237
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 235

def initialize(branches = [])
  @branches = branches
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



233
234
235
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 233

def branches
  @branches
end

Instance Method Details

#+(other) ⇒ Object



239
240
241
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 239

def +(other)
  BranchTally.new(branches + other.branches)
end

#guardObject



243
244
245
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 243

def guard
  branches.count { |branch| branch.kind == :guard }
end

#regularObject



247
248
249
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 247

def regular
  branches.count { |branch| branch.kind == :regular }
end

#totalObject



251
252
253
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 251

def total
  branches.size
end

#with_origin(name) ⇒ Object



255
256
257
258
259
# File 'lib/rubocop/cop/rspec_parity/sufficient_contexts.rb', line 255

def with_origin(name)
  BranchTally.new(branches.map do |branch|
    branch.origin ? branch : Branch.new(branch.kind, branch.label, branch.line, name, branch.detail)
  end)
end