class MinitestToRspec::Input::Subprocessors::Base

Parent class of “sub-processors”. There is one sub-processor for each `sexp_type` that `Processor` knows how to process.

For example, `Subprocessors::Call` will process an `s(:call, ..)` expression representing minitest code, and return an S-expression representing equivalent RSpec code.

Public Class Methods

new(rails, mocha) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 17
def initialize(rails, mocha)
  @rails = rails
  @mocha = mocha
end

Public Instance Methods

allow_to(msg_recipient, matcher, any_instance = false) click to toggle source

Returns a s-expression representing an rspec-mocks stub.

# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 23
def allow_to(msg_recipient, matcher, any_instance = false)
  allow_method = any_instance ? :allow_any_instance_of : :allow
  target = s(:call, nil, allow_method, msg_recipient)
  s(:call, target, :to, matcher)
end
expect(target, eager, phase, matcher, any_instance) click to toggle source

Returns a s-expression representing an RSpec expectation, i.e. the combination of an “expectation target” and a matcher.

# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 31
def expect(target, eager, phase, matcher, any_instance)
  et = expectation_target(target, eager, any_instance)
  s(:call, et, phase, matcher)
end
expect_to(matcher, target, eager, any_instance = false) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 36
def expect_to(matcher, target, eager, any_instance = false)
  expect(target, eager, :to, matcher, any_instance)
end
expect_to_not(matcher, target, eager) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 40
def expect_to_not(matcher, target, eager)
  expect(target, eager, :to_not, matcher, false)
end
expectation_target(exp, eager, any_instance) click to toggle source

In RSpec, `expect` returns an “expectation target”. This can be based on an expression, as in `expect(1 + 1)` or it can be based on a block, as in `expect { raise }`. Either way, it's called an “expectation target”.

# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 48
def expectation_target(exp, eager, any_instance)
  if eager
    expectation_target_eager(exp, any_instance)
  else
    expectation_target_lazy(exp)
  end
end
expectation_target_eager(exp, any_instance) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 56
def expectation_target_eager(exp, any_instance)
  expect_method = any_instance ? :expect_any_instance_of : :expect
  s(:call, nil, expect_method, exp)
end
expectation_target_lazy(block) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 61
def expectation_target_lazy(block)
  s(:iter,
    s(:call, nil, :expect),
    0,
    full_process(block)
  )
end
full_process(obj) click to toggle source

If it's a `Sexp`, run `obj` through a new `Processor`. Otherwise, return `obj`.

This is useful for expressions that cannot be fully understood by a single subprocessor. For example, we must begin processing all :iter expressions, because some :iter represent calls we're interested in, e.g. `assert_difference`. However, if the :iter turns out to be uninteresting (perhaps it has no assertions) we still want to fully process its sub-expressions.

TODO: `full_process` may not be the best name.

# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 80
def full_process(obj)
  obj.is_a?(Sexp) ? Processor.new(@rails, @mocha).process(obj) : obj
end
matcher(name, *args) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/base.rb, line 84
def matcher(name, *args)
  exp = s(:call, nil, name)
  exp.concat(args)
end