Commit e33c47b8 authored by 0xJMR's avatar 0xJMR
Browse files

Rewrote tests.

parent 64f8fc06
Pipeline #68983 failed with stages
in 1 minute and 1 second
......@@ -20,6 +20,7 @@
<commons.cli.version>1.4</commons.cli.version>
<jlatexmath.version>1.0.7</jlatexmath.version>
<batik.version>1.10</batik.version>
<commons.io.version>2.6</commons.io.version>
<!-- .. Plugins ....................................................... -->
......@@ -125,6 +126,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- == DISTRIBUTION ==================================================== -->
......
package de.monticore.lang.monticar.mpp.models;
import de.monticore.lang.math._ast.ASTMathCompilationUnit;
import de.monticore.lang.math._parser.MathParser;
import de.monticore.lang.monticar.mpp.montimath.MathPrettyPrinter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import static junit.framework.TestCase.*;
public abstract class AbstractModelTest {
protected MathPrettyPrinter printer;
public AbstractModelTest()
{
this(new MathPrettyPrinter());
}
public AbstractModelTest(MathPrettyPrinter printer)
{
this.printer = printer;
}
public String printVanillaModel(String name) throws IOException
{
Path model = Paths.get("src/test/resources/models/montimath/vanilla/" + name).toAbsolutePath();
MathParser parser = new MathParser();
Optional<ASTMathCompilationUnit> astOptional = parser.parse(model.toString());
assertTrue(astOptional.isPresent());
return printer.prettyPrint(astOptional.get());
}
}
package de.monticore.lang.monticar.mpp.models;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
public class ArithmeticTest extends AbstractModelTest {
@Test
@Ignore
public void printTest() throws IOException
{
System.out.println(printVanillaModel("Arithmetic.m"));
}
@Test
public void testModel() throws IOException
{
String actual = printVanillaModel("Arithmetic.m");
String expected =
"package montimath.vanilla;\n" +
"\n" +
"script Arithmetic\n" +
" //Variable Declaration\n" +
" Q rational;\n" +
" rational = 1 + 2 * 3;\n" +
"end";
assertEquals(expected, actual);
}
}
package de.monticore.lang.monticar.mpp.models;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
public class CommentsTest extends AbstractModelTest {
@Test
@Ignore
public void printTest() throws IOException
{
System.out.println(printVanillaModel("Comments.m"));
}
@Test
@Ignore
//TODO somehow this test does not work even though the results are identical
public void testModel() throws IOException
{
String actual = printVanillaModel("Comments.m");
String expected = "/*\n" +
" * (C) SE RWTH 2018\n" +
" */\n" +
"package montimath.vanilla;\n" +
"\n" +
"/*\n" +
" * A Dummy Script to show that comments can be shown.\n" +
" */\n" +
"script Comments\n" +
" /* Vector of the form [1,3,5,7,9] */\n" +
" Q(0 : 10)^{1, 5} c = 1:2:10;\n" +
" Q x = 0;\n" +
" Q y = 0;\n" +
" // Iterate over all i in c\n" +
" for i = c\n" +
" // Iterate over all j in c\n" +
" for j = c\n" +
" // y might become very small when the\n" +
" // entries in c are very large.\n" +
" y += 1 / (c(x) * j^i);\n" +
" end\n" +
" x += 1;\n" +
" end\n" +
"end";
assertEquals(expected, actual);
}
}
package de.monticore.lang.monticar.mpp.models;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
public class ForLoopTest extends AbstractModelTest {
@Test
@Ignore
public void printTest1() throws IOException
{
System.out.println(printVanillaModel("ForLoop.m"));
}
@Test
public void testModel1() throws IOException
{
String actual = printVanillaModel("ForLoop.m");
String expected =
"package montimath.vanilla;\n" +
"\n" +
"script ForLoop\n" +
" Q sum = 1;\n" +
" for i = 1:2:9\n" +
" sum += sum * i;\n" +
" end\n" +
"end";
assertEquals(expected, actual);
}
@Test
@Ignore
public void printTest2() throws IOException
{
System.out.println(printVanillaModel("ForLoop2.m"));
}
@Test
public void testModel2() throws IOException
{
String actual = printVanillaModel("ForLoop2.m");
String expected =
"package montimath.vanilla;\n" +
"\n" +
"script ForLoop2\n" +
" Q(0 m : 1000 m)^{5} c = [1 m, 3 m, 5 m, 7 m, 9 m];\n" +
" Q x = 0;\n" +
" Q(0 m^2 : 1000 m^2) y = 0 m * m;\n" +
" Q(0 m : 1000 m) z = 0 m;\n" +
" //TODO fix this to work with variable vectors, and not only vectors that are equivalent to range vectors\n" +
" for i = c\n" +
" for j = c\n" +
" y += j * i;\n" +
" z += c(x + 0);\n" +
" end\n" +
" x += 1;\n" +
" end\n" +
"end";
assertEquals(expected, actual);
}
@Test
@Ignore
public void printTest3() throws IOException
{
System.out.println(printVanillaModel("ForLoop3.m"));
}
@Test
public void testModel3() throws IOException
{
String actual = printVanillaModel("ForLoop3.m");
String expected =
"package montimath.vanilla;\n" +
"\n" +
"script ForLoop3\n" +
" Q^{3, 3} A = [1, 2, 3; 4, 5, 6; 7, 8, 9];\n" +
" Q Cmat = 0;\n" +
" for i = 0:2\n" +
" for j = 0:2\n" +
" Cmat += A(i, j);\n" +
" end\n" +
" end\n" +
"end";
assertEquals(expected, actual);
}
}
package de.monticore.lang.monticar.mpp.models;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
public class NestedTest extends AbstractModelTest {
@Test
@Ignore
public void printTest() throws IOException
{
System.out.println(printVanillaModel("Nested.m"));
}
@Test
public void testModel() throws IOException
{
String actual = printVanillaModel("Nested.m");
String expected = "package montimath.vanilla;\n" +
"\n" +
"script Nested\n" +
" Q sum = 1;\n" +
" for i = 1:2:9\n" +
" if i < 2\n" +
" sum -= sum * i;\n" +
" elseif i < 4\n" +
" sum += sum * i;\n" +
" else\n" +
" for j = 1:2:6\n" +
" sum += sum * (i - j);\n" +
" end\n" +
" end\n" +
" end\n" +
"end";
assertEquals(expected, actual);
}
}
package de.monticore.lang.monticar.mpp.models;
import de.monticore.lang.monticar.mpp.montimath.MathPrettyPrinter;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import static junit.framework.TestCase.assertEquals;
public class StatementsOnlyTest extends AbstractModelTest {
@Test
@Ignore
public void printTest3() throws IOException
{
this.printer = new MathPrettyPrinter(4, true);
System.out.println(printVanillaModel("Nested.m"));
}
@Test
public void testModel3() throws IOException
{
this.printer = new MathPrettyPrinter(4, true);
String actual = printVanillaModel("Nested.m");
String expected =
"Q sum = 1;\n" +
"for i = 1:2:9\n" +
" if i < 2\n" +
" sum -= sum * i;\n" +
" elseif i < 4\n" +
" sum += sum * i;\n" +
" else\n" +
" for j = 1:2:6\n" +
" sum += sum * (i - j);\n" +
" end\n" +
" end\n" +
"end\n";
assertEquals(expected, actual);
}
}
package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.lang.math._ast.ASTMathCompilationUnit;
import de.monticore.lang.math._parser.MathParser;
import de.se_rwth.commons.logging.Log;
import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class MathPrettyPrinterTest {
protected static File[] sourceModels;
protected static File[] targetModels;
@BeforeClass
public static void prepare() {
Path sourcePath = Paths.get("src/test/resources/models/montimath/vanilla").toAbsolutePath();
Path targetPath = Paths.get("src/test/resources/models/montimath/printed").toAbsolutePath();
File sourceFile = sourcePath.toFile();
File targetFile = targetPath.toFile();
sourceModels = sourceFile.listFiles();
targetModels = targetFile.listFiles();
}
@Test
public void testPrinter() throws IOException {
int length = sourceModels.length;
MathParser parser = new MathParser();
MathPrettyPrinter printer = new MathPrettyPrinter();
for (int i = 0; i < length; i++) {
File sourceModel = sourceModels[i];
File targetModel = targetModels[i];
String message = String.format("Checking Equality of %s.", sourceModel.getName());
Log.info(message, "MathPrettyPrinterTest");
Optional<ASTMathCompilationUnit> astOptional = parser.parse(sourceModel.toString());
String input = FileUtils.readFileToString(targetModel, "UTF-8");
String output = printer.prettyPrint(astOptional.get());
assertEquals(input, output);
}
}
@Test
public void testParser() throws IOException {
MathParser parser = new MathParser();
MathPrettyPrinter printer = new MathPrettyPrinter();
for (File sourceModel : sourceModels) {
String message = String.format("Checking Parser Characteristic of %s.", sourceModel.getName());
Log.info(message, "MathPrettyPrinterTest");
Optional<ASTMathCompilationUnit> astOptional = parser.parse(sourceModel.toString());
String output = printer.prettyPrint(astOptional.get());
astOptional = parser.parse_String(output);
assertTrue(astOptional.isPresent());
}
}
}
package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.ast.ASTNode;
import de.monticore.lang.math._ast.ASTMathCompilationUnit;
import de.monticore.lang.math._parser.MathParser;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
public class MontiMathPrettyPrinterTest {
protected void printAST(ASTNode node, int level) {
for (int i = 0; i < level; i++) {
System.out.print("-");
}
System.out.print(node.toString());
System.out.println();
node.get_Children().forEach(subNode -> printAST(subNode, level + 1));
}
@Test
@Ignore
public void test() throws IOException {
Path model = Paths.get("src/test/resources/models/montimath/vanilla/Nested.m").toAbsolutePath();
MathParser parser = new MathParser();
Optional<ASTMathCompilationUnit> astOptional = parser.parse(model.toString());
HTMLMathPrettyPrinter printer = new HTMLMathPrettyPrinter();
}
}
......@@ -2,23 +2,53 @@ package de.monticore.lang.monticar.mpp.montimath;
import de.monticore.lang.math._ast.ASTMathCompilationUnit;
import de.monticore.lang.math._parser.MathParser;
import de.se_rwth.commons.logging.Log;
import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
public class TeXHTMLMathPrettyPrinterTest {
protected static File[] sourceModels;
protected static File[] targetModels;
@BeforeClass
public static void prepare() {
Path sourcePath = Paths.get("src/test/resources/models/montimath/vanilla").toAbsolutePath();
Path targetPath = Paths.get("src/test/resources/models/montimath/html").toAbsolutePath();
File sourceFile = sourcePath.toFile();
File targetFile = targetPath.toFile();
sourceModels = sourceFile.listFiles();
targetModels = targetFile.listFiles();
}
@Test
@Ignore
public void test() throws IOException {
Path model = Paths.get("src/test/resources/models/montimath/vanilla/Example.m").toAbsolutePath();
public void testPrinter() throws IOException {
int length = sourceModels.length;
MathParser parser = new MathParser();
Optional<ASTMathCompilationUnit> astOptional = parser.parse(model.toString());
//TeXHTMLMathPrettyPrinter visitor = new TeXHTMLMathPrettyPrinter();
HTMLMathPrettyPrinter printer = new HTMLMathPrettyPrinter();
for (int i = 0; i < length; i++) {
File sourceModel = sourceModels[i];
File targetModel = targetModels[i];
String message = String.format("Checking Equality of %s.", sourceModel.getName());
Log.info(message, "HTMLMathPrettyPrinterTest");
Optional<ASTMathCompilationUnit> astOptional = parser.parse(sourceModel.toString());
String input = FileUtils.readFileToString(targetModel, "UTF-8");
String output = printer.prettyPrint(astOptional.get());
//astOptional.ifPresent(visitor::handle);
assertEquals(input, output);
}
}
}
<span class="keyword">package</span> montimath.vanilla;
<span class="keyword">script</span> Arithmetic
<span class="comment">//Variable Declaration</span>
Q rational;
rational = 1 + 2 * 3;
<span class="keyword">end</span>
\ No newline at end of file
<span class="keyword">package</span> montimath.vanilla;
<span class="keyword">script</span> Assignments
Q A = 1;
Q^{2, 2} matB = [0, 0; 0, 0];
A = 2;
matB = [1, 2; 3, 4];
Q^{2, 2} matC = [1, 1; 1, 1];
matC = matB;
Q D = A;
<span class="keyword">end</span>
\ No newline at end of file
<span class="keyword">package</span> montimath.vanilla;
<span class="keyword">script</span> Assignments2
Q a = 1;
a = 2;
<span class="keyword">end</span>
\ No newline at end of file
<span class="comment">/*</span>
<span class="comment"> * (C) SE RWTH 2018</span>
<span class="comment"> */</span>
<span class="keyword">package</span> montimath.vanilla;
<span class="comment">/*</span>
<span class="comment"> * A Dummy Script to show that comments can be shown.</span>
<span class="comment"> */</span>
<span class="keyword">script</span> Comments
Q(0 : 10)^{1, 5} c = 1:2:10;
Q x = 0;
Q y = 0;
<span class="comment">// Iterate over all i in c</span>
<span class="keyword">for</span> i = c
<span class="comment">// Iterate over all j in c</span>
<span class="keyword">for</span> j = c
<span class="comment">// y might become very small when the</span>
<span class="comment">// entries in c are very large.</span>
y += 1 / (c(x) * j^i);
<span class="keyword">end</span>
x += 1;
<span class="keyword">end</span>
<span class="keyword">end</span>
\ No newline at end of file
<span class="keyword">package</span> montimath.vanilla;
<span class="keyword">script</span> Compare
B b;
b = x < 2;
<span class="keyword">end</span>
\ No newline at end of file
<span class="keyword">package</span> montimath.vanilla;
<span class="keyword">script</span> Example
Q(0 : 10)^{1, 5} c = 1:2:10;
Q x = 0;
Q y = 0;
<span class="keyword">for</span> i = c
<span class="keyword">for</span> j = c
y += 1 / (c(x) * j^i);
<span class="keyword">end</span>