Class: RSpecify::Transformers::TestClassAndMethods

Inherits:
RubyTransform::Transformer
  • Object
show all
Defined in:
lib/rspecify/transformers/test_class_and_methods.rb

Overview

Test Class And Methods Transformer

Catches instances where a class is being defined extending ActiveSupport::TestClass and converts it into an RSpec describe block, then converts all methods contained therein beginning with “test_” to rspec “it” blocks.

Instance Method Summary collapse

Instance Method Details

#transform(e) ⇒ Object



10
11
12
# File 'lib/rspecify/transformers/test_class_and_methods.rb', line 10

def transform(e)
  super sexp?(e) ? transform_test_class_and_methods(e) : e
end

#transform_test_class(e) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rspecify/transformers/test_class_and_methods.rb', line 22

def transform_test_class(e)
  name = e.body[0].to_s.gsub /Test$/, ""
  class_body = e.body[2].body[0]
  if class_body.kind == :block
    new_block = Sexp.from_array([:block] + class_body.body.map {|e| transform_test_class_method_definition e })
  else
    new_block = transform_test_class_method_definition class_body
  end
  s(:iter, 
    s(:call, nil, :describe, s(:arglist, s(:const, name.to_sym))),
    nil,
    new_block
  )
end

#transform_test_class_and_methods(e) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rspecify/transformers/test_class_and_methods.rb', line 14

def transform_test_class_and_methods(e)
  if e.kind == :class && e.body[1] == s(:colon2, s(:const, :ActiveSupport), :TestCase)
    transform_test_class(e)
  else
    e
  end
end

#transform_test_class_method_definition(e) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rspecify/transformers/test_class_and_methods.rb', line 37

def transform_test_class_method_definition(e)
  if e.kind == :defn && e.body[0].to_s.starts_with?("test_") && e.body[1].body.empty?
    test_name = e.body[0].to_s.gsub(/^test_/, "").gsub("_", " ")

    s(:iter,
      s(:call, nil, :it, s(:arglist, s(:str, test_name))),
      nil,
      e.body[2].body[0]
    )
  elsif e.kind == :iter && e.body[0].kind == :call && e.body[0].body[1] == :test
    # All we need to do is change the name of the invoked method
    e.clone.tap do |e|
      e.body[0][2] = :it
    end
  else
    e
  end
end