Skip to content

Commit

Permalink
Fix wrong Halstead computation for Python
Browse files Browse the repository at this point in the history
We were counting wrong operands for Python Halstead's implementation
  • Loading branch information
Luni-4 committed Sep 24, 2020
1 parent f8ccc51 commit ac65d07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ impl Getter for PythonCode {

let id = node.object().kind_id();
match id.into() {
Import | DOT | From | LPAREN | COMMA | As | STAR | GTGT | Assert | COLONEQ | Return
Import | DOT | From | COMMA | As | STAR | GTGT | Assert | COLONEQ | Return | Def
| Del | Raise | Pass | Break | Continue | If | Elif | Else | Async | For | In
| While | Try | Except | Finally | With | DASHGT | EQ | Global | Exec | AT | Not
| And | Or | PLUS | DASH | SLASH | PERCENT | SLASHSLASH | STARSTAR | PIPE | AMP
| CARET | LTLT | TILDE | LT | LTEQ | EQEQ | BANGEQ | GTEQ | GT | LTGT | Is | PLUSEQ
| DASHEQ | STAREQ | SLASHEQ | ATEQ | SLASHSLASHEQ | PERCENTEQ | STARSTAREQ | GTGTEQ
| LTLTEQ | AMPEQ | CARETEQ | PIPEEQ | Yield | LBRACK | LBRACE | Await | Await2
| Print => HalsteadType::Operator,
| LTLTEQ | AMPEQ | CARETEQ | PIPEEQ | Yield | Await | Await2 | Print => {
HalsteadType::Operator
}
Identifier | Integer | Float | True | False | None => HalsteadType::Operand,
String => {
let mut operator = HalsteadType::Unknown;
Expand Down
11 changes: 11 additions & 0 deletions src/metrics/halstead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,17 @@ mod tests {
);
}

#[test]
fn test_wrong_halstead_operands() {
check_metrics!(
"()[]{}",
"foo.py",
PythonParser,
halstead,
[(u_operators, 0, usize), (operators, 0, usize)]
);
}

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

0 comments on commit ac65d07

Please sign in to comment.