Skip to content

Commit 9b49ff7

Browse files
author
ABaldwinHunter
committed
fix specs. make calculate public
1 parent db70247 commit 9b49ff7

File tree

4 files changed

+68
-64
lines changed

4 files changed

+68
-64
lines changed

lib/cc/engine/analyzers/violation.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ def report_name
3636
"#{current_sexp.file}-#{current_sexp.line}"
3737
end
3838

39+
def calculate_points
40+
if issue.mass >= threshold
41+
base_points + (overage * points_per)
42+
else
43+
DEFAULT_POINTS
44+
end
45+
end
46+
3947
private
4048

4149
attr_reader :base_points, :points_per, :threshold, :hashes
@@ -60,14 +68,6 @@ def name
6068
end
6169
end
6270

63-
def calculate_points
64-
if issue.mass >= threshold
65-
base_points + (overage * points_per)
66-
else
67-
DEFAULT_POINTS
68-
end
69-
end
70-
7171
def overage
7272
issue.mass - threshold
7373
end

spec/cc/engine/analyzers/reporter_spec.rb

Lines changed: 0 additions & 54 deletions
This file was deleted.

spec/cc/engine/analyzers/violation_spec.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,59 @@
88
RSpec.describe CC::Engine::Analyzers::Violation, in_tmpdir: true do
99
include AnalyzerSpecHelpers
1010

11+
describe "#calculate_points" do
12+
context "when issue mass exceeds threshold" do
13+
it "calculates mass overage points" do
14+
base_points = 100
15+
points_per = 50
16+
threshold = 10
17+
issue = double(:issue, mass: 15)
18+
hashes = []
19+
20+
expected_points = base_points + points_per * (issue.mass - threshold)
21+
22+
violation = CC::Engine::Analyzers::Violation.new(base_points, points_per, threshold, issue, hashes)
23+
points = violation.calculate_points
24+
25+
expect(points).to eq(expected_points)
26+
end
27+
end
28+
29+
context "when issue mass is less than threshold" do
30+
it "uses default" do
31+
base_points = 100
32+
points_per = 50
33+
threshold = 18
34+
issue = double(:issue, mass: 15)
35+
hashes = []
36+
37+
expected_points = CC::Engine::Analyzers::Violation::DEFAULT_POINTS
38+
39+
violation = CC::Engine::Analyzers::Violation.new(base_points, points_per, threshold, issue, hashes)
40+
points = violation.calculate_points
41+
42+
expect(points).to eq(CC::Engine::Analyzers::Violation::DEFAULT_POINTS)
43+
end
44+
end
45+
46+
context "when issue mass equals threshold" do
47+
it "calculates remediation points" do
48+
base_points = 100
49+
points_per = 50
50+
threshold = 15
51+
issue = double(:issue, mass: 15)
52+
hashes = []
53+
54+
expected_points = base_points + points_per * (issue.mass - threshold)
55+
56+
violation = CC::Engine::Analyzers::Violation.new(base_points, points_per, threshold, issue, hashes)
57+
points = violation.calculate_points
58+
59+
expect(points).to eq(expected_points)
60+
end
61+
end
62+
end
63+
1164
describe "#format" do
1265
it "returns correctly formatted issue" do
1366

spec/support/helpers/analyzer_spec_helpers.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,23 @@ def sample_flay_issue(
2121
mass = 99,
2222
locations = sample_locations
2323
)
24-
flay = Flay::Item.new(structural_hash, name, bonus, mass, locations)
24+
25+
Flay::Item.new(structural_hash, name, bonus, mass, locations)
2526
end
2627

2728
def sample_locations
2829
[
2930
sample_flay_location("./foo.js", 1, nil),
3031
sample_flay_location("./foo.js", 2, nil),
31-
sample_flay_location("./foo.js", 3, nil),
32+
sample_flay_location("./foo.js", 3, nil)
3233
]
3334
end
3435

3536
def sample_flay_location(file = "./foo.js", line = 1, fuzzy = nil)
3637
Flay::Location.new(file, line, fuzzy)
3738
end
39+
40+
def sample_hashes
41+
[s(:ExpressionStatement, s(:expression, s(:CallExpression, s(:callee, s(:MemberExpression, s(:object, s(:Identifier, :console)), s(:property, s(:Identifier, :log)))), s(:arguments, s(:StringLiteral, :"hello JS!"))))), s(:ExpressionStatement, s(:expression, s(:CallExpression, s(:callee, s(:MemberExpression, s(:object, s(:Identifier, :console)), s(:property, s(:Identifier, :log)))), s(:arguments, s(:StringLiteral, :"hello JS!"))))), s(:ExpressionStatement, s(:expression, s(:CallExpression, s(:callee, s(:MemberExpression, s(:object, s(:Identifier, :console)), s(:property, s(:Identifier, :log)))), s(:arguments, s(:StringLiteral, :"hello JS!")))))]
42+
end
3843
end

0 commit comments

Comments
 (0)