Class: SixthSense::Adapters::RSpec

Inherits:
FrameworkAdapter show all
Defined in:
lib/sixth_sense/adapters/rspec.rb

Defined Under Namespace

Classes: CodeUnitExtractor, Parser

Constant Summary collapse

GROUP_NAMES =
%i[describe context feature].freeze
EXAMPLE_NAMES =
%i[it specify example scenario].freeze
PENDING_EXAMPLE_NAMES =
%i[xit xspecify xexample pending skip].freeze
SHARED_GROUP_NAMES =
%i[shared_examples shared_examples_for shared_context].freeze
SHARED_USAGE_NAMES =
%i[it_behaves_like it_should_behave_like include_examples include_context].freeze
FIXTURE_NAMES =
%i[let let! subject subject! before around].freeze

Instance Method Summary collapse

Methods inherited from FrameworkAdapter

#checked_coverage_test_command, #coverage_test_command, for_path, #mutation_test_command, register, registry

Instance Method Details

#assertion_patternsObject



68
69
70
# File 'lib/sixth_sense/adapters/rspec.rb', line 68

def assertion_patterns
  %i[expect is_expected should should_not]
end

#handles?(path) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/sixth_sense/adapters/rspec.rb', line 19

def handles?(path)
  return false unless path.end_with?("_spec.rb")
  return false unless File.file?(path)

  source = File.read(path)
  source.match?(/\b(RSpec\.)?(describe|context|it|specify|example)\b/)
end

#map_to_sut(test_file) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/sixth_sense/adapters/rspec.rb', line 60

def map_to_sut(test_file)
  conventional_sut_paths(test_file.path).filter_map do |candidate|
    next unless File.file?(candidate)

    CodeUnitExtractor.new(candidate).code_unit
  end
end

#mutation_session(test_files:) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/sixth_sense/adapters/rspec.rb', line 52

def mutation_session(test_files:)
  {
    framework: :rspec,
    test_files: test_files.map(&:path),
    runner: "mutant-rspec"
  }
end

#parse(path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sixth_sense/adapters/rspec.rb', line 27

def parse(path)
  source = File.read(path)
  parsed = Prism.parse(source)
  unless parsed.success?
    messages = parsed.errors.map(&:message).join(", ")
    raise Error, "failed to parse #{path}: #{messages}"
  end

  test_file = Model::TestFile.new(
    path: path,
    framework: :rspec,
    test_cases: Parser.new(path, parsed.value).test_cases,
    sut_candidates: [],
    metadata: {}
  )
  test_file.sut_candidates = map_to_sut(test_file)
  test_file
end

#run_with_coverage(test_files:, isolation:) ⇒ Object



46
47
48
49
50
# File 'lib/sixth_sense/adapters/rspec.rb', line 46

def run_with_coverage(test_files:, isolation:)
  require_relative "../runners/coverage_runner"

  Runners::CoverageRunner.new.run(test_files: test_files, isolation: isolation)
end