Add junit tests + fix form errors and Parser.isCorrect

This commit is contained in:
Quentin Legot 2021-04-19 17:33:21 +02:00
parent 359d3eace9
commit 6ebb99fb7c
6 changed files with 73 additions and 10 deletions

View File

@ -3,6 +3,7 @@
<property name="project.bin.dir" value="bin"/>
<property name="project.lib.dir" value="lib"/>
<property name="project.resources.dir" value="resources"/>
<property name="project.test.dir" value="test"/>
<path id="project.classpath">
<fileset dir="${project.lib.dir}">
<include name="*/*.jar"/>
@ -10,7 +11,10 @@
<pathelement location="${project.bin.dir}"/>
</path>
<target name="compile" description="Compilation des classes" depends="init">
<javac srcdir="${project.sources.dir}" encoding="utf8" destdir="${project.bin.dir}" debug="on" optimize="off" deprecation="on" includeantruntime="false">
<javac srcdir="${project.sources.dir}" encoding="utf8" destdir="${project.bin.dir}" debug="on" optimize="on" deprecation="on" includeantruntime="false">
<classpath refid="project.classpath"/>
</javac>
<javac srcdir="${project.test.dir}" encoding="utf8" destdir="${project.bin.dir}" optimize="on" includeantruntime="false">
<classpath refid="project.classpath"/>
</javac>
<copy todir="${basedir}/${project.bin.dir}">
@ -32,7 +36,7 @@
<classpath refid="project.classpath"/>
</java>
</target>
<target name="javadoc">
<target description="génération de la javadoc" name="javadoc">
<javadoc sourcepath="src" destdir="doc" access="private" encoding="utf8" overview="overview.html" >
<fileset dir="src" defaultexcludes="yes">
<include name="**"/>
@ -50,4 +54,11 @@
</manifest>
<jar jarfile="build/l-system.jar" basedir="${project.bin.dir}" manifest="${project.bin.dir}/META-INF/MANIFEST.MF"/>
</target>
<target name="tests" depends="compile">
<junit printsummary="true" haltonerror="no" haltonfailure="no" showoutput="true">
<classpath refid="project.classpath"/>
<formatter type="plain"/>
<test fork="true" name="lsystem.TestLSystem" outfile="result" />
</junit>
</target>
</project>

Binary file not shown.

View File

@ -69,17 +69,17 @@ public class Parser {
return false;
}
if(old == '.'){
for(int y = (type == Type.RULE ? 0 : 1); y < 12; y++){
for(int y = (type == Type.RULE ? 0 : 1); y < 10; y++){
if(temp == Constants.VALID_CHARS[y])
return false;
}
}
old = temp;
for(char validChar : Constants.VALID_CHARS){
if(temp == validChar)
break;
if(validChar == ' ')
return false;
if(temp == validChar)
break;
}
}
return bracket == 0;
@ -156,6 +156,7 @@ public class Parser {
}
}
}
assert root == null;
return root;
}

View File

@ -65,8 +65,8 @@ public class Listener extends AbstractListener implements KeyListener, MouseWhee
break;
case "Clear":
tab.getTextArea((byte) 0).setText("Axiome : \n");
tab.getTextArea((byte) 1).setText("Règles : \n");
tab.getTextArea((byte) 0).setText("Axiome :");
tab.getTextArea((byte) 1).setText("Règles :");
tab.getTextField((byte) 0).setText("");
tab.getTextField((byte) 1).setText("");
Listener kl = (Listener) tab.getTextField((byte) 0).getKeyListeners()[0];

View File

@ -3,6 +3,7 @@ package lsystem.screen.main;
import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.stream.Collectors;
public class Tab extends JPanel{
@ -133,7 +134,7 @@ public class Tab extends JPanel{
*/
public String getAxiom() {
String str = axiomList.getText();
str = str.substring(10).replaceAll(";", "");
str = str.substring(9).replaceAll(";", "");
return str;
}
@ -142,9 +143,9 @@ public class Tab extends JPanel{
*/
public java.util.List<String> getRules(){
String str = rulesList.getText();
str = str.substring(10).replaceAll(";", "");
str = str.substring(9).replaceAll(";", "");
String[] strsplit = str.split("\n");
return Arrays.asList(strsplit);
return Arrays.stream(strsplit).filter(p -> !p.equals("")).collect(Collectors.toList()); // To eliminate empty string from the list
}
/**

View File

@ -0,0 +1,50 @@
package lsystem;
import lsystem.engine.Parser;
import lsystem.engine.Rewrite;
import lsystem.screen.gl3d.GLCanvas;
import lsystem.utils.Pair;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
public class TestLSystem {
@Test
public void parserTest() {
GLCanvas joglFrame = new GLCanvas();
Parser parser = new Parser("X", Arrays.asList("X=XY", "Y=X"), 5);
List<Pair<String, String>> lSystemRules = parser.parseRules();
joglFrame.setLSystem(parser.getAxiom(), lSystemRules, parser.getNbIterations());
System.out.println(joglFrame.getLSystem());
assertNotNull("L-System should not be null", joglFrame.getLSystem());
}
@Test
public void rewriteTest(){
Parser parser = new Parser("X", Arrays.asList("X=XY", "Y=X"), 7);
String result = Rewrite.rewrite(parser.getAxiom(), parser.parseRules(), parser.getNbIterations());
assertEquals("Not equals", "XYXXYXYXXYXXYXYXXYXYXXYXXYXYXXYXXY", result);
}
@Test
public void incorrectTest() {
Parser parser = new Parser("X", Arrays.asList("X=XY", "y=X"), 3);
assertTrue(parser.isCorrect());
parser = new Parser("X", Arrays.asList("X+XY" , "Y=X"), 3);
assertFalse(parser.isCorrect());
parser = new Parser("X", Arrays.asList("X=X++Y", "Y=X"), 3);
assertTrue(parser.isCorrect());
try {
assertNotNull(Parser.parse(Rewrite.rewrite("X", new Parser("X", Arrays.asList("X=XY", "Y=X"), 3).parseRules(), 3)));
} catch (NumberFormatException err) {
System.out.println("fail");
err.printStackTrace();
fail();
}
}
}