Class: AssertionRewriter

Inherits:
Parser::TreeRewriter
  • Object
show all
Defined in:
lib/minitest_spec_converter/assertion_rewriter.rb

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/minitest_spec_converter/assertion_rewriter.rb', line 56

def on_block(node)
  method_name = node.children[0].children[1]
  return super unless %i[assert_raises refute_raises].include?(method_name)

  new_method_name = (method_name == :assert_raises) ? :must_raise : :wont_raise
  args = node.children[0].children[2..]
  body = node.children[2]

  code = <<~RUBY
    proc do
      #{Unparser.unparse(body)}
    end.#{new_method_name} #{Unparser.unparse(args.first)}
  RUBY

  new_node = Unparser.parse(code)

  replace(node.location.expression, Unparser.unparse(new_node))
end

#on_send(node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/minitest_spec_converter/assertion_rewriter.rb', line 7

def on_send(node)
  case node.children[1]
  when :assert_equal
    new_node = convert_to_spec_method(node, :must_equal)
  when :assert_not_equal
    new_node = convert_to_spec_method(node, :wont_equal)
  when :assert_nil
    new_node = convert_to_spec_method(node, :must_be_nil)
  when :refute_nil
    new_node = convert_to_spec_method(node, :wont_be_nil)
  when :assert_empty
    new_node = convert_to_spec_method(node, :must_be_empty)
  when :refute_empty
    new_node = convert_to_spec_method(node, :wont_be_empty)
  when :assert_includes
    new_node = convert_to_spec_method(node, :must_include, reverse: true)
  when :refute_includes
    new_node = convert_to_spec_method(node, :wont_include, reverse: true)
  when :assert_match
    new_node = convert_to_spec_method(node, :must_match)
  when :refute_match
    new_node = convert_to_spec_method(node, :wont_match)
  when :assert_instance_of
    new_node = convert_to_spec_method(node, :must_be_instance_of)
  when :assert_kind_of
    new_node = convert_to_spec_method(node, :must_be_kind_of)
  when :refute_instance_of
    new_node = convert_to_spec_method(node, :wont_be_instance_of)
  when :refute_kind_of
    new_node = convert_to_spec_method(node, :wont_be_kind_of)
  when :assert
    new_node = convert_to_spec_method(node, :must_equal, value: "true", reverse: true)
  when :refute
    new_node = convert_to_spec_method(node, :must_equal, value: "false", reverse: true)
  when :assert_predicate
    new_node = convert_to_spec_predicate_method(node, :must_be)
  when :refute_predicate
    new_node = convert_to_spec_predicate_method(node, :wont_be)
  when :assert_operator
    new_node = convert_to_spec_operator_method(node, :must_be)
  when :refute_operator
    new_node = convert_to_spec_operator_method(node, :wont_be)
  else
    return super
  end

  replace(node.location.expression, Unparser.unparse(new_node))
end