Skip to content

Commit a62c822

Browse files
committed
[wip] Added kotlin duplication checks (2)
1 parent 79dc669 commit a62c822

File tree

1 file changed

+69
-89
lines changed

1 file changed

+69
-89
lines changed
Lines changed: 69 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,43 @@
1-
=begin
21
require "spec_helper"
3-
require "cc/engine/analyzers/java/main"
2+
require "cc/engine/analyzers/kotlin/main"
43
require "cc/engine/analyzers/engine_config"
54

65
module CC::Engine::Analyzers
7-
RSpec.describe Java::Main, in_tmpdir: true do
6+
RSpec.describe Kotlin::Main, in_tmpdir: true do
87
include AnalyzerSpecHelpers
98

109
describe "#run" do
1110
let(:engine_conf) { EngineConfig.new({}) }
1211

1312
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;
13+
create_source_file("foo.kt", <<-EOF)
14+
class ArrayDemo {
15+
fun foo() {
16+
val anArray: Array<Int> = Array(10)
1817
19-
anArray = new int[10];
20-
21-
for (int i = 0; i < anArray.length; i++) {
22-
anArray[i] = i;
18+
for (i in 0..10) {
19+
anArray[i] = i
2320
}
2421
25-
for (int i = 0; i < anArray.length; i++) {
26-
System.out.print(anArray[i] + " ");
22+
for (i in 0..10) {
23+
println(anArray[i])
2724
}
2825
29-
System.out.println();
26+
println("")
3027
}
3128
32-
public static void bar() {
33-
int[] anArray;
34-
35-
anArray = new int[10];
29+
fun bar() {
30+
val anArray: Array<Int> = Array(10)
3631
37-
for (int i = 0; i < anArray.length; i++) {
38-
anArray[i] = i;
32+
for (i in 0..10) {
33+
anArray[i] = i
3934
}
4035
41-
for (int i = 0; i < anArray.length; i++) {
42-
System.out.print(anArray[i] + " ");
36+
for (i in 0..10) {
37+
println(anArray[i])
4338
}
4439
45-
System.out.println();
40+
println("")
4641
}
4742
}
4843
EOF
@@ -56,35 +51,32 @@ module CC::Engine::Analyzers
5651
expect(json["description"]).to eq("Similar blocks of code found in 2 locations. Consider refactoring.")
5752
expect(json["categories"]).to eq(["Duplication"])
5853
expect(json["location"]).to eq({
59-
"path" => "foo.java",
60-
"lines" => { "begin" => 2, "end" => 16 },
54+
"path" => "foo.kt",
55+
"lines" => { "begin" => 2, "end" => 14 },
6156
})
62-
expect(json["remediation_points"]).to eq(930_000)
6357
expect(json["other_locations"]).to eq([
64-
{"path" => "foo.java", "lines" => { "begin" => 18, "end" => 32 } },
58+
{"path" => "foo.kt", "lines" => { "begin" => 16, "end" => 28 } },
6559
])
66-
expect(json["content"]["body"]).to match /This issue has a mass of 103/
67-
expect(json["fingerprint"]).to eq("48eb151dc29634f90a86ffabf9d3c4b5")
6860
expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MAJOR)
6961
end
7062

7163
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] + " ");
64+
create_source_file("foo.kt", <<-EOF)
65+
class ArrayDemo {
66+
fun foo(anArray: Array<Int>) {
67+
for (i in anArray.indices) {
68+
println(anArray[i] + " ")
7769
}
7870
79-
System.out.println();
71+
println("")
8072
}
8173
82-
public static void foo(int[] anArray) {
83-
for (int i = 0; i < anArray.length; i++) {
84-
System.out.print(anArray[i] + " ");
74+
fun foo(anArray: Array<Int>) {
75+
for (i in anArray.indices) {
76+
println(anArray[i] + " ")
8577
}
8678
87-
System.out.println();
79+
println("")
8880
}
8981
}
9082
EOF
@@ -98,20 +90,17 @@ module CC::Engine::Analyzers
9890
expect(json["description"]).to eq("Identical blocks of code found in 2 locations. Consider refactoring.")
9991
expect(json["categories"]).to eq(["Duplication"])
10092
expect(json["location"]).to eq({
101-
"path" => "foo.java",
93+
"path" => "foo.kt",
10294
"lines" => { "begin" => 2, "end" => 8 },
10395
})
104-
expect(json["remediation_points"]).to eq(420_000)
10596
expect(json["other_locations"]).to eq([
106-
{"path" => "foo.java", "lines" => { "begin" => 10, "end" => 16 } },
97+
{"path" => "foo.kt", "lines" => { "begin" => 10, "end" => 16 } },
10798
])
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)
99+
expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MAJOR)
111100
end
112101

113102
it "outputs a warning for unprocessable errors" do
114-
create_source_file("foo.java", <<-EOF)
103+
create_source_file("foo.kt", <<-EOF)
115104
---
116105
EOF
117106

@@ -121,71 +110,65 @@ module CC::Engine::Analyzers
121110
end
122111

123112
it "ignores import and package declarations" do
124-
create_source_file("foo.java", <<-EOF)
125-
package org.springframework.rules.constraint;
113+
create_source_file("foo.kt", <<-EOF)
114+
package org.springframework.rules.constraint;
126115
127-
import java.util.Comparator;
116+
import java.util.Comparator;
128117
129-
import org.springframework.rules.constraint.Constraint;
130-
import org.springframework.rules.closure.BinaryConstraint;
118+
import org.springframework.rules.constraint.Constraint;
119+
import org.springframework.rules.closure.BinaryConstraint;
131120
EOF
132121

133-
create_source_file("bar.java", <<-EOF)
134-
package org.springframework.rules.constraint;
122+
create_source_file("bar.kt", <<-EOF)
123+
package org.springframework.rules.constraint;
135124
136-
import java.util.Comparator;
125+
import java.util.Comparator;
137126
138-
import org.springframework.rules.constraint.Constraint;
139-
import org.springframework.rules.closure.BinaryConstraint;
127+
import org.springframework.rules.constraint.Constraint;
128+
import org.springframework.rules.closure.BinaryConstraint;
140129
EOF
141130

142131
issues = run_engine(engine_conf).strip.split("\0")
143132
expect(issues).to be_empty
144133
end
145134

146135
it "prints an issue for similar code when the only difference is the value of a literal" do
147-
create_source_file("foo.java", <<-EOF)
148-
public class ArrayDemo {
149-
public static void foo() {
150-
int[] scott;
151-
scott = new int[] {
136+
create_source_file("foo.kt", <<-EOF)
137+
class ArrayDemo {
138+
fun foo() {
139+
val scott = arrayOfInt(
152140
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F
153-
};
141+
)
154142
155-
int[] anArray;
143+
val anArray: Array<Int> = Array(10)
156144
157-
anArray = new int[10];
158-
159-
for (int i = 0; i < anArray.length; i++) {
160-
anArray[i] = i;
145+
for (i in 0..<10) {
146+
anArray[i] = i
161147
}
162148
163-
for (int i = 0; i < anArray.length; i++) {
164-
System.out.print(anArray[i] + " ");
149+
for (i in 0..<10) {
150+
println(anArray[i] + " ")
165151
}
166152
167-
System.out.println();
153+
println()
168154
}
169155
170-
public static void foo() {
171-
int[] scott;
172-
scott = new int[] {
156+
fun foo() {
157+
val scott = arrayOfInt(
173158
0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7
174-
};
175-
176-
int[] anArray;
159+
)
177160
178-
anArray = new int[10];
161+
val anArray: Array<Int> = Array(10)
179162
180-
for (int i = 0; i < anArray.length; i++) {
181-
anArray[i] = i;
163+
for (i in 0..<10) {
164+
anArray[i] = i
182165
}
183166
184-
for (int i = 0; i < anArray.length; i++) {
185-
System.out.print(anArray[i] + " ");
167+
for (i in 0..<10) {
168+
println(anArray[i] + " ")
186169
}
187170
188-
System.out.println();
171+
println()
189172
}
190173
}
191174
EOF
@@ -201,18 +184,15 @@ module CC::Engine::Analyzers
201184
expect(json["description"]).to eq("Similar blocks of code found in 2 locations. Consider refactoring.")
202185
expect(json["categories"]).to eq(["Duplication"])
203186
expect(json["location"]).to eq({
204-
"path" => "foo.java",
205-
"lines" => { "begin" => 2, "end" => 21 },
187+
"path" => "foo.kt",
188+
"lines" => { "begin" => 2, "end" => 18 },
206189
})
207-
expect(json["remediation_points"]).to eq(1_230_000)
208190
expect(json["other_locations"]).to eq([
209-
{"path" => "foo.java", "lines" => { "begin" => 23, "end" => 42 } },
191+
{"path" => "foo.kt", "lines" => { "begin" => 20, "end" => 36 } },
210192
])
211-
expect(json["content"]["body"]).to match /This issue has a mass of 133/
212-
expect(json["fingerprint"]).to eq("9abf88bac3a56bf708a5c4ceaf251d98")
213193
expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MAJOR)
214194
end
195+
215196
end
216197
end
217198
end
218-
=end

0 commit comments

Comments
 (0)