/*
* ******************************************************************************
* 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.prettyprint;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.StringReader;
import java.util.Optional;
import org.junit.BeforeClass;
import org.junit.Test;
import de.monticore.grammar.grammar._ast.ASTMCGrammar;
import de.monticore.grammar.grammar_withconcepts._parser.Grammar_WithConceptsParser;
import de.monticore.grammar.prettyprint.Grammar_WithConceptsPrettyPrinter;
import de.monticore.prettyprint.IndentPrinter;
import de.se_rwth.commons.logging.Log;
import de.se_rwth.commons.logging.Slf4jLog;
/**
* TODO: Write me!
*
* @author (last commit) $Author$
* @version $Revision$, $Date$
* @since TODO: add version number
*
*/
public class MCGrammarPrettyPrinterTest {
@BeforeClass
public static void setup() {
Slf4jLog.init();
Log.enableFailQuick(false);
}
@Test
// Test simple grammar
public void testStatechart() throws IOException {
String model = "src/test/resources/de/monticore/statechart/Statechart.mc4";
// Parsing input
Grammar_WithConceptsParser parser = new Grammar_WithConceptsParser();
Optional<ASTMCGrammar> result = parser.parse(model);
assertFalse(parser.hasErrors());
assertTrue(result.isPresent());
ASTMCGrammar grammar = result.get();
// Prettyprinting input
Grammar_WithConceptsPrettyPrinter prettyPrinter = new Grammar_WithConceptsPrettyPrinter(new IndentPrinter());
String output = prettyPrinter.prettyprint(grammar);
// Parsing printed input
result = parser.parse(new StringReader (output));
assertFalse(parser.hasErrors());
assertTrue(result.isPresent());
assertTrue(grammar.deepEquals(result.get()));
}
@Test
// Test grammar with concepts and java
public void testTypes() throws IOException {
String model = "src/test/resources/mc/grammars/types/TestTypes.mc4";
// Parsing input
Grammar_WithConceptsParser parser = new Grammar_WithConceptsParser();
Optional<ASTMCGrammar> result = parser.parse(model);
assertFalse(parser.hasErrors());
assertTrue(result.isPresent());
ASTMCGrammar grammar = result.get();
// Prettyprinting input
Grammar_WithConceptsPrettyPrinter prettyPrinter = new Grammar_WithConceptsPrettyPrinter(new IndentPrinter());
String output = prettyPrinter.prettyprint(grammar);
// Parsing printed input
result = parser.parse(new StringReader(output));
assertFalse(parser.hasErrors());
assertTrue(result.isPresent());
assertTrue(grammar.deepEquals(result.get()));
}
}