Skip to content

Commit f3b5dc1

Browse files
authored
Add exit metric for Java (#822)
1 parent 7b9d158 commit f3b5dc1

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

src/metrics/exit.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,16 @@ impl Exit for CppCode {
172172
}
173173
}
174174

175+
impl Exit for JavaCode {
176+
fn compute(node: &Node, stats: &mut Stats) {
177+
if matches!(node.object().kind_id().into(), Java::ReturnStatement) {
178+
stats.exit += 1;
179+
}
180+
}
181+
}
182+
175183
impl Exit for PreprocCode {}
176184
impl Exit for CcommentCode {}
177-
impl Exit for JavaCode {}
178185

179186
#[cfg(test)]
180187
mod tests {
@@ -305,4 +312,63 @@ mod tests {
305312
[(exit_average, 0.5)] // 2 functions + 2 lambdas = 4
306313
);
307314
}
315+
316+
#[test]
317+
fn java_no_exit() {
318+
check_metrics!(
319+
"int a = 42;",
320+
"foo.java",
321+
JavaParser,
322+
nexits,
323+
[
324+
(exit_sum, 0, usize),
325+
(exit_min, 0, usize),
326+
(exit_max, 0, usize)
327+
],
328+
[(exit_average, f64::NAN)] // 0 functions
329+
);
330+
}
331+
332+
#[test]
333+
fn java_simple_function() {
334+
check_metrics!(
335+
"class A {
336+
public int sum(int x, int y) {
337+
return x + y;
338+
}
339+
}",
340+
"foo.java",
341+
JavaParser,
342+
nexits,
343+
[
344+
(exit_sum, 1, usize),
345+
(exit_min, 0, usize),
346+
(exit_max, 1, usize)
347+
],
348+
[(exit_average, 1.0)] // 1 exit / 1 space
349+
);
350+
}
351+
352+
#[test]
353+
fn java_split_function() {
354+
check_metrics!(
355+
"class A {
356+
public int multiply(int x, int y) {
357+
if(x == 0 || y == 0){
358+
return 0;
359+
}
360+
return x * y;
361+
}
362+
}",
363+
"foo.java",
364+
JavaParser,
365+
nexits,
366+
[
367+
(exit_sum, 2, usize),
368+
(exit_min, 0, usize),
369+
(exit_max, 2, usize)
370+
],
371+
[(exit_average, 2.0)] // 2 exit / space 1
372+
);
373+
}
308374
}

0 commit comments

Comments
 (0)