Skip to content

Commit ee180f4

Browse files
committed
Support nocov comment at the end of a line
1 parent 91f450e commit ee180f4

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

lib/simplecov/lines_classifier.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,28 @@ class LinesClassifier
1212
COMMENT_LINE = /^\s*#/.freeze
1313
WHITESPACE_OR_COMMENT_LINE = Regexp.union(WHITESPACE_LINE, COMMENT_LINE)
1414

15-
def self.no_cov_line
15+
def self.no_cov_block
1616
/^(\s*)#(\s*)(:#{SimpleCov.nocov_token}:)/o
1717
end
1818

19+
def self.no_cov_line
20+
/#(\s*)(:#{SimpleCov.nocov_token}:)(\s*)$/o
21+
end
22+
1923
def self.no_cov_line?(line)
2024
no_cov_line.match?(line)
2125
rescue ArgumentError
2226
# E.g., line contains an invalid byte sequence in UTF-8
2327
false
2428
end
2529

30+
def self.no_cov_block?(line)
31+
no_cov_block.match?(line)
32+
rescue ArgumentError
33+
# E.g., line contains an invalid byte sequence in UTF-8
34+
false
35+
end
36+
2637
def self.whitespace_line?(line)
2738
WHITESPACE_OR_COMMENT_LINE.match?(line)
2839
rescue ArgumentError
@@ -34,10 +45,10 @@ def classify(lines)
3445
skipping = false
3546

3647
lines.map do |line|
37-
if self.class.no_cov_line?(line)
48+
if self.class.no_cov_block?(line)
3849
skipping = !skipping
3950
NOT_RELEVANT
40-
elsif skipping || self.class.whitespace_line?(line)
51+
elsif skipping || self.class.no_cov_line?(line) || self.class.whitespace_line?(line)
4152
NOT_RELEVANT
4253
else
4354
RELEVANT

spec/lines_classifier_spec.rb

+21-5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@
6565
end
6666
end
6767

68+
describe ":nocov: one liner" do
69+
it "determines :nocov: lines are not-relevant" do
70+
classified_lines = subject.classify [
71+
"def hi",
72+
"raise NotImplementedError # :nocov:",
73+
"end",
74+
""
75+
]
76+
77+
expect(classified_lines.length).to eq 4
78+
expect(classified_lines[1]).to be_irrelevant
79+
end
80+
end
81+
6882
describe ":nocov: blocks" do
6983
it "determines :nocov: blocks are not-relevant" do
7084
classified_lines = subject.classify [
@@ -80,21 +94,23 @@
8094

8195
it "determines all lines after a non-closing :nocov: as not-relevant" do
8296
classified_lines = subject.classify [
97+
"puts 'Not relevant' # :nocov:",
8398
"# :nocov:",
8499
"puts 'Not relevant'",
85100
"# :nocov:",
86101
"puts 'Relevant again'",
87102
"puts 'Still relevant'",
88103
"# :nocov:",
89-
"puts 'Not relevant till the end'",
104+
"puts 'Not relevant till the end' # :nocov:",
90105
"puts 'Ditto'"
91106
]
92107

93-
expect(classified_lines.length).to eq 8
108+
expect(classified_lines.length).to eq 9
94109

95-
expect(classified_lines[0..2]).to all be_irrelevant
96-
expect(classified_lines[3..4]).to all be_relevant
97-
expect(classified_lines[5..7]).to all be_irrelevant
110+
expect(classified_lines[0]).to be_irrelevant
111+
expect(classified_lines[1..3]).to all be_irrelevant
112+
expect(classified_lines[4..5]).to all be_relevant
113+
expect(classified_lines[6..8]).to all be_irrelevant
98114
end
99115
end
100116
end

0 commit comments

Comments
 (0)