Skip to content

Commit ea3932b

Browse files
dburrissAlex
authored andcommitted
Ops check for Halstead (mozilla#849)
1 parent 2d73fa7 commit ea3932b

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

src/getter.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ impl Getter for CppCode {
494494

495495
impl Getter for PreprocCode {}
496496
impl Getter for CcommentCode {}
497+
497498
impl Getter for JavaCode {
498499
fn get_space_kind(node: &Node) -> SpaceKind {
499500
use Java::*;
@@ -514,26 +515,27 @@ impl Getter for JavaCode {
514515
// https://www.geeksforgeeks.org/software-engineering-halsteads-software-metrics/?msclkid=5e181114abef11ecbb03527e95a34828
515516
match node.kind_id().into() {
516517
// Operator: function calls
517-
MethodInvocation
518+
//MethodInvocation
518519
// Operator: control flow
519520
| If | Else | Switch | Case | Try | Catch | Throw | Throws | Throws2 | For | While | Continue | Break | Do | Finally
520521
// Operator: keywords
521522
| New | Return | Default | Abstract | Assert | Instanceof | Extends | Final | Implements | Transient | Synchronized | Super | This | VoidType
522523
// Operator: brackets and comma and terminators (separators)
523-
| SEMI | COMMA | COLONCOLON | LBRACE | LBRACK | LPAREN | RBRACE | RBRACK | RPAREN | DOTDOTDOT | DOT
524+
| SEMI | COMMA | COLONCOLON | LBRACE | LBRACK | LPAREN // | RBRACE | RBRACK | RPAREN | DOTDOTDOT | DOT
524525
// Operator: operators
525526
| EQ | LT | GT | BANG | TILDE | QMARK | COLON // no grammar for lambda operator ->
526527
| EQEQ | LTEQ | GTEQ | BANGEQ | AMPAMP | PIPEPIPE | PLUSPLUS | DASHDASH
527528
| PLUS | DASH | STAR | SLASH | AMP | PIPE | CARET | PERCENT| LTLT | GTGT | GTGTGT
528529
| PLUSEQ | DASHEQ | STAREQ | SLASHEQ | AMPEQ | PIPEEQ | CARETEQ | PERCENTEQ | LTLTEQ | GTGTEQ | GTGTGTEQ
529530
// type identifier
530-
| TypeIdentifier | IntegralType | FloatingPointType | BooleanType
531+
//| TypeIdentifier | BooleanType| IntegralType | FloatingPointType
532+
// primitive types
533+
| Int | Float
531534
=> {
532535
HalsteadType::Operator
533536
},
534537
// Operands: variables, constants, literals
535-
Identifier | NullLiteral | ClassLiteral | StringLiteral | CharacterLiteral | HexIntegerLiteral | OctalIntegerLiteral
536-
| BinaryIntegerLiteral | DecimalIntegerLiteral | HexFloatingPointLiteral | DecimalFloatingPointLiteral => {
538+
Identifier | NullLiteral | ClassLiteral | StringLiteral | CharacterLiteral | HexIntegerLiteral | OctalIntegerLiteral | BinaryIntegerLiteral | DecimalIntegerLiteral | HexFloatingPointLiteral | DecimalFloatingPointLiteral => {
537539
HalsteadType::Operand
538540
},
539541
_ => {
@@ -542,7 +544,17 @@ impl Getter for JavaCode {
542544
}
543545
}
544546

545-
get_operator!(Java);
547+
fn get_operator_id_as_str(id: u16) -> &'static str {
548+
let typ = id.into();
549+
match typ {
550+
Java::LPAREN => "()",
551+
Java::LBRACK => "[]",
552+
Java::LBRACE => "{}",
553+
Java::VoidType => "void",
554+
_ => typ.into(),
555+
}
556+
}
557+
//get_operator!(Java);
546558
}
547559

548560
impl Getter for KotlinCode {}

src/metrics/halstead.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ mod tests {
665665
fn java_operators_and_operands() {
666666
check_metrics::<JavaParser>(
667667
"public class Main {
668-
public static void main(String args[]) {
668+
public static void main(string args[]) {
669669
int a, b, c, avg;
670670
a = 5; b = 5; c = 5;
671671
avg = (a + b + c) / 3;
@@ -680,20 +680,20 @@ mod tests {
680680
metric.halstead,
681681
@r###"
682682
{
683-
"n1": 16.0,
684-
"N1": 34.0,
683+
"n1": 10.0,
684+
"N1": 25.0,
685685
"n2": 12.0,
686686
"N2": 22.0,
687-
"length": 56.0,
688-
"estimated_program_length": 107.01955000865388,
689-
"purity_ratio": 1.9110633930116765,
690-
"vocabulary": 28.0,
691-
"volume": 269.2118756352258,
692-
"difficulty": 14.666666666666666,
693-
"level": 0.06818181818181819,
694-
"effort": 3948.440842649978,
695-
"time": 219.35782459166546,
696-
"bugs": 0.08327139413010551
687+
"length": 47.0,
688+
"estimated_program_length": 76.2388309575275,
689+
"purity_ratio": 1.6221027863303723,
690+
"vocabulary": 22.0,
691+
"volume": 209.59328607595296,
692+
"difficulty": 9.166666666666666,
693+
"level": 0.1090909090909091,
694+
"effort": 1921.2717890295687,
695+
"time": 106.73732161275382,
696+
"bugs": 0.05151550353617788
697697
}"###
698698
);
699699
},

src/ops.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,4 +648,35 @@ mod tests {
648648
],
649649
);
650650
}
651+
652+
#[test]
653+
fn java_ops() {
654+
check_ops(
655+
LANG::Java,
656+
"public class Main {
657+
public static void main(string args[]) {
658+
int a, b, c, avg;
659+
a = 5; b = 5; c = 5;
660+
avg = (a + b + c) / 3;
661+
MessageFormat.format(\"{0}\", avg);
662+
}
663+
}",
664+
"foo.java",
665+
&mut ["{}", "void", "()", "[]", ",", ";", "int", "=", "+", "/"],
666+
&mut [
667+
"Main",
668+
"main",
669+
"args",
670+
"a",
671+
"b",
672+
"c",
673+
"avg",
674+
"5",
675+
"3",
676+
"MessageFormat",
677+
"format",
678+
"\"{0}\"",
679+
],
680+
);
681+
}
651682
}

0 commit comments

Comments
 (0)