From 13a21e8492247e553f36fb57601c295c8ee6bc42 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Wed, 13 Apr 2022 08:01:02 +0200 Subject: [PATCH 1/3] Closure to checker for nom --- src/checker.rs | 2 +- src/metrics/nom.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/checker.rs b/src/checker.rs index ba99cbb04..8ce71dfa1 100644 --- a/src/checker.rs +++ b/src/checker.rs @@ -208,7 +208,7 @@ impl Checker for JavaCode { mk_checker!(is_string, StringLiteral); mk_checker!(is_call, MethodInvocation); mk_checker!(is_func, MethodDeclaration); - mk_checker!(is_closure,); + mk_checker!(is_closure, LambdaExpression); mk_checker!( is_func_space, Program, diff --git a/src/metrics/nom.rs b/src/metrics/nom.rs index d84281c8c..557f76093 100644 --- a/src/metrics/nom.rs +++ b/src/metrics/nom.rs @@ -621,4 +621,45 @@ mod tests { ] ); } + + #[test] + fn java_nom() { + check_metrics!( + "interface printable{ + void print(); + } + + interface IntFunc { + int func(int n); + } + + class Printer implements printable{ + public void print(){System.out.println(\"Hello\");} + + public static void main(String args[]){ + Printer obj = new Printer(); + obj.print(); + IntFunc meaning = (i) -> i + 42; + int i = meaning.func(1); + } + }", + "foo.java", + JavaParser, + nom, + [ + (functions_sum, 4, usize), + (closures_sum, 1, usize), + (total, 5, usize), + (functions_max, 1, usize), + (functions_min, 0, usize), + (closures_max, 1, usize), + (closures_min, 0, usize), + ], + [ + (functions_average, 0.5), // number of spaces = 8 + (closures_average, 0.125), + (average, 0.625) + ] + ); + } } From b8ab86c9941584c3135820974317bb1cece85596 Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Thu, 14 Apr 2022 18:31:28 +0200 Subject: [PATCH 2/3] Adds a simpler test --- src/metrics/nom.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/metrics/nom.rs b/src/metrics/nom.rs index 557f76093..ae60c51aa 100644 --- a/src/metrics/nom.rs +++ b/src/metrics/nom.rs @@ -123,6 +123,7 @@ impl Stats { /// Returns the average number of function definitions over all spaces #[inline(always)] pub fn functions_average(&self) -> f64 { + println!("SPACE {}", self.space_count); self.functions_sum() / self.space_count as f64 } @@ -624,6 +625,37 @@ mod tests { #[test] fn java_nom() { + check_metrics!( + "class A { + public void foo(){ + return; + } + public void bar(){ + return; + } + }", + "foo.java", + JavaParser, + nom, + [ + (functions_sum, 2, usize), + (closures_sum, 0, usize), + (total, 2, usize), + (functions_max, 1, usize), + (functions_min, 0, usize), + (closures_max, 0, usize), + (closures_min, 0, usize), + ], + [ + (functions_average, 0.5), // number of spaces = 4 + (closures_average, 0.0), + (average, 0.5) + ] + ); + } + + #[test] + fn java_closure_nom() { check_metrics!( "interface printable{ void print(); From ea6480115bd45dcd78ccd80e09c4d304c6679b6f Mon Sep 17 00:00:00 2001 From: Devon Burriss Date: Thu, 14 Apr 2022 19:20:44 +0200 Subject: [PATCH 3/3] Removed debug statement --- src/metrics/nom.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/metrics/nom.rs b/src/metrics/nom.rs index ae60c51aa..4e8000a69 100644 --- a/src/metrics/nom.rs +++ b/src/metrics/nom.rs @@ -123,7 +123,6 @@ impl Stats { /// Returns the average number of function definitions over all spaces #[inline(always)] pub fn functions_average(&self) -> f64 { - println!("SPACE {}", self.space_count); self.functions_sum() / self.space_count as f64 }