class MinitestToRspec::Input::Model::Klass

Data object. Represents a `:class` S-expression.

Public Class Methods

new(exp) click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 10
def initialize(exp)
  assert_sexp_type(:class, exp)
  @exp = exp.dup
  assert_valid_name
end

Public Instance Methods

action_controller_test_case?() click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 16
def action_controller_test_case?
  lineage?(parent, %i[ActionController TestCase])
end
action_mailer_test_case?() click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 20
def action_mailer_test_case?
  lineage?(parent, %i[ActionMailer TestCase])
end
active_support_test_case?() click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 24
def active_support_test_case?
  lineage?(parent, %i[ActiveSupport TestCase])
end
assert_valid_name() click to toggle source

Raise an error if we don't know now to process the name of this class. Specifically, classes with module-shorthand.

# File lib/minitest_to_rspec/input/model/klass.rb, line 30
def assert_valid_name
  if name.is_a?(Symbol)
    # Valid
  elsif name.respond_to?(:sexp_type) && name.sexp_type == :colon2
    raise ModuleShorthandError
  else
    raise ProcessingError, "Unexpected class expression: #{name}"
  end
end
block() click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 44
def block
  @_block ||= @exp[3..-1] || []
end
block?() click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 40
def block?
  !block.empty?
end
draper_test_case?() click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 52
def draper_test_case?
  lineage?(parent, %i[Draper TestCase])
end
name() click to toggle source

Returns the name of the class. Examples:

  • Banana #=> :Banana

  • Fruit::Banana #=> s(:colon2, s(:const, :Fruit), :Banana)

Note that the latter (module shorthand) is not supported by MinitestToRspec. See `#assert_valid_name`.

# File lib/minitest_to_rspec/input/model/klass.rb, line 64
def name
  @exp[1]
end
parent() click to toggle source

Returns the “inheritance”. Examples:

  • Inherit nothing #=> nil

  • Inherit Foo #=> s(:const, :Foo)

  • Inherit Bar::Foo #=> s(:colon2, s(:const, :Bar), :Foo)

# File lib/minitest_to_rspec/input/model/klass.rb, line 74
def parent
  @_parent ||= @exp[2]
end
test_case?() click to toggle source

Returns true if `@exp` inherits from, e.g. ActiveSupport::TestCase. TODO: Other test case parent classes.

# File lib/minitest_to_rspec/input/model/klass.rb, line 80
def test_case?
  return false unless sexp_type?(:colon2, parent)
  active_support_test_case? ||
    action_controller_test_case? ||
    action_mailer_test_case? ||
    test_unit_test_case? ||
    draper_test_case?
end
test_unit_test_case?() click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 48
def test_unit_test_case?
  lineage?(parent, %i[Test Unit TestCase])
end

Private Instance Methods

ancestor_names(exp) click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 91
def ancestor_names(exp)
  return [exp] if exp.is_a?(Symbol)

  sexp_type?(:colon2, exp) || sexp_type?(:const, exp) ||
    raise(TypeError, "Expected :const or :colon2, got #{exp.inspect}")

  exp.sexp_body.flat_map { |entry| ancestor_names(entry) }
end
lineage?(exp, names) click to toggle source
# File lib/minitest_to_rspec/input/model/klass.rb, line 100
def lineage?(exp, names)
  assert_sexp_type(:colon2, exp)
  ancestor_names(exp) == names
end