Skip to content

Commit

Permalink
Support nocov comment at the end of a line
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberdelia committed Aug 28, 2023
1 parent 53ad283 commit e05c83f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
17 changes: 14 additions & 3 deletions lib/simplecov/lines_classifier.rb
Expand Up @@ -12,17 +12,28 @@ class LinesClassifier
COMMENT_LINE = /^\s*#/.freeze
WHITESPACE_OR_COMMENT_LINE = Regexp.union(WHITESPACE_LINE, COMMENT_LINE)

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

def self.no_cov_line
/#(\s*)(:#{SimpleCov.nocov_token}:)(\s*)$/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.no_cov_block?(line)
no_cov_block.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
Expand All @@ -34,10 +45,10 @@ def classify(lines)
skipping = false

lines.map do |line|
if self.class.no_cov_line?(line)
if self.class.no_cov_block?(line)
skipping = !skipping
NOT_RELEVANT
elsif skipping || self.class.whitespace_line?(line)
elsif skipping || self.class.no_cov_line?(line) || self.class.whitespace_line?(line)
NOT_RELEVANT
else
RELEVANT
Expand Down
26 changes: 21 additions & 5 deletions spec/lines_classifier_spec.rb
Expand Up @@ -65,6 +65,20 @@
end
end

describe ":nocov: one liner" do
it "determines :nocov: lines are not-relevant" do
classified_lines = subject.classify [
"def hi",
"raise NotImplementedError # :nocov:",
"end",
""
]

expect(classified_lines.length).to eq 4
expect(classified_lines[1]).to be_irrelevant
end
end

describe ":nocov: blocks" do
it "determines :nocov: blocks are not-relevant" do
classified_lines = subject.classify [
Expand All @@ -80,21 +94,23 @@

it "determines all lines after a non-closing :nocov: as not-relevant" do
classified_lines = subject.classify [
"puts 'Not relevant' # :nocov:",
"# :nocov:",
"puts 'Not relevant'",
"# :nocov:",
"puts 'Relevant again'",
"puts 'Still relevant'",
"# :nocov:",
"puts 'Not relevant till the end'",
"puts 'Not relevant till the end' # :nocov:",
"puts 'Ditto'"
]

expect(classified_lines.length).to eq 8
expect(classified_lines.length).to eq 9

expect(classified_lines[0..2]).to all be_irrelevant
expect(classified_lines[3..4]).to all be_relevant
expect(classified_lines[5..7]).to all be_irrelevant
expect(classified_lines[0]).to be_irrelevant
expect(classified_lines[1..3]).to all be_irrelevant
expect(classified_lines[4..5]).to all be_relevant
expect(classified_lines[6..8]).to all be_irrelevant
end
end
end
Expand Down

0 comments on commit e05c83f

Please sign in to comment.