From 17e2f80b9f9c269e7530fa2a3f77caa3703023aa Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Mon, 6 Apr 2020 13:14:19 +0200 Subject: [PATCH 1/4] Implement Blank metric --- src/loc.rs | 13 ++++++++++--- src/metrics.rs | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/loc.rs b/src/loc.rs index 356e1668f..82270de0d 100644 --- a/src/loc.rs +++ b/src/loc.rs @@ -21,10 +21,11 @@ impl Serialize for Stats { where S: Serializer, { - let mut st = serializer.serialize_struct("loc", 3)?; + let mut st = serializer.serialize_struct("loc", 4)?; st.serialize_field("sloc", &self.sloc())?; st.serialize_field("lloc", &self.lloc())?; st.serialize_field("cloc", &self.cloc())?; + st.serialize_field("blank", &self.blank())?; st.end() } } @@ -33,10 +34,11 @@ impl fmt::Display for Stats { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, - "sloc: {}, lloc: {}, cloc: {}", + "sloc: {}, lloc: {}, cloc: {}, blank: {}", self.sloc(), self.lloc(), - self.cloc() + self.cloc(), + self.blank(), ) } } @@ -72,6 +74,11 @@ impl Stats { // https://en.wikipedia.org/wiki/Source_lines_of_code self.comment_lines as f64 } + + #[inline(always)] + pub fn blank(&self) -> f64 { + self.sloc() - self.lloc() - self.cloc() + } } pub trait Loc diff --git a/src/metrics.rs b/src/metrics.rs index f8cc2ad3a..33dce22e0 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -274,7 +274,8 @@ impl<'a> FuncSpace<'a> { let prefix = format!("{}{}", prefix, pref_child); Self::dump_value("sloc", stats.sloc(), &prefix, false, stdout)?; Self::dump_value("lloc", stats.lloc(), &prefix, false, stdout)?; - Self::dump_value("cloc", stats.cloc(), &prefix, true, stdout) + Self::dump_value("cloc", stats.cloc(), &prefix, false, stdout)?; + Self::dump_value("blank", stats.blank(), &prefix, true, stdout) } fn dump_nom( From af7189f2d60cad7aaa21d8085a92d52595926e5d Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Mon, 6 Apr 2020 13:20:56 +0200 Subject: [PATCH 2/4] Update tests --- src/web/server.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/web/server.rs b/src/web/server.rs index ccd3d1e16..c92b3978a 100644 --- a/src/web/server.rs +++ b/src/web/server.rs @@ -650,7 +650,7 @@ mod tests { "n2": 1.0, "n1": 2.0, "volume": 4.754_887_502_163_468}, - "loc": {"cloc": 1.0, "lloc": 2.0, "sloc": 4.0}, + "loc": {"cloc": 1.0, "lloc": 2.0, "sloc": 4.0, "blank": 1.0}, "nom": {"functions": 1.0, "closures": 0.0, "total": 1.0}, "mi": {"mi_original": 140.204_331_558_152_12, "mi_sei": 161.644_455_240_662_24, @@ -676,7 +676,7 @@ mod tests { "n2": 1.0, "n1": 2.0, "volume": 4.754_887_502_163_468}, - "loc": {"cloc": 0.0, "lloc": 2.0, "sloc": 2.0}, + "loc": {"cloc": 0.0, "lloc": 2.0, "sloc": 2.0, "blank": 0.0}, "nom": {"functions": 1.0, "closures": 0.0, "total": 1.0}, "mi": {"mi_original": 151.433_315_883_223_23, "mi_sei": 142.873_061_717_489_78, @@ -728,7 +728,7 @@ mod tests { "n2": 1.0, "n1": 2.0, "volume": 4.754_887_502_163_468}, - "loc": {"cloc": 0.0, "lloc": 2.0, "sloc": 2.0}, + "loc": {"cloc": 0.0, "lloc": 2.0, "sloc": 2.0, "blank": 0.0}, "nom": {"functions": 1.0, "closures": 0.0, "total": 1.0}, "mi": {"mi_original": 151.433_315_883_223_23, "mi_sei": 142.873_061_717_489_78, @@ -776,7 +776,7 @@ mod tests { "n2": 1.0, "n1": 2.0, "volume": 4.754_887_502_163_468}, - "loc": {"cloc": 0.0, "lloc": 2.0, "sloc": 2.0}, + "loc": {"cloc": 0.0, "lloc": 2.0, "sloc": 2.0, "blank": 0.0}, "nom": {"functions": 1.0, "closures": 0.0, "total": 1.0}, "mi": {"mi_original": 151.433_315_883_223_23, "mi_sei": 142.873_061_717_489_78, @@ -802,7 +802,7 @@ mod tests { "n2": 1.0, "n1": 2.0, "volume": 4.754_887_502_163_468}, - "loc": {"cloc": 0.0, "lloc": 2.0, "sloc": 2.0}, + "loc": {"cloc": 0.0, "lloc": 2.0, "sloc": 2.0, "blank": 0.0}, "nom": {"functions": 1.0, "closures": 0.0, "total": 1.0}, "mi": {"mi_original": 151.433_315_883_223_23, "mi_sei": 142.873_061_717_489_78, From 63e94557d375c9fd0727f8e6a138294245a37332 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Mon, 6 Apr 2020 13:22:08 +0200 Subject: [PATCH 3/4] Add Blank to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 752df2495..60a4dc4e9 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ control flow of a program. - SLOC: it counts the number of lines in a source file. - LLOC: it counts the number of logical lines (instructions) contained in a source file. - CLOC: it counts the number of comments in a source file. +- BLANK: it counts the number of blank lines in a source file. - HALSTEAD: it is a suite that provides a series of information, such as the effort required to maintain the analyzed code, the size in bits to store the program, the difficulty to understand the code, an estimate of the number of bugs present in the codebase, and an estimate of the time needed to implement the software. - MI: it is a suite that allows to evaluate the maintainability of a software. - NOM: it counts the number of functions and closures in a file/trait/class. From 3bade9e646786d7aacfcaf1e9abdb83bb692b739 Mon Sep 17 00:00:00 2001 From: Luni-4 Date: Fri, 10 Apr 2020 10:58:11 +0200 Subject: [PATCH 4/4] Add unit tests for Blank metric --- src/loc.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/loc.rs b/src/loc.rs index 82270de0d..462cb2166 100644 --- a/src/loc.rs +++ b/src/loc.rs @@ -255,6 +255,39 @@ mod tests { use super::*; + #[test] + fn test_blank_python() { + check_metrics!( + "\na = 42\n\n", + "foo.py", + PythonParser, + loc, + [(blank, 1, usize)] + ); + } + + #[test] + fn test_blank_rust() { + check_metrics!( + "\nlet a = 42;\n\n", + "foo.rs", + RustParser, + loc, + [(blank, 1, usize)] + ); + } + + #[test] + fn test_blank_c() { + check_metrics!( + "\nint a = 42;\n\n", + "foo.c", + CppParser, + loc, + [(blank, 1, usize)] + ); + } + #[test] fn test_cloc_python() { check_metrics!(