class Mutest::Meta::Example::DSL

Example DSL

Public Class Methods

call(file, type, block) click to toggle source

Run DSL on block

@return [Example]

# File lib/mutest/meta/example/dsl.rb, line 11
def self.call(file, type, block)
  instance = new(file, type)
  instance.instance_eval(&block)
  instance.example
end
new(file, type) click to toggle source

Initialize object

@return [undefined]

# File lib/mutest/meta/example/dsl.rb, line 22
def initialize(file, type)
  @file     = file
  @type     = type
  @node     = nil
  @expected = []
end

Public Instance Methods

example() click to toggle source

Example captured by DSL

@return [Example]

@raise [RuntimeError]

in case example cannot be build
# File lib/mutest/meta/example/dsl.rb, line 35
def example
  raise 'source not defined' unless @node

  Example.new(
    file:      @file,
    node:      @node,
    node_type: @type,
    expected:  @expected
  )
end

Private Instance Methods

mutation(input) click to toggle source

Add expected mutation

@param [String,Parser::AST::Node] input

@return [undefined]

# File lib/mutest/meta/example/dsl.rb, line 64
def mutation(input)
  node = node(input)
  raise "Mutation for input: #{input.inspect} is already expected" if @expected.include?(node)

  @expected << node
end
node(input) click to toggle source

Helper method to coerce input to node

@param [String,Parser::AST::Node] input

@return [Parser::AST::Node]

@raise [RuntimeError]

in case input cannot be coerced
# File lib/mutest/meta/example/dsl.rb, line 95
def node(input)
  case input
  when String
    Unparser::Preprocessor.run(Unparser.parse(input))
  when ::Parser::AST::Node
    input
  else
    raise "Cannot coerce to node: #{input.inspect}"
  end
end
regexp_mutations() click to toggle source

Add regexp mutations

@return [undefined]

# File lib/mutest/meta/example/dsl.rb, line 82
def regexp_mutations
  mutation('//')
  mutation('/nomatch\A/')
end
singleton_mutations() click to toggle source

Add singleton mutations

@return [undefined]

# File lib/mutest/meta/example/dsl.rb, line 74
def singleton_mutations
  mutation('nil')
  mutation('self')
end
source(input) click to toggle source

Set original source

@param [String,Parser::AST::Node] input

@return [undefined]

# File lib/mutest/meta/example/dsl.rb, line 53
def source(input)
  raise 'source already defined' if @node

  @node = node(input)
end