aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-css-plugin/src/test
diff options
context:
space:
mode:
authorAmaury Levé2018-06-21 11:33:42 +0200
committerElena Vilchik2018-06-21 11:33:42 +0200
commit913028fc4c913fec3bbc1800c00e526413040e01 (patch)
tree0d1b7cadc4d45d82a1f0e3c91e96670d4b813551 /sonar-css-plugin/src/test
parent6472431ad488158bfcf863a7b4a5655e1ecc55e8 (diff)
downloadsonar-css-913028fc4c913fec3bbc1800c00e526413040e01.tar.bz2
Add comment lines and lines of code metrics (#49)
Diffstat (limited to 'sonar-css-plugin/src/test')
-rw-r--r--sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java109
1 files changed, 87 insertions, 22 deletions
diff --git a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java
index e8eb31b..7e49277 100644
--- a/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java
+++ b/sonar-css-plugin/src/test/java/org/sonar/css/plugin/MetricSensorTest.java
@@ -30,8 +30,13 @@ import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.FileLinesContext;
+import org.sonar.api.measures.FileLinesContextFactory;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class MetricSensorTest {
@@ -44,93 +49,142 @@ public class MetricSensorTest {
@Test
public void should_describe() {
DefaultSensorDescriptor desc = new DefaultSensorDescriptor();
- new MetricSensor().describe(desc);
+ new MetricSensor(null).describe(desc);
assertThat(desc.languages()).containsOnly("css");
}
@Test
public void empty_input() throws Exception {
- highlight("foo");
+ executeSensor("foo");
assertThat(sensorContext.highlightingTypeAt(inputFile.key(), 1, 0)).isEmpty();
assertThat(sensorContext.highlightingTypeAt(inputFile.key(), 1, 1)).isEmpty();
}
@Test
public void comment() throws IOException {
- highlight("/* some comment */");
+ executeSensor("/* some comment */");
assertHighlighting(1, 0, 18, TypeOfText.COMMENT);
- highlight("/* some comment\nmultiline */");
+ executeSensor("/* some comment\nmultiline */");
assertHighlighting(1, 0, 15, TypeOfText.COMMENT);
assertHighlighting(2, 0, 12, TypeOfText.COMMENT);
}
@Test
public void string() throws IOException {
- highlight("\"foo\"");
+ executeSensor("\"foo\"");
assertHighlighting(1, 0, 5, TypeOfText.STRING);
- highlight("\"foo\\\nbar\"");
+ executeSensor("\"foo\\\nbar\"");
assertHighlighting(1, 0, 4, TypeOfText.STRING);
assertHighlighting(2, 0, 4, TypeOfText.STRING);
}
@Test
public void constant() throws IOException {
- highlight("1");
+ executeSensor("1");
assertHighlighting(1, 0, 1, TypeOfText.CONSTANT);
- highlight("1.0");
+ executeSensor("1.0");
assertHighlighting(1, 0, 3, TypeOfText.CONSTANT);
- highlight("0px");
+ executeSensor("0px");
assertHighlighting(1, 0, 3, TypeOfText.CONSTANT);
- highlight("1em");
+ executeSensor("1em");
assertHighlighting(1, 0, 3, TypeOfText.CONSTANT);
- highlight("#ddd");
+ executeSensor("#ddd");
assertHighlighting(1, 0, 4, TypeOfText.CONSTANT);
}
@Test
public void annotation() throws IOException {
- highlight("@bar { }");
+ executeSensor("@bar { }");
assertHighlighting(1, 0, 4, TypeOfText.ANNOTATION);
- highlight("@my-selector: banner;");
+ executeSensor("@my-selector: banner;");
assertHighlighting(1, 0, 12, TypeOfText.ANNOTATION);
- highlight("@import \"src/themes\"");
+ executeSensor("@import \"src/themes\"");
assertHighlighting(1, 0, 7, TypeOfText.ANNOTATION);
- highlight(".element { color: @@color }");
+ executeSensor(".element { color: @@color }");
assertHighlighting(1, 18, 7, TypeOfText.ANNOTATION);
}
@Test
public void keyword() throws IOException {
- highlight("$foo { }");
+ executeSensor("$foo { }");
assertHighlighting(1, 0, 4, TypeOfText.KEYWORD);
- highlight("#header { .border-radius(4px); }");
+ executeSensor("#header { .border-radius(4px); }");
assertHighlighting(1, 0, 7, TypeOfText.KEYWORD);
}
@Test
public void keyword_light() throws IOException {
- highlight("bar: foo { }");
+ executeSensor("bar: foo { }");
assertHighlighting(1, 0, 3, TypeOfText.KEYWORD_LIGHT);
- highlight("bar { foo: 1px }");
+ executeSensor("bar { foo: 1px }");
assertHighlighting(1, 6, 3, TypeOfText.KEYWORD_LIGHT);
- highlight("bar { foo-bar: 1px }");
+ executeSensor("bar { foo-bar: 1px }");
assertHighlighting(1, 6, 7, TypeOfText.KEYWORD_LIGHT);
}
- private void highlight(String content) throws IOException {
+ @Test
+ public void lines_of_code() throws IOException {
+ executeSensor("bar { }");
+ assertLinesOfCode(1);
+
+ executeSensor("bar\n{ }");
+ assertLinesOfCode(2);
+
+ // We don't count empty lines
+ executeSensor("\n\n\nsomething\n\n\n");
+ assertLinesOfCode(1);
+
+ // We don't count comments
+ executeSensor("// foo");
+ assertLinesOfCode(0);
+ executeSensor("/* dasdsa */");
+ assertLinesOfCode(0);
+ executeSensor("/* das\ndsa */");
+ assertLinesOfCode(0);
+
+ // Mix code and comment
+ executeSensor("foo {} // some comment");
+ assertLinesOfCode(1);
+ }
+
+ @Test
+ public void lines_of_comment() throws IOException {
+ executeSensor("// inline comment");
+ assertLinesOfComment(1);
+
+ executeSensor("/* single line comment */");
+ assertLinesOfComment(1);
+
+ executeSensor("/* multiline\n *\n *\n * comment\n*/");
+ assertLinesOfComment(5);
+
+ // We don't count empty lines
+ executeSensor("\n\n\n/* something */\n\n\n");
+ assertLinesOfComment(1);
+
+ // We don't count code
+ executeSensor("foo {}");
+ assertLinesOfComment(0);
+
+ // Mix code and comment
+ executeSensor("foo {} // some comment");
+ assertLinesOfComment(1);
+ }
+
+ private void executeSensor(String content) throws IOException {
File file = tempFolder.newFile();
inputFile = new TestInputFileBuilder("moduleKey", file.getName())
.setLanguage("css")
@@ -140,7 +194,10 @@ public class MetricSensorTest {
sensorContext = SensorContextTester.create(tempFolder.getRoot());
sensorContext.fileSystem().add(inputFile);
- new MetricSensor().execute(sensorContext);
+ FileLinesContext linesContext = mock(FileLinesContext.class);
+ FileLinesContextFactory linesContextFactory = mock(FileLinesContextFactory.class);
+ when(linesContextFactory.createFor(inputFile)).thenReturn(linesContext);
+ new MetricSensor(linesContextFactory).execute(sensorContext);
}
private void assertHighlighting(int line, int column, int length, TypeOfText type) {
@@ -149,4 +206,12 @@ public class MetricSensorTest {
assertThat(typeOfTexts).containsOnly(type);
}
}
+
+ private void assertLinesOfCode(int expected) {
+ assertThat(sensorContext.measure(inputFile.key(), CoreMetrics.NCLOC).value()).isEqualTo(expected);
+ }
+
+ private void assertLinesOfComment(int expected) {
+ assertThat(sensorContext.measure(inputFile.key(), CoreMetrics.COMMENT_LINES).value()).isEqualTo(expected);
+ }
}