Convert matlab tests
This commit is contained in:
@ -80,10 +80,20 @@
|
||||
<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>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.pmd</groupId>
|
||||
<artifactId>pmd-lang-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -4,102 +4,56 @@
|
||||
|
||||
package net.sourceforge.pmd.cpd;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import java.util.Properties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
|
||||
import net.sourceforge.pmd.cpd.test.CpdTextComparisonTest;
|
||||
|
||||
public class MatlabTokenizerTest extends AbstractTokenizerTest {
|
||||
public class MatlabTokenizerTest extends CpdTextComparisonTest {
|
||||
|
||||
private static final String FILENAME = "sample-matlab.m";
|
||||
|
||||
@Before
|
||||
@Override
|
||||
public void buildTokenizer() throws IOException {
|
||||
this.tokenizer = new MatlabTokenizer();
|
||||
this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), FILENAME));
|
||||
public MatlabTokenizerTest() {
|
||||
super(".m");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSampleCode() throws IOException {
|
||||
return IOUtils.toString(MatlabTokenizer.class.getResourceAsStream(FILENAME), StandardCharsets.UTF_8);
|
||||
protected String getResourcePrefix() {
|
||||
return "../lang/matlab/cpd/testdata";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tokenizer newTokenizer(Properties properties) {
|
||||
return new MatlabTokenizer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongSample() {
|
||||
doTest("sample-matlab");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tokenizeTest() throws IOException {
|
||||
this.expectedTokenCount = 3925;
|
||||
super.tokenizeTest();
|
||||
public void testIgnoreBetweenSpecialComments() {
|
||||
doTest("specialComments");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreBetweenSpecialComments() throws IOException {
|
||||
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader("% CPD-OFF" + PMD.EOL
|
||||
+ "function g = vec(op, y)" + PMD.EOL
|
||||
+ " opy = op(y);" + PMD.EOL
|
||||
+ " if ( any(size(opy) > 1) )" + PMD.EOL
|
||||
+ " g = @loopWrapperArray;" + PMD.EOL
|
||||
+ " end" + PMD.EOL
|
||||
+ " % CPD-ON" + PMD.EOL
|
||||
+ "end"
|
||||
));
|
||||
Tokens tokens = new Tokens();
|
||||
tokenizer.tokenize(sourceCode, tokens);
|
||||
assertEquals(2, tokens.size()); // 2 tokens: "end" + EOF
|
||||
public void testComments() {
|
||||
doTest("comments");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComments() throws IOException {
|
||||
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader("classdef LC" + PMD.EOL
|
||||
+ " methods" + PMD.EOL
|
||||
+ " function [obj, c,t, s ] = Classification( obj,m,t, cm )%#codegen" + PMD.EOL
|
||||
+ " end" + PMD.EOL
|
||||
+ " end" + PMD.EOL
|
||||
+ "end"));
|
||||
Tokens tokens = new Tokens();
|
||||
tokenizer.tokenize(sourceCode, tokens); // should not result in parse error
|
||||
assertEquals(28, tokens.size());
|
||||
public void testBlockComments() {
|
||||
doTest("multilineComments");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlockComments() throws IOException {
|
||||
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader("%{" + PMD.EOL
|
||||
+ " Name: helloworld.m\n" + PMD.EOL
|
||||
+ " Purpose: Say \"Hello World!\" in two different ways" + PMD.EOL
|
||||
+ "%}" + PMD.EOL
|
||||
+ PMD.EOL
|
||||
+ "% Do it the good ol' fashioned way...command window" + PMD.EOL
|
||||
+ "disp('Hello World!');\n" + PMD.EOL
|
||||
+ "%" + PMD.EOL
|
||||
+ "% Do it the new hip GUI way...with a message box" + PMD.EOL
|
||||
+ "msgbox('Hello World!','Hello World!');"));
|
||||
Tokens tokens = new Tokens();
|
||||
tokenizer.tokenize(sourceCode, tokens); // should not result in parse error
|
||||
assertEquals(13, tokens.size());
|
||||
public void testQuestionMark() {
|
||||
doTest("questionMark");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuestionMark() throws IOException {
|
||||
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader("classdef Class1" + PMD.EOL
|
||||
+ "properties (SetAccess = ?Class2)"));
|
||||
Tokens tokens = new Tokens();
|
||||
tokenizer.tokenize(sourceCode, tokens);
|
||||
assertEquals(10, tokens.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleQuotedStrings() throws IOException {
|
||||
SourceCode sourceCode = new SourceCode(new SourceCode.StringCodeLoader(
|
||||
"error(\"This is a double-quoted string\");"));
|
||||
Tokens tokens = new Tokens();
|
||||
tokenizer.tokenize(sourceCode, tokens);
|
||||
assertEquals("\"This is a double-quoted string\"", tokens.getTokens().get(2).toString());
|
||||
assertEquals(6, tokens.size());
|
||||
public void testDoubleQuotedStrings() {
|
||||
doTest("doubleQuotedStrings");
|
||||
}
|
||||
}
|
||||
|
6
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/comments.m
vendored
Normal file
6
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/comments.m
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
classdef LC
|
||||
methods
|
||||
function [obj, c,t, s ] = Classification( obj,m,t, cm )%#codegen
|
||||
end
|
||||
end
|
||||
end
|
35
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/comments.txt
vendored
Normal file
35
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/comments.txt
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
[Image] or [Truncated image[ Bcol Ecol
|
||||
L1
|
||||
[classdef] 1 8
|
||||
[LC] 10 11
|
||||
L2
|
||||
[methods] 5 11
|
||||
L3
|
||||
[function] 9 16
|
||||
[\[] 18 18
|
||||
[obj] 19 21
|
||||
[,] 22 22
|
||||
[c] 24 24
|
||||
[,] 25 25
|
||||
[t] 26 26
|
||||
[,] 27 27
|
||||
[s] 29 29
|
||||
[\]] 31 31
|
||||
[=] 33 33
|
||||
[Classification] 35 48
|
||||
[(] 49 49
|
||||
[obj] 51 53
|
||||
[,] 54 54
|
||||
[m] 55 55
|
||||
[,] 56 56
|
||||
[t] 57 57
|
||||
[,] 58 58
|
||||
[cm] 60 61
|
||||
[)] 63 63
|
||||
L4
|
||||
[end] 9 11
|
||||
L5
|
||||
[end] 5 7
|
||||
L6
|
||||
[end] 1 3
|
||||
EOF
|
@ -0,0 +1 @@
|
||||
error("This is a double-quoted string");
|
@ -0,0 +1,8 @@
|
||||
[Image] or [Truncated image[ Bcol Ecol
|
||||
L1
|
||||
[error] 1 5
|
||||
[(] 6 6
|
||||
["This is a double-quoted string"] 7 38
|
||||
[)] 39 39
|
||||
[;] 40 40
|
||||
EOF
|
12
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/multilineComments.m
vendored
Normal file
12
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/multilineComments.m
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
%{
|
||||
Name: helloworld.m
|
||||
|
||||
Purpose: Say "Hello World!" in two different ways
|
||||
%}
|
||||
|
||||
% Do it the good ol' fashioned way...command window
|
||||
disp('Hello World!');
|
||||
|
||||
%
|
||||
% Do it the new hip GUI way...with a message box
|
||||
msgbox('Hello World!','Hello World!');
|
16
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/multilineComments.txt
vendored
Normal file
16
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/multilineComments.txt
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
[Image] or [Truncated image[ Bcol Ecol
|
||||
L8
|
||||
[disp] 1 4
|
||||
[(] 5 5
|
||||
['Hello World!'] 6 19
|
||||
[)] 20 20
|
||||
[;] 21 21
|
||||
L12
|
||||
[msgbox] 1 6
|
||||
[(] 7 7
|
||||
['Hello World!'] 8 21
|
||||
[,] 22 22
|
||||
['Hello World!'] 23 36
|
||||
[)] 37 37
|
||||
[;] 38 38
|
||||
EOF
|
2
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/questionMark.m
vendored
Normal file
2
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/questionMark.m
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
classdef Class1
|
||||
properties (SetAccess = ?Class2)
|
13
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/questionMark.txt
vendored
Normal file
13
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/questionMark.txt
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
[Image] or [Truncated image[ Bcol Ecol
|
||||
L1
|
||||
[classdef] 1 8
|
||||
[Class1] 10 15
|
||||
L2
|
||||
[properties] 1 10
|
||||
[(] 12 12
|
||||
[SetAccess] 13 21
|
||||
[=] 23 23
|
||||
[?] 25 25
|
||||
[Class2] 26 31
|
||||
[)] 32 32
|
||||
EOF
|
4434
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/sample-matlab.txt
vendored
Normal file
4434
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/sample-matlab.txt
vendored
Normal file
File diff suppressed because it is too large
Load Diff
8
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/specialComments.m
vendored
Normal file
8
pmd-matlab/src/test/resources/net/sourceforge/pmd/lang/matlab/cpd/testdata/specialComments.m
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
% CPD-OFF
|
||||
function g = vec(op, y)
|
||||
opy = op(y);
|
||||
if ( any(size(opy) > 1) )
|
||||
g = @loopWrapperArray;
|
||||
end
|
||||
% CPD-ON
|
||||
end
|
@ -0,0 +1,4 @@
|
||||
[Image] or [Truncated image[ Bcol Ecol
|
||||
L8
|
||||
[end] 1 3
|
||||
EOF
|
Reference in New Issue
Block a user