Class: TestFileFinder::MappingStrategies::PatternMatching

Inherits:
Object
  • Object
show all
Defined in:
lib/test_file_finder/mapping_strategies/pattern_matching.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maps = nil) ⇒ PatternMatching

Returns a new instance of PatternMatching.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/test_file_finder/mapping_strategies/pattern_matching.rb', line 36

def initialize(maps = nil)
  @pattern_matchers = []

  # Useful for the .default_rails_mapping class method
  #
  # We don't have a file to use, but we still need an instance to be returned
  return unless maps

  maps.each do |map|
    Array(map['source']).each do |source|
      Array(map['test']).each do |test|
        relate(source, test)
      end
    end
  end
end

Instance Attribute Details

#pattern_matchersObject (readonly)

Returns the value of attribute pattern_matchers.



6
7
8
# File 'lib/test_file_finder/mapping_strategies/pattern_matching.rb', line 6

def pattern_matchers
  @pattern_matchers
end

Class Method Details

.complete?(map) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/test_file_finder/mapping_strategies/pattern_matching.rb', line 32

def self.complete?(map)
  !map['source'].nil? && !map['test'].nil?
end

.default_rails_mappingObject



16
17
18
19
20
21
22
# File 'lib/test_file_finder/mapping_strategies/pattern_matching.rb', line 16

def self.default_rails_mapping
  mapping = new
  mapping.relate(%r{^app/(.+)\.rb$}, 'spec/%s_spec.rb')
  mapping.relate(%r{^lib/(.+)\.rb$}, 'spec/lib/%s_spec.rb')
  mapping.relate(%r{^spec/(.+)_spec\.rb$}, 'spec/%s_spec.rb')
  mapping
end

.load(mapping_file) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/test_file_finder/mapping_strategies/pattern_matching.rb', line 8

def self.load(mapping_file)
  maps = YAML.safe_load_file(mapping_file)['mapping']

  validate(maps)

  new(maps)
end

.validate(maps = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/test_file_finder/mapping_strategies/pattern_matching.rb', line 24

def self.validate(maps = nil)
  raise InvalidMappingFileError, 'missing `mapping` in test mapping file' if maps.nil?

  return if maps.all? { |map| complete?(map) }

  raise InvalidMappingFileError, 'missing `source` or `test` in test mapping file'
end

Instance Method Details

#match(files) ⇒ Object



57
58
59
60
61
62
# File 'lib/test_file_finder/mapping_strategies/pattern_matching.rb', line 57

def match(files)
  @pattern_matchers.inject(Set.new) do |result, pattern_matcher|
    test_files = pattern_matcher.call(files)
    result.merge(test_files)
  end.to_a
end

#relate(source, test) ⇒ Object



53
54
55
# File 'lib/test_file_finder/mapping_strategies/pattern_matching.rb', line 53

def relate(source, test)
  @pattern_matchers << pattern_matcher_for(source, test)
end