Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #408 from jplag/frontend-tests
Lightweight frontend tests and frontend-testutils (+ JUnit 5 for every test)
- Loading branch information
Showing
37 changed files
with
317 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>de.jplag</groupId> | ||
<artifactId>aggregator</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
<artifactId>frontend-testutils</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>de.jplag</groupId> | ||
<artifactId>frontend-utils</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
24 changes: 24 additions & 0 deletions
24
jplag.frontend-testutils/src/test/java/de/jplag/testutils/TestErrorConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package de.jplag.testutils; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import de.jplag.ErrorConsumer; | ||
|
||
/** | ||
* Mock error consumer that fails the test case on error occurrence. | ||
* @author Timur Saglam | ||
*/ | ||
public class TestErrorConsumer implements ErrorConsumer { | ||
|
||
@Override | ||
public void addError(String errorMessage) { | ||
System.err.println(errorMessage); | ||
fail(errorMessage); | ||
} | ||
|
||
@Override | ||
public void print(String message, String longMessage) { | ||
System.out.println(message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,4 @@ | |
<version>${revision}</version> | ||
</parent> | ||
<artifactId>frontend-utils</artifactId> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
jplag.frontend.csharp-6/src/test/java/de/jplag/csharp/MinimalCSharpFrontendTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package de.jplag.csharp; | ||
|
||
import static de.jplag.csharp.CSharpTokenConstants.*; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import de.jplag.Token; | ||
import de.jplag.TokenConstants; | ||
import de.jplag.TokenList; | ||
import de.jplag.TokenPrinter; | ||
import de.jplag.testutils.TestErrorConsumer; | ||
|
||
class MinimalCSharpFrontendTest { | ||
private static final int EXPEXTED_NUMBER_OF_TOKENS = 15; | ||
private static final Path BASE_PATH = Path.of("src", "test", "resources", "de", "jplag", "csharp"); | ||
private static final String TEST_SUBJECT = "TestClass.cs"; | ||
|
||
private de.jplag.Language frontend; | ||
private File baseDirectory; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
TestErrorConsumer consumer = new TestErrorConsumer(); | ||
frontend = new Language(consumer); | ||
baseDirectory = BASE_PATH.toFile(); | ||
assertTrue(baseDirectory.exists(), "Could not find base directory!"); | ||
} | ||
|
||
@Test | ||
void testParsingTestClass() { | ||
List<Integer> expectedToken = List.of(CLASS, CLASS_BEGIN, DECLARE_VAR, CONSTRUCTOR, METHOD, METHOD_BEGIN, INVOCATION, METHOD_END, PROPERTY, | ||
DECLARE_VAR, PROPERTY, RETURN, ASSIGNMENT, CLASS_END, TokenConstants.FILE_END); | ||
|
||
// Parse test input | ||
String[] input = new String[] {TEST_SUBJECT}; | ||
TokenList result = frontend.parse(baseDirectory, input); | ||
System.out.println(TokenPrinter.printTokens(result, baseDirectory, Arrays.asList(input))); | ||
|
||
// Compare parsed tokens: | ||
assertEquals(EXPEXTED_NUMBER_OF_TOKENS, result.size()); | ||
List<Integer> actualToken = StreamSupport.stream(result.allTokens().spliterator(), false).map(Token::getType).collect(toList()); | ||
assertEquals(expectedToken, actualToken); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
jplag.frontend.csharp-6/src/test/resources/de/jplag/csharp/TestClass.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Class for testing the C# language frontend. | ||
*/ | ||
public class MyClass | ||
{ | ||
public string myField = string.Empty; | ||
|
||
public MyClass() | ||
{ | ||
} | ||
|
||
public void MyMethod(int parameter1, string parameter2) | ||
{ | ||
Console.WriteLine("First Parameter {0}, second parameter {1}", | ||
parameter1, parameter2); | ||
} | ||
|
||
public int MyAutoImplementedProperty { get; set; } | ||
|
||
private int myPropertyVar; | ||
|
||
public int MyProperty | ||
{ | ||
get { return myPropertyVar; } | ||
set { myPropertyVar = value; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.