Class: RSpecify::Transformers::TestUnitAssertions

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

Overview

Test Unit Assertions Transformer

Catches instances where Test::Unit assertions are being called (such as assert_equal) and morphs the calls to use the RSpec idiom.

Instance Method Summary collapse

Instance Method Details

#assert_equal_call?(e) ⇒ Boolean

assert_equal:

Returns:

  • (Boolean)


27
28
29
# File 'lib/rspecify/transformers/test_unit_assertions.rb', line 27

def assert_equal_call?(e)
  e.kind == :call && e.body[0].nil? && e.body[1] == :assert_equal
end

#assert_not_equal_call?(e) ⇒ Boolean

assert_not_equal:

Returns:

  • (Boolean)


41
42
43
# File 'lib/rspecify/transformers/test_unit_assertions.rb', line 41

def assert_not_equal_call?(e)
  e.kind == :call && e.body[0].nil? && e.body[1] == :assert_not_equal
end

#assert_not_nil_call?(e) ⇒ Boolean

assert_not_nil:

Returns:

  • (Boolean)


55
56
57
# File 'lib/rspecify/transformers/test_unit_assertions.rb', line 55

def assert_not_nil_call?(e)
  e.kind == :call && e.body[0].nil? && e.body[1] == :assert_not_nil
end

#transform(e) ⇒ Object



9
10
11
# File 'lib/rspecify/transformers/test_unit_assertions.rb', line 9

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

#transform_assert_equal_call(e) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/rspecify/transformers/test_unit_assertions.rb', line 30

def transform_assert_equal_call(e)
  method_arguments = e.body[2].body

  s(:call,
    s(:call, method_arguments[1], :should, s(:arglist)),
    :==,
    s(:arglist, method_arguments[0])
  )
end

#transform_assert_not_equal_call(e) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/rspecify/transformers/test_unit_assertions.rb', line 44

def transform_assert_not_equal_call(e)
  method_arguments = e.body[2].body

  s(:call,
    s(:call, method_arguments[1], :should_not, s(:arglist)),
    :==,
    s(:arglist, method_arguments[0])
  )
end

#transform_assert_not_nil_call(e) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rspecify/transformers/test_unit_assertions.rb', line 58

def transform_assert_not_nil_call(e)
  method_arguments = e.body[2].body

  s(:call, method_arguments[0], :should_not, 
    s(:arglist, s(:call, nil, :be_nil, s(:arglist)))
  )
end

#transform_test_unit_assertions(e) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspecify/transformers/test_unit_assertions.rb', line 13

def transform_test_unit_assertions(e)
  case
  when assert_equal_call?(e)
    transform_assert_equal_call(e)
  when assert_not_equal_call?(e)
    transform_assert_not_equal_call(e)
  when assert_not_nil_call?(e)
    transform_assert_not_nil_call(e)
  else
    e
  end
end