Skip to content

Commit b1fdbe3

Browse files
committed
thanks @timothy-volvoIdean#207
1 parent 90d06f7 commit b1fdbe3

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* backelite-sonar-swift-plugin - Enables analysis of Swift and Objective-C projects into SonarQube.
3+
* Copyright © 2015 Backelite (${email})
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.backelite.sonarqube.swift.complexity;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.mockito.ArgumentCaptor;
23+
import org.mockito.Captor;
24+
import org.mockito.Mock;
25+
import org.mockito.runners.MockitoJUnitRunner;
26+
import org.sonar.api.batch.fs.*;
27+
import org.sonar.api.batch.measure.Metric;
28+
import org.sonar.api.batch.sensor.SensorContext;
29+
import org.sonar.api.batch.sensor.measure.NewMeasure;
30+
31+
import java.io.File;
32+
import java.util.Arrays;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.mockito.Mockito.when;
36+
37+
@RunWith(MockitoJUnitRunner.class)
38+
public class LizardReportParserTest {
39+
40+
@Mock
41+
SensorContext sensorContext;
42+
43+
@Mock
44+
FileSystem fileSystem;
45+
46+
@Mock
47+
FilePredicates filePredicates;
48+
49+
@Mock
50+
FilePredicate filePredicate;
51+
52+
@Mock
53+
InputFile inputFile;
54+
55+
@Mock
56+
NewMeasure<Integer> newMeasure;
57+
58+
@Captor
59+
ArgumentCaptor<String> hasRelativePathCaptor;
60+
61+
@Captor
62+
ArgumentCaptor<InputComponent> onCaptor;
63+
64+
@Captor
65+
ArgumentCaptor<Metric<Integer>> forMetricCaptor;
66+
67+
@Captor
68+
ArgumentCaptor<Integer> withValueCaptor;
69+
70+
71+
@Test
72+
public void parseSimpleFile() {
73+
74+
LizardReportParser parser = new LizardReportParser(sensorContext);
75+
File xmlFile = new File("src/test/resources/lizard-report.xml");
76+
77+
when(sensorContext.<Integer>newMeasure()).thenReturn(newMeasure);
78+
when(newMeasure.on(onCaptor.capture())).thenReturn(newMeasure);
79+
when(newMeasure.forMetric(forMetricCaptor.capture())).thenReturn(newMeasure);
80+
when(newMeasure.withValue(withValueCaptor.capture())).thenReturn(newMeasure);
81+
82+
when(sensorContext.fileSystem()).thenReturn(fileSystem);
83+
when(fileSystem.predicates()).thenReturn(filePredicates);
84+
when(filePredicates.hasRelativePath(hasRelativePathCaptor.capture())).thenReturn(filePredicate);
85+
when(fileSystem.hasFiles(filePredicate)).thenReturn(true);
86+
when(fileSystem.inputFile(filePredicate)).thenReturn(inputFile);
87+
88+
parser.parseReport(xmlFile);
89+
90+
assertEquals(5, onCaptor.getAllValues().size());
91+
assertEquals(5, forMetricCaptor.getAllValues().size());
92+
93+
assertEquals(Arrays.asList(1, 4, 8, 5, 46), withValueCaptor.getAllValues());
94+
assertEquals(Arrays.asList("./Folder With Space/File With Space.swift"), hasRelativePathCaptor.getAllValues());
95+
}
96+
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" ?>
2+
<?xml-stylesheet type="text/xsl" href="https://raw.githubusercontent.com/terryyin/lizard/master/lizard.xsl"?>
3+
<cppncss>
4+
<measure type="Function">
5+
<labels>
6+
<label>Nr.</label>
7+
<label>NCSS</label>
8+
<label>CCN</label>
9+
</labels>
10+
<item name="methodName(...) at ./Folder With Space/File With Space.swift:14">
11+
<value>1</value>
12+
<value>4</value>
13+
<value>1</value>
14+
</item>
15+
<average label="NCSS" value="8"/>
16+
<average label="CCN" value="1"/>
17+
</measure>
18+
<measure type="File">
19+
<labels>
20+
<label>Nr.</label>
21+
<label>NCSS</label>
22+
<label>CCN</label>
23+
<label>Functions</label>
24+
</labels>
25+
<item name="./Folder With Space/File With Space.swift">
26+
<value>1</value>
27+
<value>46</value>
28+
<value>8</value>
29+
<value>5</value>
30+
</item>
31+
<average label="NCSS" value="80"/>
32+
<average label="CCN" value="13"/>
33+
<average label="Functions" value="5"/>
34+
<sum label="NCSS" value="19537"/>
35+
<sum label="CCN" value="3194"/>
36+
<sum label="Functions" value="1420"/>
37+
<average label="NCSS" value="13"/>
38+
<average label="CCN" value="2"/>
39+
</measure>
40+
</cppncss>

0 commit comments

Comments
 (0)