Port remaining dart tests

This commit is contained in:
Clément Fournier
2020-06-12 16:36:04 +02:00
parent ecc8699269
commit 1540ec6d91
28 changed files with 194 additions and 105 deletions

View File

@@ -49,6 +49,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-test</artifactId>

View File

@@ -1,65 +1,83 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
import net.sourceforge.pmd.cpd.test.CpdTextComparisonTest;
@RunWith(Parameterized.class)
public class DartTokenizerTest extends AbstractTokenizerTest {
public class DartTokenizerTest extends CpdTextComparisonTest {
private final String filename;
private final int nExpectedTokens;
public DartTokenizerTest(String filename, int nExpectedTokens) {
this.filename = filename;
this.nExpectedTokens = nExpectedTokens;
}
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[] { "comment.dart", 5 },
new Object[] { "escape_sequences.dart", 13 },
new Object[] { "escaped_backslash.dart", 14 },
new Object[] { "escaped_string.dart", 17 },
new Object[] { "increment.dart", 185 },
new Object[] { "imports.dart", 1 },
new Object[] { "regex.dart", 13 },
new Object[] { "regex2.dart", 13 },
new Object[] { "regex3.dart", 13 },
new Object[] { "string_with_backslashes.dart", 9 },
new Object[] { "string_multiline.dart", 13 }
);
}
@Before
@Override
public void buildTokenizer() throws IOException {
this.tokenizer = new DartTokenizer();
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), this.filename));
public DartTokenizerTest() {
super(".dart");
}
@Override
public String getSampleCode() throws IOException {
return IOUtils.toString(DartTokenizer.class.getResourceAsStream(this.filename), StandardCharsets.UTF_8);
public Tokenizer newTokenizer() {
return new DartTokenizer();
}
@Test
public void testComment() {
doTest("comment");
}
@Test
public void tokenizeTest() throws IOException {
this.expectedTokenCount = nExpectedTokens;
super.tokenizeTest();
public void testEscapeSequences() {
doTest("escape_sequences");
}
@Test
public void testEscapedBackslash() {
doTest("escaped_backslash");
}
@Test
public void testEscapedString() {
doTest("escaped_string");
}
@Test
public void testIncrement() {
doTest("increment");
}
@Test
public void testImports() {
doTest("imports");
}
@Test
public void testRegex() {
doTest("regex");
}
@Test
public void testRegex2() {
doTest("regex2");
}
@Test
public void testRegex3() {
doTest("regex3");
}
@Test
public void testStringWithBackslashes() {
doTest("string_with_backslashes");
}
@Test
public void testMultiline() {
doTest("string_multiline");
}
}

View File

@@ -1,50 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import org.junit.Test;
import net.sourceforge.pmd.cpd.test.CpdTextComparisonTest;
public class DartTokenizerTest2 extends CpdTextComparisonTest {
public DartTokenizerTest2() {
super(".dart");
}
@Override
protected String getResourcePrefix() {
return "";
}
@Override
public Tokenizer newTokenizer() {
return new DartTokenizer();
}
@Test
public void testComment() {
doTest("comment");
}
@Test
public void testMultiline() {
doTest("string_multiline");
}
@Test
public void testStringWithBackslashes() {
doTest("string_with_backslashes");
}
@Test
public void testIncrement() {
doTest("increment");
}
}

View File

@@ -0,0 +1,17 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[var] 1 3
[newline] 5 11
[=] 13 13
['\\n'] 15 18
L2
[var] 1 3
[dollar] 5 10
[=] 12 12
['$'] 14 16
L3
[var] 1 3
[escaped_dollar] 5 18
[=] 20 20
["\\$"] 22 25
EOF

View File

@@ -0,0 +1,17 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[var] 1 3
[separator] 5 13
[=] 15 15
['\\\\'] 17 20
L2
[var] 1 3
[separators] 5 14
[=] 16 16
[const] 18 22
[\[] 24 24
['/'] 25 27
[,] 28 28
['\\\\'] 30 33
[\]] 34 34
EOF

View File

@@ -0,0 +1,22 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[var] 1 3
[a] 5 5
[=] 7 7
["a"] 9 11
L2
[var] 1 3
[b] 5 5
[=] 7 7
["b"] 9 11
L3
[var] 1 3
[c] 5 5
[=] 7 7
["c"] 9 11
L4
[var] 1 3
[x] 5 5
[=] 7 7
["$a(b: $b, c: \\"$c\\")"] 9 30
EOF

View File

@@ -0,0 +1,2 @@
[Image] or [Truncated image[ Bcol Ecol
EOF

View File

@@ -204,4 +204,4 @@ L24
[+=] 24 25
[1] 27 27
[}] 30 30
[EOF] -1 -1
EOF

View File

@@ -0,0 +1,17 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[class] 1 5
[MyClass] 7 13
[{] 15 15
L2
[final] 3 7
[regex] 9 13
[=] 15 15
[new] 17 19
[RegExp] 21 26
[(] 27 27
[r'^--(\[a-zA-Z\\-_0-9\]+)(=(.*))?$[ 28 59
[)] 60 60
L3
[}] 1 1
EOF

View File

@@ -0,0 +1,17 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[class] 1 5
[MyClass] 7 13
[{] 15 15
L2
[final] 3 7
[regex] 9 13
[=] 15 15
[new] 17 19
[RegExp] 21 26
[(] 27 27
[r"(^\[a-zA-Z\]\[-+.a-zA-Z\\d\]*://[ 28 64
[)] 65 65
L3
[}] 1 1
EOF

View File

@@ -0,0 +1,17 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[class] 1 5
[MyClass] 7 13
[{] 15 15
L2
[final] 3 7
[regex] 9 13
[=] 15 15
[new] 17 19
[RegExp] 21 26
[(] 27 27
[r'''\[ \\t\\r\\n"'\\\\/\]'''] 28 48
[)] 49 49
L3
[}] 1 1
EOF

View File

@@ -15,4 +15,4 @@ L7
["""This is also a\nmulti-line stri[ 14 52
L9
[}] 1 1
[EOF] -1 -1
EOF

View File

@@ -10,4 +10,4 @@ L2
["Escaping\\ spaces\\ should work"] 33 63
L3
[}] 1 1
[EOF] -1 -1
EOF

View File

@@ -28,7 +28,7 @@ abstract class CpdTextComparisonTest(
get() = javaClass
override val resourcePrefix: String
get() = "cpdData"
get() = "testData"
override fun transformTextContent(sourceText: String): String {
val sourceCode = SourceCode(SourceCode.StringCodeLoader(sourceText))
@@ -45,7 +45,12 @@ abstract class CpdTextComparisonTest(
for (token in tokens.iterator()) {
if (curLine != token.beginLine && token !== TokenEntry.EOF) {
if (token === TokenEntry.EOF) {
append("EOF").appendln()
continue
}
if (curLine != token.beginLine) {
curLine = token.beginLine
append('L').append(curLine).appendln()
}

View File

@@ -45,7 +45,7 @@ abstract class BaseTextComparisonTest {
val expected = expectedFile.readText()
assertEquals(expected.normalize(), actual.normalize(), "Tree dump comparison failed, see the reference: $expectedFile")
assertEquals(expected.normalize(), actual.normalize(), "File comparison failed, see the reference: $expectedFile")
}
// Outputting a path makes for better error messages

View File

@@ -17,7 +17,9 @@ import net.sourceforge.pmd.cpd.Tokens;
/**
* @author Romain PELISSE, belaran@gmail.com
*
* @deprecated Use CpdTextComparisonTest in module pmd-lang-test
*/
@Deprecated
public abstract class AbstractTokenizerTest {
protected int expectedTokenCount;