|
| 1 | +require "spec_helper" |
| 2 | +require "cc/engine/duplication" |
| 3 | + |
| 4 | +module CC::Engine::Analyzers |
| 5 | + RSpec.describe Violations do |
| 6 | + describe "#each" do |
| 7 | + it "yields correct number of violations" do |
| 8 | + issue = double(:issue, mass: 10, identical?: true) |
| 9 | + hashes = sexps |
| 10 | + language_strategy = double(:language_strategy, calculate_points: 30) |
| 11 | + |
| 12 | + violations = [] |
| 13 | + |
| 14 | + Violations.new(language_strategy, issue, hashes).each do |v| |
| 15 | + violations << v |
| 16 | + end |
| 17 | + |
| 18 | + expect(violations.length).to eq(3) |
| 19 | + end |
| 20 | + |
| 21 | + it "yields violation objects with correct information" do |
| 22 | + issue = double(:issue, mass: 10, identical?: true) |
| 23 | + hashes = sexps |
| 24 | + language_strategy = double(:language_strategy, calculate_points: 30) |
| 25 | + |
| 26 | + violations = [] |
| 27 | + |
| 28 | + Violations.new(language_strategy, issue, hashes).each do |v| |
| 29 | + violations << v |
| 30 | + end |
| 31 | + |
| 32 | + first_formatted = violations[0].format |
| 33 | + second_formatted = violations[1].format |
| 34 | + third_formatted = violations[2].format |
| 35 | + |
| 36 | + expect(first_formatted[:type]).to eq("issue") |
| 37 | + expect(first_formatted[:check_name]).to eq("Identical code") |
| 38 | + expect(first_formatted[:description]).to eq("Identical code found in 2 other locations") |
| 39 | + expect(first_formatted[:categories]).to eq(["Duplication"]) |
| 40 | + expect(first_formatted[:remediation_points]).to eq(30) |
| 41 | + expect(first_formatted[:location]).to eq({:path=>"file.rb", :lines=>{:begin=>1, :end=>5}}) |
| 42 | + expect(first_formatted[:other_locations]).to eq([ |
| 43 | + { :path => "file.rb", :lines => { :begin => 9, :end => 13} }, |
| 44 | + { :path => "file.rb", :lines => { :begin => 17, :end => 21} }, |
| 45 | + ]) |
| 46 | + expect(first_formatted[:fingerprint]).to eq("c2712b56bff2becf4ae2a8469e1171c7") |
| 47 | + |
| 48 | + expect(second_formatted[:location]).to eq({:path=>"file.rb", :lines=>{:begin=>9, :end=>13}}) |
| 49 | + expect(second_formatted[:other_locations]).to eq([ |
| 50 | + { :path => "file.rb", :lines => { :begin => 1, :end => 5} }, |
| 51 | + { :path => "file.rb", :lines => { :begin => 17, :end => 21} }, |
| 52 | + ]) |
| 53 | + |
| 54 | + expect(third_formatted[:location]).to eq({:path=>"file.rb", :lines=>{:begin=>17, :end=>21}}) |
| 55 | + expect(third_formatted[:other_locations]).to eq([ |
| 56 | + { :path => "file.rb", :lines => { :begin => 1, :end => 5} }, |
| 57 | + { :path => "file.rb", :lines => { :begin => 9, :end => 13} }, |
| 58 | + ]) |
| 59 | + end |
| 60 | + |
| 61 | + def sexps |
| 62 | + source = <<-SOURCE |
| 63 | +describe '#ruby?' do |
| 64 | + before { subject.type = 'ruby' } |
| 65 | +
|
| 66 | + it 'returns true' do |
| 67 | + expect(subject.ruby?).to be true |
| 68 | + end |
| 69 | +end |
| 70 | +
|
| 71 | +describe '#js?' do |
| 72 | + before { subject.type = 'js' } |
| 73 | +
|
| 74 | + it 'returns true' do |
| 75 | + expect(subject.js?).to be true |
| 76 | + end |
| 77 | +end |
| 78 | +
|
| 79 | +describe '#whaddup?' do |
| 80 | + before { subject.type = 'js' } |
| 81 | +
|
| 82 | + it 'returns true' do |
| 83 | + expect(subject.js?).to be true |
| 84 | + end |
| 85 | +end |
| 86 | + SOURCE |
| 87 | + |
| 88 | + flay = Flay.new({ |
| 89 | + diff: false, |
| 90 | + mass: CC::Engine::Analyzers::Ruby::Main::DEFAULT_MASS_THRESHOLD, |
| 91 | + summary: false, |
| 92 | + verbose: false, |
| 93 | + number: true, |
| 94 | + timeout: 10, |
| 95 | + liberal: false, |
| 96 | + fuzzy: false, |
| 97 | + only: nil, |
| 98 | + }) |
| 99 | + |
| 100 | + sexp = RubyParser.new.process(source, "file.rb") |
| 101 | + flay.process_sexp(sexp) |
| 102 | + report = flay.analyze[0] |
| 103 | + sexps = flay.hashes[report.structural_hash] |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | +end |
0 commit comments