Class: MinitestToRspec::Input::Model::Klass

Inherits:
Base
  • Object
show all
Defined in:
lib/minitest_to_rspec/input/model/klass.rb

Overview

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

Instance Method Summary collapse

Methods included from SexpAssertions

#assert_sexp_type, #assert_sexp_type_array, #sexp_type?

Constructor Details

#initialize(exp) ⇒ Klass

Returns a new instance of Klass.



10
11
12
13
14
# 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

Instance Method Details

#action_controller_test_case?Boolean

Returns:

  • (Boolean)


16
17
18
# 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?Boolean

Returns:

  • (Boolean)


20
21
22
# 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?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/minitest_to_rspec/input/model/klass.rb', line 24

def active_support_test_case?
  lineage?(parent, %i[ActiveSupport TestCase])
end

#assert_valid_nameObject

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



30
31
32
33
34
35
36
37
38
# 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

#blockObject



44
45
46
# File 'lib/minitest_to_rspec/input/model/klass.rb', line 44

def block
  @_block ||= @exp[3..-1] || []
end

#block?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/minitest_to_rspec/input/model/klass.rb', line 40

def block?
  !block.empty?
end

#draper_test_case?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/minitest_to_rspec/input/model/klass.rb', line 52

def draper_test_case?
  lineage?(parent, %i[Draper TestCase])
end

#nameObject

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



64
65
66
# File 'lib/minitest_to_rspec/input/model/klass.rb', line 64

def name
  @exp[1]
end

#parentObject

Returns the “inheritance”. Examples:

  • Inherit nothing #=> nil

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

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



74
75
76
# File 'lib/minitest_to_rspec/input/model/klass.rb', line 74

def parent
  @_parent ||= @exp[2]
end

#test_case?Boolean

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

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# 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?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/minitest_to_rspec/input/model/klass.rb', line 48

def test_unit_test_case?
  lineage?(parent, %i[Test Unit TestCase])
end