Class: WTT::Core::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/wtt/core/mapper.rb

Overview

Mapping from test file to executed code (i.e. coverage without execution count).

Terminologies:

spectra: { filename => [line, numbers, executed], ... }
mapping: { test_file => spectra }

Constant Summary collapse

STORAGE_SECTION =
'mapping'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage) ⇒ Mapper

Returns a new instance of Mapper.

Parameters:



21
22
23
24
# File 'lib/wtt/core/mapper.rb', line 21

def initialize(storage)
  @storage = storage
  read!
end

Instance Attribute Details

#mappingObject (readonly)

Returns the value of attribute mapping.



18
19
20
# File 'lib/wtt/core/mapper.rb', line 18

def mapping
  @mapping
end

Instance Method Details

#append_from_coverage(test, coverage) ⇒ void

This method returns an undefined value.

Append the new mapping to test-to-code mapping file.

Parameters:

  • test (String)

    test file for which the coverage data is produced

  • coverage (Hash)

    coverage data generated using ‘Coverage.start` and `Coverage.result`



35
36
37
38
# File 'lib/wtt/core/mapper.rb', line 35

def append_from_coverage(test, coverage)
  spectra = normalize_paths(select_project_files(spectra_from_coverage(coverage)))
  @mapping[test] = spectra
end

#append_from_simplecov(test, coverage) ⇒ void

This method returns an undefined value.

Append the new mapping to test-to-code mapping file.

Parameters:

  • test (String)

    test file for which the coverage data is produced

  • coverage (Hash)

    coverage data generated using ‘Coverage.start` and `Coverage.result`



45
46
47
48
# File 'lib/wtt/core/mapper.rb', line 45

def append_from_simplecov(test, coverage)
  spectra = normalize_paths(select_project_files(spectra_from_simplecov(coverage)))
  @mapping[test] = spectra
end

#get_tests(file, lineno) ⇒ Set

Get tests affected from change of file ‘file` at line number `lineno`

Parameters:

  • file (String)

    file name which might have effects on some tests

  • lineno (Integer)

    line number in the file which might have effects on some tests

Returns:

  • (Set)

    a set of test files which might be affected by the change in file at lineno



65
66
67
68
69
70
71
72
73
74
# File 'lib/wtt/core/mapper.rb', line 65

def get_tests(file, lineno)
  tests = Set.new
  @mapping.each do |test, spectra|
    lines = spectra[file]
    next unless lines
    tests << test if matcher.match(lines, lineno)
  end
  warn "No tests found for #{file}:#{lineno}." if file.end_with?('.rb')
  tests
end

#matcherObject



26
27
28
# File 'lib/wtt/core/mapper.rb', line 26

def matcher
  @matcher ||= WTT.configuration.matcher
end

#read!Object

Read test-to-code mapping from storage.



51
52
53
# File 'lib/wtt/core/mapper.rb', line 51

def read!
  @mapping = @storage.read(STORAGE_SECTION)
end

#unmapped_testsObject



76
77
78
# File 'lib/wtt/core/mapper.rb', line 76

def unmapped_tests
  @mapping.select { |_test, spectra| spectra.count == 0 }.keys
end

#write!Object

Write test-to-code mapping to storage.



56
57
58
# File 'lib/wtt/core/mapper.rb', line 56

def write!
  @storage.write!(STORAGE_SECTION, @mapping)
end