From 8fa49b615f61b8035706c7fcfcfe73b7caa237f7 Mon Sep 17 00:00:00 2001 From: Boyan Bonev Date: Fri, 2 Jun 2017 11:54:22 +0300 Subject: [PATCH] feat(classes): Add counting of Scala classes There was no support for collecting Scala class number so I added it. Tested with the unit tests and with SonarQube 6.3.1. Everything seemed OK. --- .../scala/com/sagacify/sonar/scala/Measures.scala | 15 +++++++++------ .../com/sagacify/sonar/scala/ScalaSensor.scala | 2 +- src/test/resources/ScalaFile2.scala | 3 +++ .../sonar/scala/BaseMetricSensorSpec.scala | 5 ++++- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/scala/com/sagacify/sonar/scala/Measures.scala b/src/main/scala/com/sagacify/sonar/scala/Measures.scala index 8403b05..56e425b 100644 --- a/src/main/scala/com/sagacify/sonar/scala/Measures.scala +++ b/src/main/scala/com/sagacify/sonar/scala/Measures.scala @@ -1,17 +1,20 @@ package com.sagacify.sonar.scala import scala.annotation.tailrec - import scalariform.lexer.ScalaLexer import scalariform.lexer.Token -import scalariform.lexer.Tokens.LINE_COMMENT -import scalariform.lexer.Tokens.MULTILINE_COMMENT -import scalariform.lexer.Tokens.XML_COMMENT -import scalariform.lexer.Tokens.WS -import scalariform.lexer.Tokens.EOF +import scalariform.lexer.Tokens._ object Measures { + final def count_classes(tokens: List[Token], i: Int = 0): Int = { + var count = 0 + tokens.foreach(token => if (token.tokenType == CLASS || token.tokenType == OBJECT) count += 1) + + count + } + + /* applied on raw source code */ /* applied on lines of code */ diff --git a/src/main/scala/com/sagacify/sonar/scala/ScalaSensor.scala b/src/main/scala/com/sagacify/sonar/scala/ScalaSensor.scala index 02ff5bf..ceccb2a 100644 --- a/src/main/scala/com/sagacify/sonar/scala/ScalaSensor.scala +++ b/src/main/scala/com/sagacify/sonar/scala/ScalaSensor.scala @@ -36,7 +36,7 @@ class ScalaSensor(scala: Scala, fs: FileSystem) extends Sensor { CM.NCLOC, Measures.count_ncloc(tokens)) - // context.saveMeasure(input, CM.CLASSES, classes) + context.saveMeasure(inputFile, CM.CLASSES, Measures.count_classes(tokens)) // context.saveMeasure(input, CM.FUNCTIONS, methods) // context.saveMeasure(input, CM.ACCESSORS, accessors) // context.saveMeasure(input, CM.COMPLEXITY_IN_FUNCTIONS, complexityInMethods) diff --git a/src/test/resources/ScalaFile2.scala b/src/test/resources/ScalaFile2.scala index eb7cc9f..066bb7e 100644 --- a/src/test/resources/ScalaFile2.scala +++ b/src/test/resources/ScalaFile2.scala @@ -7,3 +7,6 @@ class ScalaFile2 { println("function called.") } } + +object ScalaFile2 { +} diff --git a/src/test/scala/com/sagacify/sonar/scala/BaseMetricSensorSpec.scala b/src/test/scala/com/sagacify/sonar/scala/BaseMetricSensorSpec.scala index 3781a7f..0002182 100644 --- a/src/test/scala/com/sagacify/sonar/scala/BaseMetricSensorSpec.scala +++ b/src/test/scala/com/sagacify/sonar/scala/BaseMetricSensorSpec.scala @@ -67,6 +67,8 @@ class ScalaSensorSpec extends FlatSpec with Matchers { .saveMeasure(file, CM.FILES, 1) verify(sensorContext, times(1)) .saveMeasure(file, CM.COMMENT_LINES, 0) + verify(sensorContext, times(1)) + .saveMeasure(file, CM.CLASSES, 1) } } @@ -87,7 +89,8 @@ class ScalaSensorSpec extends FlatSpec with Matchers { .saveMeasure(file, CM.FILES, 1) verify(sensorContext, times(1)) .saveMeasure(file, CM.COMMENT_LINES, 1) - + verify(sensorContext, times(1)) + .saveMeasure(file, CM.CLASSES, 2) } } }