Class: SimpleCov::StringFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/simplecov/filter.rb,
sig/simplecov.rbs

Overview

Matches when the project path contains the configured string at a path-segment boundary ("lib" matches "lib/foo.rb" but not "library/").

Instance Attribute Summary

Attributes inherited from Filter

#filter_argument

Instance Method Summary collapse

Methods inherited from Filter

build_filter, class_for_argument, #initialize

Constructor Details

This class inherits a constructor from SimpleCov::Filter

Instance Method Details

#compute_segment_patternRegexp

Returns:

  • (Regexp)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/simplecov/filter.rb', line 66

def compute_segment_pattern
  normalized = filter_argument.delete_prefix("/")
  escaped    = Regexp.escape(normalized)
  boundary   = '(?:\A|/)'

  if normalized.include?(".")
    # Filename pattern (e.g. "test.rb" matches "faked_test.rb"): allow
    # substring match within the last path segment, anchored to a
    # segment boundary.
    %r{#{boundary}[^/]*#{escaped}}
  elsif normalized.end_with?("/")
    # Trailing slash signals directory-only matching.
    /#{boundary}#{escaped}/
  else
    # Directory or path: require a segment-boundary match so "lib"
    # matches "lib/" but not "library/".
    %r{#{boundary}#{escaped}(?=[/.]|\z)}
  end
end

#matches?(source_file) ⇒ Boolean

Returns true when the given source file's filename matches the string configured when initializing this Filter with StringFilter.new('somestring'). Matching is path-segment-aware: the argument must appear immediately after a "/" and be followed by "/" or end-of-string, so "lib" matches "/lib/foo.rb" but not "/app/models/library.rb".

Parameters:

Returns:

  • (Boolean)


56
57
58
# File 'lib/simplecov/filter.rb', line 56

def matches?(source_file)
  source_file.project_filename.match?(segment_pattern)
end

#segment_patternRegexp

Returns:

  • (Regexp)


62
63
64
# File 'lib/simplecov/filter.rb', line 62

def segment_pattern
  @segment_pattern ||= compute_segment_pattern
end