Class: MinitestToRspec::Subprocessors::Base

Inherits:
Object
  • Object
show all
Includes:
MinitestToRspec::SexpAssertions
Defined in:
lib/minitest_to_rspec/subprocessors/base.rb

Overview

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.

Direct Known Subclasses

Call, Defn, Iter, Klass

Instance Method Summary collapse

Methods included from MinitestToRspec::SexpAssertions

#assert_sexp_type, #assert_sexp_type_array, #sexp_type?

Constructor Details

#initialize(rails, mocha) ⇒ Base

Returns a new instance of Base.



16
17
18
19
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 16

def initialize(rails, mocha)
  @rails = rails
  @mocha = mocha
end

Instance Method Details

#allow_to(msg_recipient, matcher, any_instance = false) ⇒ Object

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



22
23
24
25
26
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 22

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) ⇒ Object

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



30
31
32
33
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 30

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) ⇒ Object



35
36
37
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 35

def expect_to(matcher, target, eager, any_instance = false)
  expect(target, eager, :to, matcher, any_instance)
end

#expect_to_not(matcher, target, eager) ⇒ Object



39
40
41
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 39

def expect_to_not(matcher, target, eager)
  expect(target, eager, :to_not, matcher, false)
end

#expectation_target(exp, eager, any_instance) ⇒ Object

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”.



47
48
49
50
51
52
53
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 47

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) ⇒ Object



55
56
57
58
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 55

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) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 60

def expectation_target_lazy(block)
  s(:iter,
    s(:call, nil, :expect),
    0,
    full_process(block)
  )
end

#full_process(obj) ⇒ Object

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.



79
80
81
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 79

def full_process(obj)
  obj.is_a?(Sexp) ? Processor.new(@rails, @mocha).process(obj) : obj
end

#matcher(name, *args) ⇒ Object



83
84
85
86
# File 'lib/minitest_to_rspec/subprocessors/base.rb', line 83

def matcher(name, *args)
  exp = s(:call, nil, name)
  exp.concat(args)
end