Class: DeepCover::Node::If

Inherits:
DeepCover::Node show all
Includes:
Branch
Defined in:
lib/deep_cover/node/if.rb

Constant Summary

Constants inherited from DeepCover::Node

CLASSES, Complex, Cvasgn, Dsym, Erange, Float, Int, Ivar, Kwarg, Kwoptarg, Kwrestarg, OrAsgn, Rational, Sym, True, Zsuper

Instance Attribute Summary

Attributes inherited from DeepCover::Node

#base_node, #children, #index, #next_sibling, #parent, #previous_sibling

Instance Method Summary collapse

Methods included from Branch

#flow_completion_count

Methods inherited from DeepCover::Node

[], #[], atom, #children_nodes, #covered_code, define_module_class, #each_node, #fancy_type, #find_all, has_evaluated_segments, inherited, #initialize, #simple_literal?, #to_s, #type

Methods included from Memoize

#freeze, included

Constructor Details

This class inherits a constructor from DeepCover::Node

Instance Method Details

#branchesObject



32
33
34
# File 'lib/deep_cover/node/if.rb', line 32

def branches
  [true_branch, false_branch]
end

#branches_summary(of_branches) ⇒ Object



36
37
38
39
40
# File 'lib/deep_cover/node/if.rb', line 36

def branches_summary(of_branches)
  of_branches.map do |jump|
    "#{'implicit ' if jump.is_a?(EmptyBody) && !has_else?}#{jump == false_branch ? 'falsy' : 'truthy'} branch"
  end.join(' and ')
end

#child_can_be_empty(child, name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/deep_cover/node/if.rb', line 21

def child_can_be_empty(child, name)
  raise 'Unexpected empty body' if name == :condition || style == :ternary
  if (name == :true_branch) == [:if, :elsif].include?(style)
    (base_node.loc.begin || base_node.children[0].loc.expression.succ).end
  elsif has_else?
    base_node.loc.else.end.succ
  else
    true # implicit else
  end
end

#deepest_elsif_nodeObject



60
61
62
63
64
65
# File 'lib/deep_cover/node/if.rb', line 60

def deepest_elsif_node
  raise 'Not an elsif' if style != :elsif
  return self if loc_hash[:else] && loc_hash[:else].source == 'else'
  return self if false_branch.is_a?(EmptyBody)
  false_branch.deepest_elsif_node
end

#execution_countObject



42
43
44
# File 'lib/deep_cover/node/if.rb', line 42

def execution_count
  condition.flow_completion_count
end

#has_else?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/deep_cover/node/if.rb', line 67

def has_else?
  !!base_node.loc.to_hash[:else]
end

#root_if_nodeObject



52
53
54
55
56
57
58
# File 'lib/deep_cover/node/if.rb', line 52

def root_if_node
  if style != :elsif
    self
  else
    parent.root_if_node
  end
end

#styleObject

returns on of %i[ternary if unless elsif]



47
48
49
50
# File 'lib/deep_cover/node/if.rb', line 47

def style
  keyword = loc_hash[:keyword]
  keyword ? keyword.source.to_sym : :ternary
end