class MinitestToRspec::Input::Subprocessors::Call

Processes `s(:call, ..)` expressions.

Constants

MOCHA_METHODS

Mocha methods will only be processed if `–mocha` flag was given, i.e. `mocha` argument in constructor is true.

Public Class Methods

new(sexp, rails, mocha) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 28
def initialize(sexp, rails, mocha)
  super(rails, mocha)
  @exp = Model::Call.new(sexp)
  sexp.clear
end

Public Instance Methods

process() click to toggle source

Given a `Model::Call`, returns a `Sexp`

# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 35
def process
  if process?
    send(name_of_processing_method)
  else
    @exp.original
  end
end
process?() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 43
def process?
  respond_to?(name_of_processing_method, true) &&
    (@mocha || !MOCHA_METHODS.include?(@exp.method_name))
end

Private Instance Methods

be_a(exp) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 66
def be_a(exp)
  matcher(:be_a, exp)
end
be_empty() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 58
def be_empty
  matcher(:be_empty)
end
be_falsey() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 50
def be_falsey
  matcher(:be_falsey)
end
be_instance_of(exp) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 70
def be_instance_of(exp)
  matcher(:be_instance_of, exp)
end
be_nil() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 54
def be_nil
  matcher(:be_nil)
end
be_truthy() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 62
def be_truthy
  matcher(:be_truthy)
end
call_to_question_mark?(exp) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 74
def call_to_question_mark?(exp)
  sexp_type?(:call, exp) && Model::Call.new(exp).question_mark_method?
end
eq(exp) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 78
def eq(exp)
  matcher(:eq, exp)
end
match(pattern) click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 82
def match(pattern)
  matcher(:match, pattern)
end
method_assert() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 86
def method_assert
  refsert eq(s(:true)), be_truthy
end
method_assert_empty() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 110
def method_assert_empty
  expect_to(be_empty, @exp.arguments[0], true)
end
method_assert_equal() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 90
def method_assert_equal
  expected = @exp.arguments[0]
  calculated = @exp.arguments[1]
  expect_to(eq(expected), calculated, true)
end
method_assert_instance_of() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 126
def method_assert_instance_of
  expected = @exp.arguments[0]
  calculated = @exp.arguments[1]
  expect_to(be_instance_of(expected), calculated, true)
end
method_assert_kind_of() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 120
def method_assert_kind_of
  expected = @exp.arguments[0]
  calculated = @exp.arguments[1]
  expect_to(be_a(expected), calculated, true)
end
method_assert_match() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 96
def method_assert_match
  pattern = @exp.arguments[0]
  string = @exp.arguments[1]
  expect_to(match(pattern), string, true)
end
method_assert_nil() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 102
def method_assert_nil
  expect_to(be_nil, @exp.arguments[0], true)
end
method_assert_not_equal() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 114
def method_assert_not_equal
  expected = @exp.arguments[0]
  calculated = @exp.arguments[1]
  expect_to_not(eq(expected), calculated, true)
end
method_assert_not_nil() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 106
def method_assert_not_nil
  expect_to_not(be_nil, @exp.arguments[0], true)
end
method_expects() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 132
def method_expects
  if @exp.num_arguments == 1 &&
     %i[lit hash].include?(@exp.arguments.first.sexp_type)
    mocha_stub
  else
    @exp.original
  end
end
method_once() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 141
def method_once
  mocha_stub
end
method_refute() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 145
def method_refute
  refsert eq(s(:false)), be_falsey
end
method_refute_equal() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 149
def method_refute_equal
  unexpected = @exp.arguments[0]
  calculated = @exp.arguments[1]
  expect_to_not(eq(unexpected), calculated, true)
end
method_require() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 164
def method_require
  if @exp.require_test_helper?
    require_spec_helper
  else
    @exp.original
  end
end
method_returns() click to toggle source

Processes an entire line of code that ends in `.returns`

# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 156
def method_returns
  if @exp.num_arguments.zero?
    @exp.original
  else
    mocha_stub
  end
end
method_should() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 172
def method_should
  s(:call, nil, :it, *@exp.arguments)
end
method_stub() click to toggle source

