1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
# frozen_string_literal: true
module SimpleCov
#
# Base filter class. Inherit from this to create custom filters,
# and overwrite the passes?(source_file) instance method
#
# # A sample class that rejects all source files.
# class StupidFilter < SimpleCov::Filter
# def passes?(source_file)
# false
# end
# end
#
class Filter
attr_reader :filter_argument
def initialize(filter_argument)
@filter_argument = filter_argument
end
def matches?(_source_file)
raise "The base filter class is not intended for direct use"
end
def passes?(source_file)
warn "#{Kernel.caller.first}: [DEPRECATION] #passes? is deprecated. Use #matches? instead."
matches?(source_file)
end
def self.build_filter(filter_argument)
return filter_argument if filter_argument.is_a?(SimpleCov::Filter)
class_for_argument(filter_argument).new(filter_argument)
end
def self.class_for_argument(filter_argument)
case filter_argument
when String
SimpleCov::StringFilter
when Regexp
SimpleCov::RegexFilter
when Array
SimpleCov::ArrayFilter
when Proc
SimpleCov::BlockFilter
else
raise ArgumentError, "You have provided an unrecognized filter type"
end
end
end
class StringFilter < SimpleCov::Filter
# Returns true when the given source file's filename matches the
# string configured when initializing this Filter with StringFilter.new('somestring')
def matches?(source_file)
source_file.project_filename.include?(filter_argument)
end
end
class RegexFilter < SimpleCov::Filter
# Returns true when the given source file's filename matches the
# regex configured when initializing this Filter with RegexFilter.new(/someregex/)
def matches?(source_file)
(source_file.project_filename =~ filter_argument)
end
end
class BlockFilter < SimpleCov::Filter
# Returns true if the block given when initializing this filter with BlockFilter.new {|src_file| ... }
# returns true for the given source file.
def matches?(source_file)
filter_argument.call(source_file)
end
end
class ArrayFilter < SimpleCov::Filter
def initialize(filter_argument)
filter_objects = filter_argument.map do |arg|
Filter.build_filter(arg)
end
super(filter_objects)
end
# Returns true if any of the filters in the array match the given source file.
# Configure this Filter like StringFilter.new(['some/path', /^some_regex/, Proc.new {|src_file| ... }])
def matches?(source_files_list)
filter_argument.any? do |arg|
arg.matches?(source_files_list)
end
end
end
end
|