Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,10 @@ public JavaDSL2ODDelegator(IndentPrinter printer, ReportingRepository reporting)
MCArrayTypes2OD mcArrayTypes = new MCArrayTypes2OD(printer, reporting);
this.traverser.add4MCArrayTypes(mcArrayTypes);
this.traverser.setMCArrayTypesHandler(mcArrayTypes);

/*
* TODO
* MCBasics2OD does not implement MCBasicsHandler. Figure out if
* this is a bug or intended.
*/

MCBasics2OD mcBasics = new MCBasics2OD(printer, reporting);
this.traverser.add4MCBasics(mcBasics);
// this.traverser.setMCBasicsHandler(mcBasics);
this.traverser.setMCBasicsHandler(mcBasics);

MCBasicTypes2OD mcBasicTypes = new MCBasicTypes2OD(printer, reporting);
this.traverser.add4MCBasicTypes(mcBasicTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
import de.monticore.statements.mccommonstatements._ast.ASTFormalParameter;
import de.monticore.statements.mcvardeclarationstatements._ast.ASTDeclaratorId;
import de.monticore.statements.mcvardeclarationstatements._ast.ASTVariableDeclarator;
import de.monticore.types.mcbasictypes.MCBasicTypesMill;
import de.monticore.types.mcbasictypes._ast.ASTMCPrimitiveType;
import de.monticore.types.mcbasictypes._ast.ASTMCQualifiedName;
import de.monticore.types.typeparameters._ast.ASTTypeParameters;

public class JavaDSLNodeIdentHelper extends ASTNodeIdentHelper {

Expand Down Expand Up @@ -142,7 +140,15 @@ public String getIdent(ASTMCPrimitiveType node) {
String name = node.printType();
return format(name, type);
}


public String getIdent(ASTStringLiteral symbol) {
return format(symbol.getValue());
}

public String getIdent(ASTIntLiteral symbol) {
return format(String.valueOf(symbol.getValue()));
}

@Override
public String getIdent(ASTNode a) {
if (a instanceof ASTConstructorDeclaration) {
Expand Down Expand Up @@ -190,9 +196,6 @@ else if (a instanceof ASTMCQualifiedName) {
else if (a instanceof ASTFieldDeclaration) {
return getIdent((ASTFieldDeclaration) a);
}
else if (a instanceof ASTTypeParameters) {
return getIdent((ASTTypeParameters) a);
}
else if (a instanceof ASTEnumDeclaration) {
return getIdent((ASTEnumDeclaration) a);
}
Expand Down
3 changes: 1 addition & 2 deletions javaDSL/src/test/java/de/monticore/java/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public void reset() {
LogStub.init();
JavaDSLMill.init();
JavaDSLMill.globalScope().clear();
CD4CodeMill.init();
CD4CodeMill.globalScope().clear();
LogStub.getFindings().clear();
}

}
119 changes: 50 additions & 69 deletions javaDSL/src/test/java/de/monticore/java/JavaDSLParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@

import de.monticore.expressions.expressionsbasis._ast.ASTExpression;
import de.monticore.java.javadsl.JavaDSLMill;
import de.monticore.java.javadsl._ast.ASTCompilationUnit;
import de.monticore.java.javadsl._ast.ASTJavaBlock;
import de.monticore.java.javadsl._ast.ASTTextBlockLiteral;
import de.monticore.java.javadsl._parser.JavaDSLParser;
import de.monticore.literals.mcliteralsbasis._ast.ASTLiteral;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import static de.monticore.java.JavaDSLAssertions.*;
Expand All @@ -22,39 +21,20 @@

public class JavaDSLParserTest extends AbstractTest {

@Test
public void test1() throws IOException {
Path model = Paths.get("src/test/resources/de/monticore/java/parser/ASTClassDeclaration.java");
JavaDSLParser parser = JavaDSLMill.parser();
Optional<ASTCompilationUnit> ast = parser.parse(model.toString());
assertFalse(parser.hasErrors());
assertTrue(ast.isPresent());
// AST2ModelFiles.get().serializeASTInstance(ast.get(), "ASTClassDeclaration");
}

@Test
public void test2() throws IOException {
Path model = Paths.get("src/test/resources/de/monticore/java/parser/ParseException.java");
JavaDSLParser parser = JavaDSLMill.parser();
Optional<ASTCompilationUnit> ast = parser.parse(model.toString());
assertFalse(parser.hasErrors());
assertTrue(ast.isPresent());
// AST2ModelFiles.get().serializeASTInstance(ast.get(), "ParseException");
}

@Test
public void test3() throws IOException {
Path model = Paths.get("src/test/resources/de/monticore/java/parser/TokenMgrError.java");
JavaDSLParser parser = JavaDSLMill.parser();
Optional<ASTCompilationUnit> ast = parser.parse(model.toString());
assertFalse(parser.hasErrors());
assertTrue(ast.isPresent());
//AST2ModelFiles.get().serializeASTInstance(ast.get(), "TokenMgrError");
@ParameterizedTest
@ValueSource(strings = {
"src/test/resources/de/monticore/java/parser/ASTClassDeclaration.java",
"src/test/resources/de/monticore/java/parser/ParseException.java",
"src/test/resources/de/monticore/java/parser/TokenMgrError.java",
"src/test/resources/parsableAndCompilableModels/simpleTestClasses/HelloWorld.java"
})
public void testParser(String path) throws IOException {
assertParsingSuccess(path);
}

@Test
public void test4() throws IOException {
StringBuffer buffer = new StringBuffer("");
public void testJavaBlock() throws IOException {
StringBuffer buffer = new StringBuffer();
buffer.append("{ _channel = HIDDEN;");
buffer.append("if (getCompiler() != null) {");
buffer.append(" de.monticore.ast.Comment _comment = new de.monticore.ast.Comment(getText());");
Expand All @@ -69,51 +49,52 @@ public void test4() throws IOException {
assertFalse(parser.hasErrors());
assertTrue(ast.isPresent());
}

@Test
public void test5() throws IOException {
Path model = Paths
.get("src/test/resources/parsableAndCompilableModels/simpleTestClasses/HelloWorld.java");
JavaDSLParser parser = JavaDSLMill.parser();
Optional<ASTCompilationUnit> ast = parser.parse(model.toString());
assertFalse(parser.hasErrors());
assertTrue(ast.isPresent());
// AST2ModelFiles.get().serializeASTInstance(ast.get(), "ASTClassDeclaration");
}

@Test
public void testCondition() throws IOException {

@ParameterizedTest
@ValueSource(strings = {
"ch = str.charAt(i) < 0x20"
})
public void testCondition(String input) throws IOException {
JavaDSLParser parser = JavaDSLMill.parser();
Optional<ASTExpression> ast = parser.parse_StringExpression("ch = str.charAt(i) < 0x20");
Optional<ASTExpression> ast = parser.parse_StringExpression(input);
assertTrue(ast.isPresent());
}

@Test
public void testLambdas() throws IOException {

@ParameterizedTest
@ValueSource(strings = {
"foo -> foo",
"(foo, bar) -> foo",
"(foo, bar) -> { return foo; }"
})
public void testLambdas(String input) throws IOException {
JavaDSLParser parser = JavaDSLMill.parser();
assertTrue(parser.parse_StringExpression("foo -> foo").isPresent());
assertTrue(parser.parse_StringExpression("(foo, bar) -> foo").isPresent());
assertTrue(parser.parse_StringExpression("(foo, bar) -> { return foo; }").isPresent());
assertTrue(parser.parse_StringExpression(input).isPresent());
}

@Test
public void testMethodReferences() throws IOException {
@ParameterizedTest
@ValueSource(strings = {
"String::length",
"System::currentTimeMillis",
"List<String>::size",
"int[]::clone",
"System.out::println",
"\"abc\"::length",
"foo[x]::bar",
"(test ? list.replaceAll(String::trim) : list) :: iterator",
"super::toString",
"Arrays::<String>sort"
})
public void testMethodReferences(String input) throws IOException {
JavaDSLParser parser = JavaDSLMill.parser();
assertTrue(parser.parse_StringExpression("String::length").isPresent());
assertTrue(parser.parse_StringExpression("System::currentTimeMillis").isPresent());
assertTrue(parser.parse_StringExpression("List<String>::size").isPresent());
assertTrue(parser.parse_StringExpression("int[]::clone").isPresent());
assertTrue(parser.parse_StringExpression("System.out::println").isPresent());
assertTrue(parser.parse_StringExpression("\"abc\"::length").isPresent());
assertTrue(parser.parse_StringExpression("foo[x]::bar").isPresent());
assertTrue(parser.parse_StringExpression("(test ? list.replaceAll(String::trim) : list) :: iterator").isPresent());
assertTrue(parser.parse_StringExpression("super::toString").isPresent());
assertTrue(parser.parse_StringExpression("Arrays::<String>sort").isPresent());
assertTrue(parser.parse_StringExpression(input).isPresent());
}

@Test
public void testModuleDeclaration() {
assertParsingSuccess("src/test/resources/moduleDeclaration/module-info.java");
@ParameterizedTest
@ValueSource(strings = {
"src/test/resources/moduleDeclaration/module-info.java"
})
public void testModuleDeclaration(String path) {
assertParsingSuccess(path);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,79 +12,50 @@
import java.nio.file.Paths;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import de.se_rwth.commons.logging.Log;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.*;

public final class JavaDSLPrettyPrinterTest extends AbstractTest {


@ParameterizedTest
@ValueSource(strings = {
"src/test/resources/de/monticore/java/parser/ASTClassDeclaration.java",
"src/test/resources/de/monticore/java/parser/ParseException.java",
"src/test/resources/de/monticore/java/parser/TokenMgrError.java",
"src/test/resources/parsableAndCompilableModels/simpleTestClasses/HelloWorld.java"
})
public void testPrettyPrinter(String path) throws IOException {
// Parse input
ASTJavaDSLNode ast = parse(path);

// Prettyprinting input
String output = JavaDSLMill.prettyPrint(ast, false);

// Parsing printed input
ASTJavaDSLNode printedAST = parse(new StringReader(output));
assertTrue(ast.deepEquals(printedAST));
assertTrue(Log.getFindings().isEmpty());
}

private ASTJavaDSLNode parse(String modelName) throws IOException {
Path model = Paths.get(modelName);
JavaDSLParser parser = JavaDSLMill.parser();
Optional<ASTCompilationUnit> ast = parser.parse(model.toString());
assertFalse(parser.hasErrors());
assertTrue(ast.isPresent());
assertTrue(Log.getFindings().isEmpty());
return ast.get();
}

private ASTJavaDSLNode parse(StringReader reader) throws IOException {
JavaDSLParser parser = JavaDSLMill.parser();
Optional<ASTCompilationUnit> ast = parser.parse(reader);
assertFalse(parser.hasErrors());
assertTrue(ast.isPresent());
assertTrue(Log.getFindings().isEmpty());
return ast.get();
}

@Test
public void test1() throws IOException {
// Parse input
ASTJavaDSLNode ast = parse("src/test/resources/de/monticore/java/parser/ASTClassDeclaration.java");

// Prettyprinting input
String output = JavaDSLMill.prettyPrint(ast, false);

// Parsing printed input
ASTJavaDSLNode printedAST = parse(new StringReader(output));
assertTrue(ast.deepEquals(printedAST));
}

@Test
public void test2() throws IOException {
// Parse input
ASTJavaDSLNode ast = parse("src/test/resources/de/monticore/java/parser/ParseException.java");

// Prettyprinting input
String output = JavaDSLMill.prettyPrint(ast, false);

// Parsing printed input
ASTJavaDSLNode printedAST = parse(new StringReader(output));
assertTrue(ast.deepEquals(printedAST));
}

@Test
public void test3() throws IOException {
// Parse input
ASTJavaDSLNode ast = parse("src/test/resources/de/monticore/java/parser/TokenMgrError.java");

// Prettyprinting input
String output = JavaDSLMill.prettyPrint(ast, false);

// Parsing printed input
ASTJavaDSLNode printedAST = parse(new StringReader(output));
assertTrue(ast.deepEquals(printedAST));
}

@Test
public void test4() throws IOException {
// Parse input
ASTJavaDSLNode ast = parse("src/test/resources/parsableAndCompilableModels/simpleTestClasses/HelloWorld.java");

// Prettyprinting input
String output = JavaDSLMill.prettyPrint(ast, false);

// Parsing printed input
ASTJavaDSLNode printedAST = parse(new StringReader(output));
assertTrue(ast.deepEquals(printedAST));
}

}
Loading