Happily, the no-block signatures of [stub] are the same as [double].

  • (name)

  • (stubs)

  • (name, stubs)

# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 182
def method_stub
  raise ArgumentError unless @exp.is_a?(Model::Call)
  if @exp.receiver.nil?
    s(:call, nil, :double, *@exp.arguments)
  else
    @exp.original
  end
end
method_stub_everything() click to toggle source
stub_everything][1

responds to all messages with nil.

double.as_null_object][4

responds with self. Not a

drop-in replacement, but will work in many situations. RSpec doesn't provide an equivalent to `stub_everything`, AFAIK.

# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 196
def method_stub_everything
  if @exp.receiver.nil?
    d = s(:call, nil, :double, *@exp.arguments)
    s(:call, d, :as_null_object)
  else
    @exp.original
  end
end
method_test() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 205
def method_test
  s(:call, nil, :it, *@exp.arguments)
end
method_twice() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 209
def method_twice
  mocha_stub
end
mocha_shorthand_stub_to_rspec_stubs(shorthand_stub_hash, mt_stub) click to toggle source

Given a sexp representing the hash from a mocha shorthand stub, as in `Banana.expects(edible: true, color: “yellow”)` return an array of separate RSpec stubs, one for each hash key.

# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 216
def mocha_shorthand_stub_to_rspec_stubs(shorthand_stub_hash, mt_stub)
  Model::HashExp.new(shorthand_stub_hash).to_h.map { |k, v|
    Rspec::Stub.new(
      mt_stub.receiver,
      mt_stub.any_instance?,
      k,
      mt_stub.with,
      v,
      1
    ).to_rspec_exp
  }
end
mocha_stub() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 229
def mocha_stub
  mt_stub = Minitest::Stub.new(@exp)
  msg = mt_stub.message
  if sexp_type?(:hash, msg)
    pointless_lambda(mocha_shorthand_stub_to_rspec_stubs(msg, mt_stub))
  else
    Rspec::Stub.new(
      mt_stub.receiver,
      mt_stub.any_instance?,
      mt_stub.message,
      mt_stub.with,
      mt_stub.returns,
      mt_stub.count
    ).to_rspec_exp
  end
rescue UnknownVariant
  @exp.original
end
name_of_processing_method() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 248
def name_of_processing_method
  "method_#{@exp.method_name}".to_sym
end
pointless_lambda(array_of_calls) click to toggle source

Given `array_of_calls`, returns a `Sexp` representing a self-executing lambda.

This works around the fact that `sexp_processor` expects us to return a single `Sexp`, not an array of `Sexp`. We also can't return a `:block`, or else certain input would produce nested blocks (e.g. `s(:block, s(:block, ..))`) which `ruby2ruby` (naturally) does not know how to process. So, the easiest solution I could think of is a self-executing lambda.

Currently, the only `:call` which we process into multiple calls is the hash form of a mocha `#expects`, thankfully uncommon.

To get better output (without a pointless lambda) we would have to process `:block` and `:defn`, which we are not yet doing.

# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 267
def pointless_lambda(array_of_calls)
  assert_sexp_type_array(:call, array_of_calls)
  s(:call,
    s(:iter,
      s(:call, nil, :lambda),
      0,
      s(:block,
        s(:str, 'Sorry for the pointless lambda here.'),
        *array_of_calls
      )
    ),
    :call
  )
end
refsert(exact, fuzzy) click to toggle source

`refsert` - Code shared by refute and assert. I could also have gone with `assfute`. Wooo .. time for bed.

# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 284
def refsert(exact, fuzzy)
  actual = @exp.arguments[0]
  matcher = call_to_question_mark?(actual) ? exact : fuzzy
  expect_to(matcher, actual, true)
end
require_spec_helper() click to toggle source
# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 290
def require_spec_helper
  prefix = @rails ? 'rails' : 'spec'
  s(:call, nil, :require, s(:str, "#{prefix}_helper"))
end
wrap_sexp(obj) click to toggle source

Wraps `obj` in an `Array` if it is a `Sexp`

# File lib/minitest_to_rspec/input/subprocessors/call.rb, line 296
def wrap_sexp(obj)
  obj.is_a?(Sexp) ? [obj] : obj
end