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

Implement BLANK metric #142

Merged
merged 4 commits into from
Apr 10, 2020
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 43 additions & 3 deletions src/loc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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(),
)
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -248,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!(
Expand Down
3 changes: 2 additions & 1 deletion src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions src/web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down