Skip to content

Commit 266eb9d

Browse files
committed
Add coverage for Issue
1 parent d6512a0 commit 266eb9d

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

config/cops.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ Metrics/AbcSize:
22
base_points: 1_000_000
33
overage_points: 70_000
44
Metrics/BlockNesting:
5-
remediation_points: 100_000
5+
base_points: 100_000
6+
overage_points: 50_000
67
Metrics/ClassLength:
78
base_points: 5_000_000
89
overage_points: 35_000
910
Metrics/CyclomaticComplexity:
1011
base_points: 1_000_000
1112
overage_points: 70_000
1213
Metrics/LineLength:
13-
remediation_points: 50_000
14+
base_points: 50_000
15+
overage_points: 0
1416
Metrics/MethodLength:
1517
base_points: 1_000_000
1618
overage_points: 70_000

lib/cc/engine/issue.rb

+10-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Issue < SimpleDelegator
1212
def initialize(issue, path, cop_list: nil)
1313
@path = path
1414
@cop_list = cop_list
15+
1516
super(issue)
1617
end
1718

@@ -73,8 +74,8 @@ def overage_points
7374

7475
def multiplier
7576
result = message.scan(MULTIPLIER_REGEX)
76-
score, max = result[0]
77-
score.to_i - max.to_i
77+
score, threshold = result[0]
78+
score.to_i - threshold.to_i
7879
end
7980

8081
def category
@@ -94,29 +95,25 @@ def positions
9495
}
9596
end
9697

97-
# Increment column value as columns are 0-based in parser
98+
# Increments column values as columns are 0-based in parser
9899
def columns
99100
return @columns if defined?(@columns)
100101

101-
if location.is_a?(RuboCop::Cop::Lint::Syntax::PseudoSourceRange)
102-
@columns = [location.column + 1, location.column + 1]
103-
else
104-
@columns = [location.column + 1, location.last_column + 1]
105-
end
102+
end_column = location.try(:last_column) || location.column
103+
@columns = [location.column + 1, end_column + 1]
106104
end
107105

108106
def lines
109107
return @lines if defined?(@lines)
110108

111-
if location.is_a?(RuboCop::Cop::Lint::Syntax::PseudoSourceRange)
112-
@lines = [location.line, location.line]
113-
else
114-
@lines = [location.first_line, location.last_line]
115-
end
109+
begin_line = location.try(:first_line) || location.line
110+
end_line = location.try(:last_line) || location.line
111+
@lines = [begin_line, end_line]
116112
end
117113

118114
def content_body
119115
return @content_body if defined?(@content_body)
116+
120117
content_path = expand_config_path("contents/#{cop_name.underscore}.md")
121118
@content_body = File.exist?(content_path) && File.read(content_path)
122119
end

spec/cc/engine/issue_spec.rb

+35
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,41 @@
33

44
module CC::Engine
55
describe Issue do
6+
describe "#to_json" do
7+
let(:issue) do
8+
location = OpenStruct.new
9+
location.first_line = 10
10+
location.last_line = 10
11+
location.column = 3
12+
location.last_column = 99
13+
14+
offense = OpenStruct.new
15+
offense.cop_name = "Metrics/LineLength"
16+
offense.message = "Line too long [100/80]"
17+
offense.location = location
18+
19+
Issue.new(offense, "app/models/user.rb")
20+
end
21+
22+
it "returns a json issue for a Rubocop offense" do
23+
attributes = JSON.parse(issue.to_json)
24+
25+
expect(attributes["type"]).to eq("Issue")
26+
expect(attributes["check_name"]).to eq("Rubocop/Metrics/LineLength")
27+
expect(attributes["description"]).to eq("Line too long [100/80]")
28+
expect(attributes["categories"]).to eq(["Style"])
29+
expect(attributes["remediation_points"]).to eq(50_000)
30+
expect(attributes["location"]["path"]).to eq("app/models/user.rb")
31+
expect(attributes["location"]["positions"]["begin"]["line"]).to eq(10)
32+
expect(attributes["location"]["positions"]["end"]["line"]).to eq(10)
33+
expect(attributes["location"]["positions"]["begin"]["column"]).to eq(4)
34+
expect(attributes["location"]["positions"]["end"]["column"]).to eq(100)
35+
expect(attributes["content"]["body"]).to include(
36+
"This cop checks the length of lines in the source code."
37+
)
38+
end
39+
end
40+
641
describe "#remediation points" do
742
describe "cop has configured remediation points" do
843
describe "without a multiplier" do

0 commit comments

Comments
 (0)