Class: MinitestToRspec::Input::Subprocessors::Base
- Inherits:
-
Object
- Object
- MinitestToRspec::Input::Subprocessors::Base
- Includes:
- SexpAssertions
- Defined in:
- lib/minitest_to_rspec/input/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.
Instance Method Summary collapse
-
#allow_to(msg_recipient, matcher, any_instance = false) ⇒ Object
Returns a s-expression representing an rspec-mocks stub.
-
#expect(target, eager, phase, matcher, any_instance) ⇒ Object
Returns a s-expression representing an RSpec expectation, i.e.
- #expect_to(matcher, target, eager, any_instance = false) ⇒ Object
- #expect_to_not(matcher, target, eager) ⇒ Object
-
#expectation_target(exp, eager, any_instance) ⇒ Object
In RSpec, ‘expect` returns an “expectation target”.
- #expectation_target_eager(exp, any_instance) ⇒ Object
- #expectation_target_lazy(block) ⇒ Object
-
#full_process(obj) ⇒ Object
If it’s a ‘Sexp`, run `obj` through a new `Processor`.
-
#initialize(rails, mocha) ⇒ Base
constructor
A new instance of Base.
- #matcher(name, *args) ⇒ Object
Methods included from SexpAssertions
#assert_sexp_type, #assert_sexp_type_array, #sexp_type?
Constructor Details
#initialize(rails, mocha) ⇒ Base
Returns a new instance of Base.
17 18 19 20 |
# File 'lib/minitest_to_rspec/input/subprocessors/base.rb', line 17 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.
23 24 25 26 27 |
# 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) ⇒ Object
Returns a s-expression representing an RSpec expectation, i.e. the combination of an “expectation target” and a matcher.
31 32 33 34 |
# 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) ⇒ Object
36 37 38 |
# 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) ⇒ Object
40 41 42 |
# 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) ⇒ 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”.
48 49 50 51 52 53 54 |
# 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) ⇒ Object
56 57 58 59 |
# 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) ⇒ Object
61 62 63 64 65 66 67 |
# 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) ⇒ 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.
80 81 82 |
# 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) ⇒ Object
84 85 86 87 |
# File 'lib/minitest_to_rspec/input/subprocessors/base.rb', line 84 def matcher(name, *args) exp = s(:call, nil, name) exp.concat(args) end |