1
- =begin
2
1
require "spec_helper"
3
- require "cc/engine/analyzers/java /main"
2
+ require "cc/engine/analyzers/kotlin /main"
4
3
require "cc/engine/analyzers/engine_config"
5
4
6
5
module CC ::Engine ::Analyzers
7
- RSpec.describe Java ::Main, in_tmpdir: true do
6
+ RSpec . describe Kotlin ::Main , in_tmpdir : true do
8
7
include AnalyzerSpecHelpers
9
8
10
9
describe "#run" do
11
10
let ( :engine_conf ) { EngineConfig . new ( { } ) }
12
11
13
12
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)
18
17
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
23
20
}
24
21
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])
27
24
}
28
25
29
- System.out. println();
26
+ println("")
30
27
}
31
28
32
- public static void bar() {
33
- int[] anArray;
34
-
35
- anArray = new int[10];
29
+ fun bar() {
30
+ val anArray: Array<Int> = Array(10)
36
31
37
- for (int i = 0; i < anArray.length; i++ ) {
38
- anArray[i] = i;
32
+ for (i in 0..10 ) {
33
+ anArray[i] = i
39
34
}
40
35
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])
43
38
}
44
39
45
- System.out. println();
40
+ println("")
46
41
}
47
42
}
48
43
EOF
@@ -56,35 +51,32 @@ module CC::Engine::Analyzers
56
51
expect ( json [ "description" ] ) . to eq ( "Similar blocks of code found in 2 locations. Consider refactoring." )
57
52
expect ( json [ "categories" ] ) . to eq ( [ "Duplication" ] )
58
53
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 } ,
61
56
} )
62
- expect(json["remediation_points"]).to eq(930_000)
63
57
expect ( json [ "other_locations" ] ) . to eq ( [
64
- {"path" => "foo.java ", "lines" => { "begin" => 18 , "end" => 32 } },
58
+ { "path" => "foo.kt " , "lines" => { "begin" => 16 , "end" => 28 } } ,
65
59
] )
66
- expect(json["content"]["body"]).to match /This issue has a mass of 103/
67
- expect(json["fingerprint"]).to eq("48eb151dc29634f90a86ffabf9d3c4b5")
68
60
expect ( json [ "severity" ] ) . to eq ( CC ::Engine ::Analyzers ::Base ::MAJOR )
69
61
end
70
62
71
63
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] + " ")
77
69
}
78
70
79
- System.out. println();
71
+ println("")
80
72
}
81
73
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] + " ")
85
77
}
86
78
87
- System.out. println();
79
+ println("")
88
80
}
89
81
}
90
82
EOF
@@ -98,20 +90,17 @@ module CC::Engine::Analyzers
98
90
expect ( json [ "description" ] ) . to eq ( "Identical blocks of code found in 2 locations. Consider refactoring." )
99
91
expect ( json [ "categories" ] ) . to eq ( [ "Duplication" ] )
100
92
expect ( json [ "location" ] ) . to eq ( {
101
- "path" => "foo.java ",
93
+ "path" => "foo.kt " ,
102
94
"lines" => { "begin" => 2 , "end" => 8 } ,
103
95
} )
104
- expect(json["remediation_points"]).to eq(420_000)
105
96
expect ( json [ "other_locations" ] ) . to eq ( [
106
- {"path" => "foo.java ", "lines" => { "begin" => 10, "end" => 16 } },
97
+ { "path" => "foo.kt " , "lines" => { "begin" => 10 , "end" => 16 } } ,
107
98
] )
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 )
111
100
end
112
101
113
102
it "outputs a warning for unprocessable errors" do
114
- create_source_file("foo.java ", <<-EOF)
103
+ create_source_file ( "foo.kt " , <<-EOF )
115
104
---
116
105
EOF
117
106
@@ -121,71 +110,65 @@ module CC::Engine::Analyzers
121
110
end
122
111
123
112
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;
126
115
127
- import java.util.Comparator;
116
+ import java.util.Comparator;
128
117
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;
131
120
EOF
132
121
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;
135
124
136
- import java.util.Comparator;
125
+ import java.util.Comparator;
137
126
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;
140
129
EOF
141
130
142
131
issues = run_engine ( engine_conf ) . strip . split ( "\0 " )
143
132
expect ( issues ) . to be_empty
144
133
end
145
134
146
135
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(
152
140
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F
153
- };
141
+ )
154
142
155
- int[] anArray;
143
+ val anArray: Array<Int> = Array(10)
156
144
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
161
147
}
162
148
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] + " ")
165
151
}
166
152
167
- System.out. println();
153
+ println()
168
154
}
169
155
170
- public static void foo() {
171
- int[] scott;
172
- scott = new int[] {
156
+ fun foo() {
157
+ val scott = arrayOfInt(
173
158
0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7
174
- };
175
-
176
- int[] anArray;
159
+ )
177
160
178
- anArray = new int[10];
161
+ val anArray: Array<Int> = Array(10)
179
162
180
- for (int i = 0; i < anArray.length; i++ ) {
181
- anArray[i] = i;
163
+ for (i in 0..<10 ) {
164
+ anArray[i] = i
182
165
}
183
166
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] + " ")
186
169
}
187
170
188
- System.out. println();
171
+ println()
189
172
}
190
173
}
191
174
EOF
@@ -201,18 +184,15 @@ module CC::Engine::Analyzers
201
184
expect ( json [ "description" ] ) . to eq ( "Similar blocks of code found in 2 locations. Consider refactoring." )
202
185
expect ( json [ "categories" ] ) . to eq ( [ "Duplication" ] )
203
186
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 } ,
206
189
} )
207
- expect(json["remediation_points"]).to eq(1_230_000)
208
190
expect ( json [ "other_locations" ] ) . to eq ( [
209
- {"path" => "foo.java ", "lines" => { "begin" => 23 , "end" => 42 } },
191
+ { "path" => "foo.kt " , "lines" => { "begin" => 20 , "end" => 36 } } ,
210
192
] )
211
- expect(json["content"]["body"]).to match /This issue has a mass of 133/
212
- expect(json["fingerprint"]).to eq("9abf88bac3a56bf708a5c4ceaf251d98")
213
193
expect ( json [ "severity" ] ) . to eq ( CC ::Engine ::Analyzers ::Base ::MAJOR )
214
194
end
195
+
215
196
end
216
197
end
217
198
end
218
- =end
0 commit comments