Skip to content

Commit

Permalink
fix bug function endpoints fn construction
Browse files Browse the repository at this point in the history
A copy-paste error in built in function construction functions meant that the JVM class was incorrect. This produces unexpected results when visitors are invoked because the wrong visitor branch will be called. This doesn't happen in general because `fromNode` produces the correct behavior.
  • Loading branch information
rcoh committed Mar 15, 2023
1 parent aaa523c commit 0d45f9b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,4 @@ public Value evaluateCondition(Condition condition) {
}
return value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public BooleanEquals(FunctionNode functionNode) {
* @param right the right hand-side expression.
* @return the function defining the BooleanEquals of the left and right expressions.
*/
public static Function ofExpressions(Expression left, Expression right) {
return LibraryFunction.ofExpressions(DEFINITION, left, right);
public static BooleanEquals ofExpressions(Expression left, Expression right) {
return new BooleanEquals(FunctionNode.ofExpressions(DEFINITION.getId(), left, right));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public StringEquals(FunctionNode functionNode) {
* @return a function instance representing the StringEquals comparison.
*/
public static Function ofExpressions(Expression left, Expression right) {
return LibraryFunction.ofExpressions(DEFINITION, left, right);
return new StringEquals(FunctionNode.ofExpressions(DEFINITION.getId(), left, right));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package software.amazon.smithy.rulesengine.language.fn;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.rulesengine.language.syntax.expr.Expression;
import software.amazon.smithy.rulesengine.language.syntax.fn.FunctionDefinition;
import software.amazon.smithy.rulesengine.language.visit.ExpressionVisitor;

public class FunctionOfExprsTest {

@Test
void boolEquals() {
Expression boolEquals = Expression.of(true).equal(true);
assertEquals(boolEquals.accept(new ExpectLibFunction()), false);
assertEquals(boolEquals.accept(new ExpectBuiltInFunction()), "bool");
}


@Test
void stringEquals() {
Expression stringEquals = Expression.of("asdf").equal("asdf");
assertEquals(stringEquals.accept(new ExpectLibFunction()), false);
assertEquals(stringEquals.accept(new ExpectBuiltInFunction()), "string");
}

@Test
void not() {
Expression not = Expression.of("asdf").equal("asdf").not();
assertEquals(not.accept(new ExpectLibFunction()), false);
assertEquals(not.accept(new ExpectBuiltInFunction()), "not");
}

static class ExpectLibFunction extends ExpressionVisitor.Default<Boolean> {
@Override
public Boolean visitLibraryFunction(FunctionDefinition fn, List<Expression> args) {
return true;
}

@Override
public Boolean getDefault() {
return false;
}
}

static class ExpectBuiltInFunction extends ExpressionVisitor.Default<String> {
@Override
public String getDefault() {
return "other";
}

@Override
public String visitBoolEquals(Expression left, Expression right) {
return "bool";
}


@Override
public String visitStringEquals(Expression left, Expression right) {
return "string";
}

@Override
public String visitNot(Expression not) {
return "not";
}
}
}

0 comments on commit 0d45f9b

Please sign in to comment.