|
4 | 4 | require 'flay'
|
5 | 5 | require 'tmpdir'
|
6 | 6 |
|
7 |
| -RSpec.describe CC::Engine::Analyzers::Ruby::Main, in_tmpdir: true do |
8 |
| - include AnalyzerSpecHelpers |
| 7 | +module CC::Engine::Analyzers |
| 8 | + RSpec.describe Ruby::Main, in_tmpdir: true do |
| 9 | + include AnalyzerSpecHelpers |
9 | 10 |
|
10 |
| - describe "#run" do |
11 |
| - it "prints an issue" do |
12 |
| - create_source_file("foo.rb", <<-EORUBY) |
13 |
| - describe '#ruby?' do |
14 |
| - before { subject.type = 'ruby' } |
| 11 | + describe "#run" do |
| 12 | + it "prints an issue" do |
| 13 | + create_source_file("foo.rb", <<-EORUBY) |
| 14 | + describe '#ruby?' do |
| 15 | + before { subject.type = 'ruby' } |
15 | 16 |
|
16 |
| - it 'returns true' do |
17 |
| - expect(subject.ruby?).to be true |
| 17 | + it 'returns true' do |
| 18 | + expect(subject.ruby?).to be true |
| 19 | + end |
18 | 20 | end
|
19 |
| - end |
20 | 21 |
|
21 |
| - describe '#js?' do |
22 |
| - before { subject.type = 'js' } |
| 22 | + describe '#js?' do |
| 23 | + before { subject.type = 'js' } |
23 | 24 |
|
24 |
| - it 'returns true' do |
25 |
| - expect(subject.js?).to be true |
| 25 | + it 'returns true' do |
| 26 | + expect(subject.js?).to be true |
| 27 | + end |
26 | 28 | end
|
27 |
| - end |
28 |
| - EORUBY |
29 |
| - |
30 |
| - result = run_engine(engine_conf).strip |
31 |
| - json = JSON.parse(result) |
32 |
| - |
33 |
| - expect(json["type"]).to eq("issue") |
34 |
| - expect(json["check_name"]).to eq("Similar code") |
35 |
| - expect(json["description"]).to eq("Similar code found in 1 other location") |
36 |
| - expect(json["categories"]).to eq(["Duplication"]) |
37 |
| - expect(json["location"]).to eq({ |
38 |
| - "path" => "foo.rb", |
39 |
| - "lines" => { "begin" => 1, "end" => 5 }, |
40 |
| - }) |
41 |
| - expect(json["remediation_points"]).to eq(360000) |
42 |
| - expect(json["other_locations"]).to eq([ |
43 |
| - {"path" => "foo.rb", "lines" => { "begin" => 9, "end" => 13} }, |
44 |
| - ]) |
45 |
| - expect(json["content"]["body"]).to match /This issue has a mass of `36`/ |
46 |
| - expect(json["fingerprint"]).to eq("f21b75bbd135ec3ae6638364d5c73762") |
| 29 | + EORUBY |
| 30 | + |
| 31 | + result = run_engine(engine_conf).strip |
| 32 | + json = JSON.parse(result) |
| 33 | + |
| 34 | + expect(json["type"]).to eq("issue") |
| 35 | + expect(json["check_name"]).to eq("Similar code") |
| 36 | + expect(json["description"]).to eq("Similar code found in 1 other location") |
| 37 | + expect(json["categories"]).to eq(["Duplication"]) |
| 38 | + expect(json["location"]).to eq({ |
| 39 | + "path" => "foo.rb", |
| 40 | + "lines" => { "begin" => 1, "end" => 5 }, |
| 41 | + }) |
| 42 | + expect(json["remediation_points"]).to eq(3300000) |
| 43 | + expect(json["other_locations"]).to eq([ |
| 44 | + {"path" => "foo.rb", "lines" => { "begin" => 9, "end" => 13} }, |
| 45 | + ]) |
| 46 | + expect(json["content"]["body"]).to match /This issue has a mass of `36`/ |
| 47 | + expect(json["fingerprint"]).to eq("f21b75bbd135ec3ae6638364d5c73762") |
| 48 | + end |
| 49 | + |
| 50 | + it "skips unparsable files" do |
| 51 | + create_source_file("foo.rb", <<-EORUBY) |
| 52 | + --- |
| 53 | + EORUBY |
| 54 | + |
| 55 | + expect { |
| 56 | + expect(run_engine(engine_conf)).to eq("") |
| 57 | + }.to output(/Skipping file/).to_stderr |
| 58 | + end |
47 | 59 | end
|
48 | 60 |
|
49 |
| - it "skips unparsable files" do |
50 |
| - create_source_file("foo.rb", <<-EORUBY) |
51 |
| - --- |
52 |
| - EORUBY |
| 61 | + describe "#calculate_points(issue)" do |
| 62 | + let(:analyzer) { Ruby::Main.new(engine_config: engine_conf) } |
| 63 | + let(:base_points) { Ruby::Main::BASE_POINTS } |
| 64 | + let(:points_per) { Ruby::Main::POINTS_PER_OVERAGE } |
| 65 | + let(:threshold) { Ruby::Main::DEFAULT_MASS_THRESHOLD } |
| 66 | + |
| 67 | + context "when issue mass exceeds threshold" do |
| 68 | + it "calculates mass overage points" do |
| 69 | + issue = double(:issue, mass: threshold + 10) |
| 70 | + overage = issue.mass - threshold |
| 71 | + |
| 72 | + expected_points = base_points + overage * points_per |
| 73 | + points = analyzer.calculate_points(issue) |
| 74 | + |
| 75 | + expect(points).to eq(expected_points) |
| 76 | + end |
| 77 | + end |
53 | 78 |
|
54 |
| - expect { |
55 |
| - expect(run_engine(engine_conf)).to eq("") |
56 |
| - }.to output(/Skipping file/).to_stderr |
| 79 | + context "when issue mass is less than threshold" do |
| 80 | + it "calculates mass overage points" do |
| 81 | + issue = double(:issue, mass: threshold - 5) |
| 82 | + overage = issue.mass - threshold |
| 83 | + |
| 84 | + expected_points = base_points + overage * points_per |
| 85 | + points = analyzer.calculate_points(issue) |
| 86 | + |
| 87 | + expect(points).to eq(expected_points) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + context "when issue mass equals threshold" do |
| 92 | + it "calculates mass overage points" do |
| 93 | + issue = double(:issue, mass: threshold) |
| 94 | + overage = issue.mass - threshold |
| 95 | + |
| 96 | + expected_points = base_points + overage * points_per |
| 97 | + points = analyzer.calculate_points(issue) |
| 98 | + |
| 99 | + expect(points).to eq(expected_points) |
| 100 | + end |
| 101 | + end |
57 | 102 | end
|
58 |
| - end |
59 | 103 |
|
60 |
| - def engine_conf |
61 |
| - CC::Engine::Analyzers::EngineConfig.new({}) |
| 104 | + def engine_conf |
| 105 | + EngineConfig.new({}) |
| 106 | + end |
62 | 107 | end
|
63 | 108 | end
|
0 commit comments