aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-squid/src/test/java/org/sonar/squid/text/SourceTest.java
blob: 73d96313f3791758013145d21eed4f6831526e26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2009 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.squid.text;

import org.junit.Test;
import org.sonar.squid.measures.Metric;
import org.sonar.squid.recognizer.CodeRecognizer;

import java.io.StringReader;

import static org.junit.Assert.assertEquals;

public class SourceTest {

  private CodeRecognizer codeRecognizer = new CodeRecognizer(0.91, new JavaFootprint());

  @Test
  public void testGetLines() {
    String[] lines = { "", "int i = 0;" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(2, source.getMeasure(Metric.LINES));
  }

  @Test(expected = IllegalStateException.class)
  public void testGetIllegalMetric() {
    String[] lines = { "", "int i = 0;" };
    Source source = new Source(lines, codeRecognizer);
    source.getMeasure(Metric.COMPLEXITY);
  }

  @Test
  public void testGetBlankLines() {
    String[] lines = { "package toto;", " " };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(1, source.getMeasure(Metric.BLANK_LINES));
  }

  @Test
  public void testGetCppCommentLines() {
    String[] lines = { "package toto;", "//this is a comment", "int i = 4; //new comment" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(2, source.getMeasure(Metric.COMMENT_LINES));
    assertEquals(1, source.getMeasure(Metric.COMMENT_LINES, 2, 2));
    assertEquals(2, source.getMeasure(Metric.LINES_OF_CODE));
  }

  @Test
  public void testGetCCommentLines() {
    String[] lines = { "package toto;", " int a = 4; /*this is a comment", "new line of comment", "end of comment */ int b = 4;" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(3, source.getMeasure(Metric.COMMENT_LINES));
    assertEquals(1, source.getMeasure(Metric.COMMENT_LINES, 2, 2));
    assertEquals(3, source.getMeasure(Metric.LINES_OF_CODE));
  }
  
  @Test
  public void testGetAdjacentCCommentBlocks() {
    String[] lines = { "/*first comment*//*second ", " * + \"Ver.", "comment*/" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(3, source.getMeasure(Metric.COMMENT_LINES));
    assertEquals(3, source.getMeasure(Metric.LINES));
  }

  @Test
  public void testGetLinesOfCode() {
    String[] lines = { "package toto;", " ", "import java.util.*;" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(2, source.getMeasure(Metric.LINES_OF_CODE));
    assertEquals(0, source.getMeasure(Metric.LINES_OF_CODE, 2, 2));
  }

  @Test
  public void testGetCommentedCodeOutLines() {
    String[] lines = { "", "/*package toto;", "}*/", " ", "import java.util.*;" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(1, source.getMeasure(Metric.LINES_OF_CODE));
    assertEquals(0, source.getMeasure(Metric.COMMENT_LINES));
    assertEquals(2, source.getMeasure(Metric.COMMENTED_OUT_CODE_LINES));
  }

  @Test
  public void testBlankLinesAfterEndOfComment() {
    String[] lines = { "/*Comment*/ " };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(0, source.getMeasure(Metric.LINES_OF_CODE));
    assertEquals(1, source.getMeasure(Metric.COMMENT_LINES));
  }

  @Test
  public void testGetCommentedCodeOutLinesIntoJavadoc() {
    String[] lines = { "/**package toto;", "}*/", " ", "import java.util.*;" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(1, source.getMeasure(Metric.LINES_OF_CODE));
    assertEquals(2, source.getMeasure(Metric.COMMENT_LINES));
    assertEquals(0, source.getMeasure(Metric.COMMENTED_OUT_CODE_LINES));
  }

  @Test
  public void testGetBlankCommentLines() {
    String[] lines = { "/**", "*/", "import java.util.*;" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(3, source.getMeasure(Metric.LINES));
    assertEquals(1, source.getMeasure(Metric.LINES_OF_CODE));
    assertEquals(0, source.getMeasure(Metric.COMMENT_LINES));
    assertEquals(2, source.getMeasure(Metric.COMMENT_BLANK_LINES));
  }

  @Test
  public void testGetNoSonarTagLines() {
    String[] lines = { "import java.util.*;", "//NOSONAR comment", };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(1, source.getMeasure(Metric.COMMENT_LINES));
    assertEquals(1, source.getNoSonarTagLines().size());
  }

  @Test
  public void testGetBlankLinesFromTo() {
    String[] lines = { "package toto;", "", "import java.util.*", "   " };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(1, source.getMeasure(Metric.BLANK_LINES, 1, 3));
    assertEquals(1, source.getMeasure(Metric.BLANK_LINES, 3, 4));
    assertEquals(2, source.getMeasure(Metric.BLANK_LINES, 1, 4));
  }

  @Test
  public void endWithEmptyLine() {
    String[] lines = { "package toto;", "" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(1, source.getMeasure(Metric.BLANK_LINES));
    assertEquals(2, source.getMeasure(Metric.LINES));
  }

  @Test(expected = IllegalStateException.class)
  public void testGetBlankLinesFromToWithOutOfBoundIndex() {
    String[] lines = { "package toto;" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(1, source.getMeasure(Metric.BLANK_LINES, 1, 3));
  }

  @Test
  public void testConstructorWithReader() {
    Source source = new Source(new StringReader("package toto; \nimport java.util.*;"), codeRecognizer);
    assertEquals(2, source.getMeasure(Metric.LINES));
    assertEquals(2, source.getMeasure(Metric.LINES_OF_CODE));
  }

  @Test
  public void nativeGWTCodeRecognition() {
    String[] lines = { "/*-{", "// JavaScript code", "return this.nextSibling;", "}-*/;" };
    Source source = new Source(lines, codeRecognizer);
    assertEquals(4, source.getMeasure(Metric.LINES));
    assertEquals(3, source.getMeasure(Metric.LINES_OF_CODE));
    assertEquals(1, source.getMeasure(Metric.COMMENT_LINES));
  }

  @Test
  public void testSingleLineCommentWithDoubleDash() {
    String[] lines = { "import java.util.*;", "--NOSONAR", };
    Source source = new Source(new StringArrayReader(lines), codeRecognizer, "--");
    assertEquals(1, source.getMeasure(Metric.COMMENT_LINES));
    assertEquals(1, source.getNoSonarTagLines().size());
  }
}