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

Fix cognitive metric when there are no functions #387

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,12 @@ macro_rules! check_metrics {

$(
$(
assert!((func_space.metrics.$metric.$func_float() - $true_float_value).abs() < f64::EPSILON);
let value: f64 = $true_float_value;
if !value.is_nan() {
assert!((func_space.metrics.$metric.$func_float() - value).abs() < f64::EPSILON);
} else {
assert!(func_space.metrics.$metric.$func_float().is_nan());
}
)*
)?
}
Expand Down
50 changes: 50 additions & 0 deletions src/metrics/cognitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ impl Stats {
///
/// This value is computed dividing the `Cognitive Complexity` value
/// for the total number of functions/closures in a space.
///
/// If there are no functions in a code, its value is `NAN`.
pub fn cognitive_average(&self) -> f64 {
self.cognitive() / self.total_space_functions as f64
}
Expand Down Expand Up @@ -411,6 +413,54 @@ mod tests {

use super::*;

#[test]
fn python_no_cognitive() {
check_metrics!(
"a = 42",
"foo.py",
PythonParser,
cognitive,
[(cognitive, 0, usize)],
[(cognitive_average, f64::NAN)]
);
}

#[test]
fn rust_no_cognitive() {
check_metrics!(
"let a = 42;",
"foo.rs",
RustParser,
cognitive,
[(cognitive, 0, usize)],
[(cognitive_average, f64::NAN)]
);
}

#[test]
fn c_no_cognitive() {
check_metrics!(
"int a = 42;",
"foo.c",
CppParser,
cognitive,
[(cognitive, 0, usize)],
[(cognitive_average, f64::NAN)]
);
}

#[test]
fn mozjs_no_cognitive() {
check_metrics!(
"var a = 42;",
"foo.js",
MozjsParser,
cognitive,
[(cognitive, 0, usize)],
[(cognitive_average, f64::NAN)]
);
}

#[test]
fn python_simple_function() {
check_metrics!(
Expand Down