/*
* ******************************************************************************
* MontiCore Language Workbench
* Copyright (c) 2015, MontiCore, All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* ******************************************************************************
*/
package de.monticore.codegen.mc2cd;
import de.monticore.MontiCoreScript;
import de.monticore.generating.templateengine.GlobalExtensionManagement;
import de.monticore.grammar.grammar._ast.ASTMCGrammar;
import de.monticore.io.paths.ModelPath;
import de.monticore.symboltable.GlobalScope;
import de.monticore.types.types._ast.ASTSimpleReferenceType;
import de.monticore.types.types._ast.ASTType;
import de.monticore.umlcd4a.cd4analysis._ast.ASTCDClass;
import de.monticore.umlcd4a.cd4analysis._ast.ASTCDCompilationUnit;
import de.monticore.umlcd4a.cd4analysis._ast.ASTCDInterface;
import de.se_rwth.commons.Names;
import parser.MCGrammarParser;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
/**
* @author Sebastian Oberhoff
*/
public class TestHelper {
/**
* Convenience bundling of parsing and transformation
*
* @param model the .mc4 file that is to be parsed and transformed
* @return the root node of the resulting CD AST
*/
public static Optional<ASTCDCompilationUnit> parseAndTransform(Path model) {
Optional<ASTMCGrammar> grammar = MCGrammarParser.parse(model);
if (!grammar.isPresent()) {
return Optional.empty();
}
MontiCoreScript mc = new MontiCoreScript();
GlobalScope symbolTable = mc.initSymbolTable(new ModelPath(Paths.get("src/test/resources")));
mc.createSymbolsFromAST(symbolTable, grammar.get());
ASTCDCompilationUnit cdCompilationUnit = new MC2CDTransformation(
new GlobalExtensionManagement()).apply(grammar.get());
return Optional.of(cdCompilationUnit);
}
public static Optional<ASTCDClass> getCDClass(ASTCDCompilationUnit cdCompilationUnit, String cdClassName) {
return cdCompilationUnit.getCDDefinition().getCDClasses().stream()
.filter(cdClass -> cdClass.getName().equals(cdClassName))
.findAny();
}
public static Optional<ASTCDInterface> getCDInterface(ASTCDCompilationUnit cdCompilationUnit, String cdInterfaceName) {
return cdCompilationUnit.getCDDefinition().getCDInterfaces().stream()
.filter(cdClass -> cdClass.getName().equals(cdInterfaceName))
.findAny();
}
public static boolean isListOfType(ASTType typeRef, String typeArg) {
if (!TransformationHelper.typeToString(typeRef).equals("java.util.List")) {
return false;
}
if (!(typeRef instanceof ASTSimpleReferenceType)) {
return false;
}
ASTSimpleReferenceType type = (ASTSimpleReferenceType) typeRef;
if (!type.getTypeArguments().isPresent()) {
return false;
}
if (type.getTypeArguments().get().getTypeArguments().size() != 1) {
return false;
}
if (!(type.getTypeArguments().get().getTypeArguments()
.get(0) instanceof ASTSimpleReferenceType)) {
return false;
}
return Names.getQualifiedName(
((ASTSimpleReferenceType) type.getTypeArguments().get().getTypeArguments().get(0))
.getNames())
.equals(typeArg);
}
}