Sha256: 304ee063bbdf890b65dd8324ce0fda929bfebc38cedd5024a22ac33992734333

Contents?: true

Size: 1.16 KB

Versions: 8

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module SimpleCov
  # Classifies whether lines are relevant for code coverage analysis.
  # Comments & whitespace lines, and :nocov: token blocks, are considered not relevant.

  class LinesClassifier
    RELEVANT = 0
    NOT_RELEVANT = nil

    WHITESPACE_LINE = /^\s*$/.freeze
    COMMENT_LINE = /^\s*#/.freeze
    WHITESPACE_OR_COMMENT_LINE = Regexp.union(WHITESPACE_LINE, COMMENT_LINE)

    def self.no_cov_line
      /^(\s*)#(\s*)(:#{SimpleCov.nocov_token}:)/o
    end

    def self.no_cov_line?(line)
      no_cov_line.match?(line)
    rescue ArgumentError
      # E.g., line contains an invalid byte sequence in UTF-8
      false
    end

    def self.whitespace_line?(line)
      WHITESPACE_OR_COMMENT_LINE.match?(line)
    rescue ArgumentError
      # E.g., line contains an invalid byte sequence in UTF-8
      false
    end

    def classify(lines)
      skipping = false

      lines.map do |line|
        if self.class.no_cov_line?(line)
          skipping = !skipping
          NOT_RELEVANT
        elsif skipping || self.class.whitespace_line?(line)
          NOT_RELEVANT
        else
          RELEVANT
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 4 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/simplecov-0.22.0/lib/simplecov/lines_classifier.rb
simplecov-0.22.0 lib/simplecov/lines_classifier.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/simplecov-0.21.2/lib/simplecov/lines_classifier.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/simplecov-0.21.2/lib/simplecov/lines_classifier.rb
simplecov-0.21.2 lib/simplecov/lines_classifier.rb
simplecov-0.21.1 lib/simplecov/lines_classifier.rb
simplecov-0.21.0 lib/simplecov/lines_classifier.rb
simplecov-0.20.0 lib/simplecov/lines_classifier.rb