Class: R2M::SpecConvector

Inherits:
Object
  • Object
show all
Defined in:
lib/r2m/spec_convector.rb

Overview

Process input RSpec and convert it to minitest

Constant Summary collapse

MANUAL_AROUND_PROCESS =

rubocop:enable Metrics/MethodLength

"skip 'TODO: Remove this skip when `around` will be migrated manually by replacing `run` with `call`'"

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ SpecConvector

rubocop:todo Metrics/ClassLength



8
9
10
# File 'lib/r2m/spec_convector.rb', line 8

def initialize(command)
  @command = command
end

Instance Method Details

#convert_around(file) ⇒ Object



140
141
142
143
144
# File 'lib/r2m/spec_convector.rb', line 140

def convert_around(file)
  @command.gsub_file(file, /^\s*?\baround\b.+?\bdo\b.*?$/) do |match|
    "#{MANUAL_AROUND_PROCESS}\n#{match}"
  end
end

#convert_context_to_describe(file) ⇒ Object

Finds it cases and converts to test methods declarations

context 'with context' do # => describe 'with context' do


39
40
41
# File 'lib/r2m/spec_convector.rb', line 39

def convert_context_to_describe(file)
  @command.gsub_file(file, /\bcontext( '.*?' do\b)/, 'describe\1')
end

#convert_declarations(file) ⇒ Object

rubocop:todo Metrics/MethodLength



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/r2m/spec_convector.rb', line 80

def convert_declarations(file) # rubocop:todo Metrics/AbcSize
  @command.gsub_file(file, /RSpec\.describe (['"])(.*?)\1 do\b/) do |match|
    title = match[/RSpec\.describe (['"])(.*?)\1 do/, 2]

    # rubocop:todo Naming/VariableName
    camelCasedTitle = title.split('::').map do |title_part|
      title_part
        .split
        .reject(&:empty?)
        .map { |part| part[0].upcase + part[1..-1] }.join
    end.join('::')
    # rubocop:enable Naming/VariableName

    # rubocop:todo Naming/VariableName
    "RSpec.describe '#{camelCasedTitle}' do"
    # rubocop:enable Naming/VariableName
  end

  @command.gsub_file(
    file,
    /RSpec\.describe ['"]?(.+?Controller)['"]? do\b/,
    'class \1Test < ActionController::TestCase'
  )
  @command.gsub_file(
    file,
    /RSpec\.describe ['"]?(.+?Mailer)['"]? do\b/,
    'class \1Test < ActionMailer::TestCase'
  )

  @command.gsub_file(
    file,
    /RSpec\.describe ['"]?(.+?Helper)['"]? do\b/,
    'class \1Test < ActionView::TestCase'
  )

  if located_in_requests?(file)
    @command.gsub_file(
      file,
      /RSpec\.describe ['"]?(.+?)['"]? do\b/,
      'class \1Test < ActionDispatch::IntegrationTest'
    )
  end

  if located_in?(file, :systems)
    @command.gsub_file(
      file,
      /RSpec\.describe ['"]?(.+?)['"]? do\b/,
      'class \1Test < ApplicationSystemTestCase'
    )
  end

  @command.gsub_file(
    file,
    /RSpec\.describe ['"]?(.+?)['"]? do\b/,
    'class \1Test < ActiveSupport::TestCase'
  )
end

#convert_helpers_suites(file) ⇒ Object



75
76
77
# File 'lib/r2m/spec_convector.rb', line 75

def convert_helpers_suites(file)
  @command.gsub_file(file, /\bhelper\./, '') if located_in_helpers?(file)
end

#convert_it_to_methods(file) ⇒ Object

Finds it cases and converts to test methods declarations

it 'converts it"s to test method with # support' do
# => def test_converts_it_s_to_test_method_with_support


28
29
30
31
32
33
34
# File 'lib/r2m/spec_convector.rb', line 28

def convert_it_to_methods(file)
  @command.gsub_file(file, /(?<=\bit ').*?(?=' do\b)/) do |match|
    match.gsub(/\W+/, '_').downcase
  end

  @command.gsub_file(file, /it '_?(.*)' do/, 'def test_\1')
end

#convert_mather_be_empty(file) ⇒ Object

Finds be_empty RSpec matchers and converts to minitest matchers

expect(target).to be_empty # => expect(target).must_be_empty
expect(target).to_not be_empty # => expect(target).wont_be_empty
expect(target).not_to be_empty # => expect(target).wont_be_empty


57
58
59
60
# File 'lib/r2m/spec_convector.rb', line 57

def convert_mather_be_empty(file)
  @command.gsub_file(file, /\.to be_empty/, '.must_be_empty')
  @command.gsub_file(file, /\.(to_not|not_to) be_empty/, '.wont_be_empty')
end

#convert_require_helpers(file) ⇒ Object

Finds it cases and converts to test methods declarations

context 'with context' do # => describe 'with context' do


46
47
48
49
50
# File 'lib/r2m/spec_convector.rb', line 46

def convert_require_helpers(file)
  @command.gsub_file(file, /\bsystem_helper\b/, 'application_system_test_case')
  @command.gsub_file(file, /\b(rails_helper|spec_helper)\b/, 'test_helper')
  @command.gsub_file(file, /require (['"]?)rspec\1/, "require 'minitest/autorun'")
end

#convert_simple_matcher(file) ⇒ Object

Finds equal RSpec matchers and converts to minitest matchers

expect(target).to eq expect # => expect(target).must_equal expect
expect(target).not_to eq expect # => expect(target).wont_equal expect


66
67
68
69
70
71
72
73
# File 'lib/r2m/spec_convector.rb', line 66

def convert_simple_matcher(file)
  @command.gsub_file(file, /\.(to_not|not_to|to)\s+(eq|include|match|be_kind_of)\b/) do |match|
    match
      .gsub(/\s+/, ' ')
      .gsub(/\.\w+ /, '.to_not ' => '.wont_', '.not_to ' => '.wont_', '.to ' => '.must_')
      .gsub(/(?<=_)eq/, 'equal')
  end
end

#process(file) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/r2m/spec_convector.rb', line 12

def process(file)
  #   it 'converts it"s to test method with # support' do # => def test_converts_it_s_to_test_method_with_support
  convert_require_helpers(file)
  convert_declarations(file)
  convert_declarations(file)
  convert_context_to_describe(file)
  convert_it_to_methods(file)
  convert_helpers_suites(file)
  convert_mather_be_empty(file)
  convert_simple_matcher(file)
end