Skip to content

Commit 946bec0

Browse files
committed
Set cognitive average value to NAN when no functions
1 parent d5d92d5 commit 946bec0

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

‎src/metrics/cognitive.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ impl Stats {
7272
///
7373
/// This value is computed dividing the `Cognitive Complexity` value
7474
/// for the total number of functions/closures in a space.
75+
///
76+
/// If there are no functions in a code, its value is `NAN`.
7577
pub fn cognitive_average(&self) -> f64 {
7678
self.cognitive() / self.total_space_functions as f64
7779
}
@@ -411,6 +413,54 @@ mod tests {
411413

412414
use super::*;
413415

416+
#[test]
417+
fn python_no_cognitive() {
418+
check_metrics!(
419+
"a = 42",
420+
"foo.py",
421+
PythonParser,
422+
cognitive,
423+
[(cognitive, 0, usize)],
424+
[(cognitive_average, f64::NAN)]
425+
);
426+
}
427+
428+
#[test]
429+
fn rust_no_cognitive() {
430+
check_metrics!(
431+
"let a = 42;",
432+
"foo.rs",
433+
RustParser,
434+
cognitive,
435+
[(cognitive, 0, usize)],
436+
[(cognitive_average, f64::NAN)]
437+
);
438+
}
439+
440+
#[test]
441+
fn c_no_cognitive() {
442+
check_metrics!(
443+
"int a = 42;",
444+
"foo.c",
445+
CppParser,
446+
cognitive,
447+
[(cognitive, 0, usize)],
448+
[(cognitive_average, f64::NAN)]
449+
);
450+
}
451+
452+
#[test]
453+
fn mozjs_no_cognitive() {
454+
check_metrics!(
455+
"var a = 42;",
456+
"foo.js",
457+
MozjsParser,
458+
cognitive,
459+
[(cognitive, 0, usize)],
460+
[(cognitive_average, f64::NAN)]
461+
);
462+
}
463+
414464
#[test]
415465
fn python_simple_function() {
416466
check_metrics!(

0 commit comments

Comments
 (0)