added a test for the CUPL builder

This commit is contained in:
hneemann 2016-05-23 13:27:53 +02:00
parent fbc2e4c60a
commit 87eb1728fc
2 changed files with 32 additions and 10 deletions

View File

@ -26,7 +26,7 @@ import static de.neemann.digital.analyse.expression.Operation.or;
*/
public class CuplCreator implements BuilderInterface<CuplCreator> {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yyyy");
private static final ExpressionVisitor NOT_ALLOWED_VARIABLES_VISITOR = new NotAllowedVariablesVisitor();
private static final NotAllowedVariablesVisitor NOT_ALLOWED_VARIABLES_VISITOR = new NotAllowedVariablesVisitor();
private final StringBuilder expressions;
private final String projectName;
@ -63,7 +63,7 @@ public class CuplCreator implements BuilderInterface<CuplCreator> {
@Override
public CuplCreator addExpression(String name, Expression expression) throws BuilderException {
outVars.add(name);
addOutVar(name);
addToStr(name, expression);
return this;
}
@ -71,11 +71,16 @@ public class CuplCreator implements BuilderInterface<CuplCreator> {
@Override
public CuplCreator addState(String name, Expression expression) throws BuilderException {
sequential = true;
outVars.add(name);
addOutVar(name);
addToStr(name + ".D", expression);
return this;
}
private void addOutVar(String name) {
NOT_ALLOWED_VARIABLES_VISITOR.check(name);
outVars.add(name);
}
private void addToStr(String name, Expression expression) throws BuilderException {
expression.traverse(vars);
expression.traverse(NOT_ALLOWED_VARIABLES_VISITOR);
@ -151,12 +156,16 @@ public class CuplCreator implements BuilderInterface<CuplCreator> {
public boolean visit(Expression expression) {
if (expression instanceof Variable) {
Variable v = (Variable) expression;
if (notAllowed.contains(v.getIdentifier()))
throw new RuntimeException(Lang.get("err_varNotAllowedInCUPL_N", v.getIdentifier()));
check(v.getIdentifier());
}
return true;
}
private void check(String v) {
if (notAllowed.contains(v))
throw new RuntimeException(Lang.get("err_varNotAllowedInCUPL_N", v));
}
}
/**

View File

@ -57,17 +57,30 @@ public class CuplCreatorTest extends TestCase {
public void testCUPLBuilderInvalidVars() throws Exception {
Variable y0 = new Variable("D"); // D is not allowed in CUPL
Variable y1 = new Variable("Y_1");
// counter
Expression y0s = not(y0);
Expression y1s = or(and(not(y0), y1), and(y0, not(y1)));
try {
new CuplCreator("test", "user", new Date(0))
.addState("Y_0", y0s)
.addState("Y_1", y1s)
.addExpression("A", and(y0, y1));
.addState("Y_0", y0s);
assertTrue(false);
} catch (RuntimeException e) {
assertTrue(true);
}
}
public void testCUPLBuilderInvalidVars2() throws Exception {
Variable y0 = new Variable("Y_0"); // D is not allowed in CUPL
// counter
Expression y0s = not(y0);
try {
new CuplCreator("test", "user", new Date(0))
.addExpression("D", y0s)
.writeTo(System.out);
assertTrue(false);
} catch (RuntimeException e) {