Class: TestFileFinder::MappingStrategies::DirectMatching

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

Constant Summary collapse

JSON_ERROR_MESSAGE =
'json file should contain a json object, with array of test files as the values'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map, limit_percentage: nil, limit_min: nil) ⇒ DirectMatching

Returns a new instance of DirectMatching.



39
40
41
42
43
# File 'lib/test_file_finder/mapping_strategies/direct_matching.rb', line 39

def initialize(map, limit_percentage: nil, limit_min: nil)
  @map              = map
  @limit_percentage = limit_percentage
  @limit_min        = limit_min
end

Instance Attribute Details

#limit_minObject (readonly)

Returns the value of attribute limit_min.



10
11
12
# File 'lib/test_file_finder/mapping_strategies/direct_matching.rb', line 10

def limit_min
  @limit_min
end

#limit_percentageObject (readonly)

Returns the value of attribute limit_percentage.



10
11
12
# File 'lib/test_file_finder/mapping_strategies/direct_matching.rb', line 10

def limit_percentage
  @limit_percentage
end

#mapObject (readonly)

Returns the value of attribute map.



10
11
12
# File 'lib/test_file_finder/mapping_strategies/direct_matching.rb', line 10

def map
  @map
end

Class Method Details

.load_json(json_file, **kwargs) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/test_file_finder/mapping_strategies/direct_matching.rb', line 12

def self.load_json(json_file, **kwargs)
  map = JSON.parse(File.read(json_file))

  validate_map(map)
  validate_params(**kwargs)

  new(map, **kwargs)
end

.validate_map(map) ⇒ Object



21
22
23
24
25
# File 'lib/test_file_finder/mapping_strategies/direct_matching.rb', line 21

def self.validate_map(map)
  return if map.is_a?(Hash) && map.values.all?(Array)

  raise InvalidMappingFileError, JSON_ERROR_MESSAGE
end

.validate_params(limit_percentage: nil, limit_min: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/test_file_finder/mapping_strategies/direct_matching.rb', line 27

def self.validate_params(limit_percentage: nil, limit_min: nil)
  return if limit_percentage.nil?

  limit_percentage_valid = limit_percentage.is_a?(Integer) && (1..100).cover?(limit_percentage)
  raise "Invalid value for limit_percentage: should be an integer between 1 and 100" unless limit_percentage_valid

  limit_min_valid = limit_min.nil? || (limit_min.is_a?(Integer) && limit_min.positive?)
  return if limit_min_valid

  raise "Invalid value for limit_min: should be an integer strictly greater than zero"
end

Instance Method Details

#match(files) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/test_file_finder/mapping_strategies/direct_matching.rb', line 45

def match(files)
  Array(files).inject(Set.new) do |result, file|
    test_files = @map.fetch(file, [])

    if limit_percentage
      sample_size = ((limit_percentage / 100.0) * test_files.count).round
      sample_size = limit_min if limit_min && sample_size <= limit_min

      test_files = test_files.sample(sample_size)
    end

    result.merge(test_files)
  end.to_a
end