|
| 1 | +require "spec_helper" |
| 2 | +require "cc/engine/analyzers/java/main" |
| 3 | +require "cc/engine/analyzers/engine_config" |
| 4 | +require "cc/engine/analyzers/file_list" |
| 5 | + |
| 6 | +module CC::Engine::Analyzers |
| 7 | + RSpec.describe Java::Main, in_tmpdir: true do |
| 8 | + include AnalyzerSpecHelpers |
| 9 | + |
| 10 | + describe "#run" do |
| 11 | + let(:engine_conf) { EngineConfig.new({}) } |
| 12 | + |
| 13 | + it "prints an issue for similar code" do |
| 14 | + create_source_file("foo.java", <<-EOF) |
| 15 | + public class ArrayDemo { |
| 16 | + public static void foo() { |
| 17 | + int[] anArray; |
| 18 | + |
| 19 | + anArray = new int[10]; |
| 20 | + |
| 21 | + for (int i = 0; i < anArray.length; i++) { |
| 22 | + anArray[i] = i; |
| 23 | + } |
| 24 | + |
| 25 | + for (int i = 0; i < anArray.length; i++) { |
| 26 | + System.out.print(anArray[i] + " "); |
| 27 | + } |
| 28 | + |
| 29 | + System.out.println(); |
| 30 | + } |
| 31 | + |
| 32 | + public static void bar() { |
| 33 | + int[] anArray; |
| 34 | + |
| 35 | + anArray = new int[10]; |
| 36 | + |
| 37 | + for (int i = 0; i < anArray.length; i++) { |
| 38 | + anArray[i] = i; |
| 39 | + } |
| 40 | + |
| 41 | + for (int i = 0; i < anArray.length; i++) { |
| 42 | + System.out.print(anArray[i] + " "); |
| 43 | + } |
| 44 | + |
| 45 | + System.out.println(); |
| 46 | + } |
| 47 | + } |
| 48 | + EOF |
| 49 | + |
| 50 | + issues = run_engine(engine_conf).strip.split("0円") |
| 51 | + result = issues.first.strip |
| 52 | + json = JSON.parse(result) |
| 53 | + |
| 54 | + expect(json["type"]).to eq("issue") |
| 55 | + expect(json["check_name"]).to eq("Similar code") |
| 56 | + expect(json["description"]).to eq("Similar blocks of code found in 2 locations. Consider refactoring.") |
| 57 | + expect(json["categories"]).to eq(["Duplication"]) |
| 58 | + expect(json["location"]).to eq({ |
| 59 | + "path" => "foo.java", |
| 60 | + "lines" => { "begin" => 2, "end" => 16 }, |
| 61 | + }) |
| 62 | + expect(json["remediation_points"]).to eq(930_000) |
| 63 | + expect(json["other_locations"]).to eq([ |
| 64 | + {"path" => "foo.java", "lines" => { "begin" => 18, "end" => 32 } }, |
| 65 | + ]) |
| 66 | + expect(json["content"]["body"]).to match /This issue has a mass of 103/ |
| 67 | + expect(json["fingerprint"]).to eq("48eb151dc29634f90a86ffabf9d3c4b5") |
| 68 | + expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MAJOR) |
| 69 | + end |
| 70 | + |
| 71 | + it "prints an issue for identical code" do |
| 72 | + create_source_file("foo.java", <<-EOF) |
| 73 | + public class ArrayDemo { |
| 74 | + public static void foo(int[] anArray) { |
| 75 | + for (int i = 0; i < anArray.length; i++) { |
| 76 | + System.out.print(anArray[i] + " "); |
| 77 | + } |
| 78 | + |
| 79 | + System.out.println(); |
| 80 | + } |
| 81 | + |
| 82 | + public static void foo(int[] anArray) { |
| 83 | + for (int i = 0; i < anArray.length; i++) { |
| 84 | + System.out.print(anArray[i] + " "); |
| 85 | + } |
| 86 | + |
| 87 | + System.out.println(); |
| 88 | + } |
| 89 | + } |
| 90 | + EOF |
| 91 | + |
| 92 | + issues = run_engine(engine_conf).strip.split("0円") |
| 93 | + result = issues.first.strip |
| 94 | + json = JSON.parse(result) |
| 95 | + |
| 96 | + expect(json["type"]).to eq("issue") |
| 97 | + expect(json["check_name"]).to eq("Identical code") |
| 98 | + expect(json["description"]).to eq("Identical blocks of code found in 2 locations. Consider refactoring.") |
| 99 | + expect(json["categories"]).to eq(["Duplication"]) |
| 100 | + expect(json["location"]).to eq({ |
| 101 | + "path" => "foo.java", |
| 102 | + "lines" => { "begin" => 2, "end" => 8 }, |
| 103 | + }) |
| 104 | + expect(json["remediation_points"]).to eq(420_000) |
| 105 | + expect(json["other_locations"]).to eq([ |
| 106 | + {"path" => "foo.java", "lines" => { "begin" => 10, "end" => 16 } }, |
| 107 | + ]) |
| 108 | + expect(json["content"]["body"]).to match /This issue has a mass of 52/ |
| 109 | + expect(json["fingerprint"]).to eq("dbb957b34f7b5312538235c0aa3f52a0") |
| 110 | + expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MINOR) |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments