Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cognitive complexity metric for Java #850

Merged
Merged
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 231 additions & 1 deletion src/metrics/cognitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,44 @@ impl Cognitive for TsxCode {
js_cognitive!(Tsx);
}

implement_metric_trait!(Cognitive, PreprocCode, CcommentCode, JavaCode, KotlinCode);
impl Cognitive for JavaCode {
fn compute(
node: &Node,
stats: &mut Stats,
nesting_map: &mut FxHashMap<usize, (usize, usize, usize)>,
) {
use Java::*;

let (mut nesting, depth, mut lambda) = get_nesting_from_map(node, nesting_map);

match node.kind_id().into() {
IfStatement => {
if !Self::is_else_if(node) {
increase_nesting(stats,&mut nesting, depth, lambda);
}
}
ForStatement | WhileStatement | DoStatement | SwitchBlock | CatchClause => {
increase_nesting(stats,&mut nesting, depth, lambda);
}
Else /* else-if also */ => {
increment_by_one(stats);
}
UnaryExpression => {
stats.boolean_seq.not_operator(node.kind_id());
}
BinaryExpression => {
compute_booleans::<language_java::Java>(node, stats, AMPAMP, PIPEPIPE);
}
LambdaExpression => {
lambda += 1;
}
_ => {}
}
nesting_map.insert(node.id(), (nesting, depth, lambda));
}
}

implement_metric_trait!(Cognitive, PreprocCode, CcommentCode, KotlinCode);

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -1696,4 +1733,197 @@ mod tests {
},
);
}

#[test]
fn java_no_cognitive() {
check_metrics::<JavaParser>("int a = 42;", "foo.java", |metric| {
insta::assert_json_snapshot!(
metric.cognitive,
@r###"
{
"sum": 0.0,
"average": null,
"min": 0.0,
"max": 0.0
}
"###
);
});
}

#[test]
fn java_single_branch_function() {
check_metrics::<JavaParser>(
"class X {
public static void print(boolean a){
if(a){ // +1
System.out.println(\"test1\");
}
}
}",
"foo.java",
|metric| {
insta::assert_json_snapshot!(
metric.cognitive,
@r###"
{
"sum": 1.0,
"average": 1.0,
"min": 0.0,
"max": 1.0
}
"###
);
},
);
}

#[test]
fn java_multiple_branch_function() {
check_metrics::<JavaParser>(
"class X {
public static void print(boolean a, boolean b){
if(a){ // +1
System.out.println(\"test1\");
}
if(b){ // +1
System.out.println(\"test2\");
}
else { // +1
System.out.println(\"test3\");
}
}
}",
"foo.java",
|metric| {
insta::assert_json_snapshot!(
metric.cognitive,
@r###"
{
"sum": 3.0,
"average": 3.0,
"min": 0.0,
"max": 3.0
}
"###
);
},
);
}

#[test]
fn java_compound_conditions() {
check_metrics::<JavaParser>(
"class X {
public static void print(boolean a, boolean b, boolean c, boolean d){
if(a && b){ // +2
System.out.println(\"test1\");
}
if(c && d){ // +2
System.out.println(\"test2\");
}
}
}",
"foo.java",
|metric| {
insta::assert_json_snapshot!(
metric.cognitive,
@r###"
{
"sum": 4.0,
"average": 4.0,
"min": 0.0,
"max": 4.0
}"###
);
},
);
}

#[test]
fn java_switch_statement() {
check_metrics::<JavaParser>(
"class X {
public static void print(boolean a, boolean b, boolean c, boolean d){
switch(expr){ //+1
case 1:
System.out.println(\"test1\");
break;
case 2:
System.out.println(\"test2\");
break;
default:
System.out.println(\"test\");
}
}
}",
"foo.java",
|metric| {
insta::assert_json_snapshot!(
metric.cognitive,
@r###"
{
"sum": 1.0,
"average": 1.0,
"min": 0.0,
"max": 1.0
}"###
);
},
);
}

#[test]
fn java_switch_expression() {
check_metrics::<JavaParser>(
"class X {
public static void print(boolean a, boolean b, boolean c, boolean d){
switch(expr){ // +1
case 1 -> System.out.println(\"test1\");
case 2 -> System.out.println(\"test2\");
default -> System.out.println(\"test\");
}
}
}",
"foo.java",
|metric| {
insta::assert_json_snapshot!(
metric.cognitive,
@r###"
{
"sum": 1.0,
"average": 1.0,
"min": 0.0,
"max": 1.0
}"###
);
},
);
}

#[test]
fn java_not_booleans() {
check_metrics::<JavaParser>(
"class X {
public static void print(boolean a, boolean b, boolean c, boolean d){
if (a && !(b && c)) { // +3 (+1 &&, +1 &&)
Luni-4 marked this conversation as resolved.
Show resolved Hide resolved
printf(\"test\");
}
}
}",
"foo.java",
|metric| {
insta::assert_json_snapshot!(
metric.cognitive,
@r###"
{
"sum": 3.0,
"average": 3.0,
"min": 0.0,
"max": 3.0
}"###
);
},
);
}
}