Convert JS tests

This commit is contained in:
Clément Fournier
2020-06-13 19:16:22 +02:00
parent 4e21c1a947
commit e48fc7cca2
15 changed files with 268 additions and 127 deletions

View File

@ -4,158 +4,64 @@
package net.sourceforge.pmd.cpd;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.cpd.test.CpdTextComparisonTest;
public class EcmascriptTokenizerTest {
public class EcmascriptTokenizerTest extends CpdTextComparisonTest {
@Test
public void test1() throws IOException {
Tokenizer tokenizer = new EcmascriptTokenizer();
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getCode1()));
Tokens tokens = new Tokens();
tokenizer.tokenize(sourceCode, tokens);
assertEquals(40, tokens.size());
public EcmascriptTokenizerTest() {
super(".js");
}
@NotNull
@Override
public Tokenizer newTokenizer() {
return new EcmascriptTokenizer();
}
@NotNull
@Override
protected String getResourcePrefix() {
return "../lang/ecmascript/cpd/testdata";
}
@Test
public void test2() throws IOException {
Tokenizer t = new EcmascriptTokenizer();
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(getCode2()));
Tokens tokens = new Tokens();
t.tokenize(sourceCode, tokens);
assertEquals(45, tokens.size());
public void testSimple() {
doTest("simple");
}
@Test
public void testIgnoreBetweenSpecialComments() throws IOException {
final String code = "// CPD-OFF" + PMD.EOL
+ "function switchToRealPassword() {" + PMD.EOL
+ "var real = $('realPass');" + PMD.EOL
+ " var prompt = $('promptPass');" + PMD.EOL
+ "// CPD-ON" + PMD.EOL
+ "}" + PMD.EOL;
public void testSimplewithSemis() {
doTest("simpleWithSemis");
}
Tokenizer t = new EcmascriptTokenizer();
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(code));
Tokens tokens = new Tokens();
t.tokenize(sourceCode, tokens);
assertEquals(2, tokens.size()); // Only "}" and EOL
@Test
public void testIgnoreBetweenSpecialComments() {
doTest("specialComments");
}
/**
* See: https://sourceforge.net/p/pmd/bugs/1239/
*
* @throws IOException
* IO Exception
*/
@Test
public void parseStringNotAsMultiline() throws IOException {
Tokenizer t = new EcmascriptTokenizer();
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(
"var s = \"a string \\\n" + "continues\";\n" + "var s = \"a string \\\n" + "continues2\";\n"));
Tokens tokens = new Tokens();
t.tokenize(sourceCode, tokens);
assertEquals(11, tokens.size());
List<TokenEntry> list = tokens.getTokens();
assertEquals("var", list.get(0).getIdentifier(), list.get(5).getIdentifier());
assertEquals("s", list.get(1).getIdentifier(), list.get(6).getIdentifier());
assertEquals("=", list.get(2).getIdentifier(), list.get(7).getIdentifier());
assertEquals("\"a string continues\"", list.get(3).toString());
assertEquals("\"a string continues2\"", list.get(8).toString());
assertFalse(list.get(3).getIdentifier() == list.get(8).getIdentifier());
public void parseStringNotAsMultiline() {
doTest("lineContinuations");
}
@Test
public void testIgnoreSingleLineComments() throws IOException {
Tokenizer t = new EcmascriptTokenizer();
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(
"//This is a single line comment\n" + "var i = 0;\n\n" + "//This is another comment\n" + "i++;"));
Tokens tokens = new Tokens();
t.tokenize(sourceCode, tokens);
assertEquals(9, tokens.size());
List<TokenEntry> list = tokens.getTokens();
assertEquals("var", list.get(0).toString());
assertEquals("++", list.get(6).toString());
public void testIgnoreSingleLineComments() {
doTest("singleLineCommentIgnore");
}
@Test
public void testIgnoreMultiLineComments() throws IOException {
Tokenizer t = new EcmascriptTokenizer();
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader("/* This is a multi line comment\n"
+ " * \n" + " */ \n" + "var i = 0;\n\n"
+ "/* This is another multi line comment\n" + " * second line \n"
+ " * third line */\n" + "i++;"));
Tokens tokens = new Tokens();
t.tokenize(sourceCode, tokens);
assertEquals(9, tokens.size());
List<TokenEntry> list = tokens.getTokens();
assertEquals("var", list.get(0).toString());
assertEquals("++", list.get(6).toString());
}
// no semi-colons
private String getCode1() {
StringBuilder sb = new StringBuilder();
sb.append("function switchToRealPassword() {").append(PMD.EOL);
sb.append(" var real = $('realPass')").append(PMD.EOL);
sb.append(" var prompt = $('promptPass')").append(PMD.EOL);
sb.append(" real.style.display = 'inline'").append(PMD.EOL);
sb.append(" prompt.style.display = 'none'").append(PMD.EOL);
sb.append(" real.focus()").append(PMD.EOL);
sb.append("}").append(PMD.EOL);
return sb.toString();
}
// same as getCode1, but lines are ended with semi-colons
private String getCode2() {
StringBuilder sb = new StringBuilder();
sb.append("function switchToRealPassword() {").append(PMD.EOL);
sb.append(" var real = $('realPass');").append(PMD.EOL);
sb.append(" var prompt = $('promptPass');").append(PMD.EOL);
sb.append(" real.style.display = 'inline';").append(PMD.EOL);
sb.append(" prompt.style.display = 'none';").append(PMD.EOL);
sb.append(" real.focus();").append(PMD.EOL);
sb.append("}").append(PMD.EOL);
return sb.toString();
public void testIgnoreMultiLineComments() {
doTest("multilineCommentIgnore");
}
@Test
public void testTemplateStrings() throws IOException {
Tokenizer t = new EcmascriptTokenizer();
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(
"export default class DrawLocation extends joint.shapes.basic.Generic {" + PMD.EOL
+ " constructor(location: ILocation) {" + PMD.EOL
+ " this.markup = `<g>" + PMD.EOL
+ " <path class=\"location\"/>" + PMD.EOL
+ " <text x=\"0\" y=\"0\" text-anchor=\"middle\" class=\"location-text\"></text>" + PMD.EOL
+ PMD.EOL
+ " <path class=\"location\"/>" + PMD.EOL
+ " <circle class=\"location-circle\"/>" + PMD.EOL
+ " ${drawIndicators.Check.markup}" + PMD.EOL
+ PMD.EOL
+ " </g>`;" + PMD.EOL
+ " }" + PMD.EOL
+ "" + PMD.EOL
+ "}"));
final Tokens tokens = new Tokens();
t.tokenize(sourceCode, tokens);
final String templateString = "`<g>\n"
+ " <path class=\"location\"/>" + "\n"
+ " <text x=\"0\" y=\"0\" text-anchor=\"middle\" class=\"location-text\"></text>" + "\n"
+ "\n"
+ " <path class=\"location\"/>" + "\n"
+ " <circle class=\"location-circle\"/>" + "\n"
+ " ${drawIndicators.Check.markup}" + "\n"
+ "\n"
+ " </g>`";
assertEquals(templateString, tokens.getTokens().get(24).toString());
public void testTemplateStrings() {
doTest("templateStrings");
}
}

