Added option to exclude C# using directives from duplicated code analysis.
To exclude C# using directives the system property 'ignore_usings=true' has to be specified on the command line with '-Dignore_usings=true'.
This commit is contained in:
@ -3,15 +3,24 @@
|
|||||||
*/
|
*/
|
||||||
package net.sourceforge.pmd.cpd;
|
package net.sourceforge.pmd.cpd;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Language implementation for C#
|
* Language implementation for C#
|
||||||
*/
|
*/
|
||||||
public class CsLanguage extends AbstractLanguage {
|
public class CsLanguage extends AbstractLanguage {
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new C# Language instance.
|
|
||||||
*/
|
|
||||||
public CsLanguage() {
|
public CsLanguage() {
|
||||||
|
this(System.getProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
public CsLanguage(Properties properties) {
|
||||||
super("C#", "cs", new CsTokenizer(), ".cs");
|
super("C#", "cs", new CsTokenizer(), ".cs");
|
||||||
|
setProperties(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void setProperties(Properties properties) {
|
||||||
|
CsTokenizer tokenizer = (CsTokenizer)getTokenizer();
|
||||||
|
tokenizer.setProperties(properties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -5,6 +5,7 @@
|
|||||||
package net.sourceforge.pmd.cpd;
|
package net.sourceforge.pmd.cpd;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -13,132 +14,152 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class CsTokenizerTest {
|
public class CsTokenizerTest {
|
||||||
|
|
||||||
private CsTokenizer tokenizer = new CsTokenizer();
|
private CsTokenizer tokenizer;
|
||||||
|
|
||||||
private Tokens tokens;
|
private Tokens tokens;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
tokens = new Tokens();
|
tokenizer = new CsTokenizer();
|
||||||
TokenEntry.clearImages();
|
tokens = new Tokens();
|
||||||
|
TokenEntry.clearImages();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleClass() {
|
public void testSimpleClass() {
|
||||||
tokenizer.tokenize(toSourceCode("class Foo {}"), tokens);
|
tokenizer.tokenize(toSourceCode("class Foo {}"), tokens);
|
||||||
assertEquals(5, tokens.size());
|
assertEquals(5, tokens.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleClassDuplicatedTokens() {
|
public void testSimpleClassDuplicatedTokens() {
|
||||||
tokenizer.tokenize(toSourceCode("class Foo { class Foo { } }"), tokens);
|
tokenizer.tokenize(toSourceCode("class Foo { class Foo { } }"), tokens);
|
||||||
assertEquals(9, tokens.size());
|
assertEquals(9, tokens.size());
|
||||||
List<TokenEntry> tokenList = tokens.getTokens();
|
List<TokenEntry> tokenList = tokens.getTokens();
|
||||||
assertEquals(tokenList.get(0).getIdentifier(), tokenList.get(3).getIdentifier());
|
assertEquals(tokenList.get(0).getIdentifier(), tokenList.get(3).getIdentifier());
|
||||||
assertEquals(tokenList.get(1).getIdentifier(), tokenList.get(4).getIdentifier());
|
assertEquals(tokenList.get(1).getIdentifier(), tokenList.get(4).getIdentifier());
|
||||||
assertEquals(tokenList.get(2).getIdentifier(), tokenList.get(5).getIdentifier());
|
assertEquals(tokenList.get(2).getIdentifier(), tokenList.get(5).getIdentifier());
|
||||||
assertEquals(tokenList.get(6).getIdentifier(), tokenList.get(7).getIdentifier());
|
assertEquals(tokenList.get(6).getIdentifier(), tokenList.get(7).getIdentifier());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimpleClassMethodMultipleLines() {
|
public void testSimpleClassMethodMultipleLines() {
|
||||||
tokenizer.tokenize(toSourceCode(
|
tokenizer.tokenize(toSourceCode(
|
||||||
"class Foo {\n"
|
"class Foo {\n"
|
||||||
+ " public String foo(int a) {\n"
|
+ " public String foo(int a) {\n"
|
||||||
+ " int i = a;\n"
|
+ " int i = a;\n"
|
||||||
+ " return \"x\" + a;\n"
|
+ " return \"x\" + a;\n"
|
||||||
+ " }\n"
|
+ " }\n"
|
||||||
+ "}"), tokens);
|
+ "}"), tokens);
|
||||||
assertEquals(22, tokens.size());
|
assertEquals(22, tokens.size());
|
||||||
List<TokenEntry> tokenList = tokens.getTokens();
|
List<TokenEntry> tokenList = tokens.getTokens();
|
||||||
assertEquals(1, tokenList.get(0).getBeginLine());
|
assertEquals(1, tokenList.get(0).getBeginLine());
|
||||||
assertEquals(2, tokenList.get(4).getBeginLine());
|
assertEquals(2, tokenList.get(4).getBeginLine());
|
||||||
assertEquals(3, tokenList.get(11).getBeginLine());
|
assertEquals(3, tokenList.get(11).getBeginLine());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStrings() {
|
public void testStrings() {
|
||||||
tokenizer.tokenize(toSourceCode("String s =\"aaa \\\"b\\n\";"), tokens);
|
tokenizer.tokenize(toSourceCode("String s =\"aaa \\\"b\\n\";"), tokens);
|
||||||
assertEquals(5, tokens.size());
|
assertEquals(5, tokens.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOpenString() {
|
public void testOpenString() {
|
||||||
tokenizer.tokenize(toSourceCode("String s =\"aaa \\\"b\\"), tokens);
|
tokenizer.tokenize(toSourceCode("String s =\"aaa \\\"b\\"), tokens);
|
||||||
assertEquals(5, tokens.size());
|
assertEquals(5, tokens.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCommentsIgnored1() {
|
public void testCommentsIgnored1() {
|
||||||
tokenizer.tokenize(toSourceCode("class Foo { /* class * ** X */ }"), tokens);
|
tokenizer.tokenize(toSourceCode("class Foo { /* class * ** X */ }"), tokens);
|
||||||
assertEquals(5, tokens.size());
|
assertEquals(5, tokens.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCommentsIgnored2() {
|
public void testCommentsIgnored2() {
|
||||||
tokenizer.tokenize(toSourceCode("class Foo { // class X /* aaa */ \n }"), tokens);
|
tokenizer.tokenize(toSourceCode("class Foo { // class X /* aaa */ \n }"), tokens);
|
||||||
assertEquals(5, tokens.size());
|
assertEquals(5, tokens.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCommentsIgnored3() {
|
public void testCommentsIgnored3() {
|
||||||
tokenizer.tokenize(toSourceCode("class Foo { /// class X /* aaa */ \n }"), tokens);
|
tokenizer.tokenize(toSourceCode("class Foo { /// class X /* aaa */ \n }"), tokens);
|
||||||
assertEquals(5, tokens.size());
|
assertEquals(5, tokens.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMoreTokens() {
|
public void testMoreTokens() {
|
||||||
tokenizer.tokenize(toSourceCode(
|
tokenizer.tokenize(toSourceCode(
|
||||||
"class Foo {\n"
|
"class Foo {\n"
|
||||||
+ " void bar() {\n"
|
+ " void bar() {\n"
|
||||||
+ " int a = 1 >> 2; \n"
|
+ " int a = 1 >> 2; \n"
|
||||||
+ " a += 1; \n"
|
+ " a += 1; \n"
|
||||||
+ " a++; \n"
|
+ " a++; \n"
|
||||||
+ " a /= 3e2; \n"
|
+ " a /= 3e2; \n"
|
||||||
+ " float f = -3.1; \n"
|
+ " float f = -3.1; \n"
|
||||||
+ " f *= 2; \n"
|
+ " f *= 2; \n"
|
||||||
+ " bool b = ! (f == 2.0 || f >= 1.0 && f <= 2.0) \n"
|
+ " bool b = ! (f == 2.0 || f >= 1.0 && f <= 2.0) \n"
|
||||||
+ " }\n"
|
+ " }\n"
|
||||||
+ "}"
|
+ "}"
|
||||||
), tokens);
|
), tokens);
|
||||||
assertEquals(50, tokens.size());
|
assertEquals(50, tokens.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLineNumberAfterMultilineComment() {
|
public void testLineNumberAfterMultilineComment() {
|
||||||
tokenizer.tokenize(toSourceCode(
|
tokenizer.tokenize(toSourceCode(
|
||||||
"/* This is a multiline comment \n"
|
"/* This is a multiline comment \n"
|
||||||
+ " * \n"
|
+ " * \n"
|
||||||
+ " * Lorem ipsum dolor sit amet, \n"
|
+ " * Lorem ipsum dolor sit amet, \n"
|
||||||
+ " * consectetur adipiscing elit \n"
|
+ " * consectetur adipiscing elit \n"
|
||||||
+ " */\n"
|
+ " */\n"
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "class Foo {\n"
|
+ "class Foo {\n"
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ "}"
|
+ "}"
|
||||||
), tokens);
|
), tokens);
|
||||||
assertEquals(5, tokens.size());
|
assertEquals(5, tokens.size());
|
||||||
assertEquals(7, tokens.getTokens().get(0).getBeginLine());
|
assertEquals(7, tokens.getTokens().get(0).getBeginLine());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLineNumberAfterMultilineString() {
|
public void testLineNumberAfterMultilineString() {
|
||||||
tokenizer.tokenize(toSourceCode(
|
tokenizer.tokenize(toSourceCode(
|
||||||
"class Foo {\n"
|
"class Foo {\n"
|
||||||
+ " void bar() {\n"
|
+ " void bar() {\n"
|
||||||
+ " String query = \n"
|
+ " String query = \n"
|
||||||
+ " @\"SELECT foo, bar\n"
|
+ " @\"SELECT foo, bar\n"
|
||||||
+ " FROM table \n"
|
+ " FROM table \n"
|
||||||
+ " WHERE id = 42\"; \n"
|
+ " WHERE id = 42\"; \n"
|
||||||
+ " }\n"
|
+ " }\n"
|
||||||
+ "}"
|
+ "}"
|
||||||
), tokens);
|
), tokens);
|
||||||
assertEquals(16, tokens.size());
|
assertEquals(16, tokens.size());
|
||||||
assertEquals(8, tokens.getTokens().get(14).getBeginLine());
|
assertEquals(8, tokens.getTokens().get(14).getBeginLine());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIgnoreUsingDirectives() {
|
||||||
|
tokenizer.setIgnoreUsings(true);
|
||||||
|
tokenizer.tokenize(toSourceCode("using System.Text;\n"), tokens);
|
||||||
|
assertNotEquals("using", tokens.getTokens().get(0).toString());
|
||||||
|
assertEquals(2, tokens.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUsingStatementsAreNotIgnored() {
|
||||||
|
tokenizer.setIgnoreUsings(true);
|
||||||
|
tokenizer.tokenize(toSourceCode(
|
||||||
|
"using (Font font1 = new Font(\"Arial\", 10.0f)) {\n"
|
||||||
|
+ " byte charset = font1.GdiCharSet;\n"
|
||||||
|
+ "}\n"
|
||||||
|
), tokens);
|
||||||
|
assertEquals("using", tokens.getTokens().get(0).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private SourceCode toSourceCode(String source) {
|
private SourceCode toSourceCode(String source) {
|
||||||
return new SourceCode(new SourceCode.StringCodeLoader(source));
|
return new SourceCode(new SourceCode.StringCodeLoader(source));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user