|
| 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=>3, :end=>9}}) |
| 42 | + expect(first_formatted[:other_locations]).to eq([ |
| 43 | + { :path => "file.rb", :lines => { :begin => 5, :end => 9} }, |
| 44 | + { :path => "file.rb", :lines => { :begin => 7, :end => 9} }, |
| 45 | + ]) |
| 46 | + expect(first_formatted[:fingerprint]).to eq("c2712b56bff2becf4ae2a8469e1171c7") |
| 47 | + |
| 48 | + expect(second_formatted[:location]).to eq({:path=>"file.rb", :lines=>{:begin=>5, :end=>9}}) |
| 49 | + expect(second_formatted[:other_locations]).to eq([ |
| 50 | + { :path => "file.rb", :lines => { :begin => 3, :end => 9} }, |
| 51 | + { :path => "file.rb", :lines => { :begin => 7, :end => 9} }, |
| 52 | + ]) |
| 53 | + |
| 54 | + expect(third_formatted[:location]).to eq({:path=>"file.rb", :lines=>{:begin=>7, :end=>9}}) |
| 55 | + expect(third_formatted[:other_locations]).to eq([ |
| 56 | + { :path => "file.rb", :lines => { :begin => 3, :end => 9} }, |
| 57 | + { :path => "file.rb", :lines => { :begin => 5, :end => 9} }, |
| 58 | + ]) |
| 59 | + end |
| 60 | + |
| 61 | + def sexps |
| 62 | + source = <<-SOURCE |
| 63 | + begin |
| 64 | + foo |
| 65 | + rescue Exception => e |
| 66 | + Jekyll.logger.warn "Error reading file \#{File.join(base, name)}: \#{e.message}" |
| 67 | + rescue Exception => e |
| 68 | + Jekyll.logger.warn "Error reading file \#{File.join(base, name)}: \#{e.message}" |
| 69 | + rescue Exception => e |
| 70 | + Jekyll.logger.warn "Error reading file \#{File.join(base, name)}: \#{e.message}" |
| 71 | + end |
| 72 | + SOURCE |
| 73 | + |
| 74 | + flay = Flay.new({ |
| 75 | + diff: false, |
| 76 | + mass: CC::Engine::Analyzers::Ruby::Main::DEFAULT_MASS_THRESHOLD, |
| 77 | + summary: false, |
| 78 | + verbose: false, |
| 79 | + number: true, |
| 80 | + timeout: 10, |
| 81 | + liberal: false, |
| 82 | + fuzzy: false, |
| 83 | + only: nil, |
| 84 | + }) |
| 85 | + |
| 86 | + sexp = RubyParser.new.process(source, "file.rb") |
| 87 | + flay.process_sexp(sexp) |
| 88 | + report = flay.analyze[0] |
| 89 | + sexps = flay.hashes[report.structural_hash] |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments