Skip to content

Commit

Permalink
Add exit metric for Java (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
dburriss authored Apr 15, 2022
1 parent 7b9d158 commit f3b5dc1
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion src/metrics/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,16 @@ impl Exit for CppCode {
}
}

impl Exit for JavaCode {
fn compute(node: &Node, stats: &mut Stats) {
if matches!(node.object().kind_id().into(), Java::ReturnStatement) {
stats.exit += 1;
}
}
}

impl Exit for PreprocCode {}
impl Exit for CcommentCode {}
impl Exit for JavaCode {}

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -305,4 +312,63 @@ mod tests {
[(exit_average, 0.5)] // 2 functions + 2 lambdas = 4
);
}

#[test]
fn java_no_exit() {
check_metrics!(
"int a = 42;",
"foo.java",
JavaParser,
nexits,
[
(exit_sum, 0, usize),
(exit_min, 0, usize),
(exit_max, 0, usize)
],
[(exit_average, f64::NAN)] // 0 functions
);
}

#[test]
fn java_simple_function() {
check_metrics!(
"class A {
public int sum(int x, int y) {
return x + y;
}
}",
"foo.java",
JavaParser,
nexits,
[
(exit_sum, 1, usize),
(exit_min, 0, usize),
(exit_max, 1, usize)
],
[(exit_average, 1.0)] // 1 exit / 1 space
);
}

#[test]
fn java_split_function() {
check_metrics!(
"class A {
public int multiply(int x, int y) {
if(x == 0 || y == 0){
return 0;
}
return x * y;
}
}",
"foo.java",
JavaParser,
nexits,
[
(exit_sum, 2, usize),
(exit_min, 0, usize),
(exit_max, 2, usize)
],
[(exit_average, 2.0)] // 2 exit / space 1
);
}
}

0 comments on commit f3b5dc1

Please sign in to comment.