View File

@ -0,0 +1,4 @@
var s = "a string \
continues";
var s = "a string \
continues2";

View File

@ -0,0 +1,16 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[var] 1 3
[s] 5 5
[=] 7 7
["a string continues"] 9 10
L2
[;] 11 11
L3
[var] 1 3
[s] 5 5
[=] 7 7
["a string continues2"] 9 11
L4
[;] 12 12
EOF

View File

@ -0,0 +1,10 @@
/* This is a multi line comment
*
*/
var i = 0;
/* This is another multi line comment
* second line
* third line */
i++;

View File

@ -0,0 +1,12 @@
[Image] or [Truncated image[ Bcol Ecol
L5
[var] 1 3
[i] 5 5
[=] 7 7
[0] 9 9
[;] 10 10
L10
[i] 1 1
[++] 2 3
[;] 4 4
EOF

View File

@ -0,0 +1,7 @@
function switchToRealPassword() {
var real = $('realPass')
var prompt = $('promptPass')
real.style.display = 'inline'
prompt.style.display = 'none'
real.focus()
}

View File

@ -0,0 +1,48 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[function] 1 8
[switchToRealPassword] 10 29
[(] 30 30
[)] 31 31
[{] 33 33
L2
[var] 5 7
[real] 9 12
[=] 14 14
[$] 16 16
[(] 17 17
['realPass'] 18 27
[)] 28 28
L3
[var] 5 7
[prompt] 9 14
[=] 16 16
[$] 18 18
[(] 19 19
['promptPass'] 20 31
[)] 32 32
L4
[real] 5 8
[.] 9 9
[style] 10 14
[.] 15 15
[display] 16 22
[=] 24 24
['inline'] 26 33
L5
[prompt] 5 10
[.] 11 11
[style] 12 16
[.] 17 17
[display] 18 24
[=] 26 26
['none'] 28 33
L6
[real] 5 8
[.] 9 9
[focus] 10 14
[(] 15 15
[)] 16 16
L7
[}] 1 1
EOF

View File

@ -0,0 +1,7 @@
function switchToRealPassword() {
var real = $('realPass');
var prompt = $('promptPass');
real.style.display = 'inline';
prompt.style.display = 'none';
real.focus();
}

View File

@ -0,0 +1,53 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[function] 1 8
[switchToRealPassword] 10 29
[(] 30 30
[)] 31 31
[{] 33 33
L2
[var] 5 7
[real] 9 12
[=] 14 14
[$] 16 16
[(] 17 17
['realPass'] 18 27
[)] 28 28
[;] 29 29
L3
[var] 5 7
[prompt] 9 14
[=] 16 16
[$] 18 18
[(] 19 19
['promptPass'] 20 31
[)] 32 32
[;] 33 33
L4
[real] 5 8
[.] 9 9
[style] 10 14
[.] 15 15
[display] 16 22
[=] 24 24
['inline'] 26 33
[;] 34 34
L5
[prompt] 5 10
[.] 11 11
[style] 12 16
[.] 17 17
[display] 18 24
[=] 26 26
['none'] 28 33
[;] 34 34
L6
[real] 5 8
[.] 9 9
[focus] 10 14
[(] 15 15
[)] 16 16
[;] 17 17
L7
[}] 1 1
EOF

View File

@ -0,0 +1,6 @@
//This is a single line comment
var i = 0;
//This is another comment
i++;

View File

@ -0,0 +1,12 @@
[Image] or [Truncated image[ Bcol Ecol
L3
[var] 1 3
[i] 5 5
[=] 7 7
[0] 9 9
[;] 10 10
L6
[i] 1 1
[++] 2 3
[;] 4 4
EOF

View File

@ -0,0 +1,6 @@
// CPD-OFF
function switchToRealPassword() {
var real = $('realPass');
var prompt = $('promptPass');
// CPD-ON
}

View File

@ -0,0 +1,4 @@
[Image] or [Truncated image[ Bcol Ecol
L6
[}] 1 1
EOF

View File

@ -0,0 +1,14 @@
export default class DrawLocation extends joint.shapes.basic.Generic {
constructor(location: ILocation) {
this.markup = `<g>
<path class="location"/>
<text x="0" y="0" text-anchor="middle" class="location-text"></text>
<path class="location"/>
<circle class="location-circle"/>
${drawIndicators.Check.markup}
</g>`;
}
}

View File

@ -0,0 +1,36 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[export] 1 6
[default] 8 14
[class] 16 20
[DrawLocation] 22 33
[extends] 35 41
[joint] 43 47
[.] 48 48
[shapes] 49 54
[.] 55 55
[basic] 56 60
[.] 61 61
[Generic] 62 68
[{] 70 70
L2
[constructor] 5 15
[(] 16 16
[location] 17 24
[:] 25 25
[ILocation] 27 35
[)] 36 36
[{] 38 38
L3
[this] 9 12
[.] 13 13
[markup] 14 19
[=] 21 21
[`<g>\n <path class="locatio[ 23 11
L11
[;] 12 12
L12
[}] 5 5
L14
[}] 1 1
EOF