class Crispr::Mutations::Hash

Mutations targeting hash literals.

def mutations_for(node)

Returns:
  • (Array) - mutated AST nodes

Parameters:
  • node (Parser::AST::Node) -- the hash AST node
def mutations_for(node)
  return [] unless node.is_a?(Parser::AST::Node) && node.type == :hash
  mutations = []
  node.children.each_with_index do |pair, index|
    # Remove individual pair
    new_pairs = node.children.dup
    new_pairs.delete_at(index)
    mutations << s(:hash, *new_pairs)
    # Replace value with nil
    next unless pair.type == :pair
    key, _value = *pair
    new_pair = s(:pair, key, s(:nil))
    new_pairs = node.children.dup
    new_pairs[index] = new_pair
    mutations << s(:hash, *new_pairs)
  end
  # Add a new nil => nil pair
  mutations << s(:hash, *(node.children + [s(:pair, s(:nil), s(:nil))]))
  mutations
end