diff --git a/src/macros.rs b/src/macros.rs index 7c154900d..132d96931 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -277,7 +277,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()); + } )* )? } diff --git a/src/metrics/cognitive.rs b/src/metrics/cognitive.rs index adee9a135..13ff2f181 100644 --- a/src/metrics/cognitive.rs +++ b/src/metrics/cognitive.rs @@ -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 } @@ -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!(